xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 4ac140bfdcc99966c46f47a8c9696fa79597795d)
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>
28c1064fd7SJerry Jelinek  * Copyright 2019 Joyent, Inc.
29663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
300c06d385Sjwpoduska  * Copyright (c) 2019, Datto Inc. All rights reserved.
31fa9e4066Sahrens  */
32fa9e4066Sahrens 
33fa9e4066Sahrens #include <sys/zfs_context.h>
34ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
35fa9e4066Sahrens #include <sys/spa.h>
36fa9e4066Sahrens #include <sys/spa_impl.h>
375cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h>
38fa9e4066Sahrens #include <sys/dmu.h>
39fa9e4066Sahrens #include <sys/dmu_tx.h>
405cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h>
41fa9e4066Sahrens #include <sys/vdev_impl.h>
42fa9e4066Sahrens #include <sys/uberblock_impl.h>
43fa9e4066Sahrens #include <sys/metaslab.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/space_map.h>
460713e232SGeorge Wilson #include <sys/space_reftree.h>
47fa9e4066Sahrens #include <sys/zio.h>
48fa9e4066Sahrens #include <sys/zap.h>
49fa9e4066Sahrens #include <sys/fs/zfs.h>
50c5904d13Seschrock #include <sys/arc.h>
51e6ca193dSGeorge Wilson #include <sys/zil.h>
523f9d6ad7SLin Ling #include <sys/dsl_scan.h>
53770499e1SDan Kimmel #include <sys/abd.h>
54094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
55084fd14fSBrian Behlendorf #include <sys/vdev_trim.h>
56fa9e4066Sahrens 
57fa9e4066Sahrens /*
58fa9e4066Sahrens  * Virtual device management.
59fa9e4066Sahrens  */
60fa9e4066Sahrens 
61fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
62fa9e4066Sahrens 	&vdev_root_ops,
63fa9e4066Sahrens 	&vdev_raidz_ops,
64fa9e4066Sahrens 	&vdev_mirror_ops,
65fa9e4066Sahrens 	&vdev_replacing_ops,
6699653d4eSeschrock 	&vdev_spare_ops,
67fa9e4066Sahrens 	&vdev_disk_ops,
68fa9e4066Sahrens 	&vdev_file_ops,
69fa9e4066Sahrens 	&vdev_missing_ops,
7088ecc943SGeorge Wilson 	&vdev_hole_ops,
715cabbc6bSPrashanth Sreenivasa 	&vdev_indirect_ops,
72fa9e4066Sahrens 	NULL
73fa9e4066Sahrens };
74fa9e4066Sahrens 
75088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
76088f3894Sahrens int zfs_scrub_limit = 10;
7705b2b3b8Smishra 
78a0b03b16SSerapheim Dimitropoulos /* default target for number of metaslabs per top-level vdev */
79a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_count = 200;
8086714001SSerapheim Dimitropoulos 
81b4bf0cf0SDon Brady /* minimum number of metaslabs per top-level vdev */
82a0b03b16SSerapheim Dimitropoulos int zfs_vdev_min_ms_count = 16;
8386714001SSerapheim Dimitropoulos 
84b4bf0cf0SDon Brady /* practical upper limit of total metaslabs per top-level vdev */
85a0b03b16SSerapheim Dimitropoulos int zfs_vdev_ms_count_limit = 1ULL << 17;
86b4bf0cf0SDon Brady 
87b4bf0cf0SDon Brady /* lower limit for metaslab size (512M) */
88a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_shift = 29;
89bf3e216cSMatthew Ahrens 
90a0b03b16SSerapheim Dimitropoulos /* upper limit for metaslab size (16G) */
91a0b03b16SSerapheim Dimitropoulos int zfs_vdev_max_ms_shift = 34;
92b4bf0cf0SDon Brady 
936f793812SPavel Zakharov boolean_t vdev_validate_skip = B_FALSE;
946f793812SPavel Zakharov 
9586714001SSerapheim Dimitropoulos /*
9686714001SSerapheim Dimitropoulos  * Since the DTL space map of a vdev is not expected to have a lot of
9786714001SSerapheim Dimitropoulos  * entries, we default its block size to 4K.
9886714001SSerapheim Dimitropoulos  */
99814dcd43SSerapheim Dimitropoulos int zfs_vdev_dtl_sm_blksz = (1 << 12);
10086714001SSerapheim Dimitropoulos 
10186714001SSerapheim Dimitropoulos /*
102165c5c6fSJohn Poduska  * Ignore errors during scrub/resilver.  Allows to work around resilver
103165c5c6fSJohn Poduska  * upon import when there are pool errors.
104165c5c6fSJohn Poduska  */
105165c5c6fSJohn Poduska int zfs_scan_ignore_errors = 0;
106165c5c6fSJohn Poduska 
107165c5c6fSJohn Poduska /*
10886714001SSerapheim Dimitropoulos  * vdev-wide space maps that have lots of entries written to them at
10986714001SSerapheim Dimitropoulos  * the end of each transaction can benefit from a higher I/O bandwidth
11086714001SSerapheim Dimitropoulos  * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
11186714001SSerapheim Dimitropoulos  */
112814dcd43SSerapheim Dimitropoulos int zfs_vdev_standard_sm_blksz = (1 << 17);
11386714001SSerapheim Dimitropoulos 
11493a1902eSMatthew Ahrens int zfs_ashift_min;
11593a1902eSMatthew Ahrens 
1163ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
1173ee8c80cSPavel Zakharov void
vdev_dbgmsg(vdev_t * vd,const char * fmt,...)1183ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
1193ee8c80cSPavel Zakharov {
1203ee8c80cSPavel Zakharov 	va_list adx;
1213ee8c80cSPavel Zakharov 	char buf[256];
1223ee8c80cSPavel Zakharov 
1233ee8c80cSPavel Zakharov 	va_start(adx, fmt);
1243ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
1253ee8c80cSPavel Zakharov 	va_end(adx);
1263ee8c80cSPavel Zakharov 
1273ee8c80cSPavel Zakharov 	if (vd->vdev_path != NULL) {
1283ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
1293ee8c80cSPavel Zakharov 		    vd->vdev_path, buf);
1303ee8c80cSPavel Zakharov 	} else {
1313ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
1323ee8c80cSPavel Zakharov 		    vd->vdev_ops->vdev_op_type,
1333ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_id,
1343ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_guid, buf);
1353ee8c80cSPavel Zakharov 	}
1363ee8c80cSPavel Zakharov }
1373ee8c80cSPavel Zakharov 
1386f793812SPavel Zakharov void
vdev_dbgmsg_print_tree(vdev_t * vd,int indent)1396f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
1406f793812SPavel Zakharov {
1416f793812SPavel Zakharov 	char state[20];
1426f793812SPavel Zakharov 
1436f793812SPavel Zakharov 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
1446f793812SPavel Zakharov 		zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
1456f793812SPavel Zakharov 		    vd->vdev_ops->vdev_op_type);
1466f793812SPavel Zakharov 		return;
1476f793812SPavel Zakharov 	}
1486f793812SPavel Zakharov 
1496f793812SPavel Zakharov 	switch (vd->vdev_state) {
1506f793812SPavel Zakharov 	case VDEV_STATE_UNKNOWN:
1516f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "unknown");
1526f793812SPavel Zakharov 		break;
1536f793812SPavel Zakharov 	case VDEV_STATE_CLOSED:
1546f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "closed");
1556f793812SPavel Zakharov 		break;
1566f793812SPavel Zakharov 	case VDEV_STATE_OFFLINE:
1576f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "offline");
1586f793812SPavel Zakharov 		break;
1596f793812SPavel Zakharov 	case VDEV_STATE_REMOVED:
1606f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "removed");
1616f793812SPavel Zakharov 		break;
1626f793812SPavel Zakharov 	case VDEV_STATE_CANT_OPEN:
1636f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "can't open");
1646f793812SPavel Zakharov 		break;
1656f793812SPavel Zakharov 	case VDEV_STATE_FAULTED:
1666f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "faulted");
1676f793812SPavel Zakharov 		break;
1686f793812SPavel Zakharov 	case VDEV_STATE_DEGRADED:
1696f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "degraded");
1706f793812SPavel Zakharov 		break;
1716f793812SPavel Zakharov 	case VDEV_STATE_HEALTHY:
1726f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "healthy");
1736f793812SPavel Zakharov 		break;
1746f793812SPavel Zakharov 	default:
1756f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "<state %u>",
1766f793812SPavel Zakharov 		    (uint_t)vd->vdev_state);
1776f793812SPavel Zakharov 	}
1786f793812SPavel Zakharov 
1796f793812SPavel Zakharov 	zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
180c7a7b2faSAndriy Gapon 	    "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
1816f793812SPavel Zakharov 	    vd->vdev_islog ? " (log)" : "",
1826f793812SPavel Zakharov 	    (u_longlong_t)vd->vdev_guid,
1836f793812SPavel Zakharov 	    vd->vdev_path ? vd->vdev_path : "N/A", state);
1846f793812SPavel Zakharov 
1856f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++)
1866f793812SPavel Zakharov 		vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
1876f793812SPavel Zakharov }
1886f793812SPavel Zakharov 
189bf3e216cSMatthew Ahrens /*
190fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
191fa9e4066Sahrens  */
192fa9e4066Sahrens static vdev_ops_t *
vdev_getops(const char * type)193fa9e4066Sahrens vdev_getops(const char *type)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
196fa9e4066Sahrens 
197fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
198fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
199fa9e4066Sahrens 			break;
200fa9e4066Sahrens 
201fa9e4066Sahrens 	return (ops);
202fa9e4066Sahrens }
203fa9e4066Sahrens 
204663207adSDon Brady /*
205663207adSDon Brady  * Derive the enumerated alloction bias from string input.
206bbf21555SRichard Lowe  * String origin is either the per-vdev zap or zpool(8).
207663207adSDon Brady  */
208663207adSDon Brady static vdev_alloc_bias_t
vdev_derive_alloc_bias(const char * bias)209663207adSDon Brady vdev_derive_alloc_bias(const char *bias)
210663207adSDon Brady {
211663207adSDon Brady 	vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
212663207adSDon Brady 
213663207adSDon Brady 	if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
214663207adSDon Brady 		alloc_bias = VDEV_BIAS_LOG;
215663207adSDon Brady 	else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
216663207adSDon Brady 		alloc_bias = VDEV_BIAS_SPECIAL;
217663207adSDon Brady 	else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
218663207adSDon Brady 		alloc_bias = VDEV_BIAS_DEDUP;
219663207adSDon Brady 
220663207adSDon Brady 	return (alloc_bias);
221663207adSDon Brady }
222663207adSDon Brady 
223094e47e9SGeorge Wilson /* ARGSUSED */
224094e47e9SGeorge Wilson void
vdev_default_xlate(vdev_t * vd,const range_seg64_t * in,range_seg64_t * res)2254d7988d6SPaul Dagnelie vdev_default_xlate(vdev_t *vd, const range_seg64_t *in, range_seg64_t *res)
226094e47e9SGeorge Wilson {
227094e47e9SGeorge Wilson 	res->rs_start = in->rs_start;
228094e47e9SGeorge Wilson 	res->rs_end = in->rs_end;
229094e47e9SGeorge Wilson }
230094e47e9SGeorge Wilson 
231fa9e4066Sahrens /*
232fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
233fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
234fa9e4066Sahrens  */
235fa9e4066Sahrens uint64_t
vdev_default_asize(vdev_t * vd,uint64_t psize)236fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
237fa9e4066Sahrens {
238ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
239fa9e4066Sahrens 	uint64_t csize;
240fa9e4066Sahrens 
241573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
242fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
243fa9e4066Sahrens 		asize = MAX(asize, csize);
244fa9e4066Sahrens 	}
245fa9e4066Sahrens 
246fa9e4066Sahrens 	return (asize);
247fa9e4066Sahrens }
248fa9e4066Sahrens 
2492a79c5feSlling /*
250573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
251573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
252573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
253573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
2542a79c5feSlling  */
2552a79c5feSlling uint64_t
vdev_get_min_asize(vdev_t * vd)256573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
2572a79c5feSlling {
258573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
2592a79c5feSlling 
2602a79c5feSlling 	/*
2614263d13fSGeorge Wilson 	 * If our parent is NULL (inactive spare or cache) or is the root,
262573ca77eSGeorge Wilson 	 * just return our own asize.
2632a79c5feSlling 	 */
264573ca77eSGeorge Wilson 	if (pvd == NULL)
265573ca77eSGeorge Wilson 		return (vd->vdev_asize);
2662a79c5feSlling 
267573ca77eSGeorge Wilson 	/*
268573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
269573ca77eSGeorge Wilson 	 * to the nearest metaslab.
270573ca77eSGeorge Wilson 	 */
271573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
272573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
2732a79c5feSlling 
274573ca77eSGeorge Wilson 	/*
275573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
276573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
277573ca77eSGeorge Wilson 	 */
278573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
279c040c10cSSteven Hartland 		return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
280c040c10cSSteven Hartland 		    pvd->vdev_children);
281573ca77eSGeorge Wilson 
282573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
2832a79c5feSlling }
2842a79c5feSlling 
285573ca77eSGeorge Wilson void
vdev_set_min_asize(vdev_t * vd)286573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
287573ca77eSGeorge Wilson {
288573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
289573ca77eSGeorge Wilson 
290573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
291573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
2922a79c5feSlling }
2932a79c5feSlling 
294fa9e4066Sahrens vdev_t *
vdev_lookup_top(spa_t * spa,uint64_t vdev)295fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
296fa9e4066Sahrens {
297fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
298fa9e4066Sahrens 
299e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
300e05725b1Sbonwick 
301088f3894Sahrens 	if (vdev < rvd->vdev_children) {
302088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
303fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
304088f3894Sahrens 	}
305fa9e4066Sahrens 
306fa9e4066Sahrens 	return (NULL);
307fa9e4066Sahrens }
308fa9e4066Sahrens 
309fa9e4066Sahrens vdev_t *
vdev_lookup_by_guid(vdev_t * vd,uint64_t guid)310fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
311fa9e4066Sahrens {
312fa9e4066Sahrens 	vdev_t *mvd;
313fa9e4066Sahrens 
3140e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
315fa9e4066Sahrens 		return (vd);
316fa9e4066Sahrens 
317573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
318fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
319fa9e4066Sahrens 		    NULL)
320fa9e4066Sahrens 			return (mvd);
321fa9e4066Sahrens 
322fa9e4066Sahrens 	return (NULL);
323fa9e4066Sahrens }
324fa9e4066Sahrens 
32512380e1eSArne Jansen static int
vdev_count_leaves_impl(vdev_t * vd)32612380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd)
32712380e1eSArne Jansen {
32812380e1eSArne Jansen 	int n = 0;
32912380e1eSArne Jansen 
33012380e1eSArne Jansen 	if (vd->vdev_ops->vdev_op_leaf)
33112380e1eSArne Jansen 		return (1);
33212380e1eSArne Jansen 
33312380e1eSArne Jansen 	for (int c = 0; c < vd->vdev_children; c++)
33412380e1eSArne Jansen 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
33512380e1eSArne Jansen 
33612380e1eSArne Jansen 	return (n);
33712380e1eSArne Jansen }
33812380e1eSArne Jansen 
33912380e1eSArne Jansen int
vdev_count_leaves(spa_t * spa)34012380e1eSArne Jansen vdev_count_leaves(spa_t *spa)
34112380e1eSArne Jansen {
3420a663d1eSOlaf Faaland 	int rc;
3430a663d1eSOlaf Faaland 
3440a663d1eSOlaf Faaland 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3450a663d1eSOlaf Faaland 	rc = vdev_count_leaves_impl(spa->spa_root_vdev);
3460a663d1eSOlaf Faaland 	spa_config_exit(spa, SCL_VDEV, FTAG);
3470a663d1eSOlaf Faaland 
3480a663d1eSOlaf Faaland 	return (rc);
34912380e1eSArne Jansen }
35012380e1eSArne Jansen 
351fa9e4066Sahrens void
vdev_add_child(vdev_t * pvd,vdev_t * cvd)352fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
353fa9e4066Sahrens {
354fa9e4066Sahrens 	size_t oldsize, newsize;
355fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
356fa9e4066Sahrens 	vdev_t **newchild;
35781cd5c55SMatthew Ahrens 	spa_t *spa = cvd->vdev_spa;
358fa9e4066Sahrens 
35981cd5c55SMatthew Ahrens 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
360fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
361fa9e4066Sahrens 
362fa9e4066Sahrens 	cvd->vdev_parent = pvd;
363fa9e4066Sahrens 
364fa9e4066Sahrens 	if (pvd == NULL)
365fa9e4066Sahrens 		return;
366fa9e4066Sahrens 
367fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
368fa9e4066Sahrens 
369fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
370fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
371fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
372fa9e4066Sahrens 
373fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
374fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
375fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
376fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
377fa9e4066Sahrens 	}
378fa9e4066Sahrens 
379fa9e4066Sahrens 	pvd->vdev_child = newchild;
380fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
381fa9e4066Sahrens 
382fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
383fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
384fa9e4066Sahrens 
385fa9e4066Sahrens 	/*
386fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
387fa9e4066Sahrens 	 */
388fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
389fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
390e0f1c0afSOlaf Faaland 
391e0f1c0afSOlaf Faaland 	if (cvd->vdev_ops->vdev_op_leaf) {
392e0f1c0afSOlaf Faaland 		list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
393e0f1c0afSOlaf Faaland 		cvd->vdev_spa->spa_leaf_list_gen++;
394e0f1c0afSOlaf Faaland 	}
395fa9e4066Sahrens }
396fa9e4066Sahrens 
397fa9e4066Sahrens void
vdev_remove_child(vdev_t * pvd,vdev_t * cvd)398fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
399fa9e4066Sahrens {
400fa9e4066Sahrens 	int c;
401fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
402fa9e4066Sahrens 
403fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
404fa9e4066Sahrens 
405fa9e4066Sahrens 	if (pvd == NULL)
406fa9e4066Sahrens 		return;
407fa9e4066Sahrens 
408fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
409fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
410fa9e4066Sahrens 
411fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
412fa9e4066Sahrens 	cvd->vdev_parent = NULL;
413fa9e4066Sahrens 
414fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
415fa9e4066Sahrens 		if (pvd->vdev_child[c])
416fa9e4066Sahrens 			break;
417fa9e4066Sahrens 
418fa9e4066Sahrens 	if (c == pvd->vdev_children) {
419fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
420fa9e4066Sahrens 		pvd->vdev_child = NULL;
421fa9e4066Sahrens 		pvd->vdev_children = 0;
422fa9e4066Sahrens 	}
423fa9e4066Sahrens 
424e0f1c0afSOlaf Faaland 	if (cvd->vdev_ops->vdev_op_leaf) {
425e0f1c0afSOlaf Faaland 		spa_t *spa = cvd->vdev_spa;
426e0f1c0afSOlaf Faaland 		list_remove(&spa->spa_leaf_list, cvd);
427e0f1c0afSOlaf Faaland 		spa->spa_leaf_list_gen++;
428e0f1c0afSOlaf Faaland 	}
429e0f1c0afSOlaf Faaland 
430fa9e4066Sahrens 	/*
431fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
432fa9e4066Sahrens 	 */
433fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
434fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
435fa9e4066Sahrens }
436fa9e4066Sahrens 
437fa9e4066Sahrens /*
438fa9e4066Sahrens  * Remove any holes in the child array.
439fa9e4066Sahrens  */
440fa9e4066Sahrens void
vdev_compact_children(vdev_t * pvd)441fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
442fa9e4066Sahrens {
443fa9e4066Sahrens 	vdev_t **newchild, *cvd;
444fa9e4066Sahrens 	int oldc = pvd->vdev_children;
445573ca77eSGeorge Wilson 	int newc;
446fa9e4066Sahrens 
447e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
448fa9e4066Sahrens 
449573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
450fa9e4066Sahrens 		if (pvd->vdev_child[c])
451fa9e4066Sahrens 			newc++;
452fa9e4066Sahrens 
453*4ac140bfSBill Sommerfeld 	/*
454*4ac140bfSBill Sommerfeld 	 * If there are no remaining children (possible if this is an indirect
455*4ac140bfSBill Sommerfeld 	 * vdev) just free the old child array and return to avoid a pointless
456*4ac140bfSBill Sommerfeld 	 * zero-byte alloc.
457*4ac140bfSBill Sommerfeld 	 */
458*4ac140bfSBill Sommerfeld 	if (newc == 0) {
459*4ac140bfSBill Sommerfeld 		kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
460*4ac140bfSBill Sommerfeld 		pvd->vdev_child = NULL;
461*4ac140bfSBill Sommerfeld 		pvd->vdev_children = newc;
462*4ac140bfSBill Sommerfeld 		return;
463*4ac140bfSBill Sommerfeld 	}
464*4ac140bfSBill Sommerfeld 
465fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
466fa9e4066Sahrens 
467573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
468fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
469fa9e4066Sahrens 			newchild[newc] = cvd;
470fa9e4066Sahrens 			cvd->vdev_id = newc++;
471fa9e4066Sahrens 		}
472fa9e4066Sahrens 	}
473fa9e4066Sahrens 
474fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
475fa9e4066Sahrens 	pvd->vdev_child = newchild;
476fa9e4066Sahrens 	pvd->vdev_children = newc;
477fa9e4066Sahrens }
478fa9e4066Sahrens 
479fa9e4066Sahrens /*
480fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
481fa9e4066Sahrens  */
48288ecc943SGeorge Wilson vdev_t *
vdev_alloc_common(spa_t * spa,uint_t id,uint64_t guid,vdev_ops_t * ops)483fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
484fa9e4066Sahrens {
485fa9e4066Sahrens 	vdev_t *vd;
4865cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
487fa9e4066Sahrens 
488fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
4895cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
490fa9e4066Sahrens 
4910e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
4920e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
4930e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
494e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
4950e34b6a7Sbonwick 	}
4960e34b6a7Sbonwick 
49788ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
4980e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
4990e34b6a7Sbonwick 			/*
5000e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
5010e34b6a7Sbonwick 			 * which must be unique among all pools.
5020e34b6a7Sbonwick 			 */
5031195e687SMark J Musante 			guid = spa_generate_guid(NULL);
5040e34b6a7Sbonwick 		} else {
5050e34b6a7Sbonwick 			/*
5060e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
5070e34b6a7Sbonwick 			 */
5081195e687SMark J Musante 			guid = spa_generate_guid(spa);
5090e34b6a7Sbonwick 		}
5100e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
5110e34b6a7Sbonwick 	}
5120e34b6a7Sbonwick 
513fa9e4066Sahrens 	vd->vdev_spa = spa;
514fa9e4066Sahrens 	vd->vdev_id = id;
515fa9e4066Sahrens 	vd->vdev_guid = guid;
516fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
517fa9e4066Sahrens 	vd->vdev_ops = ops;
518fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
51988ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
5205cabbc6bSPrashanth Sreenivasa 	vic->vic_prev_indirect_vdev = UINT64_MAX;
5215cabbc6bSPrashanth Sreenivasa 
5225cabbc6bSPrashanth Sreenivasa 	rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
5235cabbc6bSPrashanth Sreenivasa 	mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
5244d7988d6SPaul Dagnelie 	vd->vdev_obsolete_segments = range_tree_create(NULL, RANGE_SEG64, NULL,
5254d7988d6SPaul Dagnelie 	    0, 0);
526fa9e4066Sahrens 
527084fd14fSBrian Behlendorf 	list_link_init(&vd->vdev_initialize_node);
528e0f1c0afSOlaf Faaland 	list_link_init(&vd->vdev_leaf_node);
529084fd14fSBrian Behlendorf 	list_link_init(&vd->vdev_trim_node);
530fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
5315ad82045Snd150628 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
532e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
533a3874b8bSToomas Soome 	mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL);
534094e47e9SGeorge Wilson 	mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
535094e47e9SGeorge Wilson 	mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
536094e47e9SGeorge Wilson 	cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
537094e47e9SGeorge Wilson 	cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
538084fd14fSBrian Behlendorf 	mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL);
539084fd14fSBrian Behlendorf 	mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL);
540084fd14fSBrian Behlendorf 	mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL);
541084fd14fSBrian Behlendorf 	cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL);
542084fd14fSBrian Behlendorf 	cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL);
543084fd14fSBrian Behlendorf 	cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL);
544094e47e9SGeorge Wilson 
5458ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
5464d7988d6SPaul Dagnelie 		vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
5474d7988d6SPaul Dagnelie 		    0);
5488ad4d6ddSJeff Bonwick 	}
549b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_ms_list, spa,
550fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
551b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_dtl_list, spa,
552fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
553fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
5543d7072f8Seschrock 	vdev_queue_init(vd);
5553d7072f8Seschrock 	vdev_cache_init(vd);
556fa9e4066Sahrens 
557fa9e4066Sahrens 	return (vd);
558fa9e4066Sahrens }
559fa9e4066Sahrens 
560fa9e4066Sahrens /*
561fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
562fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
563fa9e4066Sahrens  * different for each case.
564fa9e4066Sahrens  */
56599653d4eSeschrock int
vdev_alloc(spa_t * spa,vdev_t ** vdp,nvlist_t * nv,vdev_t * parent,uint_t id,int alloctype)56699653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
56799653d4eSeschrock     int alloctype)
568fa9e4066Sahrens {
569fa9e4066Sahrens 	vdev_ops_t *ops;
570fa9e4066Sahrens 	char *type;
5718654d025Sperrin 	uint64_t guid = 0, islog, nparity;
572fa9e4066Sahrens 	vdev_t *vd;
5735cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
574663207adSDon Brady 	vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
575663207adSDon Brady 	boolean_t top_level = (parent && !parent->vdev_parent);
576fa9e4066Sahrens 
577e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
578fa9e4066Sahrens 
579fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
580be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
581fa9e4066Sahrens 
582fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
583be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
584fa9e4066Sahrens 
585fa9e4066Sahrens 	/*
586fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
587fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
588fa9e4066Sahrens 	 */
589fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
590fa9e4066Sahrens 		uint64_t label_id;
591fa9e4066Sahrens 
592fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
593fa9e4066Sahrens 		    label_id != id)
594be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
595fa9e4066Sahrens 
596fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
597be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
59899653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
59999653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
600be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
601fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
602fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
603be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
60421ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
60521ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
606be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
607fa9e4066Sahrens 	}
608fa9e4066Sahrens 
60999653d4eSeschrock 	/*
61099653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
61199653d4eSeschrock 	 */
61299653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
613be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
61499653d4eSeschrock 
6158654d025Sperrin 	/*
6168654d025Sperrin 	 * Determine whether we're a log vdev.
6178654d025Sperrin 	 */
6188654d025Sperrin 	islog = 0;
6198654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
620990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
621be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
6228654d025Sperrin 
62388ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
624be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
62588ecc943SGeorge Wilson 
6268654d025Sperrin 	/*
6278654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
6288654d025Sperrin 	 */
6298654d025Sperrin 	nparity = -1ULL;
6308654d025Sperrin 	if (ops == &vdev_raidz_ops) {
6318654d025Sperrin 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
6328654d025Sperrin 		    &nparity) == 0) {
633b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
634be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
6358654d025Sperrin 			/*
636f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
637f94275ceSAdam Leventhal 			 * device.
6388654d025Sperrin 			 */
639f94275ceSAdam Leventhal 			if (nparity > 1 &&
640f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
641be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
642f94275ceSAdam Leventhal 			if (nparity > 2 &&
643f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
644be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
6458654d025Sperrin 		} else {
6468654d025Sperrin 			/*
6478654d025Sperrin 			 * We require the parity to be specified for SPAs that
6488654d025Sperrin 			 * support multiple parity levels.
6498654d025Sperrin 			 */
650f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
651be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
6528654d025Sperrin 			/*
6538654d025Sperrin 			 * Otherwise, we default to 1 parity device for RAID-Z.
6548654d025Sperrin 			 */
6558654d025Sperrin 			nparity = 1;
6568654d025Sperrin 		}
6578654d025Sperrin 	} else {
6588654d025Sperrin 		nparity = 0;
6598654d025Sperrin 	}
6608654d025Sperrin 	ASSERT(nparity != -1ULL);
6618654d025Sperrin 
662663207adSDon Brady 	/*
663663207adSDon Brady 	 * If creating a top-level vdev, check for allocation classes input
664663207adSDon Brady 	 */
665663207adSDon Brady 	if (top_level && alloctype == VDEV_ALLOC_ADD) {
666663207adSDon Brady 		char *bias;
667663207adSDon Brady 
668663207adSDon Brady 		if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
669663207adSDon Brady 		    &bias) == 0) {
670663207adSDon Brady 			alloc_bias = vdev_derive_alloc_bias(bias);
671663207adSDon Brady 
672663207adSDon Brady 			/* spa_vdev_add() expects feature to be enabled */
673c1064fd7SJerry Jelinek 			if (alloc_bias != VDEV_BIAS_LOG &&
674c1064fd7SJerry Jelinek 			    spa->spa_load_state != SPA_LOAD_CREATE &&
675663207adSDon Brady 			    !spa_feature_is_enabled(spa,
676663207adSDon Brady 			    SPA_FEATURE_ALLOCATION_CLASSES)) {
677663207adSDon Brady 				return (SET_ERROR(ENOTSUP));
678663207adSDon Brady 			}
679663207adSDon Brady 		}
680663207adSDon Brady 	}
681663207adSDon Brady 
682fa9e4066Sahrens 	vd = vdev_alloc_common(spa, id, guid, ops);
6835cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
684fa9e4066Sahrens 
6858654d025Sperrin 	vd->vdev_islog = islog;
6868654d025Sperrin 	vd->vdev_nparity = nparity;
687663207adSDon Brady 	if (top_level && alloc_bias != VDEV_BIAS_NONE)
688663207adSDon Brady 		vd->vdev_alloc_bias = alloc_bias;
6898654d025Sperrin 
690fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
691fa9e4066Sahrens 		vd->vdev_path = spa_strdup(vd->vdev_path);
692fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
693fa9e4066Sahrens 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
6943d7072f8Seschrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
6953d7072f8Seschrock 	    &vd->vdev_physpath) == 0)
6963d7072f8Seschrock 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
6976809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
6986809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
699fa9e4066Sahrens 
700fa9e4066Sahrens 	/*
701afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
702afefbcddSeschrock 	 * as -1.
703afefbcddSeschrock 	 */
704afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
705afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
706afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
707afefbcddSeschrock 
7085cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_mapping_object);
7095cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
7105cabbc6bSPrashanth Sreenivasa 	    &vic->vic_mapping_object);
7115cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_births_object);
7125cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
7135cabbc6bSPrashanth Sreenivasa 	    &vic->vic_births_object);
7145cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
7155cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
7165cabbc6bSPrashanth Sreenivasa 	    &vic->vic_prev_indirect_vdev);
7175cabbc6bSPrashanth Sreenivasa 
718afefbcddSeschrock 	/*
719ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
720ea8dc4b6Seschrock 	 * was not present at the time of import.
721ea8dc4b6Seschrock 	 */
722ea8dc4b6Seschrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
723ea8dc4b6Seschrock 	    &vd->vdev_not_present);
724ea8dc4b6Seschrock 
725ea8dc4b6Seschrock 	/*
726ecc2d604Sbonwick 	 * Get the alignment requirement.
727ecc2d604Sbonwick 	 */
728ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
729ecc2d604Sbonwick 
730ecc2d604Sbonwick 	/*
73188ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
73288ecc943SGeorge Wilson 	 */
73388ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
73488ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
73588ecc943SGeorge Wilson 
73688ecc943SGeorge Wilson 	/*
737fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
738fa9e4066Sahrens 	 */
739663207adSDon Brady 	if (top_level &&
7401195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
741fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
742fa9e4066Sahrens 		    &vd->vdev_ms_array);
743fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
744fa9e4066Sahrens 		    &vd->vdev_ms_shift);
745fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
746fa9e4066Sahrens 		    &vd->vdev_asize);
7473f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
7483f9d6ad7SLin Ling 		    &vd->vdev_removing);
749215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
750215198a6SJoe Stein 		    &vd->vdev_top_zap);
751215198a6SJoe Stein 	} else {
752215198a6SJoe Stein 		ASSERT0(vd->vdev_top_zap);
753fa9e4066Sahrens 	}
754fa9e4066Sahrens 
755663207adSDon Brady 	if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
756a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
7579f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
7581195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
7599f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
760663207adSDon Brady 		/* Note: metaslab_group_create() is now deferred */
761a1521560SJeff Bonwick 	}
762a1521560SJeff Bonwick 
763215198a6SJoe Stein 	if (vd->vdev_ops->vdev_op_leaf &&
764215198a6SJoe Stein 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
765215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv,
766215198a6SJoe Stein 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
767215198a6SJoe Stein 	} else {
768215198a6SJoe Stein 		ASSERT0(vd->vdev_leaf_zap);
769215198a6SJoe Stein 	}
770215198a6SJoe Stein 
771fa9e4066Sahrens 	/*
7723d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
773fa9e4066Sahrens 	 */
774215198a6SJoe Stein 
775c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
77621ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
77721ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
778c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
779fa9e4066Sahrens 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
7800713e232SGeorge Wilson 			    &vd->vdev_dtl_object);
7813d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
7823d7072f8Seschrock 			    &vd->vdev_unspare);
783c5904d13Seschrock 		}
78421ecdf64SLin Ling 
78521ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
78621ecdf64SLin Ling 			uint64_t spare = 0;
78721ecdf64SLin Ling 
78821ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
78921ecdf64SLin Ling 			    &spare) == 0 && spare)
79021ecdf64SLin Ling 				spa_spare_add(vd);
79121ecdf64SLin Ling 		}
79221ecdf64SLin Ling 
793c5904d13Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
794c5904d13Seschrock 		    &vd->vdev_offline);
795c5904d13Seschrock 
796b4952e17SGeorge Wilson 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
797b4952e17SGeorge Wilson 		    &vd->vdev_resilver_txg);
798cb04b873SMark J Musante 
799e4c795beSTom Caputi 		if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
8000c06d385Sjwpoduska 			vdev_defer_resilver(vd);
801e4c795beSTom Caputi 
8023d7072f8Seschrock 		/*
8033d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
8043d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
805069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
806069f55e2SEric Schrock 		 * remain in the faulted state.
8073d7072f8Seschrock 		 */
808b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
8093d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
8103d7072f8Seschrock 			    &vd->vdev_faulted);
8113d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
8123d7072f8Seschrock 			    &vd->vdev_degraded);
8133d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
8143d7072f8Seschrock 			    &vd->vdev_removed);
815069f55e2SEric Schrock 
816069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
817069f55e2SEric Schrock 				char *aux;
818069f55e2SEric Schrock 
819069f55e2SEric Schrock 				vd->vdev_label_aux =
820069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
821069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
822069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
823069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
824069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
825069f55e2SEric Schrock 			}
8263d7072f8Seschrock 		}
827fa9e4066Sahrens 	}
828fa9e4066Sahrens 
829fa9e4066Sahrens 	/*
830fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
831fa9e4066Sahrens 	 */
832fa9e4066Sahrens 	vdev_add_child(parent, vd);
833fa9e4066Sahrens 
83499653d4eSeschrock 	*vdp = vd;
83599653d4eSeschrock 
83699653d4eSeschrock 	return (0);
837fa9e4066Sahrens }
838fa9e4066Sahrens 
839fa9e4066Sahrens void
vdev_free(vdev_t * vd)840fa9e4066Sahrens vdev_free(vdev_t *vd)
841fa9e4066Sahrens {
8423d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
843084fd14fSBrian Behlendorf 
844094e47e9SGeorge Wilson 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
845084fd14fSBrian Behlendorf 	ASSERT3P(vd->vdev_trim_thread, ==, NULL);
846084fd14fSBrian Behlendorf 	ASSERT3P(vd->vdev_autotrim_thread, ==, NULL);
847fa9e4066Sahrens 
848fa9e4066Sahrens 	/*
849a3874b8bSToomas Soome 	 * Scan queues are normally destroyed at the end of a scan. If the
850a3874b8bSToomas Soome 	 * queue exists here, that implies the vdev is being removed while
851a3874b8bSToomas Soome 	 * the scan is still running.
852a3874b8bSToomas Soome 	 */
853a3874b8bSToomas Soome 	if (vd->vdev_scan_io_queue != NULL) {
854a3874b8bSToomas Soome 		mutex_enter(&vd->vdev_scan_io_queue_lock);
855a3874b8bSToomas Soome 		dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue);
856a3874b8bSToomas Soome 		vd->vdev_scan_io_queue = NULL;
857a3874b8bSToomas Soome 		mutex_exit(&vd->vdev_scan_io_queue_lock);
858a3874b8bSToomas Soome 	}
859a3874b8bSToomas Soome 
860a3874b8bSToomas Soome 	/*
861fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
862fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
863fa9e4066Sahrens 	 */
864fa9e4066Sahrens 	vdev_close(vd);
865fa9e4066Sahrens 
866e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
867b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
868fa9e4066Sahrens 
869fa9e4066Sahrens 	/*
870fa9e4066Sahrens 	 * Free all children.
871fa9e4066Sahrens 	 */
872573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
873fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
874fa9e4066Sahrens 
875fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
876fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
877fa9e4066Sahrens 
878fa9e4066Sahrens 	/*
879fa9e4066Sahrens 	 * Discard allocation state.
880fa9e4066Sahrens 	 */
881a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
882fa9e4066Sahrens 		vdev_metaslab_fini(vd);
883a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
884814dcd43SSerapheim Dimitropoulos 		vd->vdev_mg = NULL;
885a1521560SJeff Bonwick 	}
886fa9e4066Sahrens 
887fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_space);
888fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_dspace);
889fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
890fa9e4066Sahrens 
891fa9e4066Sahrens 	/*
892fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
893fa9e4066Sahrens 	 */
894fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
895fa9e4066Sahrens 
896fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
897e0f1c0afSOlaf Faaland 	ASSERT(!list_link_active(&vd->vdev_leaf_node));
898fa9e4066Sahrens 
8993d7072f8Seschrock 	/*
9003d7072f8Seschrock 	 * Clean up vdev structure.
9013d7072f8Seschrock 	 */
9023d7072f8Seschrock 	vdev_queue_fini(vd);
9033d7072f8Seschrock 	vdev_cache_fini(vd);
9043d7072f8Seschrock 
9053d7072f8Seschrock 	if (vd->vdev_path)
9063d7072f8Seschrock 		spa_strfree(vd->vdev_path);
9073d7072f8Seschrock 	if (vd->vdev_devid)
9083d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
9093d7072f8Seschrock 	if (vd->vdev_physpath)
9103d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
9116809eb4eSEric Schrock 	if (vd->vdev_fru)
9126809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
9133d7072f8Seschrock 
9143d7072f8Seschrock 	if (vd->vdev_isspare)
9153d7072f8Seschrock 		spa_spare_remove(vd);
916fa94a07fSbrendan 	if (vd->vdev_isl2cache)
917fa94a07fSbrendan 		spa_l2cache_remove(vd);
9183d7072f8Seschrock 
9193d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
9203d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
9218ad4d6ddSJeff Bonwick 
9223d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
9230713e232SGeorge Wilson 	space_map_close(vd->vdev_dtl_sm);
9248ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
9250713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
9260713e232SGeorge Wilson 		range_tree_destroy(vd->vdev_dtl[t]);
9278ad4d6ddSJeff Bonwick 	}
9283d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
9298ad4d6ddSJeff Bonwick 
9305cabbc6bSPrashanth Sreenivasa 	EQUIV(vd->vdev_indirect_births != NULL,
9315cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping != NULL);
9325cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_indirect_births != NULL) {
9335cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
9345cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_close(vd->vdev_indirect_births);
9355cabbc6bSPrashanth Sreenivasa 	}
9365cabbc6bSPrashanth Sreenivasa 
9375cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
9385cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
9395cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
9405cabbc6bSPrashanth Sreenivasa 		space_map_close(vd->vdev_obsolete_sm);
9415cabbc6bSPrashanth Sreenivasa 		vd->vdev_obsolete_sm = NULL;
9425cabbc6bSPrashanth Sreenivasa 	}
9435cabbc6bSPrashanth Sreenivasa 	range_tree_destroy(vd->vdev_obsolete_segments);
9445cabbc6bSPrashanth Sreenivasa 	rw_destroy(&vd->vdev_indirect_rwlock);
9455cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&vd->vdev_obsolete_lock);
9465cabbc6bSPrashanth Sreenivasa 
9473d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
9483d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
949e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
950a3874b8bSToomas Soome 	mutex_destroy(&vd->vdev_scan_io_queue_lock);
951094e47e9SGeorge Wilson 	mutex_destroy(&vd->vdev_initialize_lock);
952094e47e9SGeorge Wilson 	mutex_destroy(&vd->vdev_initialize_io_lock);
953094e47e9SGeorge Wilson 	cv_destroy(&vd->vdev_initialize_io_cv);
954094e47e9SGeorge Wilson 	cv_destroy(&vd->vdev_initialize_cv);
955084fd14fSBrian Behlendorf 	mutex_destroy(&vd->vdev_trim_lock);
956084fd14fSBrian Behlendorf 	mutex_destroy(&vd->vdev_autotrim_lock);
957084fd14fSBrian Behlendorf 	mutex_destroy(&vd->vdev_trim_io_lock);
958084fd14fSBrian Behlendorf 	cv_destroy(&vd->vdev_trim_cv);
959084fd14fSBrian Behlendorf 	cv_destroy(&vd->vdev_autotrim_cv);
960084fd14fSBrian Behlendorf 	cv_destroy(&vd->vdev_trim_io_cv);
9613d7072f8Seschrock 
9623d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
9633d7072f8Seschrock 		spa->spa_root_vdev = NULL;
9643d7072f8Seschrock 
9653d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
966fa9e4066Sahrens }
967fa9e4066Sahrens 
968fa9e4066Sahrens /*
969fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
970fa9e4066Sahrens  */
971fa9e4066Sahrens static void
vdev_top_transfer(vdev_t * svd,vdev_t * tvd)972fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
973fa9e4066Sahrens {
974fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
975fa9e4066Sahrens 	metaslab_t *msp;
976fa9e4066Sahrens 	vdev_t *vd;
977fa9e4066Sahrens 	int t;
978fa9e4066Sahrens 
979fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
980fa9e4066Sahrens 
981fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
982fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
983fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
984215198a6SJoe Stein 	tvd->vdev_top_zap = svd->vdev_top_zap;
985fa9e4066Sahrens 
986fa9e4066Sahrens 	svd->vdev_ms_array = 0;
987fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
988fa9e4066Sahrens 	svd->vdev_ms_count = 0;
989215198a6SJoe Stein 	svd->vdev_top_zap = 0;
990fa9e4066Sahrens 
991cd0837ccSGeorge Wilson 	if (tvd->vdev_mg)
992cd0837ccSGeorge Wilson 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
993fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
994fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
995fa9e4066Sahrens 
996fa9e4066Sahrens 	svd->vdev_mg = NULL;
997fa9e4066Sahrens 	svd->vdev_ms = NULL;
998ecc2d604Sbonwick 
999ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
1000ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
1001fa9e4066Sahrens 
100286714001SSerapheim Dimitropoulos 	tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
100386714001SSerapheim Dimitropoulos 	svd->vdev_checkpoint_sm = NULL;
100486714001SSerapheim Dimitropoulos 
1005663207adSDon Brady 	tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1006663207adSDon Brady 	svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1007663207adSDon Brady 
1008fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
1009fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
101099653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
1011fa9e4066Sahrens 
1012fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
1013fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
101499653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
1015fa9e4066Sahrens 
10163a4b1be9SMatthew Ahrens 	/*
10173a4b1be9SMatthew Ahrens 	 * State which may be set on a top-level vdev that's in the
10183a4b1be9SMatthew Ahrens 	 * process of being removed.
10193a4b1be9SMatthew Ahrens 	 */
10203a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_indirect_config.vic_births_object);
10213a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
10223a4b1be9SMatthew Ahrens 	ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
10233a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
10243a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
10253a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
10263a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_removing);
10273a4b1be9SMatthew Ahrens 	tvd->vdev_removing = svd->vdev_removing;
10283a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_config = svd->vdev_indirect_config;
10293a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
10303a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_births = svd->vdev_indirect_births;
10313a4b1be9SMatthew Ahrens 	range_tree_swap(&svd->vdev_obsolete_segments,
10323a4b1be9SMatthew Ahrens 	    &tvd->vdev_obsolete_segments);
10333a4b1be9SMatthew Ahrens 	tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
10343a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_mapping_object = 0;
10353a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_births_object = 0;
10363a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
10373a4b1be9SMatthew Ahrens 	svd->vdev_indirect_mapping = NULL;
10383a4b1be9SMatthew Ahrens 	svd->vdev_indirect_births = NULL;
10393a4b1be9SMatthew Ahrens 	svd->vdev_obsolete_sm = NULL;
10403a4b1be9SMatthew Ahrens 	svd->vdev_removing = 0;
10413a4b1be9SMatthew Ahrens 
1042fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
1043fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
1044fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
1045fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
1046fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
1047fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
1048fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
1049fa9e4066Sahrens 	}
1050fa9e4066Sahrens 
1051e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
1052fa9e4066Sahrens 		vdev_config_clean(svd);
1053fa9e4066Sahrens 		vdev_config_dirty(tvd);
1054fa9e4066Sahrens 	}
1055fa9e4066Sahrens 
1056e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
1057e14bb325SJeff Bonwick 		vdev_state_clean(svd);
1058e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
1059e14bb325SJeff Bonwick 	}
1060e14bb325SJeff Bonwick 
106199653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
106299653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
10638654d025Sperrin 
10648654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
10658654d025Sperrin 	svd->vdev_islog = 0;
1066a3874b8bSToomas Soome 
1067a3874b8bSToomas Soome 	dsl_scan_io_queue_vdev_xfer(svd, tvd);
1068fa9e4066Sahrens }
1069fa9e4066Sahrens 
1070fa9e4066Sahrens static void
vdev_top_update(vdev_t * tvd,vdev_t * vd)1071fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
1072fa9e4066Sahrens {
1073fa9e4066Sahrens 	if (vd == NULL)
1074fa9e4066Sahrens 		return;
1075fa9e4066Sahrens 
1076fa9e4066Sahrens 	vd->vdev_top = tvd;
1077fa9e4066Sahrens 
1078573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1079fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
1080fa9e4066Sahrens }
1081fa9e4066Sahrens 
1082fa9e4066Sahrens /*
1083fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
1084fa9e4066Sahrens  */
1085fa9e4066Sahrens vdev_t *
vdev_add_parent(vdev_t * cvd,vdev_ops_t * ops)1086fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1087fa9e4066Sahrens {
1088fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
1089fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
1090fa9e4066Sahrens 	vdev_t *mvd;
1091fa9e4066Sahrens 
1092e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1093fa9e4066Sahrens 
1094fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1095ecc2d604Sbonwick 
1096ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
1097573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
10984263d13fSGeorge Wilson 	mvd->vdev_max_asize = cvd->vdev_max_asize;
10995cabbc6bSPrashanth Sreenivasa 	mvd->vdev_psize = cvd->vdev_psize;
1100ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
1101ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
110288ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
1103ecc2d604Sbonwick 
1104fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
1105fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
1106fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
1107fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
1108fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1109fa9e4066Sahrens 
1110fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
1111fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
1112fa9e4066Sahrens 
1113fa9e4066Sahrens 	return (mvd);
1114fa9e4066Sahrens }
1115fa9e4066Sahrens 
1116fa9e4066Sahrens /*
1117fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
1118fa9e4066Sahrens  */
1119fa9e4066Sahrens void
vdev_remove_parent(vdev_t * cvd)1120fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
1121fa9e4066Sahrens {
1122fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
1123fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
1124fa9e4066Sahrens 
1125e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1126fa9e4066Sahrens 
1127fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
1128fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
112999653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
113099653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
1131ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
1132fa9e4066Sahrens 
1133fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
1134fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
11358ad4d6ddSJeff Bonwick 
1136e14bb325SJeff Bonwick 	/*
1137e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1138e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
1139e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
1140e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
1141e14bb325SJeff Bonwick 	 */
11428ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
11438ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
11441195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
11458ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
11468ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
11478ad4d6ddSJeff Bonwick 	}
1148fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_id;
1149fa9e4066Sahrens 	vdev_add_child(pvd, cvd);
1150fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1151fa9e4066Sahrens 
1152fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
1153fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
1154fa9e4066Sahrens 
1155fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
1156fa9e4066Sahrens 	vdev_free(mvd);
1157fa9e4066Sahrens }
1158fa9e4066Sahrens 
1159663207adSDon Brady static void
vdev_metaslab_group_create(vdev_t * vd)1160663207adSDon Brady vdev_metaslab_group_create(vdev_t *vd)
1161663207adSDon Brady {
1162663207adSDon Brady 	spa_t *spa = vd->vdev_spa;
1163663207adSDon Brady 
1164663207adSDon Brady 	/*
1165663207adSDon Brady 	 * metaslab_group_create was delayed until allocation bias was available
1166663207adSDon Brady 	 */
1167663207adSDon Brady 	if (vd->vdev_mg == NULL) {
1168663207adSDon Brady 		metaslab_class_t *mc;
1169663207adSDon Brady 
1170663207adSDon Brady 		if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1171663207adSDon Brady 			vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1172663207adSDon Brady 
1173663207adSDon Brady 		ASSERT3U(vd->vdev_islog, ==,
1174663207adSDon Brady 		    (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1175663207adSDon Brady 
1176663207adSDon Brady 		switch (vd->vdev_alloc_bias) {
1177663207adSDon Brady 		case VDEV_BIAS_LOG:
1178663207adSDon Brady 			mc = spa_log_class(spa);
1179663207adSDon Brady 			break;
1180663207adSDon Brady 		case VDEV_BIAS_SPECIAL:
1181663207adSDon Brady 			mc = spa_special_class(spa);
1182663207adSDon Brady 			break;
1183663207adSDon Brady 		case VDEV_BIAS_DEDUP:
1184663207adSDon Brady 			mc = spa_dedup_class(spa);
1185663207adSDon Brady 			break;
1186663207adSDon Brady 		default:
1187663207adSDon Brady 			mc = spa_normal_class(spa);
1188663207adSDon Brady 		}
1189663207adSDon Brady 
1190663207adSDon Brady 		vd->vdev_mg = metaslab_group_create(mc, vd,
1191663207adSDon Brady 		    spa->spa_alloc_count);
1192663207adSDon Brady 
1193663207adSDon Brady 		/*
1194663207adSDon Brady 		 * The spa ashift values currently only reflect the
1195663207adSDon Brady 		 * general vdev classes. Class destination is late
1196663207adSDon Brady 		 * binding so ashift checking had to wait until now
1197663207adSDon Brady 		 */
1198663207adSDon Brady 		if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1199663207adSDon Brady 		    mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1200663207adSDon Brady 			if (vd->vdev_ashift > spa->spa_max_ashift)
1201663207adSDon Brady 				spa->spa_max_ashift = vd->vdev_ashift;
1202663207adSDon Brady 			if (vd->vdev_ashift < spa->spa_min_ashift)
1203663207adSDon Brady 				spa->spa_min_ashift = vd->vdev_ashift;
1204663207adSDon Brady 		}
1205663207adSDon Brady 	}
1206663207adSDon Brady }
1207663207adSDon Brady 
1208ea8dc4b6Seschrock int
vdev_metaslab_init(vdev_t * vd,uint64_t txg)1209fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1210fa9e4066Sahrens {
1211fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
1212ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1213ecc2d604Sbonwick 	uint64_t m;
1214fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
1215fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1216ecc2d604Sbonwick 	metaslab_t **mspp;
1217ecc2d604Sbonwick 	int error;
1218663207adSDon Brady 	boolean_t expanding = (oldc != 0);
1219fa9e4066Sahrens 
1220a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1221a1521560SJeff Bonwick 
122288ecc943SGeorge Wilson 	/*
122388ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
122488ecc943SGeorge Wilson 	 */
122588ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
12260e34b6a7Sbonwick 		return (0);
12270e34b6a7Sbonwick 
122888ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
122988ecc943SGeorge Wilson 
1230fa9e4066Sahrens 	ASSERT(oldc <= newc);
1231fa9e4066Sahrens 
1232ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1233fa9e4066Sahrens 
1234663207adSDon Brady 	if (expanding) {
1235ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1236ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1237ecc2d604Sbonwick 	}
1238ecc2d604Sbonwick 
1239ecc2d604Sbonwick 	vd->vdev_ms = mspp;
1240ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
1241ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
1242ecc2d604Sbonwick 		uint64_t object = 0;
12430713e232SGeorge Wilson 
12445cabbc6bSPrashanth Sreenivasa 		/*
12455cabbc6bSPrashanth Sreenivasa 		 * vdev_ms_array may be 0 if we are creating the "fake"
12465cabbc6bSPrashanth Sreenivasa 		 * metaslabs for an indirect vdev for zdb's leak detection.
12475cabbc6bSPrashanth Sreenivasa 		 * See zdb_leak_init().
12485cabbc6bSPrashanth Sreenivasa 		 */
12495cabbc6bSPrashanth Sreenivasa 		if (txg == 0 && vd->vdev_ms_array != 0) {
1250ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
12517bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
12527bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
12533ee8c80cSPavel Zakharov 			if (error != 0) {
12543ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "unable to read the metaslab "
12553ee8c80cSPavel Zakharov 				    "array [error=%d]", error);
1256ecc2d604Sbonwick 				return (error);
1257ecc2d604Sbonwick 			}
12583ee8c80cSPavel Zakharov 		}
12591e9bd7ecSPrakash Surya 
1260663207adSDon Brady #ifndef _KERNEL
1261663207adSDon Brady 		/*
1262663207adSDon Brady 		 * To accomodate zdb_leak_init() fake indirect
1263663207adSDon Brady 		 * metaslabs, we allocate a metaslab group for
1264663207adSDon Brady 		 * indirect vdevs which normally don't have one.
1265663207adSDon Brady 		 */
1266663207adSDon Brady 		if (vd->vdev_mg == NULL) {
1267663207adSDon Brady 			ASSERT0(vdev_is_concrete(vd));
1268663207adSDon Brady 			vdev_metaslab_group_create(vd);
1269663207adSDon Brady 		}
1270663207adSDon Brady #endif
12711e9bd7ecSPrakash Surya 		error = metaslab_init(vd->vdev_mg, m, object, txg,
12721e9bd7ecSPrakash Surya 		    &(vd->vdev_ms[m]));
12733ee8c80cSPavel Zakharov 		if (error != 0) {
12743ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
12753ee8c80cSPavel Zakharov 			    error);
12761e9bd7ecSPrakash Surya 			return (error);
1277fa9e4066Sahrens 		}
12783ee8c80cSPavel Zakharov 	}
1279fa9e4066Sahrens 
1280a1521560SJeff Bonwick 	if (txg == 0)
1281a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1282a1521560SJeff Bonwick 
12833f9d6ad7SLin Ling 	/*
12843f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
12853f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
12863f9d6ad7SLin Ling 	 * allocations are performed on this device.
12873f9d6ad7SLin Ling 	 */
1288663207adSDon Brady 	if (!expanding && !vd->vdev_removing) {
1289a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
1290663207adSDon Brady 	}
1291a1521560SJeff Bonwick 
1292a1521560SJeff Bonwick 	if (txg == 0)
1293a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
1294a1521560SJeff Bonwick 
1295814dcd43SSerapheim Dimitropoulos 	/*
1296814dcd43SSerapheim Dimitropoulos 	 * Regardless whether this vdev was just added or it is being
1297814dcd43SSerapheim Dimitropoulos 	 * expanded, the metaslab count has changed. Recalculate the
1298814dcd43SSerapheim Dimitropoulos 	 * block limit.
1299814dcd43SSerapheim Dimitropoulos 	 */
1300814dcd43SSerapheim Dimitropoulos 	spa_log_sm_set_blocklimit(spa);
1301814dcd43SSerapheim Dimitropoulos 
1302ea8dc4b6Seschrock 	return (0);
1303fa9e4066Sahrens }
1304fa9e4066Sahrens 
1305fa9e4066Sahrens void
vdev_metaslab_fini(vdev_t * vd)1306fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
1307fa9e4066Sahrens {
130886714001SSerapheim Dimitropoulos 	if (vd->vdev_checkpoint_sm != NULL) {
130986714001SSerapheim Dimitropoulos 		ASSERT(spa_feature_is_active(vd->vdev_spa,
131086714001SSerapheim Dimitropoulos 		    SPA_FEATURE_POOL_CHECKPOINT));
131186714001SSerapheim Dimitropoulos 		space_map_close(vd->vdev_checkpoint_sm);
131286714001SSerapheim Dimitropoulos 		/*
131386714001SSerapheim Dimitropoulos 		 * Even though we close the space map, we need to set its
131486714001SSerapheim Dimitropoulos 		 * pointer to NULL. The reason is that vdev_metaslab_fini()
131586714001SSerapheim Dimitropoulos 		 * may be called multiple times for certain operations
131686714001SSerapheim Dimitropoulos 		 * (i.e. when destroying a pool) so we need to ensure that
131786714001SSerapheim Dimitropoulos 		 * this clause never executes twice. This logic is similar
131886714001SSerapheim Dimitropoulos 		 * to the one used for the vdev_ms clause below.
131986714001SSerapheim Dimitropoulos 		 */
132086714001SSerapheim Dimitropoulos 		vd->vdev_checkpoint_sm = NULL;
132186714001SSerapheim Dimitropoulos 	}
132286714001SSerapheim Dimitropoulos 
13235cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms != NULL) {
1324555d674dSSerapheim Dimitropoulos 		metaslab_group_t *mg = vd->vdev_mg;
1325555d674dSSerapheim Dimitropoulos 		metaslab_group_passivate(mg);
1326fa9e4066Sahrens 
1327555d674dSSerapheim Dimitropoulos 		uint64_t count = vd->vdev_ms_count;
13285cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < count; m++) {
13290713e232SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
13300713e232SGeorge Wilson 			if (msp != NULL)
13310713e232SGeorge Wilson 				metaslab_fini(msp);
13320713e232SGeorge Wilson 		}
1333fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1334fa9e4066Sahrens 		vd->vdev_ms = NULL;
13355cabbc6bSPrashanth Sreenivasa 
13365cabbc6bSPrashanth Sreenivasa 		vd->vdev_ms_count = 0;
1337555d674dSSerapheim Dimitropoulos 
1338555d674dSSerapheim Dimitropoulos 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
1339555d674dSSerapheim Dimitropoulos 			ASSERT0(mg->mg_histogram[i]);
1340fa9e4066Sahrens 	}
13415cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_ms_count);
1342fa9e4066Sahrens }
1343fa9e4066Sahrens 
1344e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
1345e14bb325SJeff Bonwick 	boolean_t	vps_readable;
1346e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
1347e14bb325SJeff Bonwick 	int		vps_flags;
1348e14bb325SJeff Bonwick } vdev_probe_stats_t;
1349e14bb325SJeff Bonwick 
1350e14bb325SJeff Bonwick static void
vdev_probe_done(zio_t * zio)1351e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
13520a4e9518Sgw25295 {
13538ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
1354a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
1355e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
1356a3f829aeSBill Moore 
1357a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
1358e14bb325SJeff Bonwick 
1359e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
1360e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1361e14bb325SJeff Bonwick 			vps->vps_readable = 1;
13628ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
1363a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1364770499e1SDan Kimmel 			    zio->io_offset, zio->io_size, zio->io_abd,
1365e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1366e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1367e14bb325SJeff Bonwick 		} else {
1368770499e1SDan Kimmel 			abd_free(zio->io_abd);
1369e14bb325SJeff Bonwick 		}
1370e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
1371e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1372e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
1373770499e1SDan Kimmel 		abd_free(zio->io_abd);
1374e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
1375a3f829aeSBill Moore 		zio_t *pio;
1376e14bb325SJeff Bonwick 
1377e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
1378e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
1379e14bb325SJeff Bonwick 
1380e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
13818ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
1382e14bb325SJeff Bonwick 			zio->io_error = 0;
1383e14bb325SJeff Bonwick 		} else {
1384e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
13853ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "failed probe");
13869b088140SToomas Soome 			(void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1387eb633035STom Caputi 			    spa, vd, NULL, NULL, 0, 0);
1388be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
1389e14bb325SJeff Bonwick 		}
1390a3f829aeSBill Moore 
1391a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
1392a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
1393a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
1394a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
1395a3f829aeSBill Moore 
13960f7643c7SGeorge Wilson 		zio_link_t *zl = NULL;
13970f7643c7SGeorge Wilson 		while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1398a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
1399be6fd75aSMatthew Ahrens 				pio->io_error = SET_ERROR(ENXIO);
1400a3f829aeSBill Moore 
1401e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
1402e14bb325SJeff Bonwick 	}
1403e14bb325SJeff Bonwick }
14040a4e9518Sgw25295 
14050a4e9518Sgw25295 /*
1406f7170741SWill Andrews  * Determine whether this device is accessible.
1407f7170741SWill Andrews  *
1408f7170741SWill Andrews  * Read and write to several known locations: the pad regions of each
1409f7170741SWill Andrews  * vdev label but the first, which we leave alone in case it contains
1410f7170741SWill Andrews  * a VTOC.
14110a4e9518Sgw25295  */
1412e14bb325SJeff Bonwick zio_t *
vdev_probe(vdev_t * vd,zio_t * zio)1413a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
1414e14bb325SJeff Bonwick {
1415e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1416a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
1417a3f829aeSBill Moore 	zio_t *pio;
14180a4e9518Sgw25295 
1419a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1420a3f829aeSBill Moore 
1421a3f829aeSBill Moore 	/*
1422a3f829aeSBill Moore 	 * Don't probe the probe.
1423a3f829aeSBill Moore 	 */
1424a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1425a3f829aeSBill Moore 		return (NULL);
1426a3f829aeSBill Moore 
1427a3f829aeSBill Moore 	/*
1428a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
1429a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
1430a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
1431a3f829aeSBill Moore 	 */
1432a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
1433a3f829aeSBill Moore 
1434a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
1435e14bb325SJeff Bonwick 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1436e14bb325SJeff Bonwick 
1437e14bb325SJeff Bonwick 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1438a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
14398956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
1440e14bb325SJeff Bonwick 
1441e14bb325SJeff Bonwick 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1442e14bb325SJeff Bonwick 			/*
1443a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
1444a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
1445a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
1446a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
1447a3f829aeSBill Moore 			 * any zio looking at these values can assume that
1448a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
1449a3f829aeSBill Moore 			 * important because when a device has intermittent
1450a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
1451a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1452a3f829aeSBill Moore 			 * the zio (EIO).
1453e14bb325SJeff Bonwick 			 *
1454a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1455a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1456a3f829aeSBill Moore 			 * principles.
1457e14bb325SJeff Bonwick 			 */
1458e14bb325SJeff Bonwick 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1459e14bb325SJeff Bonwick 			vd->vdev_cant_read = B_FALSE;
1460e14bb325SJeff Bonwick 			vd->vdev_cant_write = B_FALSE;
1461e14bb325SJeff Bonwick 		}
1462e14bb325SJeff Bonwick 
1463a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1464a3f829aeSBill Moore 		    vdev_probe_done, vps,
1465a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1466e14bb325SJeff Bonwick 
146798d1cbfeSGeorge Wilson 		/*
146898d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
146998d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
147098d1cbfeSGeorge Wilson 		 */
1471a3f829aeSBill Moore 		if (zio != NULL) {
1472a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1473a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1474a3f829aeSBill Moore 		}
1475a3f829aeSBill Moore 	}
1476e14bb325SJeff Bonwick 
1477a3f829aeSBill Moore 	if (zio != NULL)
1478a3f829aeSBill Moore 		zio_add_child(zio, pio);
1479a3f829aeSBill Moore 
1480a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1481a3f829aeSBill Moore 
1482a3f829aeSBill Moore 	if (vps == NULL) {
1483a3f829aeSBill Moore 		ASSERT(zio != NULL);
1484a3f829aeSBill Moore 		return (NULL);
1485a3f829aeSBill Moore 	}
1486e14bb325SJeff Bonwick 
1487e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1488a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1489e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1490c4ecba8aSPaul Dagnelie 		    offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE,
1491770499e1SDan Kimmel 		    abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1492e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1493e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1494e14bb325SJeff Bonwick 	}
1495e14bb325SJeff Bonwick 
1496a3f829aeSBill Moore 	if (zio == NULL)
1497a3f829aeSBill Moore 		return (pio);
1498a3f829aeSBill Moore 
1499a3f829aeSBill Moore 	zio_nowait(pio);
1500a3f829aeSBill Moore 	return (NULL);
15010a4e9518Sgw25295 }
15020a4e9518Sgw25295 
1503f64c0e34SEric Taylor static void
vdev_open_child(void * arg)1504f64c0e34SEric Taylor vdev_open_child(void *arg)
1505f64c0e34SEric Taylor {
1506f64c0e34SEric Taylor 	vdev_t *vd = arg;
1507f64c0e34SEric Taylor 
1508f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1509f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1510f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1511f64c0e34SEric Taylor }
1512f64c0e34SEric Taylor 
1513681d9761SEric Taylor boolean_t
vdev_uses_zvols(vdev_t * vd)1514681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1515681d9761SEric Taylor {
1516681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1517681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1518681d9761SEric Taylor 		return (B_TRUE);
1519681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1520681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1521681d9761SEric Taylor 			return (B_TRUE);
1522681d9761SEric Taylor 	return (B_FALSE);
1523681d9761SEric Taylor }
1524681d9761SEric Taylor 
1525f64c0e34SEric Taylor void
vdev_open_children(vdev_t * vd)1526f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1527f64c0e34SEric Taylor {
1528f64c0e34SEric Taylor 	taskq_t *tq;
1529f64c0e34SEric Taylor 	int children = vd->vdev_children;
1530f64c0e34SEric Taylor 
1531681d9761SEric Taylor 	/*
1532681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1533681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1534681d9761SEric Taylor 	 * spa_namespace_lock
1535681d9761SEric Taylor 	 */
1536681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
153712a8814cSTom Caputi retry_sync:
1538681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1539681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1540681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
154112a8814cSTom Caputi 	} else {
1542f64c0e34SEric Taylor 		tq = taskq_create("vdev_open", children, minclsyspri,
1543f64c0e34SEric Taylor 		    children, children, TASKQ_PREPOPULATE);
154412a8814cSTom Caputi 		if (tq == NULL)
154512a8814cSTom Caputi 			goto retry_sync;
1546f64c0e34SEric Taylor 
1547f64c0e34SEric Taylor 		for (int c = 0; c < children; c++)
154812a8814cSTom Caputi 			VERIFY(taskq_dispatch(tq, vdev_open_child,
154912a8814cSTom Caputi 			    vd->vdev_child[c], TQ_SLEEP) != TASKQID_INVALID);
1550f64c0e34SEric Taylor 
1551f64c0e34SEric Taylor 		taskq_destroy(tq);
1552f64c0e34SEric Taylor 	}
1553f64c0e34SEric Taylor 
155412a8814cSTom Caputi 	vd->vdev_nonrot = B_TRUE;
155512a8814cSTom Caputi 
155612a8814cSTom Caputi 	for (int c = 0; c < children; c++)
155712a8814cSTom Caputi 		vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
155812a8814cSTom Caputi }
155912a8814cSTom Caputi 
1560fa9e4066Sahrens /*
15615cabbc6bSPrashanth Sreenivasa  * Compute the raidz-deflation ratio.  Note, we hard-code
15625cabbc6bSPrashanth Sreenivasa  * in 128k (1 << 17) because it is the "typical" blocksize.
15635cabbc6bSPrashanth Sreenivasa  * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
15645cabbc6bSPrashanth Sreenivasa  * otherwise it would inconsistently account for existing bp's.
15655cabbc6bSPrashanth Sreenivasa  */
15665cabbc6bSPrashanth Sreenivasa static void
vdev_set_deflate_ratio(vdev_t * vd)15675cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd)
15685cabbc6bSPrashanth Sreenivasa {
15695cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
15705cabbc6bSPrashanth Sreenivasa 		vd->vdev_deflate_ratio = (1 << 17) /
15715cabbc6bSPrashanth Sreenivasa 		    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
15725cabbc6bSPrashanth Sreenivasa 	}
15735cabbc6bSPrashanth Sreenivasa }
15745cabbc6bSPrashanth Sreenivasa 
15755cabbc6bSPrashanth Sreenivasa /*
1576fa9e4066Sahrens  * Prepare a virtual device for access.
1577fa9e4066Sahrens  */
1578fa9e4066Sahrens int
vdev_open(vdev_t * vd)1579fa9e4066Sahrens vdev_open(vdev_t *vd)
1580fa9e4066Sahrens {
15818ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1582fa9e4066Sahrens 	int error;
1583fa9e4066Sahrens 	uint64_t osize = 0;
15844263d13fSGeorge Wilson 	uint64_t max_osize = 0;
15854263d13fSGeorge Wilson 	uint64_t asize, max_asize, psize;
1586ecc2d604Sbonwick 	uint64_t ashift = 0;
1587fa9e4066Sahrens 
1588f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1589f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1590fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1591fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1592fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1593fa9e4066Sahrens 
1594fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1595e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1596e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1597573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1598fa9e4066Sahrens 
1599069f55e2SEric Schrock 	/*
1600069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1601069f55e2SEric Schrock 	 * faulted, bail out of the open.
1602069f55e2SEric Schrock 	 */
16033d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
16043d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1605069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1606069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
16073d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1608069f55e2SEric Schrock 		    vd->vdev_label_aux);
1609be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
16103d7072f8Seschrock 	} else if (vd->vdev_offline) {
1611fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1612ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1613be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1614fa9e4066Sahrens 	}
1615fa9e4066Sahrens 
16164263d13fSGeorge Wilson 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1617fa9e4066Sahrens 
1618095bcd66SGeorge Wilson 	/*
1619095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1620095bcd66SGeorge Wilson 	 * the vdev on error.
1621095bcd66SGeorge Wilson 	 */
1622095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1623ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
16248956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1625ea8dc4b6Seschrock 
1626fa9e4066Sahrens 	if (error) {
16273d7072f8Seschrock 		if (vd->vdev_removed &&
16283d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
16293d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
16303d7072f8Seschrock 
16316f793812SPavel Zakharov 		if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
16326f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
16336f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
16346f793812SPavel Zakharov 		} else {
1635ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1636fa9e4066Sahrens 			    vd->vdev_stat.vs_aux);
16376f793812SPavel Zakharov 		}
1638fa9e4066Sahrens 		return (error);
1639fa9e4066Sahrens 	}
1640fa9e4066Sahrens 
16413d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
16423d7072f8Seschrock 
1643096d22d4SEric Schrock 	/*
1644096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1645096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1646096d22d4SEric Schrock 	 */
1647096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1648096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1649096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1650096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1651096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1652096d22d4SEric Schrock 		    vd->vdev_label_aux);
1653be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1654096d22d4SEric Schrock 	}
1655096d22d4SEric Schrock 
16563d7072f8Seschrock 	if (vd->vdev_degraded) {
16573d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
16583d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
16593d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
16603d7072f8Seschrock 	} else {
1661069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
16623d7072f8Seschrock 	}
1663fa9e4066Sahrens 
166488ecc943SGeorge Wilson 	/*
166588ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
166688ecc943SGeorge Wilson 	 */
166788ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
166888ecc943SGeorge Wilson 		return (0);
166988ecc943SGeorge Wilson 
1670573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1671ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1672ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1673ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1674ea8dc4b6Seschrock 			break;
1675ea8dc4b6Seschrock 		}
1676573ca77eSGeorge Wilson 	}
1677fa9e4066Sahrens 
1678fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
16794263d13fSGeorge Wilson 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1680fa9e4066Sahrens 
1681fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1682fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1683ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1684ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1685be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1686fa9e4066Sahrens 		}
1687fa9e4066Sahrens 		psize = osize;
1688fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
16894263d13fSGeorge Wilson 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
16904263d13fSGeorge Wilson 		    VDEV_LABEL_END_SIZE);
1691fa9e4066Sahrens 	} else {
1692ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1693fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1694ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1695ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1696be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1697fa9e4066Sahrens 		}
1698fa9e4066Sahrens 		psize = 0;
1699fa9e4066Sahrens 		asize = osize;
17004263d13fSGeorge Wilson 		max_asize = max_osize;
1701fa9e4066Sahrens 	}
1702fa9e4066Sahrens 
1703fa9e4066Sahrens 	vd->vdev_psize = psize;
1704fa9e4066Sahrens 
1705573ca77eSGeorge Wilson 	/*
1706c040c10cSSteven Hartland 	 * Make sure the allocatable size hasn't shrunk too much.
1707573ca77eSGeorge Wilson 	 */
1708573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1709573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1710573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1711be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1712573ca77eSGeorge Wilson 	}
1713573ca77eSGeorge Wilson 
1714fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1715fa9e4066Sahrens 		/*
1716fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
17175711d393Sloli10K 		 * For compatibility, a different ashift can be requested.
1718fa9e4066Sahrens 		 */
1719fa9e4066Sahrens 		vd->vdev_asize = asize;
17204263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
17215711d393Sloli10K 		if (vd->vdev_ashift == 0) {
17225711d393Sloli10K 			vd->vdev_ashift = ashift; /* use detected value */
17235711d393Sloli10K 		}
17245711d393Sloli10K 		if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
17255711d393Sloli10K 		    vd->vdev_ashift > ASHIFT_MAX)) {
17265711d393Sloli10K 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
17275711d393Sloli10K 			    VDEV_AUX_BAD_ASHIFT);
17285711d393Sloli10K 			return (SET_ERROR(EDOM));
17295711d393Sloli10K 		}
1730fa9e4066Sahrens 	} else {
1731fa9e4066Sahrens 		/*
17322384d9f8SGeorge Wilson 		 * Detect if the alignment requirement has increased.
17332384d9f8SGeorge Wilson 		 * We don't want to make the pool unavailable, just
17345711d393Sloli10K 		 * post an event instead.
1735fa9e4066Sahrens 		 */
17362384d9f8SGeorge Wilson 		if (ashift > vd->vdev_top->vdev_ashift &&
17372384d9f8SGeorge Wilson 		    vd->vdev_ops->vdev_op_leaf) {
17389b088140SToomas Soome 			(void) zfs_ereport_post(
17399b088140SToomas Soome 			    FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
17405711d393Sloli10K 			    spa, vd, NULL, NULL, 0, 0);
1741fa9e4066Sahrens 		}
17425711d393Sloli10K 
17434263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1744fa9e4066Sahrens 	}
1745fa9e4066Sahrens 
1746fa9e4066Sahrens 	/*
1747c040c10cSSteven Hartland 	 * If all children are healthy we update asize if either:
1748c040c10cSSteven Hartland 	 * The asize has increased, due to a device expansion caused by dynamic
1749c040c10cSSteven Hartland 	 * LUN growth or vdev replacement, and automatic expansion is enabled;
1750c040c10cSSteven Hartland 	 * making the additional space available.
1751c040c10cSSteven Hartland 	 *
1752c040c10cSSteven Hartland 	 * The asize has decreased, due to a device shrink usually caused by a
1753c040c10cSSteven Hartland 	 * vdev replace with a smaller device. This ensures that calculations
1754c040c10cSSteven Hartland 	 * based of max_asize and asize e.g. esize are always valid. It's safe
1755c040c10cSSteven Hartland 	 * to do this as we've already validated that asize is greater than
1756c040c10cSSteven Hartland 	 * vdev_min_asize.
1757fa9e4066Sahrens 	 */
1758c040c10cSSteven Hartland 	if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1759c040c10cSSteven Hartland 	    ((asize > vd->vdev_asize &&
1760c040c10cSSteven Hartland 	    (vd->vdev_expanding || spa->spa_autoexpand)) ||
1761c040c10cSSteven Hartland 	    (asize < vd->vdev_asize)))
1762fa9e4066Sahrens 		vd->vdev_asize = asize;
1763573ca77eSGeorge Wilson 
1764573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1765fa9e4066Sahrens 
1766ea8dc4b6Seschrock 	/*
17670a4e9518Sgw25295 	 * Ensure we can issue some IO before declaring the
17680a4e9518Sgw25295 	 * vdev open for business.
17690a4e9518Sgw25295 	 */
1770e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1771e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
177298d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
177398d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
17740a4e9518Sgw25295 		return (error);
17750a4e9518Sgw25295 	}
17760a4e9518Sgw25295 
17770a4e9518Sgw25295 	/*
177881cd5c55SMatthew Ahrens 	 * Track the min and max ashift values for normal data devices.
1779663207adSDon Brady 	 *
1780663207adSDon Brady 	 * DJB - TBD these should perhaps be tracked per allocation class
1781663207adSDon Brady 	 * (e.g. spa_min_ashift is used to round up post compression buffers)
178281cd5c55SMatthew Ahrens 	 */
178381cd5c55SMatthew Ahrens 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1784663207adSDon Brady 	    vd->vdev_alloc_bias == VDEV_BIAS_NONE &&
1785663207adSDon Brady 	    vd->vdev_aux == NULL) {
178681cd5c55SMatthew Ahrens 		if (vd->vdev_ashift > spa->spa_max_ashift)
178781cd5c55SMatthew Ahrens 			spa->spa_max_ashift = vd->vdev_ashift;
178881cd5c55SMatthew Ahrens 		if (vd->vdev_ashift < spa->spa_min_ashift)
178981cd5c55SMatthew Ahrens 			spa->spa_min_ashift = vd->vdev_ashift;
179081cd5c55SMatthew Ahrens 	}
179181cd5c55SMatthew Ahrens 
179281cd5c55SMatthew Ahrens 	/*
17930c06d385Sjwpoduska 	 * If this is a leaf vdev, assess whether a resilver is needed.
17940c06d385Sjwpoduska 	 * But don't do this if we are doing a reopen for a scrub, since
17950c06d385Sjwpoduska 	 * this would just restart the scrub we are already doing.
1796088f3894Sahrens 	 */
17970c06d385Sjwpoduska 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen)
17980c06d385Sjwpoduska 		dsl_scan_assess_vdev(spa->spa_dsl_pool, vd);
1799088f3894Sahrens 
1800fa9e4066Sahrens 	return (0);
1801fa9e4066Sahrens }
1802fa9e4066Sahrens 
1803fa9e4066Sahrens /*
1804560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
1805560e6e96Seschrock  * contents. This needs to be done before vdev_load() so that we don't
18063d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1807560e6e96Seschrock  *
1808560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1809560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1810560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1811560e6e96Seschrock  * will be updated but the function will return 0.
1812560e6e96Seschrock  */
1813560e6e96Seschrock int
vdev_validate(vdev_t * vd)18146f793812SPavel Zakharov vdev_validate(vdev_t *vd)
1815560e6e96Seschrock {
1816560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1817560e6e96Seschrock 	nvlist_t *label;
18186f793812SPavel Zakharov 	uint64_t guid = 0, aux_guid = 0, top_guid;
1819560e6e96Seschrock 	uint64_t state;
18206f793812SPavel Zakharov 	nvlist_t *nvl;
18216f793812SPavel Zakharov 	uint64_t txg;
1822560e6e96Seschrock 
18236f793812SPavel Zakharov 	if (vdev_validate_skip)
18246f793812SPavel Zakharov 		return (0);
18256f793812SPavel Zakharov 
18266f793812SPavel Zakharov 	for (uint64_t c = 0; c < vd->vdev_children; c++)
18276f793812SPavel Zakharov 		if (vdev_validate(vd->vdev_child[c]) != 0)
1828be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1829560e6e96Seschrock 
1830b5989ec7Seschrock 	/*
1831b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1832b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1833b5989ec7Seschrock 	 * overwrite the previous state.
1834b5989ec7Seschrock 	 */
18356f793812SPavel Zakharov 	if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
18366f793812SPavel Zakharov 		return (0);
18376f793812SPavel Zakharov 
18386f793812SPavel Zakharov 	/*
18396f793812SPavel Zakharov 	 * If we are performing an extreme rewind, we allow for a label that
18406f793812SPavel Zakharov 	 * was modified at a point after the current txg.
1841d1de72cfSPavel Zakharov 	 * If config lock is not held do not check for the txg. spa_sync could
1842d1de72cfSPavel Zakharov 	 * be updating the vdev's label before updating spa_last_synced_txg.
18436f793812SPavel Zakharov 	 */
1844d1de72cfSPavel Zakharov 	if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
1845d1de72cfSPavel Zakharov 	    spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
18466f793812SPavel Zakharov 		txg = UINT64_MAX;
18476f793812SPavel Zakharov 	else
18486f793812SPavel Zakharov 		txg = spa_last_synced_txg(spa);
1849560e6e96Seschrock 
1850dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1851560e6e96Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1852560e6e96Seschrock 		    VDEV_AUX_BAD_LABEL);
1853b6bf6e15SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
1854b6bf6e15SPavel Zakharov 		    "txg %llu", (u_longlong_t)txg);
1855560e6e96Seschrock 		return (0);
1856560e6e96Seschrock 	}
1857560e6e96Seschrock 
18581195e687SMark J Musante 	/*
18591195e687SMark J Musante 	 * Determine if this vdev has been split off into another
18601195e687SMark J Musante 	 * pool.  If so, then refuse to open it.
18611195e687SMark J Musante 	 */
18621195e687SMark J Musante 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
18631195e687SMark J Musante 	    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
18641195e687SMark J Musante 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
18651195e687SMark J Musante 		    VDEV_AUX_SPLIT_POOL);
18661195e687SMark J Musante 		nvlist_free(label);
18676f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
18681195e687SMark J Musante 		return (0);
18691195e687SMark J Musante 	}
18701195e687SMark J Musante 
18716f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
1872560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1873560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1874560e6e96Seschrock 		nvlist_free(label);
18756f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
18766f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_GUID);
18776f793812SPavel Zakharov 		return (0);
18786f793812SPavel Zakharov 	}
18796f793812SPavel Zakharov 
18806f793812SPavel Zakharov 	/*
18816f793812SPavel Zakharov 	 * If config is not trusted then ignore the spa guid check. This is
18826f793812SPavel Zakharov 	 * necessary because if the machine crashed during a re-guid the new
18836f793812SPavel Zakharov 	 * guid might have been written to all of the vdev labels, but not the
18846f793812SPavel Zakharov 	 * cached config. The check will be performed again once we have the
18856f793812SPavel Zakharov 	 * trusted config from the MOS.
18866f793812SPavel Zakharov 	 */
18876f793812SPavel Zakharov 	if (spa->spa_trust_config && guid != spa_guid(spa)) {
18886f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
18896f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
18906f793812SPavel Zakharov 		nvlist_free(label);
18916f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
18926f793812SPavel Zakharov 		    "match config (%llu != %llu)", (u_longlong_t)guid,
18933ee8c80cSPavel Zakharov 		    (u_longlong_t)spa_guid(spa));
1894560e6e96Seschrock 		return (0);
1895560e6e96Seschrock 	}
1896560e6e96Seschrock 
18971195e687SMark J Musante 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
18981195e687SMark J Musante 	    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
18991195e687SMark J Musante 	    &aux_guid) != 0)
19001195e687SMark J Musante 		aux_guid = 0;
19011195e687SMark J Musante 
19026f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
19036f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
19046f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
19056f793812SPavel Zakharov 		nvlist_free(label);
19066f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
19076f793812SPavel Zakharov 		    ZPOOL_CONFIG_GUID);
19086f793812SPavel Zakharov 		return (0);
19096f793812SPavel Zakharov 	}
19106f793812SPavel Zakharov 
19116f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
19126f793812SPavel Zakharov 	    != 0) {
19136f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
19146f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
19156f793812SPavel Zakharov 		nvlist_free(label);
19166f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
19176f793812SPavel Zakharov 		    ZPOOL_CONFIG_TOP_GUID);
19186f793812SPavel Zakharov 		return (0);
19196f793812SPavel Zakharov 	}
19206f793812SPavel Zakharov 
1921e14bb325SJeff Bonwick 	/*
19226f793812SPavel Zakharov 	 * If this vdev just became a top-level vdev because its sibling was
19236f793812SPavel Zakharov 	 * detached, it will have adopted the parent's vdev guid -- but the
19246f793812SPavel Zakharov 	 * label may or may not be on disk yet. Fortunately, either version
19256f793812SPavel Zakharov 	 * of the label will have the same top guid, so if we're a top-level
19266f793812SPavel Zakharov 	 * vdev, we can safely compare to that instead.
19276f793812SPavel Zakharov 	 * However, if the config comes from a cachefile that failed to update
19286f793812SPavel Zakharov 	 * after the detach, a top-level vdev will appear as a non top-level
19296f793812SPavel Zakharov 	 * vdev in the config. Also relax the constraints if we perform an
19306f793812SPavel Zakharov 	 * extreme rewind.
19311195e687SMark J Musante 	 *
19321195e687SMark J Musante 	 * If we split this vdev off instead, then we also check the
19331195e687SMark J Musante 	 * original pool's guid. We don't want to consider the vdev
19341195e687SMark J Musante 	 * corrupt if it is partway through a split operation.
1935e14bb325SJeff Bonwick 	 */
19366f793812SPavel Zakharov 	if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
19376f793812SPavel Zakharov 		boolean_t mismatch = B_FALSE;
19386f793812SPavel Zakharov 		if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
19396f793812SPavel Zakharov 			if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
19406f793812SPavel Zakharov 				mismatch = B_TRUE;
19416f793812SPavel Zakharov 		} else {
19426f793812SPavel Zakharov 			if (vd->vdev_guid != top_guid &&
19436f793812SPavel Zakharov 			    vd->vdev_top->vdev_guid != guid)
19446f793812SPavel Zakharov 				mismatch = B_TRUE;
19456f793812SPavel Zakharov 		}
19466f793812SPavel Zakharov 
19476f793812SPavel Zakharov 		if (mismatch) {
1948560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1949560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1950560e6e96Seschrock 			nvlist_free(label);
19516f793812SPavel Zakharov 			vdev_dbgmsg(vd, "vdev_validate: config guid "
19526f793812SPavel Zakharov 			    "doesn't match label guid");
19536f793812SPavel Zakharov 			vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
19546f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_guid,
19556f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_top->vdev_guid);
19566f793812SPavel Zakharov 			vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
19576f793812SPavel Zakharov 			    "aux_guid %llu", (u_longlong_t)guid,
19586f793812SPavel Zakharov 			    (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1959560e6e96Seschrock 			return (0);
1960560e6e96Seschrock 		}
19616f793812SPavel Zakharov 	}
1962560e6e96Seschrock 
1963560e6e96Seschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1964560e6e96Seschrock 	    &state) != 0) {
1965560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1966560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1967560e6e96Seschrock 		nvlist_free(label);
19686f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
19693ee8c80cSPavel Zakharov 		    ZPOOL_CONFIG_POOL_STATE);
1970560e6e96Seschrock 		return (0);
1971560e6e96Seschrock 	}
1972560e6e96Seschrock 
1973560e6e96Seschrock 	nvlist_free(label);
1974560e6e96Seschrock 
1975bc758434SLin Ling 	/*
19764b964adaSGeorge Wilson 	 * If this is a verbatim import, no need to check the
1977bc758434SLin Ling 	 * state of the pool.
1978bc758434SLin Ling 	 */
19794b964adaSGeorge Wilson 	if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1980b16da2e2SGeorge Wilson 	    spa_load_state(spa) == SPA_LOAD_OPEN &&
19813ee8c80cSPavel Zakharov 	    state != POOL_STATE_ACTIVE) {
19826f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
19836f793812SPavel Zakharov 		    "for spa %s", (u_longlong_t)state, spa->spa_name);
1984be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
19853ee8c80cSPavel Zakharov 	}
1986560e6e96Seschrock 
1987560e6e96Seschrock 	/*
198851ece835Seschrock 	 * If we were able to open and validate a vdev that was
198951ece835Seschrock 	 * previously marked permanently unavailable, clear that state
199051ece835Seschrock 	 * now.
1991560e6e96Seschrock 	 */
1992560e6e96Seschrock 	if (vd->vdev_not_present)
1993560e6e96Seschrock 		vd->vdev_not_present = 0;
1994560e6e96Seschrock 
1995560e6e96Seschrock 	return (0);
1996560e6e96Seschrock }
1997560e6e96Seschrock 
19986f793812SPavel Zakharov static void
vdev_copy_path_impl(vdev_t * svd,vdev_t * dvd)19996f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
20006f793812SPavel Zakharov {
20016f793812SPavel Zakharov 	if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
20026f793812SPavel Zakharov 		if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
20036f793812SPavel Zakharov 			zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
20046f793812SPavel Zakharov 			    "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
20056f793812SPavel Zakharov 			    dvd->vdev_path, svd->vdev_path);
20066f793812SPavel Zakharov 			spa_strfree(dvd->vdev_path);
20076f793812SPavel Zakharov 			dvd->vdev_path = spa_strdup(svd->vdev_path);
20086f793812SPavel Zakharov 		}
20096f793812SPavel Zakharov 	} else if (svd->vdev_path != NULL) {
20106f793812SPavel Zakharov 		dvd->vdev_path = spa_strdup(svd->vdev_path);
20116f793812SPavel Zakharov 		zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
20126f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
20136f793812SPavel Zakharov 	}
20146f793812SPavel Zakharov }
20156f793812SPavel Zakharov 
20166f793812SPavel Zakharov /*
20176f793812SPavel Zakharov  * Recursively copy vdev paths from one vdev to another. Source and destination
20186f793812SPavel Zakharov  * vdev trees must have same geometry otherwise return error. Intended to copy
20196f793812SPavel Zakharov  * paths from userland config into MOS config.
20206f793812SPavel Zakharov  */
20216f793812SPavel Zakharov int
vdev_copy_path_strict(vdev_t * svd,vdev_t * dvd)20226f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
20236f793812SPavel Zakharov {
20246f793812SPavel Zakharov 	if ((svd->vdev_ops == &vdev_missing_ops) ||
20256f793812SPavel Zakharov 	    (svd->vdev_ishole && dvd->vdev_ishole) ||
20266f793812SPavel Zakharov 	    (dvd->vdev_ops == &vdev_indirect_ops))
20276f793812SPavel Zakharov 		return (0);
20286f793812SPavel Zakharov 
20296f793812SPavel Zakharov 	if (svd->vdev_ops != dvd->vdev_ops) {
20306f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
20316f793812SPavel Zakharov 		    svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
20326f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
20336f793812SPavel Zakharov 	}
20346f793812SPavel Zakharov 
20356f793812SPavel Zakharov 	if (svd->vdev_guid != dvd->vdev_guid) {
20366f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
20376f793812SPavel Zakharov 		    "%llu)", (u_longlong_t)svd->vdev_guid,
20386f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid);
20396f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
20406f793812SPavel Zakharov 	}
20416f793812SPavel Zakharov 
20426f793812SPavel Zakharov 	if (svd->vdev_children != dvd->vdev_children) {
20436f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
20446f793812SPavel Zakharov 		    "%llu != %llu", (u_longlong_t)svd->vdev_children,
20456f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_children);
20466f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
20476f793812SPavel Zakharov 	}
20486f793812SPavel Zakharov 
20496f793812SPavel Zakharov 	for (uint64_t i = 0; i < svd->vdev_children; i++) {
20506f793812SPavel Zakharov 		int error = vdev_copy_path_strict(svd->vdev_child[i],
20516f793812SPavel Zakharov 		    dvd->vdev_child[i]);
20526f793812SPavel Zakharov 		if (error != 0)
20536f793812SPavel Zakharov 			return (error);
20546f793812SPavel Zakharov 	}
20556f793812SPavel Zakharov 
20566f793812SPavel Zakharov 	if (svd->vdev_ops->vdev_op_leaf)
20576f793812SPavel Zakharov 		vdev_copy_path_impl(svd, dvd);
20586f793812SPavel Zakharov 
20596f793812SPavel Zakharov 	return (0);
20606f793812SPavel Zakharov }
20616f793812SPavel Zakharov 
20626f793812SPavel Zakharov static void
vdev_copy_path_search(vdev_t * stvd,vdev_t * dvd)20636f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
20646f793812SPavel Zakharov {
20656f793812SPavel Zakharov 	ASSERT(stvd->vdev_top == stvd);
20666f793812SPavel Zakharov 	ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
20676f793812SPavel Zakharov 
20686f793812SPavel Zakharov 	for (uint64_t i = 0; i < dvd->vdev_children; i++) {
20696f793812SPavel Zakharov 		vdev_copy_path_search(stvd, dvd->vdev_child[i]);
20706f793812SPavel Zakharov 	}
20716f793812SPavel Zakharov 
20726f793812SPavel Zakharov 	if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
20736f793812SPavel Zakharov 		return;
20746f793812SPavel Zakharov 
20756f793812SPavel Zakharov 	/*
20766f793812SPavel Zakharov 	 * The idea here is that while a vdev can shift positions within
20776f793812SPavel Zakharov 	 * a top vdev (when replacing, attaching mirror, etc.) it cannot
20786f793812SPavel Zakharov 	 * step outside of it.
20796f793812SPavel Zakharov 	 */
20806f793812SPavel Zakharov 	vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
20816f793812SPavel Zakharov 
20826f793812SPavel Zakharov 	if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
20836f793812SPavel Zakharov 		return;
20846f793812SPavel Zakharov 
20856f793812SPavel Zakharov 	ASSERT(vd->vdev_ops->vdev_op_leaf);
20866f793812SPavel Zakharov 
20876f793812SPavel Zakharov 	vdev_copy_path_impl(vd, dvd);
20886f793812SPavel Zakharov }
20896f793812SPavel Zakharov 
20906f793812SPavel Zakharov /*
20916f793812SPavel Zakharov  * Recursively copy vdev paths from one root vdev to another. Source and
20926f793812SPavel Zakharov  * destination vdev trees may differ in geometry. For each destination leaf
20936f793812SPavel Zakharov  * vdev, search a vdev with the same guid and top vdev id in the source.
20946f793812SPavel Zakharov  * Intended to copy paths from userland config into MOS config.
20956f793812SPavel Zakharov  */
20966f793812SPavel Zakharov void
vdev_copy_path_relaxed(vdev_t * srvd,vdev_t * drvd)20976f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
20986f793812SPavel Zakharov {
20996f793812SPavel Zakharov 	uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
21006f793812SPavel Zakharov 	ASSERT(srvd->vdev_ops == &vdev_root_ops);
21016f793812SPavel Zakharov 	ASSERT(drvd->vdev_ops == &vdev_root_ops);
21026f793812SPavel Zakharov 
21036f793812SPavel Zakharov 	for (uint64_t i = 0; i < children; i++) {
21046f793812SPavel Zakharov 		vdev_copy_path_search(srvd->vdev_child[i],
21056f793812SPavel Zakharov 		    drvd->vdev_child[i]);
21066f793812SPavel Zakharov 	}
21076f793812SPavel Zakharov }
21086f793812SPavel Zakharov 
2109560e6e96Seschrock /*
2110fa9e4066Sahrens  * Close a virtual device.
2111fa9e4066Sahrens  */
2112fa9e4066Sahrens void
vdev_close(vdev_t * vd)2113fa9e4066Sahrens vdev_close(vdev_t *vd)
2114fa9e4066Sahrens {
21158ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
2116095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
21178ad4d6ddSJeff Bonwick 
21188ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
21198ad4d6ddSJeff Bonwick 
21201195e687SMark J Musante 	/*
21211195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
21221195e687SMark J Musante 	 * going offline.
21231195e687SMark J Musante 	 */
2124095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
21251195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2126095bcd66SGeorge Wilson 
2127fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
2128fa9e4066Sahrens 
21293d7072f8Seschrock 	vdev_cache_purge(vd);
2130fa9e4066Sahrens 
2131560e6e96Seschrock 	/*
2132560e6e96Seschrock 	 * We record the previous state before we close it, so that if we are
2133560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
2134560e6e96Seschrock 	 * it's still faulted.
2135560e6e96Seschrock 	 */
2136560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
2137560e6e96Seschrock 
2138fa9e4066Sahrens 	if (vd->vdev_offline)
2139fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
2140fa9e4066Sahrens 	else
2141fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
2142ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2143fa9e4066Sahrens }
2144fa9e4066Sahrens 
2145dcba9f3fSGeorge Wilson void
vdev_hold(vdev_t * vd)2146dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
2147dcba9f3fSGeorge Wilson {
2148dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2149dcba9f3fSGeorge Wilson 
2150dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
2151dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2152dcba9f3fSGeorge Wilson 		return;
2153dcba9f3fSGeorge Wilson 
2154dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
2155dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
2156dcba9f3fSGeorge Wilson 
2157c65bd187SPatrick Mooney 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL)
2158dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
2159dcba9f3fSGeorge Wilson }
2160dcba9f3fSGeorge Wilson 
2161dcba9f3fSGeorge Wilson void
vdev_rele(vdev_t * vd)2162dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
2163dcba9f3fSGeorge Wilson {
2164dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2165dcba9f3fSGeorge Wilson 
2166dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
2167dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
2168dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
2169dcba9f3fSGeorge Wilson 
2170c65bd187SPatrick Mooney 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL)
2171dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
2172dcba9f3fSGeorge Wilson }
2173dcba9f3fSGeorge Wilson 
2174095bcd66SGeorge Wilson /*
2175095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
2176095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
2177095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
2178095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
2179095bcd66SGeorge Wilson  */
2180fa9e4066Sahrens void
vdev_reopen(vdev_t * vd)2181ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
2182fa9e4066Sahrens {
2183ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
2184fa9e4066Sahrens 
2185e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2186ea8dc4b6Seschrock 
21871195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
21881195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
2189fa9e4066Sahrens 	vdev_close(vd);
2190fa9e4066Sahrens 	(void) vdev_open(vd);
2191fa9e4066Sahrens 
2192fa9e4066Sahrens 	/*
219339c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
219439c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
219539c23413Seschrock 	 * opened in response to vdev_reopen().
219639c23413Seschrock 	 */
2197c5904d13Seschrock 	if (vd->vdev_aux) {
2198c5904d13Seschrock 		(void) vdev_validate_aux(vd);
2199e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
2200f0a05239SGeorge Amanakis 		    vd->vdev_aux == &spa->spa_l2cache) {
2201f0a05239SGeorge Amanakis 			/*
2202f0a05239SGeorge Amanakis 			 * When reopening we can assume the device label has
2203f0a05239SGeorge Amanakis 			 * already the attribute l2cache_persistent, since we've
2204f0a05239SGeorge Amanakis 			 * opened the device in the past and updated the label.
2205f0a05239SGeorge Amanakis 			 * In case the vdev is present we should evict all ARC
2206f0a05239SGeorge Amanakis 			 * buffers and pointers to log blocks and reclaim their
2207f0a05239SGeorge Amanakis 			 * space before restoring its contents to L2ARC.
2208f0a05239SGeorge Amanakis 			 */
2209f0a05239SGeorge Amanakis 			if (l2arc_vdev_present(vd)) {
2210f0a05239SGeorge Amanakis 				l2arc_rebuild_vdev(vd, B_TRUE);
2211f0a05239SGeorge Amanakis 			} else {
2212573ca77eSGeorge Wilson 				l2arc_add_vdev(spa, vd);
2213f0a05239SGeorge Amanakis 			}
2214f0a05239SGeorge Amanakis 			spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2215f0a05239SGeorge Amanakis 		}
2216c5904d13Seschrock 	} else {
22176f793812SPavel Zakharov 		(void) vdev_validate(vd);
2218c5904d13Seschrock 	}
221939c23413Seschrock 
222039c23413Seschrock 	/*
22213d7072f8Seschrock 	 * Reassess parent vdev's health.
2222fa9e4066Sahrens 	 */
22233d7072f8Seschrock 	vdev_propagate_state(vd);
2224fa9e4066Sahrens }
2225fa9e4066Sahrens 
2226fa9e4066Sahrens int
vdev_create(vdev_t * vd,uint64_t txg,boolean_t isreplacing)222799653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2228fa9e4066Sahrens {
2229fa9e4066Sahrens 	int error;
2230fa9e4066Sahrens 
2231fa9e4066Sahrens 	/*
2232fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
2233fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
2234fa9e4066Sahrens 	 * there are any components we can't open.
2235fa9e4066Sahrens 	 */
2236fa9e4066Sahrens 	error = vdev_open(vd);
2237fa9e4066Sahrens 
2238fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2239fa9e4066Sahrens 		vdev_close(vd);
2240fa9e4066Sahrens 		return (error ? error : ENXIO);
2241fa9e4066Sahrens 	}
2242fa9e4066Sahrens 
2243fa9e4066Sahrens 	/*
22440713e232SGeorge Wilson 	 * Recursively load DTLs and initialize all labels.
2245fa9e4066Sahrens 	 */
22460713e232SGeorge Wilson 	if ((error = vdev_dtl_load(vd)) != 0 ||
22470713e232SGeorge Wilson 	    (error = vdev_label_init(vd, txg, isreplacing ?
224839c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2249fa9e4066Sahrens 		vdev_close(vd);
2250fa9e4066Sahrens 		return (error);
2251fa9e4066Sahrens 	}
2252fa9e4066Sahrens 
2253fa9e4066Sahrens 	return (0);
2254fa9e4066Sahrens }
2255fa9e4066Sahrens 
22560e34b6a7Sbonwick void
vdev_metaslab_set_size(vdev_t * vd)2257573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
2258fa9e4066Sahrens {
225986714001SSerapheim Dimitropoulos 	uint64_t asize = vd->vdev_asize;
2260a0b03b16SSerapheim Dimitropoulos 	uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2261b4bf0cf0SDon Brady 	uint64_t ms_shift;
226286714001SSerapheim Dimitropoulos 
2263a3874b8bSToomas Soome 	/* BEGIN CSTYLED */
2264fa9e4066Sahrens 	/*
2265b4bf0cf0SDon Brady 	 * There are two dimensions to the metaslab sizing calculation:
2266b4bf0cf0SDon Brady 	 * the size of the metaslab and the count of metaslabs per vdev.
2267b4bf0cf0SDon Brady 	 *
2268a0b03b16SSerapheim Dimitropoulos 	 * The default values used below are a good balance between memory
2269a0b03b16SSerapheim Dimitropoulos 	 * usage (larger metaslab size means more memory needed for loaded
2270a0b03b16SSerapheim Dimitropoulos 	 * metaslabs; more metaslabs means more memory needed for the
2271a0b03b16SSerapheim Dimitropoulos 	 * metaslab_t structs), metaslab load time (larger metaslabs take
2272a0b03b16SSerapheim Dimitropoulos 	 * longer to load), and metaslab sync time (more metaslabs means
2273a0b03b16SSerapheim Dimitropoulos 	 * more time spent syncing all of them).
2274a0b03b16SSerapheim Dimitropoulos 	 *
2275a0b03b16SSerapheim Dimitropoulos 	 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2276a0b03b16SSerapheim Dimitropoulos 	 * The range of the dimensions are as follows:
2277a0b03b16SSerapheim Dimitropoulos 	 *
2278a0b03b16SSerapheim Dimitropoulos 	 *	2^29 <= ms_size  <= 2^34
2279b4bf0cf0SDon Brady 	 *	  16 <= ms_count <= 131,072
2280b4bf0cf0SDon Brady 	 *
2281b4bf0cf0SDon Brady 	 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2282b4bf0cf0SDon Brady 	 * at least 512MB (2^29) to minimize fragmentation effects when
2283b4bf0cf0SDon Brady 	 * testing with smaller devices.  However, the count constraint
2284b4bf0cf0SDon Brady 	 * of at least 16 metaslabs will override this minimum size goal.
2285b4bf0cf0SDon Brady 	 *
2286b4bf0cf0SDon Brady 	 * On the upper end of vdev sizes, we aim for a maximum metaslab
2287a0b03b16SSerapheim Dimitropoulos 	 * size of 16GB.  However, we will cap the total count to 2^17
2288a0b03b16SSerapheim Dimitropoulos 	 * metaslabs to keep our memory footprint in check and let the
2289a0b03b16SSerapheim Dimitropoulos 	 * metaslab size grow from there if that limit is hit.
2290b4bf0cf0SDon Brady 	 *
2291b4bf0cf0SDon Brady 	 * The net effect of applying above constrains is summarized below.
2292b4bf0cf0SDon Brady 	 *
2293b4bf0cf0SDon Brady 	 *   vdev size	    metaslab count
2294a0b03b16SSerapheim Dimitropoulos 	 *  --------------|-----------------
2295b4bf0cf0SDon Brady 	 *	< 8GB		~16
2296b4bf0cf0SDon Brady 	 *  8GB   - 100GB	one per 512MB
2297a0b03b16SSerapheim Dimitropoulos 	 *  100GB - 3TB		~200
2298a0b03b16SSerapheim Dimitropoulos 	 *  3TB   - 2PB		one per 16GB
2299a0b03b16SSerapheim Dimitropoulos 	 *	> 2PB		~131,072
2300a0b03b16SSerapheim Dimitropoulos 	 *  --------------------------------
2301a0b03b16SSerapheim Dimitropoulos 	 *
2302a0b03b16SSerapheim Dimitropoulos 	 *  Finally, note that all of the above calculate the initial
2303a0b03b16SSerapheim Dimitropoulos 	 *  number of metaslabs. Expanding a top-level vdev will result
2304a0b03b16SSerapheim Dimitropoulos 	 *  in additional metaslabs being allocated making it possible
2305a0b03b16SSerapheim Dimitropoulos 	 *  to exceed the zfs_vdev_ms_count_limit.
2306fa9e4066Sahrens 	 */
2307a3874b8bSToomas Soome 	/* END CSTYLED */
2308b4bf0cf0SDon Brady 
2309a0b03b16SSerapheim Dimitropoulos 	if (ms_count < zfs_vdev_min_ms_count)
2310a0b03b16SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2311a0b03b16SSerapheim Dimitropoulos 	else if (ms_count > zfs_vdev_default_ms_count)
2312a0b03b16SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2313b4bf0cf0SDon Brady 	else
2314a0b03b16SSerapheim Dimitropoulos 		ms_shift = zfs_vdev_default_ms_shift;
231586714001SSerapheim Dimitropoulos 
2316b4bf0cf0SDon Brady 	if (ms_shift < SPA_MAXBLOCKSHIFT) {
2317b4bf0cf0SDon Brady 		ms_shift = SPA_MAXBLOCKSHIFT;
2318a0b03b16SSerapheim Dimitropoulos 	} else if (ms_shift > zfs_vdev_max_ms_shift) {
2319a0b03b16SSerapheim Dimitropoulos 		ms_shift = zfs_vdev_max_ms_shift;
2320b4bf0cf0SDon Brady 		/* cap the total count to constrain memory footprint */
2321a0b03b16SSerapheim Dimitropoulos 		if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2322a0b03b16SSerapheim Dimitropoulos 			ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
232386714001SSerapheim Dimitropoulos 	}
232486714001SSerapheim Dimitropoulos 
232586714001SSerapheim Dimitropoulos 	vd->vdev_ms_shift = ms_shift;
232686714001SSerapheim Dimitropoulos 	ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2327fa9e4066Sahrens }
2328fa9e4066Sahrens 
2329fa9e4066Sahrens void
vdev_dirty(vdev_t * vd,int flags,void * arg,uint64_t txg)2330ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2331fa9e4066Sahrens {
2332ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
23335cabbc6bSPrashanth Sreenivasa 	/* indirect vdevs don't have metaslabs or dtls */
23345cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd) || flags == 0);
2335ecc2d604Sbonwick 	ASSERT(ISP2(flags));
2336f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
2337fa9e4066Sahrens 
2338ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
2339ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2340ecc2d604Sbonwick 
2341ecc2d604Sbonwick 	if (flags & VDD_DTL)
2342ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2343ecc2d604Sbonwick 
2344ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2345fa9e4066Sahrens }
2346fa9e4066Sahrens 
23470713e232SGeorge Wilson void
vdev_dirty_leaves(vdev_t * vd,int flags,uint64_t txg)23480713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
23490713e232SGeorge Wilson {
23500713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
23510713e232SGeorge Wilson 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
23520713e232SGeorge Wilson 
23530713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
23540713e232SGeorge Wilson 		vdev_dirty(vd->vdev_top, flags, vd, txg);
23550713e232SGeorge Wilson }
23560713e232SGeorge Wilson 
23578ad4d6ddSJeff Bonwick /*
23588ad4d6ddSJeff Bonwick  * DTLs.
23598ad4d6ddSJeff Bonwick  *
23608ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
23619fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
23628ad4d6ddSJeff Bonwick  *
23638ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
23648ad4d6ddSJeff Bonwick  *
23658ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
23668ad4d6ddSJeff Bonwick  *
23678ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
23688ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
23698ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
23708ad4d6ddSJeff Bonwick  *
23718ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
23728ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
23738ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
23748ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
23758ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
23768ad4d6ddSJeff Bonwick  *
23778ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
23788ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
23798ad4d6ddSJeff Bonwick  *
23808ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
23818ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
23828ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
23838ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
23848ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
23858ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
23868ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
23878ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
23888ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
23898ad4d6ddSJeff Bonwick  *
23908ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
23918ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
23928ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
23938ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
23948ad4d6ddSJeff Bonwick  */
2395fa9e4066Sahrens void
vdev_dtl_dirty(vdev_t * vd,vdev_dtl_type_t t,uint64_t txg,uint64_t size)23968ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2397fa9e4066Sahrens {
23980713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
23998ad4d6ddSJeff Bonwick 
24008ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
24018ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2402f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
24038ad4d6ddSJeff Bonwick 
24045cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
24050713e232SGeorge Wilson 	if (!range_tree_contains(rt, txg, size))
24060713e232SGeorge Wilson 		range_tree_add(rt, txg, size);
24075cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2408fa9e4066Sahrens }
2409fa9e4066Sahrens 
24108ad4d6ddSJeff Bonwick boolean_t
vdev_dtl_contains(vdev_t * vd,vdev_dtl_type_t t,uint64_t txg,uint64_t size)24118ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2412fa9e4066Sahrens {
24130713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
24148ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
2415fa9e4066Sahrens 
24168ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
24178ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2418fa9e4066Sahrens 
24195cabbc6bSPrashanth Sreenivasa 	/*
24205cabbc6bSPrashanth Sreenivasa 	 * While we are loading the pool, the DTLs have not been loaded yet.
24215cabbc6bSPrashanth Sreenivasa 	 * Ignore the DTLs and try all devices.  This avoids a recursive
24225cabbc6bSPrashanth Sreenivasa 	 * mutex enter on the vdev_dtl_lock, and also makes us try hard
24235cabbc6bSPrashanth Sreenivasa 	 * when loading the pool (relying on the checksum to ensure that
24245cabbc6bSPrashanth Sreenivasa 	 * we get the right data -- note that we while loading, we are
24255cabbc6bSPrashanth Sreenivasa 	 * only reading the MOS, which is always checksummed).
24265cabbc6bSPrashanth Sreenivasa 	 */
24275cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
24285cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
24295cabbc6bSPrashanth Sreenivasa 
24305cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
243186714001SSerapheim Dimitropoulos 	if (!range_tree_is_empty(rt))
24320713e232SGeorge Wilson 		dirty = range_tree_contains(rt, txg, size);
24335cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2434fa9e4066Sahrens 
2435fa9e4066Sahrens 	return (dirty);
2436fa9e4066Sahrens }
2437fa9e4066Sahrens 
24388ad4d6ddSJeff Bonwick boolean_t
vdev_dtl_empty(vdev_t * vd,vdev_dtl_type_t t)24398ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
24408ad4d6ddSJeff Bonwick {
24410713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
24428ad4d6ddSJeff Bonwick 	boolean_t empty;
24438ad4d6ddSJeff Bonwick 
24445cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
244586714001SSerapheim Dimitropoulos 	empty = range_tree_is_empty(rt);
24465cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
24478ad4d6ddSJeff Bonwick 
24488ad4d6ddSJeff Bonwick 	return (empty);
24498ad4d6ddSJeff Bonwick }
24508ad4d6ddSJeff Bonwick 
2451fa9e4066Sahrens /*
2452a3874b8bSToomas Soome  * Returns B_TRUE if vdev determines offset needs to be resilvered.
2453a3874b8bSToomas Soome  */
2454a3874b8bSToomas Soome boolean_t
vdev_dtl_need_resilver(vdev_t * vd,uint64_t offset,size_t psize)2455a3874b8bSToomas Soome vdev_dtl_need_resilver(vdev_t *vd, uint64_t offset, size_t psize)
2456a3874b8bSToomas Soome {
2457a3874b8bSToomas Soome 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2458a3874b8bSToomas Soome 
2459a3874b8bSToomas Soome 	if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
2460a3874b8bSToomas Soome 	    vd->vdev_ops->vdev_op_leaf)
2461a3874b8bSToomas Soome 		return (B_TRUE);
2462a3874b8bSToomas Soome 
2463a3874b8bSToomas Soome 	return (vd->vdev_ops->vdev_op_need_resilver(vd, offset, psize));
2464a3874b8bSToomas Soome }
2465a3874b8bSToomas Soome 
2466a3874b8bSToomas Soome /*
2467b4952e17SGeorge Wilson  * Returns the lowest txg in the DTL range.
2468b4952e17SGeorge Wilson  */
2469b4952e17SGeorge Wilson static uint64_t
vdev_dtl_min(vdev_t * vd)2470b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd)
2471b4952e17SGeorge Wilson {
2472b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
24730713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2474b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2475b4952e17SGeorge Wilson 
24764d7988d6SPaul Dagnelie 	return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1);
2477b4952e17SGeorge Wilson }
2478b4952e17SGeorge Wilson 
2479b4952e17SGeorge Wilson /*
2480b4952e17SGeorge Wilson  * Returns the highest txg in the DTL.
2481b4952e17SGeorge Wilson  */
2482b4952e17SGeorge Wilson static uint64_t
vdev_dtl_max(vdev_t * vd)2483b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd)
2484b4952e17SGeorge Wilson {
2485b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
24860713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2487b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2488b4952e17SGeorge Wilson 
24894d7988d6SPaul Dagnelie 	return (range_tree_max(vd->vdev_dtl[DTL_MISSING]));
2490b4952e17SGeorge Wilson }
2491b4952e17SGeorge Wilson 
2492b4952e17SGeorge Wilson /*
2493b4952e17SGeorge Wilson  * Determine if a resilvering vdev should remove any DTL entries from
2494b4952e17SGeorge Wilson  * its range. If the vdev was resilvering for the entire duration of the
2495b4952e17SGeorge Wilson  * scan then it should excise that range from its DTLs. Otherwise, this
2496b4952e17SGeorge Wilson  * vdev is considered partially resilvered and should leave its DTL
2497b4952e17SGeorge Wilson  * entries intact. The comment in vdev_dtl_reassess() describes how we
2498b4952e17SGeorge Wilson  * excise the DTLs.
2499b4952e17SGeorge Wilson  */
2500b4952e17SGeorge Wilson static boolean_t
vdev_dtl_should_excise(vdev_t * vd)2501b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd)
2502b4952e17SGeorge Wilson {
2503b4952e17SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2504b4952e17SGeorge Wilson 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2505b4952e17SGeorge Wilson 
2506b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2507b4952e17SGeorge Wilson 
25082d2f193aSMatthew Ahrens 	if (vd->vdev_state < VDEV_STATE_DEGRADED)
25092d2f193aSMatthew Ahrens 		return (B_FALSE);
25102d2f193aSMatthew Ahrens 
2511e4c795beSTom Caputi 	if (vd->vdev_resilver_deferred)
2512e4c795beSTom Caputi 		return (B_FALSE);
2513e4c795beSTom Caputi 
2514b4952e17SGeorge Wilson 	if (vd->vdev_resilver_txg == 0 ||
251586714001SSerapheim Dimitropoulos 	    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2516b4952e17SGeorge Wilson 		return (B_TRUE);
2517b4952e17SGeorge Wilson 
2518b4952e17SGeorge Wilson 	/*
2519b4952e17SGeorge Wilson 	 * When a resilver is initiated the scan will assign the scn_max_txg
2520b4952e17SGeorge Wilson 	 * value to the highest txg value that exists in all DTLs. If this
2521b4952e17SGeorge Wilson 	 * device's max DTL is not part of this scan (i.e. it is not in
2522b4952e17SGeorge Wilson 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2523b4952e17SGeorge Wilson 	 * for excision.
2524b4952e17SGeorge Wilson 	 */
2525b4952e17SGeorge Wilson 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2526b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2527b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2528b4952e17SGeorge Wilson 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2529b4952e17SGeorge Wilson 		return (B_TRUE);
2530b4952e17SGeorge Wilson 	}
2531b4952e17SGeorge Wilson 	return (B_FALSE);
2532b4952e17SGeorge Wilson }
2533b4952e17SGeorge Wilson 
2534b4952e17SGeorge Wilson /*
2535fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
2536fa9e4066Sahrens  */
2537fa9e4066Sahrens void
vdev_dtl_reassess(vdev_t * vd,uint64_t txg,uint64_t scrub_txg,int scrub_done)2538fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2539fa9e4066Sahrens {
2540ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
25418ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
25428ad4d6ddSJeff Bonwick 	int minref;
2543fa9e4066Sahrens 
25448ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2545fa9e4066Sahrens 
25468ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
25478ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
25488ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
25498ad4d6ddSJeff Bonwick 
25505cabbc6bSPrashanth Sreenivasa 	if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
25518ad4d6ddSJeff Bonwick 		return;
25528ad4d6ddSJeff Bonwick 
25538ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
25543f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2555165c5c6fSJohn Poduska 		boolean_t wasempty = B_TRUE;
25563f9d6ad7SLin Ling 
2557fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
2558b4952e17SGeorge Wilson 
2559b4952e17SGeorge Wilson 		/*
2560165c5c6fSJohn Poduska 		 * If requested, pretend the scan completed cleanly.
2561165c5c6fSJohn Poduska 		 */
2562165c5c6fSJohn Poduska 		if (zfs_scan_ignore_errors && scn)
2563165c5c6fSJohn Poduska 			scn->scn_phys.scn_errors = 0;
2564165c5c6fSJohn Poduska 
2565165c5c6fSJohn Poduska 		if (scrub_txg != 0 &&
2566165c5c6fSJohn Poduska 		    !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
2567165c5c6fSJohn Poduska 			wasempty = B_FALSE;
2568165c5c6fSJohn Poduska 			zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
2569165c5c6fSJohn Poduska 			    "dtl:%llu/%llu errors:%llu",
2570165c5c6fSJohn Poduska 			    (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg,
2571165c5c6fSJohn Poduska 			    (u_longlong_t)scrub_txg, spa->spa_scrub_started,
2572165c5c6fSJohn Poduska 			    (u_longlong_t)vdev_dtl_min(vd),
2573165c5c6fSJohn Poduska 			    (u_longlong_t)vdev_dtl_max(vd),
2574165c5c6fSJohn Poduska 			    (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0));
2575165c5c6fSJohn Poduska 		}
2576165c5c6fSJohn Poduska 
2577165c5c6fSJohn Poduska 		/*
2578b4952e17SGeorge Wilson 		 * If we've completed a scan cleanly then determine
2579b4952e17SGeorge Wilson 		 * if this vdev should remove any DTLs. We only want to
2580b4952e17SGeorge Wilson 		 * excise regions on vdevs that were available during
2581b4952e17SGeorge Wilson 		 * the entire duration of this scan.
2582b4952e17SGeorge Wilson 		 */
2583088f3894Sahrens 		if (scrub_txg != 0 &&
25843f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
2585b4952e17SGeorge Wilson 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2586b4952e17SGeorge Wilson 		    vdev_dtl_should_excise(vd)) {
2587fa9e4066Sahrens 			/*
2588088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
2589088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
2590088f3894Sahrens 			 * will be valid, so excise the old region and
2591088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
2592088f3894Sahrens 			 * dtl as-is if there was an error.
25938ad4d6ddSJeff Bonwick 			 *
25948ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
25958ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
25968ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
25978ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
25988ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
25998ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
26008ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
26018ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
26028ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
2603fa9e4066Sahrens 			 */
26040713e232SGeorge Wilson 			space_reftree_create(&reftree);
26050713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
26060713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
26070713e232SGeorge Wilson 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
26080713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
26090713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_SCRUB], 2);
26100713e232SGeorge Wilson 			space_reftree_generate_map(&reftree,
26110713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
26120713e232SGeorge Wilson 			space_reftree_destroy(&reftree);
2613165c5c6fSJohn Poduska 
2614165c5c6fSJohn Poduska 			if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
2615165c5c6fSJohn Poduska 				zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
2616165c5c6fSJohn Poduska 				    (u_longlong_t)vdev_dtl_min(vd),
2617165c5c6fSJohn Poduska 				    (u_longlong_t)vdev_dtl_max(vd));
2618165c5c6fSJohn Poduska 			} else if (!wasempty) {
2619165c5c6fSJohn Poduska 				zfs_dbgmsg("DTL_MISSING is now empty");
2620165c5c6fSJohn Poduska 			}
2621fa9e4066Sahrens 		}
26220713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
26230713e232SGeorge Wilson 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
26240713e232SGeorge Wilson 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2625fa9e4066Sahrens 		if (scrub_done)
26260713e232SGeorge Wilson 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
26270713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
26288ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
26290713e232SGeorge Wilson 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
26308ad4d6ddSJeff Bonwick 		else
26310713e232SGeorge Wilson 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
26320713e232SGeorge Wilson 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2633b4952e17SGeorge Wilson 
2634b4952e17SGeorge Wilson 		/*
2635b4952e17SGeorge Wilson 		 * If the vdev was resilvering and no longer has any
2636b4952e17SGeorge Wilson 		 * DTLs then reset its resilvering flag.
2637b4952e17SGeorge Wilson 		 */
2638b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0 &&
263986714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
264086714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE]))
2641b4952e17SGeorge Wilson 			vd->vdev_resilver_txg = 0;
2642b4952e17SGeorge Wilson 
2643fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2644088f3894Sahrens 
2645ecc2d604Sbonwick 		if (txg != 0)
2646ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2647fa9e4066Sahrens 		return;
2648fa9e4066Sahrens 	}
2649fa9e4066Sahrens 
2650fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
26518ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
265299bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
265399bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
26548ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
26558ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
26568ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
26578ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
26588ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
26598ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
26608ad4d6ddSJeff Bonwick 		else
26618ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
26620713e232SGeorge Wilson 		space_reftree_create(&reftree);
26638ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2664fa9e4066Sahrens 			vdev_t *cvd = vd->vdev_child[c];
26658ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
26660713e232SGeorge Wilson 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
26678ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
2668fa9e4066Sahrens 		}
26690713e232SGeorge Wilson 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
26700713e232SGeorge Wilson 		space_reftree_destroy(&reftree);
26718ad4d6ddSJeff Bonwick 	}
26728ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
2673fa9e4066Sahrens }
2674fa9e4066Sahrens 
26750713e232SGeorge Wilson int
vdev_dtl_load(vdev_t * vd)2676fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
2677fa9e4066Sahrens {
2678fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2679ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
26800713e232SGeorge Wilson 	int error = 0;
2681fa9e4066Sahrens 
26820713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
26835cabbc6bSPrashanth Sreenivasa 		ASSERT(vdev_is_concrete(vd));
268488ecc943SGeorge Wilson 
26850713e232SGeorge Wilson 		error = space_map_open(&vd->vdev_dtl_sm, mos,
26865cabbc6bSPrashanth Sreenivasa 		    vd->vdev_dtl_object, 0, -1ULL, 0);
26870713e232SGeorge Wilson 		if (error)
2688ea8dc4b6Seschrock 			return (error);
26890713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2690fa9e4066Sahrens 
2691fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
26920713e232SGeorge Wilson 		error = space_map_load(vd->vdev_dtl_sm,
26930713e232SGeorge Wilson 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
2694fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2695fa9e4066Sahrens 
2696fa9e4066Sahrens 		return (error);
2697fa9e4066Sahrens 	}
2698fa9e4066Sahrens 
26990713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
27000713e232SGeorge Wilson 		error = vdev_dtl_load(vd->vdev_child[c]);
27010713e232SGeorge Wilson 		if (error != 0)
27020713e232SGeorge Wilson 			break;
27030713e232SGeorge Wilson 	}
27040713e232SGeorge Wilson 
27050713e232SGeorge Wilson 	return (error);
27060713e232SGeorge Wilson }
27070713e232SGeorge Wilson 
2708663207adSDon Brady static void
vdev_zap_allocation_data(vdev_t * vd,dmu_tx_t * tx)2709663207adSDon Brady vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
2710663207adSDon Brady {
2711663207adSDon Brady 	spa_t *spa = vd->vdev_spa;
2712663207adSDon Brady 	objset_t *mos = spa->spa_meta_objset;
2713663207adSDon Brady 	vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
2714663207adSDon Brady 	const char *string;
2715663207adSDon Brady 
2716663207adSDon Brady 	ASSERT(alloc_bias != VDEV_BIAS_NONE);
2717663207adSDon Brady 
2718663207adSDon Brady 	string =
2719663207adSDon Brady 	    (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
2720663207adSDon Brady 	    (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
2721663207adSDon Brady 	    (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
2722663207adSDon Brady 
2723663207adSDon Brady 	ASSERT(string != NULL);
2724663207adSDon Brady 	VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
2725663207adSDon Brady 	    1, strlen(string) + 1, string, tx));
2726663207adSDon Brady 
2727663207adSDon Brady 	if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
2728663207adSDon Brady 		spa_activate_allocation_classes(spa, tx);
2729663207adSDon Brady 	}
2730663207adSDon Brady }
2731663207adSDon Brady 
2732fa9e4066Sahrens void
vdev_destroy_unlink_zap(vdev_t * vd,uint64_t zapobj,dmu_tx_t * tx)2733215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2734215198a6SJoe Stein {
2735215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2736215198a6SJoe Stein 
2737215198a6SJoe Stein 	VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2738215198a6SJoe Stein 	VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2739215198a6SJoe Stein 	    zapobj, tx));
2740215198a6SJoe Stein }
2741215198a6SJoe Stein 
2742215198a6SJoe Stein uint64_t
vdev_create_link_zap(vdev_t * vd,dmu_tx_t * tx)2743215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2744215198a6SJoe Stein {
2745215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2746215198a6SJoe Stein 	uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2747215198a6SJoe Stein 	    DMU_OT_NONE, 0, tx);
2748215198a6SJoe Stein 
2749215198a6SJoe Stein 	ASSERT(zap != 0);
2750215198a6SJoe Stein 	VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2751215198a6SJoe Stein 	    zap, tx));
2752215198a6SJoe Stein 
2753215198a6SJoe Stein 	return (zap);
2754215198a6SJoe Stein }
2755215198a6SJoe Stein 
2756215198a6SJoe Stein void
vdev_construct_zaps(vdev_t * vd,dmu_tx_t * tx)2757215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2758215198a6SJoe Stein {
2759215198a6SJoe Stein 	if (vd->vdev_ops != &vdev_hole_ops &&
2760215198a6SJoe Stein 	    vd->vdev_ops != &vdev_missing_ops &&
2761215198a6SJoe Stein 	    vd->vdev_ops != &vdev_root_ops &&
2762215198a6SJoe Stein 	    !vd->vdev_top->vdev_removing) {
2763215198a6SJoe Stein 		if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2764215198a6SJoe Stein 			vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2765215198a6SJoe Stein 		}
2766215198a6SJoe Stein 		if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2767215198a6SJoe Stein 			vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2768663207adSDon Brady 			if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
2769663207adSDon Brady 				vdev_zap_allocation_data(vd, tx);
2770215198a6SJoe Stein 		}
2771215198a6SJoe Stein 	}
2772663207adSDon Brady 
2773215198a6SJoe Stein 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
2774215198a6SJoe Stein 		vdev_construct_zaps(vd->vdev_child[i], tx);
2775215198a6SJoe Stein 	}
2776215198a6SJoe Stein }
2777215198a6SJoe Stein 
2778215198a6SJoe Stein void
vdev_dtl_sync(vdev_t * vd,uint64_t txg)2779fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2780fa9e4066Sahrens {
2781fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
27820713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2783ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
27840713e232SGeorge Wilson 	range_tree_t *rtsync;
2785fa9e4066Sahrens 	dmu_tx_t *tx;
27860713e232SGeorge Wilson 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
2787fa9e4066Sahrens 
27885cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
27890713e232SGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
279088ecc943SGeorge Wilson 
2791fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2792fa9e4066Sahrens 
27930713e232SGeorge Wilson 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
27940713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
27950713e232SGeorge Wilson 		space_map_free(vd->vdev_dtl_sm, tx);
27960713e232SGeorge Wilson 		space_map_close(vd->vdev_dtl_sm);
27970713e232SGeorge Wilson 		vd->vdev_dtl_sm = NULL;
27980713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2799215198a6SJoe Stein 
2800215198a6SJoe Stein 		/*
2801215198a6SJoe Stein 		 * We only destroy the leaf ZAP for detached leaves or for
2802215198a6SJoe Stein 		 * removed log devices. Removed data devices handle leaf ZAP
2803215198a6SJoe Stein 		 * cleanup later, once cancellation is no longer possible.
2804215198a6SJoe Stein 		 */
2805215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2806215198a6SJoe Stein 		    vd->vdev_top->vdev_islog)) {
2807215198a6SJoe Stein 			vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2808215198a6SJoe Stein 			vd->vdev_leaf_zap = 0;
2809215198a6SJoe Stein 		}
2810215198a6SJoe Stein 
2811fa9e4066Sahrens 		dmu_tx_commit(tx);
2812fa9e4066Sahrens 		return;
2813fa9e4066Sahrens 	}
2814fa9e4066Sahrens 
28150713e232SGeorge Wilson 	if (vd->vdev_dtl_sm == NULL) {
28160713e232SGeorge Wilson 		uint64_t new_object;
28170713e232SGeorge Wilson 
2818814dcd43SSerapheim Dimitropoulos 		new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx);
28190713e232SGeorge Wilson 		VERIFY3U(new_object, !=, 0);
28200713e232SGeorge Wilson 
28210713e232SGeorge Wilson 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
28225cabbc6bSPrashanth Sreenivasa 		    0, -1ULL, 0));
28230713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
28240713e232SGeorge Wilson 	}
28250713e232SGeorge Wilson 
28264d7988d6SPaul Dagnelie 	rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
28270713e232SGeorge Wilson 
28280713e232SGeorge Wilson 	mutex_enter(&vd->vdev_dtl_lock);
28290713e232SGeorge Wilson 	range_tree_walk(rt, range_tree_add, rtsync);
28300713e232SGeorge Wilson 	mutex_exit(&vd->vdev_dtl_lock);
28310713e232SGeorge Wilson 
2832814dcd43SSerapheim Dimitropoulos 	space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx);
283317f11284SSerapheim Dimitropoulos 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
28340713e232SGeorge Wilson 	range_tree_vacate(rtsync, NULL, NULL);
28350713e232SGeorge Wilson 
28360713e232SGeorge Wilson 	range_tree_destroy(rtsync);
28370713e232SGeorge Wilson 
28380713e232SGeorge Wilson 	/*
28390713e232SGeorge Wilson 	 * If the object for the space map has changed then dirty
28400713e232SGeorge Wilson 	 * the top level so that we update the config.
28410713e232SGeorge Wilson 	 */
28420713e232SGeorge Wilson 	if (object != space_map_object(vd->vdev_dtl_sm)) {
28433ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
28443ee8c80cSPavel Zakharov 		    "new object %llu", (u_longlong_t)txg, spa_name(spa),
28453ee8c80cSPavel Zakharov 		    (u_longlong_t)object,
28463ee8c80cSPavel Zakharov 		    (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
2847fa9e4066Sahrens 		vdev_config_dirty(vd->vdev_top);
2848fa9e4066Sahrens 	}
2849fa9e4066Sahrens 
28500713e232SGeorge Wilson 	dmu_tx_commit(tx);
2851fa9e4066Sahrens }
2852fa9e4066Sahrens 
2853088f3894Sahrens /*
28548ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
28558ad4d6ddSJeff Bonwick  * without losing data.
28568ad4d6ddSJeff Bonwick  */
28578ad4d6ddSJeff Bonwick boolean_t
vdev_dtl_required(vdev_t * vd)28588ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
28598ad4d6ddSJeff Bonwick {
28608ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
28618ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
28628ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
28638ad4d6ddSJeff Bonwick 	boolean_t required;
28648ad4d6ddSJeff Bonwick 
28658ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
28668ad4d6ddSJeff Bonwick 
28678ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
28688ad4d6ddSJeff Bonwick 		return (B_TRUE);
28698ad4d6ddSJeff Bonwick 
28708ad4d6ddSJeff Bonwick 	/*
28718ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
28728ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
28738ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
28748ad4d6ddSJeff Bonwick 	 */
28758ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
28768ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
28778ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
28788ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
28798ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
28808ad4d6ddSJeff Bonwick 
2881cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
2882cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2883cb04b873SMark J Musante 
28848ad4d6ddSJeff Bonwick 	return (required);
28858ad4d6ddSJeff Bonwick }
28868ad4d6ddSJeff Bonwick 
28878ad4d6ddSJeff Bonwick /*
2888088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
2889088f3894Sahrens  */
2890088f3894Sahrens boolean_t
vdev_resilver_needed(vdev_t * vd,uint64_t * minp,uint64_t * maxp)2891088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2892088f3894Sahrens {
2893088f3894Sahrens 	boolean_t needed = B_FALSE;
2894088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
2895088f3894Sahrens 	uint64_t thismax = 0;
2896088f3894Sahrens 
2897088f3894Sahrens 	if (vd->vdev_children == 0) {
2898088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
289986714001SSerapheim Dimitropoulos 		if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
29008ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
2901088f3894Sahrens 
2902b4952e17SGeorge Wilson 			thismin = vdev_dtl_min(vd);
2903b4952e17SGeorge Wilson 			thismax = vdev_dtl_max(vd);
2904088f3894Sahrens 			needed = B_TRUE;
2905088f3894Sahrens 		}
2906088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2907088f3894Sahrens 	} else {
29088ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2909088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
2910088f3894Sahrens 			uint64_t cmin, cmax;
2911088f3894Sahrens 
2912088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2913088f3894Sahrens 				thismin = MIN(thismin, cmin);
2914088f3894Sahrens 				thismax = MAX(thismax, cmax);
2915088f3894Sahrens 				needed = B_TRUE;
2916088f3894Sahrens 			}
2917088f3894Sahrens 		}
2918088f3894Sahrens 	}
2919088f3894Sahrens 
2920088f3894Sahrens 	if (needed && minp) {
2921088f3894Sahrens 		*minp = thismin;
2922088f3894Sahrens 		*maxp = thismax;
2923088f3894Sahrens 	}
2924088f3894Sahrens 	return (needed);
2925088f3894Sahrens }
2926088f3894Sahrens 
292786714001SSerapheim Dimitropoulos /*
292886714001SSerapheim Dimitropoulos  * Gets the checkpoint space map object from the vdev's ZAP.
292986714001SSerapheim Dimitropoulos  * Returns the spacemap object, or 0 if it wasn't in the ZAP
293086714001SSerapheim Dimitropoulos  * or the ZAP doesn't exist yet.
293186714001SSerapheim Dimitropoulos  */
293286714001SSerapheim Dimitropoulos int
vdev_checkpoint_sm_object(vdev_t * vd)293386714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd)
293486714001SSerapheim Dimitropoulos {
293586714001SSerapheim Dimitropoulos 	ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
293686714001SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0) {
293786714001SSerapheim Dimitropoulos 		return (0);
293886714001SSerapheim Dimitropoulos 	}
293986714001SSerapheim Dimitropoulos 
294086714001SSerapheim Dimitropoulos 	uint64_t sm_obj = 0;
294186714001SSerapheim Dimitropoulos 	int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
294286714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj);
294386714001SSerapheim Dimitropoulos 
294486714001SSerapheim Dimitropoulos 	ASSERT(err == 0 || err == ENOENT);
294586714001SSerapheim Dimitropoulos 
294686714001SSerapheim Dimitropoulos 	return (sm_obj);
294786714001SSerapheim Dimitropoulos }
294886714001SSerapheim Dimitropoulos 
29495cabbc6bSPrashanth Sreenivasa int
vdev_load(vdev_t * vd)2950ea8dc4b6Seschrock vdev_load(vdev_t *vd)
2951fa9e4066Sahrens {
29525cabbc6bSPrashanth Sreenivasa 	int error = 0;
2953fa9e4066Sahrens 	/*
2954fa9e4066Sahrens 	 * Recursively load all children.
2955fa9e4066Sahrens 	 */
29565cabbc6bSPrashanth Sreenivasa 	for (int c = 0; c < vd->vdev_children; c++) {
29575cabbc6bSPrashanth Sreenivasa 		error = vdev_load(vd->vdev_child[c]);
29585cabbc6bSPrashanth Sreenivasa 		if (error != 0) {
29595cabbc6bSPrashanth Sreenivasa 			return (error);
29605cabbc6bSPrashanth Sreenivasa 		}
29615cabbc6bSPrashanth Sreenivasa 	}
29625cabbc6bSPrashanth Sreenivasa 
29635cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
2964fa9e4066Sahrens 
2965fa9e4066Sahrens 	/*
2966663207adSDon Brady 	 * On spa_load path, grab the allocation bias from our zap
2967663207adSDon Brady 	 */
2968663207adSDon Brady 	if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
2969663207adSDon Brady 		spa_t *spa = vd->vdev_spa;
2970663207adSDon Brady 		char bias_str[64];
2971663207adSDon Brady 
2972663207adSDon Brady 		if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
2973663207adSDon Brady 		    VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
2974663207adSDon Brady 		    bias_str) == 0) {
2975663207adSDon Brady 			ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
2976663207adSDon Brady 			vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
2977663207adSDon Brady 		}
2978663207adSDon Brady 	}
2979663207adSDon Brady 
2980663207adSDon Brady 	/*
29810e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
2982fa9e4066Sahrens 	 */
29835cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
2984663207adSDon Brady 		vdev_metaslab_group_create(vd);
2985663207adSDon Brady 
29865cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
2987ea8dc4b6Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2988fa9e4066Sahrens 			    VDEV_AUX_CORRUPT_DATA);
29893ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
29903ee8c80cSPavel Zakharov 			    "asize=%llu", (u_longlong_t)vd->vdev_ashift,
29913ee8c80cSPavel Zakharov 			    (u_longlong_t)vd->vdev_asize);
29925cabbc6bSPrashanth Sreenivasa 			return (SET_ERROR(ENXIO));
2993555d674dSSerapheim Dimitropoulos 		}
2994555d674dSSerapheim Dimitropoulos 
2995555d674dSSerapheim Dimitropoulos 		error = vdev_metaslab_init(vd, 0);
2996555d674dSSerapheim Dimitropoulos 		if (error != 0) {
29973ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
29983ee8c80cSPavel Zakharov 			    "[error=%d]", error);
29995cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
30005cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
30015cabbc6bSPrashanth Sreenivasa 			return (error);
30025cabbc6bSPrashanth Sreenivasa 		}
300386714001SSerapheim Dimitropoulos 
300486714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd);
300586714001SSerapheim Dimitropoulos 		if (checkpoint_sm_obj != 0) {
300686714001SSerapheim Dimitropoulos 			objset_t *mos = spa_meta_objset(vd->vdev_spa);
300786714001SSerapheim Dimitropoulos 			ASSERT(vd->vdev_asize != 0);
300886714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
300986714001SSerapheim Dimitropoulos 
3010555d674dSSerapheim Dimitropoulos 			error = space_map_open(&vd->vdev_checkpoint_sm,
301186714001SSerapheim Dimitropoulos 			    mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3012555d674dSSerapheim Dimitropoulos 			    vd->vdev_ashift);
3013555d674dSSerapheim Dimitropoulos 			if (error != 0) {
301486714001SSerapheim Dimitropoulos 				vdev_dbgmsg(vd, "vdev_load: space_map_open "
301586714001SSerapheim Dimitropoulos 				    "failed for checkpoint spacemap (obj %llu) "
301686714001SSerapheim Dimitropoulos 				    "[error=%d]",
301786714001SSerapheim Dimitropoulos 				    (u_longlong_t)checkpoint_sm_obj, error);
301886714001SSerapheim Dimitropoulos 				return (error);
301986714001SSerapheim Dimitropoulos 			}
302086714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
302186714001SSerapheim Dimitropoulos 
302286714001SSerapheim Dimitropoulos 			/*
302386714001SSerapheim Dimitropoulos 			 * Since the checkpoint_sm contains free entries
3024555d674dSSerapheim Dimitropoulos 			 * exclusively we can use space_map_allocated() to
3025555d674dSSerapheim Dimitropoulos 			 * indicate the cumulative checkpointed space that
3026555d674dSSerapheim Dimitropoulos 			 * has been freed.
302786714001SSerapheim Dimitropoulos 			 */
302886714001SSerapheim Dimitropoulos 			vd->vdev_stat.vs_checkpoint_space =
3029555d674dSSerapheim Dimitropoulos 			    -space_map_allocated(vd->vdev_checkpoint_sm);
303086714001SSerapheim Dimitropoulos 			vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
303186714001SSerapheim Dimitropoulos 			    vd->vdev_stat.vs_checkpoint_space;
303286714001SSerapheim Dimitropoulos 		}
30335cabbc6bSPrashanth Sreenivasa 	}
3034fa9e4066Sahrens 
3035fa9e4066Sahrens 	/*
3036fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
3037fa9e4066Sahrens 	 */
30385cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3039ea8dc4b6Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3040fa9e4066Sahrens 		    VDEV_AUX_CORRUPT_DATA);
30413ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
30423ee8c80cSPavel Zakharov 		    "[error=%d]", error);
30435cabbc6bSPrashanth Sreenivasa 		return (error);
30445cabbc6bSPrashanth Sreenivasa 	}
30455cabbc6bSPrashanth Sreenivasa 
30465cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
30475cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
30485cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
30495cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_asize != 0);
305086714001SSerapheim Dimitropoulos 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
30515cabbc6bSPrashanth Sreenivasa 
30525cabbc6bSPrashanth Sreenivasa 		if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
30535cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object, 0, vd->vdev_asize, 0))) {
30545cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
30555cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
30563ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
30573ee8c80cSPavel Zakharov 			    "obsolete spacemap (obj %llu) [error=%d]",
30583ee8c80cSPavel Zakharov 			    (u_longlong_t)obsolete_sm_object, error);
30595cabbc6bSPrashanth Sreenivasa 			return (error);
30605cabbc6bSPrashanth Sreenivasa 		}
30615cabbc6bSPrashanth Sreenivasa 	}
30625cabbc6bSPrashanth Sreenivasa 
30635cabbc6bSPrashanth Sreenivasa 	return (0);
3064fa9e4066Sahrens }
3065fa9e4066Sahrens 
306699653d4eSeschrock /*
3067fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
3068fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
3069fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
3070fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
3071fa94a07fSbrendan  * repurposed to another pool.
307299653d4eSeschrock  */
307399653d4eSeschrock int
vdev_validate_aux(vdev_t * vd)3074fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
307599653d4eSeschrock {
307699653d4eSeschrock 	nvlist_t *label;
307799653d4eSeschrock 	uint64_t guid, version;
307899653d4eSeschrock 	uint64_t state;
307999653d4eSeschrock 
3080e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
3081c5904d13Seschrock 		return (0);
3082c5904d13Seschrock 
3083dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
308499653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
308599653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
308699653d4eSeschrock 		return (-1);
308799653d4eSeschrock 	}
308899653d4eSeschrock 
308999653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3090ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(version) ||
309199653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
309299653d4eSeschrock 	    guid != vd->vdev_guid ||
309399653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
309499653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
309599653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
309699653d4eSeschrock 		nvlist_free(label);
309799653d4eSeschrock 		return (-1);
309899653d4eSeschrock 	}
309999653d4eSeschrock 
310099653d4eSeschrock 	/*
310199653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
310299653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
310399653d4eSeschrock 	 */
310499653d4eSeschrock 	nvlist_free(label);
310599653d4eSeschrock 	return (0);
310699653d4eSeschrock }
310799653d4eSeschrock 
3108814dcd43SSerapheim Dimitropoulos static void
vdev_destroy_ms_flush_data(vdev_t * vd,dmu_tx_t * tx)3109814dcd43SSerapheim Dimitropoulos vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx)
3110814dcd43SSerapheim Dimitropoulos {
3111814dcd43SSerapheim Dimitropoulos 	objset_t *mos = spa_meta_objset(vd->vdev_spa);
3112814dcd43SSerapheim Dimitropoulos 
3113814dcd43SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0)
3114814dcd43SSerapheim Dimitropoulos 		return;
3115814dcd43SSerapheim Dimitropoulos 
3116814dcd43SSerapheim Dimitropoulos 	uint64_t object = 0;
3117814dcd43SSerapheim Dimitropoulos 	int err = zap_lookup(mos, vd->vdev_top_zap,
3118814dcd43SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object);
3119814dcd43SSerapheim Dimitropoulos 	if (err == ENOENT)
3120814dcd43SSerapheim Dimitropoulos 		return;
3121814dcd43SSerapheim Dimitropoulos 
3122814dcd43SSerapheim Dimitropoulos 	VERIFY0(dmu_object_free(mos, object, tx));
3123814dcd43SSerapheim Dimitropoulos 	VERIFY0(zap_remove(mos, vd->vdev_top_zap,
3124814dcd43SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx));
3125814dcd43SSerapheim Dimitropoulos }
3126814dcd43SSerapheim Dimitropoulos 
31275cabbc6bSPrashanth Sreenivasa /*
31285cabbc6bSPrashanth Sreenivasa  * Free the objects used to store this vdev's spacemaps, and the array
31295cabbc6bSPrashanth Sreenivasa  * that points to them.
31305cabbc6bSPrashanth Sreenivasa  */
3131fa9e4066Sahrens void
vdev_destroy_spacemaps(vdev_t * vd,dmu_tx_t * tx)31325cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
31335cabbc6bSPrashanth Sreenivasa {
31345cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0)
31355cabbc6bSPrashanth Sreenivasa 		return;
31365cabbc6bSPrashanth Sreenivasa 
31375cabbc6bSPrashanth Sreenivasa 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
31385cabbc6bSPrashanth Sreenivasa 	uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
31395cabbc6bSPrashanth Sreenivasa 	size_t array_bytes = array_count * sizeof (uint64_t);
31405cabbc6bSPrashanth Sreenivasa 	uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
31415cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
31425cabbc6bSPrashanth Sreenivasa 	    array_bytes, smobj_array, 0));
31435cabbc6bSPrashanth Sreenivasa 
31445cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < array_count; i++) {
31455cabbc6bSPrashanth Sreenivasa 		uint64_t smobj = smobj_array[i];
31465cabbc6bSPrashanth Sreenivasa 		if (smobj == 0)
31475cabbc6bSPrashanth Sreenivasa 			continue;
31485cabbc6bSPrashanth Sreenivasa 
31495cabbc6bSPrashanth Sreenivasa 		space_map_free_obj(mos, smobj, tx);
31505cabbc6bSPrashanth Sreenivasa 	}
31515cabbc6bSPrashanth Sreenivasa 
31525cabbc6bSPrashanth Sreenivasa 	kmem_free(smobj_array, array_bytes);
31535cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
3154814dcd43SSerapheim Dimitropoulos 	vdev_destroy_ms_flush_data(vd, tx);
31555cabbc6bSPrashanth Sreenivasa 	vd->vdev_ms_array = 0;
31565cabbc6bSPrashanth Sreenivasa }
31575cabbc6bSPrashanth Sreenivasa 
31585cabbc6bSPrashanth Sreenivasa static void
vdev_remove_empty_log(vdev_t * vd,uint64_t txg)31594e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
316088ecc943SGeorge Wilson {
316188ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
316288ecc943SGeorge Wilson 
31634e75ba68SSerapheim Dimitropoulos 	ASSERT(vd->vdev_islog);
3164215198a6SJoe Stein 	ASSERT(vd == vd->vdev_top);
3165215198a6SJoe Stein 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
316688ecc943SGeorge Wilson 
31674e75ba68SSerapheim Dimitropoulos 	dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
3168215198a6SJoe Stein 
31694e75ba68SSerapheim Dimitropoulos 	vdev_destroy_spacemaps(vd, tx);
31704e75ba68SSerapheim Dimitropoulos 	if (vd->vdev_top_zap != 0) {
3171215198a6SJoe Stein 		vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
3172215198a6SJoe Stein 		vd->vdev_top_zap = 0;
3173215198a6SJoe Stein 	}
31744e75ba68SSerapheim Dimitropoulos 
317588ecc943SGeorge Wilson 	dmu_tx_commit(tx);
317688ecc943SGeorge Wilson }
317788ecc943SGeorge Wilson 
317888ecc943SGeorge Wilson void
vdev_sync_done(vdev_t * vd,uint64_t txg)3179fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
3180fa9e4066Sahrens {
3181fa9e4066Sahrens 	metaslab_t *msp;
318280eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
3183fa9e4066Sahrens 
31845cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
318588ecc943SGeorge Wilson 
3186094e47e9SGeorge Wilson 	while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
3187094e47e9SGeorge Wilson 	    != NULL)
3188fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
318980eb36f2SGeorge Wilson 
319080eb36f2SGeorge Wilson 	if (reassess)
319180eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
3192fa9e4066Sahrens }
3193fa9e4066Sahrens 
3194fa9e4066Sahrens void
vdev_sync(vdev_t * vd,uint64_t txg)3195fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
3196fa9e4066Sahrens {
3197fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
3198fa9e4066Sahrens 	vdev_t *lvd;
3199fa9e4066Sahrens 	metaslab_t *msp;
3200fa9e4066Sahrens 
3201555d674dSSerapheim Dimitropoulos 	ASSERT3U(txg, ==, spa->spa_syncing_txg);
3202555d674dSSerapheim Dimitropoulos 	dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
32035cabbc6bSPrashanth Sreenivasa 	if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
32045cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
32055cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
32065cabbc6bSPrashanth Sreenivasa 
32075cabbc6bSPrashanth Sreenivasa 		vdev_indirect_sync_obsolete(vd, tx);
32085cabbc6bSPrashanth Sreenivasa 
32095cabbc6bSPrashanth Sreenivasa 		/*
32105cabbc6bSPrashanth Sreenivasa 		 * If the vdev is indirect, it can't have dirty
32115cabbc6bSPrashanth Sreenivasa 		 * metaslabs or DTLs.
32125cabbc6bSPrashanth Sreenivasa 		 */
32135cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ops == &vdev_indirect_ops) {
32145cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
32155cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3216555d674dSSerapheim Dimitropoulos 			dmu_tx_commit(tx);
32175cabbc6bSPrashanth Sreenivasa 			return;
32185cabbc6bSPrashanth Sreenivasa 		}
32195cabbc6bSPrashanth Sreenivasa 	}
32205cabbc6bSPrashanth Sreenivasa 
32215cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
32225cabbc6bSPrashanth Sreenivasa 
32235cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
32245cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_removing) {
3225ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
32265cabbc6bSPrashanth Sreenivasa 		ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3227ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3228ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3229ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
3230ecc2d604Sbonwick 		vdev_config_dirty(vd);
3231ecc2d604Sbonwick 	}
3232fa9e4066Sahrens 
3233ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3234fa9e4066Sahrens 		metaslab_sync(msp, txg);
3235ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3236ecc2d604Sbonwick 	}
3237fa9e4066Sahrens 
3238fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3239fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
3240fa9e4066Sahrens 
32415cabbc6bSPrashanth Sreenivasa 	/*
32424e75ba68SSerapheim Dimitropoulos 	 * If this is an empty log device being removed, destroy the
32434e75ba68SSerapheim Dimitropoulos 	 * metadata associated with it.
32445cabbc6bSPrashanth Sreenivasa 	 */
32454e75ba68SSerapheim Dimitropoulos 	if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
32464e75ba68SSerapheim Dimitropoulos 		vdev_remove_empty_log(vd, txg);
32475cabbc6bSPrashanth Sreenivasa 
3248fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
3249555d674dSSerapheim Dimitropoulos 	dmu_tx_commit(tx);
3250fa9e4066Sahrens }
3251fa9e4066Sahrens 
3252fa9e4066Sahrens uint64_t
vdev_psize_to_asize(vdev_t * vd,uint64_t psize)3253fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
3254fa9e4066Sahrens {
3255fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
3256fa9e4066Sahrens }
3257fa9e4066Sahrens 
32583d7072f8Seschrock /*
32593d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
32603d7072f8Seschrock  * not be opened, and no I/O is attempted.
32613d7072f8Seschrock  */
3262fa9e4066Sahrens int
vdev_fault(spa_t * spa,uint64_t guid,vdev_aux_t aux)3263069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3264fa9e4066Sahrens {
32654b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
3266fa9e4066Sahrens 
32678f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
3268fa9e4066Sahrens 
3269c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3270e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3271e14bb325SJeff Bonwick 
32720e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
3273e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
32740e34b6a7Sbonwick 
32754b964adaSGeorge Wilson 	tvd = vd->vdev_top;
32764b964adaSGeorge Wilson 
32773d7072f8Seschrock 	/*
3278069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
3279069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
3280069f55e2SEric Schrock 	 * were faulted.
3281069f55e2SEric Schrock 	 */
3282069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
3283069f55e2SEric Schrock 
3284069f55e2SEric Schrock 	/*
32853d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
32863d7072f8Seschrock 	 */
328798d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
32883d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
32893d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
3290069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
3291fa9e4066Sahrens 
32923d7072f8Seschrock 	/*
3293c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
3294c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
32953d7072f8Seschrock 	 */
32964b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
32973d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
32983d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
32993d7072f8Seschrock 
33003d7072f8Seschrock 		/*
33013d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
33023d7072f8Seschrock 		 * mark it degraded.
33033d7072f8Seschrock 		 */
33044b964adaSGeorge Wilson 		vdev_reopen(tvd);
33053d7072f8Seschrock 
3306069f55e2SEric Schrock 		if (vdev_readable(vd))
3307069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
33083d7072f8Seschrock 	}
3309fa9e4066Sahrens 
3310e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
33113d7072f8Seschrock }
33123d7072f8Seschrock 
33133d7072f8Seschrock /*
33143d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
33153d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
33163d7072f8Seschrock  * as I/O is concerned.
33173d7072f8Seschrock  */
33183d7072f8Seschrock int
vdev_degrade(spa_t * spa,uint64_t guid,vdev_aux_t aux)3319069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
33203d7072f8Seschrock {
3321c5904d13Seschrock 	vdev_t *vd;
33223d7072f8Seschrock 
33238f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
33243d7072f8Seschrock 
3325c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3326e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3327e14bb325SJeff Bonwick 
33283d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
3329e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
33303d7072f8Seschrock 
33313d7072f8Seschrock 	/*
33323d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
33333d7072f8Seschrock 	 */
3334e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
3335e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
33363d7072f8Seschrock 
33373d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
33383d7072f8Seschrock 	if (!vdev_is_dead(vd))
33393d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3340069f55e2SEric Schrock 		    aux);
33413d7072f8Seschrock 
3342e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
3343fa9e4066Sahrens }
3344fa9e4066Sahrens 
33453d7072f8Seschrock /*
3346f7170741SWill Andrews  * Online the given vdev.
3347f7170741SWill Andrews  *
3348f7170741SWill Andrews  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
3349f7170741SWill Andrews  * spare device should be detached when the device finishes resilvering.
3350f7170741SWill Andrews  * Second, the online should be treated like a 'test' online case, so no FMA
3351f7170741SWill Andrews  * events are generated if the device fails to open.
33523d7072f8Seschrock  */
3353fa9e4066Sahrens int
vdev_online(spa_t * spa,uint64_t guid,uint64_t flags,vdev_state_t * newstate)3354e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
3355fa9e4066Sahrens {
3356573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
33575f368aefSYuri Pankov 	boolean_t wasoffline;
33585f368aefSYuri Pankov 	vdev_state_t oldstate;
3359fa9e4066Sahrens 
33608f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
3361fa9e4066Sahrens 
3362c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3363e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3364fa9e4066Sahrens 
33650e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
3366e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
33670e34b6a7Sbonwick 
33685f368aefSYuri Pankov 	wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
33695f368aefSYuri Pankov 	oldstate = vd->vdev_state;
337014372834SHans Rosenfeld 
3371573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
33723d7072f8Seschrock 	vd->vdev_offline = B_FALSE;
33733d7072f8Seschrock 	vd->vdev_tmpoffline = B_FALSE;
3374e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3375e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3376573ca77eSGeorge Wilson 
3377573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
3378573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3379573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3380573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
3381573ca77eSGeorge Wilson 	}
3382573ca77eSGeorge Wilson 
3383573ca77eSGeorge Wilson 	vdev_reopen(tvd);
33843d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
33853d7072f8Seschrock 
3386573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3387573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3388573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
3389573ca77eSGeorge Wilson 	}
3390573ca77eSGeorge Wilson 
33913d7072f8Seschrock 	if (newstate)
33923d7072f8Seschrock 		*newstate = vd->vdev_state;
33933d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
33943d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
33953d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
33963d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
33973d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
33983d7072f8Seschrock 
3399573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3400573ca77eSGeorge Wilson 
3401573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
3402573ca77eSGeorge Wilson 		if (vd->vdev_aux)
3403573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3404573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3405573ca77eSGeorge Wilson 	}
340614372834SHans Rosenfeld 
3407094e47e9SGeorge Wilson 	/* Restart initializing if necessary */
3408094e47e9SGeorge Wilson 	mutex_enter(&vd->vdev_initialize_lock);
3409094e47e9SGeorge Wilson 	if (vdev_writeable(vd) &&
3410094e47e9SGeorge Wilson 	    vd->vdev_initialize_thread == NULL &&
3411094e47e9SGeorge Wilson 	    vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
3412094e47e9SGeorge Wilson 		(void) vdev_initialize(vd);
3413094e47e9SGeorge Wilson 	}
3414094e47e9SGeorge Wilson 	mutex_exit(&vd->vdev_initialize_lock);
3415094e47e9SGeorge Wilson 
3416084fd14fSBrian Behlendorf 	/* Restart trimming if necessary */
3417084fd14fSBrian Behlendorf 	mutex_enter(&vd->vdev_trim_lock);
3418084fd14fSBrian Behlendorf 	if (vdev_writeable(vd) &&
3419084fd14fSBrian Behlendorf 	    vd->vdev_trim_thread == NULL &&
3420084fd14fSBrian Behlendorf 	    vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
3421084fd14fSBrian Behlendorf 		(void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
3422084fd14fSBrian Behlendorf 		    vd->vdev_trim_secure);
3423084fd14fSBrian Behlendorf 	}
3424084fd14fSBrian Behlendorf 	mutex_exit(&vd->vdev_trim_lock);
3425084fd14fSBrian Behlendorf 
34265f368aefSYuri Pankov 	if (wasoffline ||
34275f368aefSYuri Pankov 	    (oldstate < VDEV_STATE_DEGRADED &&
34285f368aefSYuri Pankov 	    vd->vdev_state >= VDEV_STATE_DEGRADED))
3429ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
343014372834SHans Rosenfeld 
34318ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
34323d7072f8Seschrock }
34333d7072f8Seschrock 
3434a1521560SJeff Bonwick static int
vdev_offline_locked(spa_t * spa,uint64_t guid,uint64_t flags)3435a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
34363d7072f8Seschrock {
3437e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
34388f18d1faSGeorge Wilson 	int error = 0;
34398f18d1faSGeorge Wilson 	uint64_t generation;
34408f18d1faSGeorge Wilson 	metaslab_group_t *mg;
34413d7072f8Seschrock 
34428f18d1faSGeorge Wilson top:
34438f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
34443d7072f8Seschrock 
3445c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3446e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
34473d7072f8Seschrock 
34483d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
3449e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3450fa9e4066Sahrens 
3451e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
34528f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
34538f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
3454e6ca193dSGeorge Wilson 
3455ecc2d604Sbonwick 	/*
3456ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
3457ecc2d604Sbonwick 	 */
3458ecc2d604Sbonwick 	if (!vd->vdev_offline) {
3459fa9e4066Sahrens 		/*
34608ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
3461e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
3462e6ca193dSGeorge Wilson 		 * expendable.
3463fa9e4066Sahrens 		 */
3464e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3465e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
3466e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3467fa9e4066Sahrens 
3468fa9e4066Sahrens 		/*
3469b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
3470b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
3471b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
3472b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
34738f18d1faSGeorge Wilson 		 */
34748f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
34758f18d1faSGeorge Wilson 			/*
34768f18d1faSGeorge Wilson 			 * Prevent any future allocations.
34778f18d1faSGeorge Wilson 			 */
3478a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
34798f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
34808f18d1faSGeorge Wilson 
34815cabbc6bSPrashanth Sreenivasa 			error = spa_reset_logs(spa);
34828f18d1faSGeorge Wilson 
348386714001SSerapheim Dimitropoulos 			/*
348486714001SSerapheim Dimitropoulos 			 * If the log device was successfully reset but has
348586714001SSerapheim Dimitropoulos 			 * checkpointed data, do not offline it.
348686714001SSerapheim Dimitropoulos 			 */
348786714001SSerapheim Dimitropoulos 			if (error == 0 &&
348886714001SSerapheim Dimitropoulos 			    tvd->vdev_checkpoint_sm != NULL) {
348986714001SSerapheim Dimitropoulos 				error = ZFS_ERR_CHECKPOINT_EXISTS;
349086714001SSerapheim Dimitropoulos 			}
349186714001SSerapheim Dimitropoulos 
34928f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
34938f18d1faSGeorge Wilson 
34948f18d1faSGeorge Wilson 			/*
34958f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
34968f18d1faSGeorge Wilson 			 */
34978f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
3498a1521560SJeff Bonwick 				metaslab_group_activate(mg);
34998f18d1faSGeorge Wilson 				if (error)
35008f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
35018f18d1faSGeorge Wilson 					    vd, error));
35028f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
35038f18d1faSGeorge Wilson 				goto top;
35048f18d1faSGeorge Wilson 			}
3505fb09f5aaSMadhav Suresh 			ASSERT0(tvd->vdev_stat.vs_alloc);
35068f18d1faSGeorge Wilson 		}
35078f18d1faSGeorge Wilson 
35088f18d1faSGeorge Wilson 		/*
3509ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
3510e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
3511e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
3512e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
3513fa9e4066Sahrens 		 */
3514fa9e4066Sahrens 		vd->vdev_offline = B_TRUE;
3515e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
3516e6ca193dSGeorge Wilson 
3517e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3518e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
3519fa9e4066Sahrens 			vd->vdev_offline = B_FALSE;
3520e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
3521e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3522fa9e4066Sahrens 		}
35238f18d1faSGeorge Wilson 
35248f18d1faSGeorge Wilson 		/*
35258f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
35268f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
35278f18d1faSGeorge Wilson 		 */
35288f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
3529a1521560SJeff Bonwick 			metaslab_group_activate(mg);
3530ecc2d604Sbonwick 	}
3531fa9e4066Sahrens 
3532e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3533ecc2d604Sbonwick 
3534e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
3535fa9e4066Sahrens }
3536fa9e4066Sahrens 
3537a1521560SJeff Bonwick int
vdev_offline(spa_t * spa,uint64_t guid,uint64_t flags)3538a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3539a1521560SJeff Bonwick {
3540a1521560SJeff Bonwick 	int error;
3541a1521560SJeff Bonwick 
3542a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
3543a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
3544a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
3545a1521560SJeff Bonwick 
3546a1521560SJeff Bonwick 	return (error);
3547a1521560SJeff Bonwick }
3548a1521560SJeff Bonwick 
3549ea8dc4b6Seschrock /*
3550ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
3551ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
3552ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
3553ea8dc4b6Seschrock  */
3554ea8dc4b6Seschrock void
vdev_clear(spa_t * spa,vdev_t * vd)3555e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
3556fa9e4066Sahrens {
3557e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3558e14bb325SJeff Bonwick 
3559e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3560fa9e4066Sahrens 
3561ea8dc4b6Seschrock 	if (vd == NULL)
3562e14bb325SJeff Bonwick 		vd = rvd;
3563fa9e4066Sahrens 
3564ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
3565ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
3566ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
3567dd50e0ccSTony Hutter 	vd->vdev_stat.vs_slow_ios = 0;
3568fa9e4066Sahrens 
3569e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
3570e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
35713d7072f8Seschrock 
35723d7072f8Seschrock 	/*
35735cabbc6bSPrashanth Sreenivasa 	 * It makes no sense to "clear" an indirect vdev.
35745cabbc6bSPrashanth Sreenivasa 	 */
35755cabbc6bSPrashanth Sreenivasa 	if (!vdev_is_concrete(vd))
35765cabbc6bSPrashanth Sreenivasa 		return;
35775cabbc6bSPrashanth Sreenivasa 
35785cabbc6bSPrashanth Sreenivasa 	/*
35798a79c1b5Sek110237 	 * If we're in the FAULTED state or have experienced failed I/O, then
35808a79c1b5Sek110237 	 * clear the persistent state and attempt to reopen the device.  We
35818a79c1b5Sek110237 	 * also mark the vdev config dirty, so that the new faulted state is
35828a79c1b5Sek110237 	 * written out to disk.
35833d7072f8Seschrock 	 */
3584e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
3585e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
35868a79c1b5Sek110237 
3587096d22d4SEric Schrock 		/*
3588096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
3589096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
3590096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
3591096d22d4SEric Schrock 		 */
3592096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
3593096d22d4SEric Schrock 
35944b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3595e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
3596e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
35973d7072f8Seschrock 
3598f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
3599e14bb325SJeff Bonwick 
3600096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
3601096d22d4SEric Schrock 
3602f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
3603e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
3604e14bb325SJeff Bonwick 
36050c06d385Sjwpoduska 		/* If a resilver isn't required, check if vdevs can be culled */
36060c06d385Sjwpoduska 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd) &&
36070c06d385Sjwpoduska 		    !dsl_scan_resilvering(spa->spa_dsl_pool) &&
36080c06d385Sjwpoduska 		    !dsl_scan_resilver_scheduled(spa->spa_dsl_pool))
36090c06d385Sjwpoduska 			spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
36103d7072f8Seschrock 
3611ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
36123d7072f8Seschrock 	}
3613096d22d4SEric Schrock 
3614096d22d4SEric Schrock 	/*
3615096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
3616096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
3617096d22d4SEric Schrock 	 * done in response to the FMA fault.
3618096d22d4SEric Schrock 	 */
3619096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3620096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3621096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
3622096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
3623fa9e4066Sahrens }
3624fa9e4066Sahrens 
3625e14bb325SJeff Bonwick boolean_t
vdev_is_dead(vdev_t * vd)3626fa9e4066Sahrens vdev_is_dead(vdev_t *vd)
3627fa9e4066Sahrens {
362888ecc943SGeorge Wilson 	/*
362988ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
363088ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
363188ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
363288ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
363388ecc943SGeorge Wilson 	 * before issuing I/O to them.
363488ecc943SGeorge Wilson 	 */
36355cabbc6bSPrashanth Sreenivasa 	return (vd->vdev_state < VDEV_STATE_DEGRADED ||
36365cabbc6bSPrashanth Sreenivasa 	    vd->vdev_ops == &vdev_hole_ops ||
363788ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
3638fa9e4066Sahrens }
3639fa9e4066Sahrens 
3640e14bb325SJeff Bonwick boolean_t
vdev_readable(vdev_t * vd)3641e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
3642fa9e4066Sahrens {
3643e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
3644fa9e4066Sahrens }
3645fa9e4066Sahrens 
3646e14bb325SJeff Bonwick boolean_t
vdev_writeable(vdev_t * vd)3647e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
3648e14bb325SJeff Bonwick {
36495cabbc6bSPrashanth Sreenivasa 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
36505cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd));
3651e14bb325SJeff Bonwick }
3652e14bb325SJeff Bonwick 
3653e14bb325SJeff Bonwick boolean_t
vdev_allocatable(vdev_t * vd)3654a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
3655a31e6787SGeorge Wilson {
36568ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
36578ad4d6ddSJeff Bonwick 
3658a31e6787SGeorge Wilson 	/*
3659a31e6787SGeorge Wilson 	 * We currently allow allocations from vdevs which may be in the
3660a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3661a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
36628ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
36638ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
36648ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
3665a31e6787SGeorge Wilson 	 */
36668ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
36675cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_cant_write && vdev_is_concrete(vd) &&
36680f7643c7SGeorge Wilson 	    vd->vdev_mg->mg_initialized);
3669a31e6787SGeorge Wilson }
3670a31e6787SGeorge Wilson 
3671a31e6787SGeorge Wilson boolean_t
vdev_accessible(vdev_t * vd,zio_t * zio)3672e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
3673e14bb325SJeff Bonwick {
3674e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
3675e14bb325SJeff Bonwick 
3676e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3677e14bb325SJeff Bonwick 		return (B_FALSE);
3678e14bb325SJeff Bonwick 
3679e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
3680e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
3681e14bb325SJeff Bonwick 
3682e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
3683e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
3684e14bb325SJeff Bonwick 
3685e14bb325SJeff Bonwick 	return (B_TRUE);
3686fa9e4066Sahrens }
3687fa9e4066Sahrens 
3688dd50e0ccSTony Hutter static void
vdev_get_child_stat(vdev_t * cvd,vdev_stat_t * vs,vdev_stat_t * cvs)3689dd50e0ccSTony Hutter vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
3690dd50e0ccSTony Hutter {
3691dd50e0ccSTony Hutter 	for (int t = 0; t < VS_ZIO_TYPES; t++) {
3692dd50e0ccSTony Hutter 		vs->vs_ops[t] += cvs->vs_ops[t];
3693dd50e0ccSTony Hutter 		vs->vs_bytes[t] += cvs->vs_bytes[t];
3694dd50e0ccSTony Hutter 	}
3695dd50e0ccSTony Hutter 
3696dd50e0ccSTony Hutter 	cvs->vs_scan_removing = cvd->vdev_removing;
3697dd50e0ccSTony Hutter }
3698dd50e0ccSTony Hutter 
3699dd50e0ccSTony Hutter /*
3700dd50e0ccSTony Hutter  * Get extended stats
3701dd50e0ccSTony Hutter  */
3702dd50e0ccSTony Hutter static void
vdev_get_child_stat_ex(vdev_t * cvd,vdev_stat_ex_t * vsx,vdev_stat_ex_t * cvsx)3703dd50e0ccSTony Hutter vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
3704dd50e0ccSTony Hutter {
3705dd50e0ccSTony Hutter 	int t, b;
3706dd50e0ccSTony Hutter 	for (t = 0; t < ZIO_TYPES; t++) {
3707dd50e0ccSTony Hutter 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
3708dd50e0ccSTony Hutter 			vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
3709dd50e0ccSTony Hutter 
3710dd50e0ccSTony Hutter 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
3711dd50e0ccSTony Hutter 			vsx->vsx_total_histo[t][b] +=
3712dd50e0ccSTony Hutter 			    cvsx->vsx_total_histo[t][b];
3713dd50e0ccSTony Hutter 		}
3714dd50e0ccSTony Hutter 	}
3715dd50e0ccSTony Hutter 
3716dd50e0ccSTony Hutter 	for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
3717dd50e0ccSTony Hutter 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
3718dd50e0ccSTony Hutter 			vsx->vsx_queue_histo[t][b] +=
3719dd50e0ccSTony Hutter 			    cvsx->vsx_queue_histo[t][b];
3720dd50e0ccSTony Hutter 		}
3721dd50e0ccSTony Hutter 		vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
3722dd50e0ccSTony Hutter 		vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
3723dd50e0ccSTony Hutter 
3724dd50e0ccSTony Hutter 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
3725dd50e0ccSTony Hutter 			vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
3726dd50e0ccSTony Hutter 
3727dd50e0ccSTony Hutter 		for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
3728dd50e0ccSTony Hutter 			vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
3729dd50e0ccSTony Hutter 	}
3730dd50e0ccSTony Hutter 
3731dd50e0ccSTony Hutter }
3732dd50e0ccSTony Hutter 
373386714001SSerapheim Dimitropoulos boolean_t
vdev_is_spacemap_addressable(vdev_t * vd)373486714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd)
373586714001SSerapheim Dimitropoulos {
3736a0b03b16SSerapheim Dimitropoulos 	if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
3737a0b03b16SSerapheim Dimitropoulos 		return (B_TRUE);
3738a0b03b16SSerapheim Dimitropoulos 
373986714001SSerapheim Dimitropoulos 	/*
3740a0b03b16SSerapheim Dimitropoulos 	 * If double-word space map entries are not enabled we assume
3741a0b03b16SSerapheim Dimitropoulos 	 * 47 bits of the space map entry are dedicated to the entry's
3742a0b03b16SSerapheim Dimitropoulos 	 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
3743a0b03b16SSerapheim Dimitropoulos 	 * to calculate the maximum address that can be described by a
3744a0b03b16SSerapheim Dimitropoulos 	 * space map entry for the given device.
374586714001SSerapheim Dimitropoulos 	 */
3746a0b03b16SSerapheim Dimitropoulos 	uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
374786714001SSerapheim Dimitropoulos 
374886714001SSerapheim Dimitropoulos 	if (shift >= 63) /* detect potential overflow */
374986714001SSerapheim Dimitropoulos 		return (B_TRUE);
375086714001SSerapheim Dimitropoulos 
375186714001SSerapheim Dimitropoulos 	return (vd->vdev_asize < (1ULL << shift));
375286714001SSerapheim Dimitropoulos }
375386714001SSerapheim Dimitropoulos 
3754fa9e4066Sahrens /*
3755fa9e4066Sahrens  * Get statistics for the given vdev.
3756fa9e4066Sahrens  */
3757dd50e0ccSTony Hutter static void
vdev_get_stats_ex_impl(vdev_t * vd,vdev_stat_t * vs,vdev_stat_ex_t * vsx)3758dd50e0ccSTony Hutter vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
3759fa9e4066Sahrens {
3760dd50e0ccSTony Hutter 	int t;
3761dd50e0ccSTony Hutter 	/*
3762dd50e0ccSTony Hutter 	 * If we're getting stats on the root vdev, aggregate the I/O counts
3763dd50e0ccSTony Hutter 	 * over all top-level vdevs (i.e. the direct children of the root).
3764dd50e0ccSTony Hutter 	 */
3765dd50e0ccSTony Hutter 	if (!vd->vdev_ops->vdev_op_leaf) {
3766dd50e0ccSTony Hutter 		if (vs) {
3767dd50e0ccSTony Hutter 			memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
3768dd50e0ccSTony Hutter 			memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
3769dd50e0ccSTony Hutter 		}
3770dd50e0ccSTony Hutter 		if (vsx)
3771dd50e0ccSTony Hutter 			memset(vsx, 0, sizeof (*vsx));
3772dd50e0ccSTony Hutter 
3773dd50e0ccSTony Hutter 		for (int c = 0; c < vd->vdev_children; c++) {
3774dd50e0ccSTony Hutter 			vdev_t *cvd = vd->vdev_child[c];
3775dd50e0ccSTony Hutter 			vdev_stat_t *cvs = &cvd->vdev_stat;
3776dd50e0ccSTony Hutter 			vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
3777dd50e0ccSTony Hutter 
3778dd50e0ccSTony Hutter 			vdev_get_stats_ex_impl(cvd, cvs, cvsx);
3779dd50e0ccSTony Hutter 			if (vs)
3780dd50e0ccSTony Hutter 				vdev_get_child_stat(cvd, vs, cvs);
3781dd50e0ccSTony Hutter 			if (vsx)
3782dd50e0ccSTony Hutter 				vdev_get_child_stat_ex(cvd, vsx, cvsx);
3783dd50e0ccSTony Hutter 
3784dd50e0ccSTony Hutter 		}
3785dd50e0ccSTony Hutter 	} else {
3786dd50e0ccSTony Hutter 		/*
3787dd50e0ccSTony Hutter 		 * We're a leaf.  Just copy our ZIO active queue stats in.  The
3788dd50e0ccSTony Hutter 		 * other leaf stats are updated in vdev_stat_update().
3789dd50e0ccSTony Hutter 		 */
3790dd50e0ccSTony Hutter 		if (!vsx)
3791dd50e0ccSTony Hutter 			return;
3792dd50e0ccSTony Hutter 
3793dd50e0ccSTony Hutter 		memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
3794dd50e0ccSTony Hutter 
3795dd50e0ccSTony Hutter 		for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) {
3796dd50e0ccSTony Hutter 			vsx->vsx_active_queue[t] =
3797dd50e0ccSTony Hutter 			    vd->vdev_queue.vq_class[t].vqc_active;
3798dd50e0ccSTony Hutter 			vsx->vsx_pend_queue[t] = avl_numnodes(
3799dd50e0ccSTony Hutter 			    &vd->vdev_queue.vq_class[t].vqc_queued_tree);
3800dd50e0ccSTony Hutter 		}
3801dd50e0ccSTony Hutter 	}
3802dd50e0ccSTony Hutter }
3803dd50e0ccSTony Hutter 
3804dd50e0ccSTony Hutter void
vdev_get_stats_ex(vdev_t * vd,vdev_stat_t * vs,vdev_stat_ex_t * vsx)3805dd50e0ccSTony Hutter vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
3806dd50e0ccSTony Hutter {
3807c39a2aaeSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
38089a4eed61SToomas Soome 	spa_t *spa = vd->vdev_spa;
38099a4eed61SToomas Soome 
3810fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
3811dd50e0ccSTony Hutter 	if (vs) {
3812fa9e4066Sahrens 		bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3813fa9e4066Sahrens 		vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3814fa9e4066Sahrens 		vs->vs_state = vd->vdev_state;
3815573ca77eSGeorge Wilson 		vs->vs_rsize = vdev_get_min_asize(vd);
3816094e47e9SGeorge Wilson 		if (vd->vdev_ops->vdev_op_leaf) {
3817dd50e0ccSTony Hutter 			vs->vs_rsize += VDEV_LABEL_START_SIZE +
3818dd50e0ccSTony Hutter 			    VDEV_LABEL_END_SIZE;
3819c39a2aaeSGeorge Wilson 			/*
3820dd50e0ccSTony Hutter 			 * Report initializing progress. Since we don't
3821dd50e0ccSTony Hutter 			 * have the initializing locks held, this is only
3822dd50e0ccSTony Hutter 			 * an estimate (although a fairly accurate one).
3823094e47e9SGeorge Wilson 			 */
3824dd50e0ccSTony Hutter 			vs->vs_initialize_bytes_done =
3825dd50e0ccSTony Hutter 			    vd->vdev_initialize_bytes_done;
3826dd50e0ccSTony Hutter 			vs->vs_initialize_bytes_est =
3827dd50e0ccSTony Hutter 			    vd->vdev_initialize_bytes_est;
3828094e47e9SGeorge Wilson 			vs->vs_initialize_state = vd->vdev_initialize_state;
3829dd50e0ccSTony Hutter 			vs->vs_initialize_action_time =
3830dd50e0ccSTony Hutter 			    vd->vdev_initialize_action_time;
3831084fd14fSBrian Behlendorf 
3832084fd14fSBrian Behlendorf 			/*
3833084fd14fSBrian Behlendorf 			 * Report manual TRIM progress. Since we don't have
3834084fd14fSBrian Behlendorf 			 * the manual TRIM locks held, this is only an
3835084fd14fSBrian Behlendorf 			 * estimate (although fairly accurate one).
3836084fd14fSBrian Behlendorf 			 */
3837084fd14fSBrian Behlendorf 			vs->vs_trim_notsup = !vd->vdev_has_trim;
3838084fd14fSBrian Behlendorf 			vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
3839084fd14fSBrian Behlendorf 			vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
3840084fd14fSBrian Behlendorf 			vs->vs_trim_state = vd->vdev_trim_state;
3841084fd14fSBrian Behlendorf 			vs->vs_trim_action_time = vd->vdev_trim_action_time;
3842dd50e0ccSTony Hutter 		}
3843094e47e9SGeorge Wilson 		/*
3844dd50e0ccSTony Hutter 		 * Report expandable space on top-level, non-auxiliary devices
3845dd50e0ccSTony Hutter 		 * only. The expandable space is reported in terms of metaslab
3846dd50e0ccSTony Hutter 		 * sized units since that determines how much space the pool
3847dd50e0ccSTony Hutter 		 * can expand.
3848c39a2aaeSGeorge Wilson 		 */
3849c39a2aaeSGeorge Wilson 		if (vd->vdev_aux == NULL && tvd != NULL) {
3850dd50e0ccSTony Hutter 			vs->vs_esize = P2ALIGN(
38519a4eed61SToomas Soome 			    vd->vdev_max_asize - vd->vdev_asize -
38529a4eed61SToomas Soome 			    spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3853c39a2aaeSGeorge Wilson 		}
38545cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
38555cabbc6bSPrashanth Sreenivasa 		    vdev_is_concrete(vd)) {
3856663207adSDon Brady 			vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
3857663207adSDon Brady 			    vd->vdev_mg->mg_fragmentation : 0;
38582986efa8SAlex Reece 		}
3859e4c795beSTom Caputi 		if (vd->vdev_ops->vdev_op_leaf)
3860e4c795beSTom Caputi 			vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
3861dd50e0ccSTony Hutter 	}
3862fa9e4066Sahrens 
3863dd50e0ccSTony Hutter 	vdev_get_stats_ex_impl(vd, vs, vsx);
3864fa9e4066Sahrens 	mutex_exit(&vd->vdev_stat_lock);
3865fa9e4066Sahrens }
3866fa9e4066Sahrens 
3867fa9e4066Sahrens void
vdev_get_stats(vdev_t * vd,vdev_stat_t * vs)3868dd50e0ccSTony Hutter vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3869dd50e0ccSTony Hutter {
3870dd50e0ccSTony Hutter 	return (vdev_get_stats_ex(vd, vs, NULL));
3871dd50e0ccSTony Hutter }
3872dd50e0ccSTony Hutter 
3873dd50e0ccSTony Hutter void
vdev_clear_stats(vdev_t * vd)3874fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
3875fa94a07fSbrendan {
3876fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
3877fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
3878fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
3879fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
3880fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
3881fa94a07fSbrendan }
3882fa94a07fSbrendan 
3883fa94a07fSbrendan void
vdev_scan_stat_init(vdev_t * vd)38843f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
38853f9d6ad7SLin Ling {
38863f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
38873f9d6ad7SLin Ling 
38883f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
38893f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
38903f9d6ad7SLin Ling 
38913f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
38923f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
38933f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
38943f9d6ad7SLin Ling }
38953f9d6ad7SLin Ling 
38963f9d6ad7SLin Ling void
vdev_stat_update(zio_t * zio,uint64_t psize)3897e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
3898fa9e4066Sahrens {
38998ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
39008ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3901e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3902fa9e4066Sahrens 	vdev_t *pvd;
3903fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
3904fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
3905dd50e0ccSTony Hutter 	vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
3906fa9e4066Sahrens 	zio_type_t type = zio->io_type;
3907fa9e4066Sahrens 	int flags = zio->io_flags;
3908fa9e4066Sahrens 
3909e14bb325SJeff Bonwick 	/*
3910e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
3911e14bb325SJeff Bonwick 	 */
3912e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
3913e14bb325SJeff Bonwick 		return;
3914e14bb325SJeff Bonwick 
3915fa9e4066Sahrens 	if (zio->io_error == 0) {
3916e14bb325SJeff Bonwick 		/*
3917e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
3918e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
3919e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
3920e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
3921e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
3922e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
3923e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
3924e14bb325SJeff Bonwick 		 *
3925e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
3926e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
3927e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
3928e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
3929e14bb325SJeff Bonwick 		 */
3930e14bb325SJeff Bonwick 		if (vd == rvd)
3931e14bb325SJeff Bonwick 			return;
3932e14bb325SJeff Bonwick 
3933e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
39348ad4d6ddSJeff Bonwick 
39358ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
39368ad4d6ddSJeff Bonwick 			return;
39378ad4d6ddSJeff Bonwick 
3938fa9e4066Sahrens 		mutex_enter(&vd->vdev_stat_lock);
39398ad4d6ddSJeff Bonwick 
3940e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
394144ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
39423f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
39433f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
39443f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
39453f9d6ad7SLin Ling 
39463f9d6ad7SLin Ling 				/* XXX cleanup? */
39473f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
39483f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
39493f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
39503f9d6ad7SLin Ling 			}
39513f9d6ad7SLin Ling 
39528ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
3953e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
3954fa9e4066Sahrens 		}
39558ad4d6ddSJeff Bonwick 
3956dd50e0ccSTony Hutter 		/*
3957dd50e0ccSTony Hutter 		 * The bytes/ops/histograms are recorded at the leaf level and
3958dd50e0ccSTony Hutter 		 * aggregated into the higher level vdevs in vdev_get_stats().
3959dd50e0ccSTony Hutter 		 */
3960dd50e0ccSTony Hutter 		if (vd->vdev_ops->vdev_op_leaf &&
3961dd50e0ccSTony Hutter 		    (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
3962084fd14fSBrian Behlendorf 			zio_type_t vs_type = type;
3963084fd14fSBrian Behlendorf 
3964084fd14fSBrian Behlendorf 			/*
3965084fd14fSBrian Behlendorf 			 * TRIM ops and bytes are reported to user space as
3966084fd14fSBrian Behlendorf 			 * ZIO_TYPE_IOCTL.  This is done to preserve the
3967084fd14fSBrian Behlendorf 			 * vdev_stat_t structure layout for user space.
3968084fd14fSBrian Behlendorf 			 */
3969084fd14fSBrian Behlendorf 			if (type == ZIO_TYPE_TRIM)
3970084fd14fSBrian Behlendorf 				vs_type = ZIO_TYPE_IOCTL;
3971084fd14fSBrian Behlendorf 
3972084fd14fSBrian Behlendorf 			vs->vs_ops[vs_type]++;
3973084fd14fSBrian Behlendorf 			vs->vs_bytes[vs_type] += psize;
39748ad4d6ddSJeff Bonwick 
3975dd50e0ccSTony Hutter 			if (flags & ZIO_FLAG_DELEGATED) {
3976dd50e0ccSTony Hutter 				vsx->vsx_agg_histo[zio->io_priority]
3977dd50e0ccSTony Hutter 				    [RQ_HISTO(zio->io_size)]++;
3978dd50e0ccSTony Hutter 			} else {
3979dd50e0ccSTony Hutter 				vsx->vsx_ind_histo[zio->io_priority]
3980dd50e0ccSTony Hutter 				    [RQ_HISTO(zio->io_size)]++;
3981dd50e0ccSTony Hutter 			}
3982dd50e0ccSTony Hutter 
3983dd50e0ccSTony Hutter 			if (zio->io_delta && zio->io_delay) {
3984dd50e0ccSTony Hutter 				vsx->vsx_queue_histo[zio->io_priority]
3985dd50e0ccSTony Hutter 				    [L_HISTO(zio->io_delta - zio->io_delay)]++;
3986dd50e0ccSTony Hutter 				vsx->vsx_disk_histo[type]
3987dd50e0ccSTony Hutter 				    [L_HISTO(zio->io_delay)]++;
3988dd50e0ccSTony Hutter 				vsx->vsx_total_histo[type]
3989dd50e0ccSTony Hutter 				    [L_HISTO(zio->io_delta)]++;
3990dd50e0ccSTony Hutter 			}
3991dd50e0ccSTony Hutter 		}
3992dd50e0ccSTony Hutter 
39938ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
3994fa9e4066Sahrens 		return;
3995fa9e4066Sahrens 	}
3996fa9e4066Sahrens 
3997fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
3998fa9e4066Sahrens 		return;
3999fa9e4066Sahrens 
40008956713aSEric Schrock 	/*
40018956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
40028956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
40038956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
40048956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
40058956713aSEric Schrock 	 */
40068956713aSEric Schrock 	if (zio->io_error == EIO &&
40078956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
40088956713aSEric Schrock 		return;
40098956713aSEric Schrock 
40108f18d1faSGeorge Wilson 	/*
40118f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
40128f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
40138f18d1faSGeorge Wilson 	 * errors.
40148f18d1faSGeorge Wilson 	 */
40158f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
40168f18d1faSGeorge Wilson 		return;
40178f18d1faSGeorge Wilson 
4018fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
4019b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
4020fa9e4066Sahrens 		if (zio->io_error == ECKSUM)
4021fa9e4066Sahrens 			vs->vs_checksum_errors++;
4022fa9e4066Sahrens 		else
4023fa9e4066Sahrens 			vs->vs_read_errors++;
4024fa9e4066Sahrens 	}
4025b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
4026fa9e4066Sahrens 		vs->vs_write_errors++;
4027fa9e4066Sahrens 	mutex_exit(&vd->vdev_stat_lock);
4028fa9e4066Sahrens 
40295cabbc6bSPrashanth Sreenivasa 	if (spa->spa_load_state == SPA_LOAD_NONE &&
40305cabbc6bSPrashanth Sreenivasa 	    type == ZIO_TYPE_WRITE && txg != 0 &&
40318ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
403244ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
4033b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
40348ad4d6ddSJeff Bonwick 		/*
4035b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
4036b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
4037b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
4038b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
4039b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
4040b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
4041b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
4042b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
40438ad4d6ddSJeff Bonwick 		 *
40448ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
40458ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
40468ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
40478ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
40488ad4d6ddSJeff Bonwick 		 */
40498ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
40508ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
405144ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
4052fa9e4066Sahrens 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
40538ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
40548ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
4055b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
4056b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
4057b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4058b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
4059fa9e4066Sahrens 			}
4060b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
40618ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
4062fa9e4066Sahrens 				return;
40638ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
40648ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
40658ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
4066fa9e4066Sahrens 		}
40678ad4d6ddSJeff Bonwick 		if (vd != rvd)
40688ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
4069fa9e4066Sahrens 	}
4070fa9e4066Sahrens }
4071fa9e4066Sahrens 
4072663207adSDon Brady int64_t
vdev_deflated_space(vdev_t * vd,int64_t space)4073663207adSDon Brady vdev_deflated_space(vdev_t *vd, int64_t space)
4074663207adSDon Brady {
4075663207adSDon Brady 	ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
4076663207adSDon Brady 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
4077663207adSDon Brady 
4078663207adSDon Brady 	return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
4079663207adSDon Brady }
4080663207adSDon Brady 
4081fa9e4066Sahrens /*
4082084fd14fSBrian Behlendorf  * Update the in-core space usage stats for this vdev, its metaslab class,
4083084fd14fSBrian Behlendorf  * and the root vdev.
4084fa9e4066Sahrens  */
4085fa9e4066Sahrens void
vdev_space_update(vdev_t * vd,int64_t alloc_delta,int64_t defer_delta,int64_t space_delta)4086b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
4087b24ab676SJeff Bonwick     int64_t space_delta)
4088fa9e4066Sahrens {
4089663207adSDon Brady 	int64_t dspace_delta;
40908654d025Sperrin 	spa_t *spa = vd->vdev_spa;
40918654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
4092fa9e4066Sahrens 
40938654d025Sperrin 	ASSERT(vd == vd->vdev_top);
40948654d025Sperrin 
409599653d4eSeschrock 	/*
40968654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
40978654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
40988654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
40998654d025Sperrin 	 * childrens', thus not accurate enough for us.
410099653d4eSeschrock 	 */
4101663207adSDon Brady 	dspace_delta = vdev_deflated_space(vd, space_delta);
410299653d4eSeschrock 
4103fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
41049740f25fSSerapheim Dimitropoulos 	/* ensure we won't underflow */
41059740f25fSSerapheim Dimitropoulos 	if (alloc_delta < 0) {
41069740f25fSSerapheim Dimitropoulos 		ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
41079740f25fSSerapheim Dimitropoulos 	}
41089740f25fSSerapheim Dimitropoulos 
4109fa9e4066Sahrens 	vd->vdev_stat.vs_alloc += alloc_delta;
4110b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
411199653d4eSeschrock 	vd->vdev_stat.vs_dspace += dspace_delta;
4112fa9e4066Sahrens 	mutex_exit(&vd->vdev_stat_lock);
41138654d025Sperrin 
4114663207adSDon Brady 	/* every class but log contributes to root space stats */
4115663207adSDon Brady 	if (vd->vdev_mg != NULL && !vd->vdev_islog) {
41169740f25fSSerapheim Dimitropoulos 		ASSERT(!vd->vdev_isl2cache);
4117b24ab676SJeff Bonwick 		mutex_enter(&rvd->vdev_stat_lock);
4118b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_alloc += alloc_delta;
4119b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
4120b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_dspace += dspace_delta;
4121b24ab676SJeff Bonwick 		mutex_exit(&rvd->vdev_stat_lock);
4122b24ab676SJeff Bonwick 	}
4123663207adSDon Brady 	/* Note: metaslab_class_space_update moved to metaslab_space_update */
4124fa94a07fSbrendan }
4125fa9e4066Sahrens 
4126fa9e4066Sahrens /*
4127fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
4128fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
4129fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
4130fa9e4066Sahrens  */
4131fa9e4066Sahrens void
vdev_config_dirty(vdev_t * vd)4132fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
4133fa9e4066Sahrens {
4134fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
4135fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
4136fa9e4066Sahrens 	int c;
4137fa9e4066Sahrens 
4138f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
4139f9af39baSGeorge Wilson 
41405dabedeeSbonwick 	/*
41416809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
41426809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
4143c5904d13Seschrock 	 */
4144c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
4145c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
4146c5904d13Seschrock 		nvlist_t **aux;
4147c5904d13Seschrock 		uint_t naux;
4148c5904d13Seschrock 
4149c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
4150c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
4151c5904d13Seschrock 				break;
4152c5904d13Seschrock 		}
4153c5904d13Seschrock 
4154e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
4155e14bb325SJeff Bonwick 			/*
4156e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
4157e14bb325SJeff Bonwick 			 */
4158e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
4159e14bb325SJeff Bonwick 			return;
4160e14bb325SJeff Bonwick 		}
4161e14bb325SJeff Bonwick 
4162c5904d13Seschrock 		sav->sav_sync = B_TRUE;
4163c5904d13Seschrock 
41646809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
41656809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
4166c5904d13Seschrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
41676809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
41686809eb4eSEric Schrock 		}
4169c5904d13Seschrock 
4170c5904d13Seschrock 		ASSERT(c < naux);
4171c5904d13Seschrock 
4172c5904d13Seschrock 		/*
4173c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
4174c5904d13Seschrock 		 * sketchy, but it will work.
4175c5904d13Seschrock 		 */
4176c5904d13Seschrock 		nvlist_free(aux[c]);
41773f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
4178c5904d13Seschrock 
4179c5904d13Seschrock 		return;
4180c5904d13Seschrock 	}
4181c5904d13Seschrock 
4182c5904d13Seschrock 	/*
4183e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
4184e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
4185e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
41865dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
41875dabedeeSbonwick 	 */
4188e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4189e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4190e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
41915dabedeeSbonwick 
4192fa9e4066Sahrens 	if (vd == rvd) {
4193fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
4194fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
4195fa9e4066Sahrens 	} else {
4196fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
4197fa9e4066Sahrens 
419888ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
41995cabbc6bSPrashanth Sreenivasa 		    vdev_is_concrete(vd)) {
4200e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
4201fa9e4066Sahrens 		}
4202fa9e4066Sahrens 	}
42035cabbc6bSPrashanth Sreenivasa }
4204fa9e4066Sahrens 
4205fa9e4066Sahrens void
vdev_config_clean(vdev_t * vd)4206fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
4207fa9e4066Sahrens {
42085dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
42095dabedeeSbonwick 
4210e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4211e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4212e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
42135dabedeeSbonwick 
4214e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
4215e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
4216e14bb325SJeff Bonwick }
4217e14bb325SJeff Bonwick 
4218e14bb325SJeff Bonwick /*
4219e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
4220e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
4221e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
4222e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
4223e14bb325SJeff Bonwick  */
4224e14bb325SJeff Bonwick void
vdev_state_dirty(vdev_t * vd)4225e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
4226e14bb325SJeff Bonwick {
4227e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
4228e14bb325SJeff Bonwick 
4229f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
4230e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
4231e14bb325SJeff Bonwick 
4232e14bb325SJeff Bonwick 	/*
4233e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
4234e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
4235e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
4236e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
4237e14bb325SJeff Bonwick 	 */
4238e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4239e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4240e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
4241e14bb325SJeff Bonwick 
42425cabbc6bSPrashanth Sreenivasa 	if (!list_link_active(&vd->vdev_state_dirty_node) &&
42435cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd))
4244e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
4245e14bb325SJeff Bonwick }
4246e14bb325SJeff Bonwick 
4247e14bb325SJeff Bonwick void
vdev_state_clean(vdev_t * vd)4248e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
4249e14bb325SJeff Bonwick {
4250e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
4251e14bb325SJeff Bonwick 
4252e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4253e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4254e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
4255e14bb325SJeff Bonwick 
4256e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
4257e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
4258fa9e4066Sahrens }
4259fa9e4066Sahrens 
426032b87932Sek110237 /*
426132b87932Sek110237  * Propagate vdev state up from children to parent.
426232b87932Sek110237  */
426344cd46caSbillm void
vdev_propagate_state(vdev_t * vd)426444cd46caSbillm vdev_propagate_state(vdev_t *vd)
426544cd46caSbillm {
42668ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
42678ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
426844cd46caSbillm 	int degraded = 0, faulted = 0;
426944cd46caSbillm 	int corrupted = 0;
427044cd46caSbillm 	vdev_t *child;
427144cd46caSbillm 
42723d7072f8Seschrock 	if (vd->vdev_children > 0) {
4273573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
427444cd46caSbillm 			child = vd->vdev_child[c];
427551ece835Seschrock 
427688ecc943SGeorge Wilson 			/*
42775cabbc6bSPrashanth Sreenivasa 			 * Don't factor holes or indirect vdevs into the
42785cabbc6bSPrashanth Sreenivasa 			 * decision.
427988ecc943SGeorge Wilson 			 */
42805cabbc6bSPrashanth Sreenivasa 			if (!vdev_is_concrete(child))
428188ecc943SGeorge Wilson 				continue;
428288ecc943SGeorge Wilson 
4283e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
42848ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
428551ece835Seschrock 				/*
428651ece835Seschrock 				 * Root special: if there is a top-level log
428751ece835Seschrock 				 * device, treat the root vdev as if it were
428851ece835Seschrock 				 * degraded.
428951ece835Seschrock 				 */
429051ece835Seschrock 				if (child->vdev_islog && vd == rvd)
429144cd46caSbillm 					degraded++;
429251ece835Seschrock 				else
429351ece835Seschrock 					faulted++;
429451ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
429551ece835Seschrock 				degraded++;
429651ece835Seschrock 			}
429744cd46caSbillm 
429844cd46caSbillm 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
429944cd46caSbillm 				corrupted++;
430044cd46caSbillm 		}
430144cd46caSbillm 
430244cd46caSbillm 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
430344cd46caSbillm 
430444cd46caSbillm 		/*
4305e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
430644cd46caSbillm 		 * opened due to corrupted metadata, then propagate the root
430744cd46caSbillm 		 * vdev's aux state as 'corrupt' rather than 'insufficient
430844cd46caSbillm 		 * replicas'.
430944cd46caSbillm 		 */
43103d7072f8Seschrock 		if (corrupted && vd == rvd &&
43113d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
431244cd46caSbillm 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
431344cd46caSbillm 			    VDEV_AUX_CORRUPT_DATA);
431444cd46caSbillm 	}
431544cd46caSbillm 
431651ece835Seschrock 	if (vd->vdev_parent)
43173d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
43183d7072f8Seschrock }
43193d7072f8Seschrock 
4320fa9e4066Sahrens /*
4321ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
4322ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
4323ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
4324ea8dc4b6Seschrock  *
4325ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
4326ea8dc4b6Seschrock  * generated.
4327fa9e4066Sahrens  */
4328fa9e4066Sahrens void
vdev_set_state(vdev_t * vd,boolean_t isopen,vdev_state_t state,vdev_aux_t aux)4329ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
4330fa9e4066Sahrens {
4331560e6e96Seschrock 	uint64_t save_state;
4332c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
4333ea8dc4b6Seschrock 
4334ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
4335ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
4336fa9e4066Sahrens 		return;
4337ea8dc4b6Seschrock 	}
4338ea8dc4b6Seschrock 
4339560e6e96Seschrock 	save_state = vd->vdev_state;
4340fa9e4066Sahrens 
4341fa9e4066Sahrens 	vd->vdev_state = state;
4342fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
4343fa9e4066Sahrens 
43443d7072f8Seschrock 	/*
43453d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
434698d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
434798d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
434898d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
434998d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
435098d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
435198d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
435298d1cbfeSGeorge Wilson 	 * children.
43533d7072f8Seschrock 	 */
435498d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
435598d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
43563d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
43573d7072f8Seschrock 
4358069f55e2SEric Schrock 	/*
4359069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
4360069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
4361069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
4362069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
4363069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
4364069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
4365069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
4366069f55e2SEric Schrock 	 */
4367069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
4368069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
4369069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
4370069f55e2SEric Schrock 
43713d7072f8Seschrock 	if (vd->vdev_removed &&
43723d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
43733d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
43743d7072f8Seschrock 		/*
43753d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
43763d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
43773d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
43783d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
43793d7072f8Seschrock 		 * it is one of our special test online cases, which is only
43803d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
43813d7072f8Seschrock 		 * fault.
43823d7072f8Seschrock 		 */
43833d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
43843d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
43853d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
43863d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
43873d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
4388ea8dc4b6Seschrock 		/*
4389cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
4390cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
4391cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
4392cb04b873SMark J Musante 		 * is not considered an error.
4393ea8dc4b6Seschrock 		 */
4394cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
4395cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
4396560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
4397560e6e96Seschrock 			vd->vdev_not_present = 1;
4398560e6e96Seschrock 
4399560e6e96Seschrock 		/*
4400560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
4401560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4402560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
4403560e6e96Seschrock 		 * want to post the ereport if the device was already in the
4404560e6e96Seschrock 		 * CANT_OPEN state beforehand.
44053d7072f8Seschrock 		 *
44063d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
44073d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
44083d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
44093d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
44103d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
44113d7072f8Seschrock 		 * about to replace the device, or attempt an online with
44123d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
4413560e6e96Seschrock 		 */
44143d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
44153d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
4416c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
4417ea8dc4b6Seschrock 			const char *class;
4418ea8dc4b6Seschrock 
4419ea8dc4b6Seschrock 			switch (aux) {
4420ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
4421ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
4422ea8dc4b6Seschrock 				break;
4423ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
4424ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
4425ea8dc4b6Seschrock 				break;
4426ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
4427ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
4428ea8dc4b6Seschrock 				break;
4429ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
4430ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
4431ea8dc4b6Seschrock 				break;
4432ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
4433ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
4434ea8dc4b6Seschrock 				break;
4435ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
4436ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
4437ea8dc4b6Seschrock 				break;
44385711d393Sloli10K 			case VDEV_AUX_BAD_ASHIFT:
44395711d393Sloli10K 				class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
44405711d393Sloli10K 				break;
4441ea8dc4b6Seschrock 			default:
4442ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
4443ea8dc4b6Seschrock 			}
4444ea8dc4b6Seschrock 
44459b088140SToomas Soome 			(void) zfs_ereport_post(class, spa, vd, NULL, NULL,
4446eb633035STom Caputi 			    save_state, 0);
4447ea8dc4b6Seschrock 		}
44483d7072f8Seschrock 
44493d7072f8Seschrock 		/* Erase any notion of persistent removed state */
44503d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
44513d7072f8Seschrock 	} else {
44523d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
4453ea8dc4b6Seschrock 	}
4454ea8dc4b6Seschrock 
44558b33d774STim Haley 	if (!isopen && vd->vdev_parent)
44568b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
4457fa9e4066Sahrens }
445815e6edf1Sgw25295 
44596f793812SPavel Zakharov boolean_t
vdev_children_are_offline(vdev_t * vd)44606f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd)
44616f793812SPavel Zakharov {
44626f793812SPavel Zakharov 	ASSERT(!vd->vdev_ops->vdev_op_leaf);
44636f793812SPavel Zakharov 
44646f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
44656f793812SPavel Zakharov 		if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
44666f793812SPavel Zakharov 			return (B_FALSE);
44676f793812SPavel Zakharov 	}
44686f793812SPavel Zakharov 
44696f793812SPavel Zakharov 	return (B_TRUE);
44706f793812SPavel Zakharov }
44716f793812SPavel Zakharov 
447215e6edf1Sgw25295 /*
447315e6edf1Sgw25295  * Check the vdev configuration to ensure that it's capable of supporting
4474c8811bd3SToomas Soome  * a root pool. We do not support partial configuration.
447515e6edf1Sgw25295  */
447615e6edf1Sgw25295 boolean_t
vdev_is_bootable(vdev_t * vd)447715e6edf1Sgw25295 vdev_is_bootable(vdev_t *vd)
447815e6edf1Sgw25295 {
447915e6edf1Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf) {
448015e6edf1Sgw25295 		char *vdev_type = vd->vdev_ops->vdev_op_type;
448115e6edf1Sgw25295 
44825623f66bSPatrick Mooney 		if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
448315e6edf1Sgw25295 			return (B_FALSE);
448415e6edf1Sgw25295 		}
448515e6edf1Sgw25295 	}
448615e6edf1Sgw25295 
4487573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
448815e6edf1Sgw25295 		if (!vdev_is_bootable(vd->vdev_child[c]))
448915e6edf1Sgw25295 			return (B_FALSE);
449015e6edf1Sgw25295 	}
449115e6edf1Sgw25295 	return (B_TRUE);
449215e6edf1Sgw25295 }
4493e6ca193dSGeorge Wilson 
44945cabbc6bSPrashanth Sreenivasa boolean_t
vdev_is_concrete(vdev_t * vd)44955cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd)
44965cabbc6bSPrashanth Sreenivasa {
44975cabbc6bSPrashanth Sreenivasa 	vdev_ops_t *ops = vd->vdev_ops;
44985cabbc6bSPrashanth Sreenivasa 	if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
44995cabbc6bSPrashanth Sreenivasa 	    ops == &vdev_missing_ops || ops == &vdev_root_ops) {
45005cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
45015cabbc6bSPrashanth Sreenivasa 	} else {
45025cabbc6bSPrashanth Sreenivasa 		return (B_TRUE);
45035cabbc6bSPrashanth Sreenivasa 	}
45045cabbc6bSPrashanth Sreenivasa }
45055cabbc6bSPrashanth Sreenivasa 
450688ecc943SGeorge Wilson /*
45074b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
45084b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
45094b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
45104b964adaSGeorge Wilson  */
45114b964adaSGeorge Wilson boolean_t
vdev_log_state_valid(vdev_t * vd)45124b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
45134b964adaSGeorge Wilson {
45144b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
45154b964adaSGeorge Wilson 	    !vd->vdev_removed)
45164b964adaSGeorge Wilson 		return (B_TRUE);
45174b964adaSGeorge Wilson 
45184b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
45194b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
45204b964adaSGeorge Wilson 			return (B_TRUE);
45214b964adaSGeorge Wilson 
45224b964adaSGeorge Wilson 	return (B_FALSE);
45234b964adaSGeorge Wilson }
45244b964adaSGeorge Wilson 
45254b964adaSGeorge Wilson /*
4526573ca77eSGeorge Wilson  * Expand a vdev if possible.
4527573ca77eSGeorge Wilson  */
4528573ca77eSGeorge Wilson void
vdev_expand(vdev_t * vd,uint64_t txg)4529573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
4530573ca77eSGeorge Wilson {
4531573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
4532573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
453311f6a968SSerapheim Dimitropoulos 	ASSERT(vdev_is_concrete(vd));
4534573ca77eSGeorge Wilson 
45355cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
45365cabbc6bSPrashanth Sreenivasa 
4537663207adSDon Brady 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
4538663207adSDon Brady 	    vdev_is_concrete(vd)) {
4539663207adSDon Brady 		vdev_metaslab_group_create(vd);
4540573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
4541573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
4542573ca77eSGeorge Wilson 	}
4543573ca77eSGeorge Wilson }
45441195e687SMark J Musante 
45451195e687SMark J Musante /*
45461195e687SMark J Musante  * Split a vdev.
45471195e687SMark J Musante  */
45481195e687SMark J Musante void
vdev_split(vdev_t * vd)45491195e687SMark J Musante vdev_split(vdev_t *vd)
45501195e687SMark J Musante {
45511195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
45521195e687SMark J Musante 
45531195e687SMark J Musante 	vdev_remove_child(pvd, vd);
45541195e687SMark J Musante 	vdev_compact_children(pvd);
45551195e687SMark J Musante 
45561195e687SMark J Musante 	cvd = pvd->vdev_child[0];
45571195e687SMark J Musante 	if (pvd->vdev_children == 1) {
45581195e687SMark J Musante 		vdev_remove_parent(cvd);
45591195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
45601195e687SMark J Musante 	}
45611195e687SMark J Musante 	vdev_propagate_state(cvd);
45621195e687SMark J Musante }
4563283b8460SGeorge.Wilson 
4564283b8460SGeorge.Wilson void
vdev_deadman(vdev_t * vd)4565283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd)
4566283b8460SGeorge.Wilson {
4567283b8460SGeorge.Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
4568283b8460SGeorge.Wilson 		vdev_t *cvd = vd->vdev_child[c];
4569283b8460SGeorge.Wilson 
4570283b8460SGeorge.Wilson 		vdev_deadman(cvd);
4571283b8460SGeorge.Wilson 	}
4572283b8460SGeorge.Wilson 
4573283b8460SGeorge.Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
4574283b8460SGeorge.Wilson 		vdev_queue_t *vq = &vd->vdev_queue;
4575283b8460SGeorge.Wilson 
4576283b8460SGeorge.Wilson 		mutex_enter(&vq->vq_lock);
457769962b56SMatthew Ahrens 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
4578283b8460SGeorge.Wilson 			spa_t *spa = vd->vdev_spa;
4579283b8460SGeorge.Wilson 			zio_t *fio;
4580283b8460SGeorge.Wilson 			uint64_t delta;
4581283b8460SGeorge.Wilson 
4582283b8460SGeorge.Wilson 			/*
4583283b8460SGeorge.Wilson 			 * Look at the head of all the pending queues,
4584283b8460SGeorge.Wilson 			 * if any I/O has been outstanding for longer than
4585283b8460SGeorge.Wilson 			 * the spa_deadman_synctime we panic the system.
4586283b8460SGeorge.Wilson 			 */
458769962b56SMatthew Ahrens 			fio = avl_first(&vq->vq_active_tree);
4588c55e05cbSMatthew Ahrens 			delta = gethrtime() - fio->io_timestamp;
4589c55e05cbSMatthew Ahrens 			if (delta > spa_deadman_synctime(spa)) {
45903ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
45913ee8c80cSPavel Zakharov 				    "%lluns, delta %lluns, last io %lluns",
45923ee8c80cSPavel Zakharov 				    fio->io_timestamp, (u_longlong_t)delta,
4593283b8460SGeorge.Wilson 				    vq->vq_io_complete_ts);
4594283b8460SGeorge.Wilson 				fm_panic("I/O to pool '%s' appears to be "
4595283b8460SGeorge.Wilson 				    "hung.", spa_name(spa));
4596283b8460SGeorge.Wilson 			}
4597283b8460SGeorge.Wilson 		}
4598283b8460SGeorge.Wilson 		mutex_exit(&vq->vq_lock);
4599283b8460SGeorge.Wilson 	}
4600283b8460SGeorge.Wilson }
4601e4c795beSTom Caputi 
4602e4c795beSTom Caputi void
vdev_defer_resilver(vdev_t * vd)46030c06d385Sjwpoduska vdev_defer_resilver(vdev_t *vd)
4604e4c795beSTom Caputi {
46050c06d385Sjwpoduska 	ASSERT(vd->vdev_ops->vdev_op_leaf);
4606e4c795beSTom Caputi 
4607e4c795beSTom Caputi 	vd->vdev_resilver_deferred = B_TRUE;
46080c06d385Sjwpoduska 	vd->vdev_spa->spa_resilver_deferred = B_TRUE;
46090c06d385Sjwpoduska }
46100c06d385Sjwpoduska 
46110c06d385Sjwpoduska /*
46120c06d385Sjwpoduska  * Clears the resilver deferred flag on all leaf devs under vd. Returns
46130c06d385Sjwpoduska  * B_TRUE if we have devices that need to be resilvered and are available to
46140c06d385Sjwpoduska  * accept resilver I/Os.
46150c06d385Sjwpoduska  */
46160c06d385Sjwpoduska boolean_t
vdev_clear_resilver_deferred(vdev_t * vd,dmu_tx_t * tx)46170c06d385Sjwpoduska vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx)
46180c06d385Sjwpoduska {
46190c06d385Sjwpoduska 	boolean_t resilver_needed = B_FALSE;
46200c06d385Sjwpoduska 	spa_t *spa = vd->vdev_spa;
46210c06d385Sjwpoduska 
46220c06d385Sjwpoduska 	for (int c = 0; c < vd->vdev_children; c++) {
46230c06d385Sjwpoduska 		vdev_t *cvd = vd->vdev_child[c];
46240c06d385Sjwpoduska 		resilver_needed |= vdev_clear_resilver_deferred(cvd, tx);
46250c06d385Sjwpoduska 	}
46260c06d385Sjwpoduska 
46270c06d385Sjwpoduska 	if (vd == spa->spa_root_vdev &&
46280c06d385Sjwpoduska 	    spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) {
46290c06d385Sjwpoduska 		spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
46300c06d385Sjwpoduska 		vdev_config_dirty(vd);
46310c06d385Sjwpoduska 		spa->spa_resilver_deferred = B_FALSE;
46320c06d385Sjwpoduska 		return (resilver_needed);
46330c06d385Sjwpoduska 	}
46340c06d385Sjwpoduska 
46350c06d385Sjwpoduska 	if (!vdev_is_concrete(vd) || vd->vdev_aux ||
46360c06d385Sjwpoduska 	    !vd->vdev_ops->vdev_op_leaf)
46370c06d385Sjwpoduska 		return (resilver_needed);
46380c06d385Sjwpoduska 
46390c06d385Sjwpoduska 	vd->vdev_resilver_deferred = B_FALSE;
46400c06d385Sjwpoduska 
46410c06d385Sjwpoduska 	return (!vdev_is_dead(vd) && !vd->vdev_offline &&
46420c06d385Sjwpoduska 	    vdev_resilver_needed(vd, NULL, NULL));
4643e4c795beSTom Caputi }
4644084fd14fSBrian Behlendorf 
4645084fd14fSBrian Behlendorf /*
4646084fd14fSBrian Behlendorf  * Translate a logical range to the physical range for the specified vdev_t.
4647084fd14fSBrian Behlendorf  * This function is initially called with a leaf vdev and will walk each
4648084fd14fSBrian Behlendorf  * parent vdev until it reaches a top-level vdev. Once the top-level is
4649084fd14fSBrian Behlendorf  * reached the physical range is initialized and the recursive function
4650084fd14fSBrian Behlendorf  * begins to unwind. As it unwinds it calls the parent's vdev specific
4651084fd14fSBrian Behlendorf  * translation function to do the real conversion.
4652084fd14fSBrian Behlendorf  */
4653084fd14fSBrian Behlendorf void
vdev_xlate(vdev_t * vd,const range_seg64_t * logical_rs,range_seg64_t * physical_rs)46544d7988d6SPaul Dagnelie vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
46554d7988d6SPaul Dagnelie     range_seg64_t *physical_rs)
4656084fd14fSBrian Behlendorf {
4657084fd14fSBrian Behlendorf 	/*
4658084fd14fSBrian Behlendorf 	 * Walk up the vdev tree
4659084fd14fSBrian Behlendorf 	 */
4660084fd14fSBrian Behlendorf 	if (vd != vd->vdev_top) {
4661084fd14fSBrian Behlendorf 		vdev_xlate(vd->vdev_parent, logical_rs, physical_rs);
4662084fd14fSBrian Behlendorf 	} else {
4663084fd14fSBrian Behlendorf 		/*
4664084fd14fSBrian Behlendorf 		 * We've reached the top-level vdev, initialize the
4665084fd14fSBrian Behlendorf 		 * physical range to the logical range and start to
4666084fd14fSBrian Behlendorf 		 * unwind.
4667084fd14fSBrian Behlendorf 		 */
4668084fd14fSBrian Behlendorf 		physical_rs->rs_start = logical_rs->rs_start;
4669084fd14fSBrian Behlendorf 		physical_rs->rs_end = logical_rs->rs_end;
4670084fd14fSBrian Behlendorf 		return;
4671084fd14fSBrian Behlendorf 	}
4672084fd14fSBrian Behlendorf 
4673084fd14fSBrian Behlendorf 	vdev_t *pvd = vd->vdev_parent;
4674084fd14fSBrian Behlendorf 	ASSERT3P(pvd, !=, NULL);
4675084fd14fSBrian Behlendorf 	ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
4676084fd14fSBrian Behlendorf 
4677084fd14fSBrian Behlendorf 	/*
4678084fd14fSBrian Behlendorf 	 * As this recursive function unwinds, translate the logical
4679084fd14fSBrian Behlendorf 	 * range into its physical components by calling the
4680084fd14fSBrian Behlendorf 	 * vdev specific translate function.
4681084fd14fSBrian Behlendorf 	 */
46824d7988d6SPaul Dagnelie 	range_seg64_t intermediate = { 0 };
4683084fd14fSBrian Behlendorf 	pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate);
4684084fd14fSBrian Behlendorf 
4685084fd14fSBrian Behlendorf 	physical_rs->rs_start = intermediate.rs_start;
4686084fd14fSBrian Behlendorf 	physical_rs->rs_end = intermediate.rs_end;
4687084fd14fSBrian Behlendorf }
4688