xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision e0f1c0af)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
2398d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
255f368aefSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
26c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
27c8811bd3SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
28ce1577b0SDave Eddy  * Copyright 2017 Joyent, Inc.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/zfs_context.h>
32ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
355cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h>
36fa9e4066Sahrens #include <sys/dmu.h>
37fa9e4066Sahrens #include <sys/dmu_tx.h>
385cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h>
39fa9e4066Sahrens #include <sys/vdev_impl.h>
40fa9e4066Sahrens #include <sys/uberblock_impl.h>
41fa9e4066Sahrens #include <sys/metaslab.h>
42fa9e4066Sahrens #include <sys/metaslab_impl.h>
43fa9e4066Sahrens #include <sys/space_map.h>
440713e232SGeorge Wilson #include <sys/space_reftree.h>
45fa9e4066Sahrens #include <sys/zio.h>
46fa9e4066Sahrens #include <sys/zap.h>
47fa9e4066Sahrens #include <sys/fs/zfs.h>
48c5904d13Seschrock #include <sys/arc.h>
49e6ca193dSGeorge Wilson #include <sys/zil.h>
503f9d6ad7SLin Ling #include <sys/dsl_scan.h>
51770499e1SDan Kimmel #include <sys/abd.h>
52094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
53fa9e4066Sahrens 
54fa9e4066Sahrens /*
55fa9e4066Sahrens  * Virtual device management.
56fa9e4066Sahrens  */
57fa9e4066Sahrens 
58fa9e4066Sahrens static vdev_ops_t *vdev_ops_table[] = {
59fa9e4066Sahrens 	&vdev_root_ops,
60fa9e4066Sahrens 	&vdev_raidz_ops,
61fa9e4066Sahrens 	&vdev_mirror_ops,
62fa9e4066Sahrens 	&vdev_replacing_ops,
6399653d4eSeschrock 	&vdev_spare_ops,
64fa9e4066Sahrens 	&vdev_disk_ops,
65fa9e4066Sahrens 	&vdev_file_ops,
66fa9e4066Sahrens 	&vdev_missing_ops,
6788ecc943SGeorge Wilson 	&vdev_hole_ops,
685cabbc6bSPrashanth Sreenivasa 	&vdev_indirect_ops,
69fa9e4066Sahrens 	NULL
70fa9e4066Sahrens };
71fa9e4066Sahrens 
72088f3894Sahrens /* maximum scrub/resilver I/O queue per leaf vdev */
73088f3894Sahrens int zfs_scrub_limit = 10;
7405b2b3b8Smishra 
75a0b03b16SSerapheim Dimitropoulos /* default target for number of metaslabs per top-level vdev */
76a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_count = 200;
7786714001SSerapheim Dimitropoulos 
78b4bf0cf0SDon Brady /* minimum number of metaslabs per top-level vdev */
79a0b03b16SSerapheim Dimitropoulos int zfs_vdev_min_ms_count = 16;
8086714001SSerapheim Dimitropoulos 
81b4bf0cf0SDon Brady /* practical upper limit of total metaslabs per top-level vdev */
82a0b03b16SSerapheim Dimitropoulos int zfs_vdev_ms_count_limit = 1ULL << 17;
83b4bf0cf0SDon Brady 
84b4bf0cf0SDon Brady /* lower limit for metaslab size (512M) */
85a0b03b16SSerapheim Dimitropoulos int zfs_vdev_default_ms_shift = 29;
8686714001SSerapheim Dimitropoulos 
87a0b03b16SSerapheim Dimitropoulos /* upper limit for metaslab size (16G) */
88a0b03b16SSerapheim Dimitropoulos int zfs_vdev_max_ms_shift = 34;
89b4bf0cf0SDon Brady 
9086714001SSerapheim Dimitropoulos boolean_t vdev_validate_skip = B_FALSE;
9186714001SSerapheim Dimitropoulos 
92bf3e216cSMatthew Ahrens /*
9386714001SSerapheim Dimitropoulos  * Since the DTL space map of a vdev is not expected to have a lot of
9486714001SSerapheim Dimitropoulos  * entries, we default its block size to 4K.
95bf3e216cSMatthew Ahrens  */
9686714001SSerapheim Dimitropoulos int vdev_dtl_sm_blksz = (1 << 12);
97bf3e216cSMatthew Ahrens 
9886714001SSerapheim Dimitropoulos /*
9986714001SSerapheim Dimitropoulos  * vdev-wide space maps that have lots of entries written to them at
10086714001SSerapheim Dimitropoulos  * the end of each transaction can benefit from a higher I/O bandwidth
10186714001SSerapheim Dimitropoulos  * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
10286714001SSerapheim Dimitropoulos  */
10386714001SSerapheim Dimitropoulos int vdev_standard_sm_blksz = (1 << 17);
1046f793812SPavel Zakharov 
10593a1902eSMatthew Ahrens int zfs_ashift_min;
10693a1902eSMatthew Ahrens 
1073ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
1083ee8c80cSPavel Zakharov void
1093ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
1103ee8c80cSPavel Zakharov {
1113ee8c80cSPavel Zakharov 	va_list adx;
1123ee8c80cSPavel Zakharov 	char buf[256];
1133ee8c80cSPavel Zakharov 
1143ee8c80cSPavel Zakharov 	va_start(adx, fmt);
1153ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
1163ee8c80cSPavel Zakharov 	va_end(adx);
1173ee8c80cSPavel Zakharov 
1183ee8c80cSPavel Zakharov 	if (vd->vdev_path != NULL) {
1193ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
1203ee8c80cSPavel Zakharov 		    vd->vdev_path, buf);
1213ee8c80cSPavel Zakharov 	} else {
1223ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
1233ee8c80cSPavel Zakharov 		    vd->vdev_ops->vdev_op_type,
1243ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_id,
1253ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_guid, buf);
1263ee8c80cSPavel Zakharov 	}
1273ee8c80cSPavel Zakharov }
1283ee8c80cSPavel Zakharov 
1296f793812SPavel Zakharov void
1306f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
1316f793812SPavel Zakharov {
1326f793812SPavel Zakharov 	char state[20];
1336f793812SPavel Zakharov 
1346f793812SPavel Zakharov 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
1356f793812SPavel Zakharov 		zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
1366f793812SPavel Zakharov 		    vd->vdev_ops->vdev_op_type);
1376f793812SPavel Zakharov 		return;
1386f793812SPavel Zakharov 	}
1396f793812SPavel Zakharov 
1406f793812SPavel Zakharov 	switch (vd->vdev_state) {
1416f793812SPavel Zakharov 	case VDEV_STATE_UNKNOWN:
1426f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "unknown");
1436f793812SPavel Zakharov 		break;
1446f793812SPavel Zakharov 	case VDEV_STATE_CLOSED:
1456f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "closed");
1466f793812SPavel Zakharov 		break;
1476f793812SPavel Zakharov 	case VDEV_STATE_OFFLINE:
1486f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "offline");
1496f793812SPavel Zakharov 		break;
1506f793812SPavel Zakharov 	case VDEV_STATE_REMOVED:
1516f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "removed");
1526f793812SPavel Zakharov 		break;
1536f793812SPavel Zakharov 	case VDEV_STATE_CANT_OPEN:
1546f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "can't open");
1556f793812SPavel Zakharov 		break;
1566f793812SPavel Zakharov 	case VDEV_STATE_FAULTED:
1576f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "faulted");
1586f793812SPavel Zakharov 		break;
1596f793812SPavel Zakharov 	case VDEV_STATE_DEGRADED:
1606f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "degraded");
1616f793812SPavel Zakharov 		break;
1626f793812SPavel Zakharov 	case VDEV_STATE_HEALTHY:
1636f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "healthy");
1646f793812SPavel Zakharov 		break;
1656f793812SPavel Zakharov 	default:
1666f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "<state %u>",
1676f793812SPavel Zakharov 		    (uint_t)vd->vdev_state);
1686f793812SPavel Zakharov 	}
1696f793812SPavel Zakharov 
1706f793812SPavel Zakharov 	zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
171c7a7b2faSAndriy Gapon 	    "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
1726f793812SPavel Zakharov 	    vd->vdev_islog ? " (log)" : "",
1736f793812SPavel Zakharov 	    (u_longlong_t)vd->vdev_guid,
1746f793812SPavel Zakharov 	    vd->vdev_path ? vd->vdev_path : "N/A", state);
1756f793812SPavel Zakharov 
1766f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++)
1776f793812SPavel Zakharov 		vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
1786f793812SPavel Zakharov }
1796f793812SPavel Zakharov 
180fa9e4066Sahrens /*
181fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
182fa9e4066Sahrens  */
183fa9e4066Sahrens static vdev_ops_t *
184fa9e4066Sahrens vdev_getops(const char *type)
185fa9e4066Sahrens {
186fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
187fa9e4066Sahrens 
188fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
189fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
190fa9e4066Sahrens 			break;
191fa9e4066Sahrens 
192fa9e4066Sahrens 	return (ops);
193fa9e4066Sahrens }
194fa9e4066Sahrens 
195094e47e9SGeorge Wilson /* ARGSUSED */
196094e47e9SGeorge Wilson void
197094e47e9SGeorge Wilson vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res)
198094e47e9SGeorge Wilson {
199094e47e9SGeorge Wilson 	res->rs_start = in->rs_start;
200094e47e9SGeorge Wilson 	res->rs_end = in->rs_end;
201094e47e9SGeorge Wilson }
202094e47e9SGeorge Wilson 
203fa9e4066Sahrens /*
204fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
205fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
206fa9e4066Sahrens  */
207fa9e4066Sahrens uint64_t
208fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
209fa9e4066Sahrens {
210ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
211fa9e4066Sahrens 	uint64_t csize;
212fa9e4066Sahrens 
213573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
214fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
215fa9e4066Sahrens 		asize = MAX(asize, csize);
216fa9e4066Sahrens 	}
217fa9e4066Sahrens 
218fa9e4066Sahrens 	return (asize);
219fa9e4066Sahrens }
220fa9e4066Sahrens 
2212a79c5feSlling /*
222573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
223573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
224573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
225573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
2262a79c5feSlling  */
2272a79c5feSlling uint64_t
228573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
2292a79c5feSlling {
230573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
2312a79c5feSlling 
232573ca77eSGeorge Wilson 	/*
2334263d13fSGeorge Wilson 	 * If our parent is NULL (inactive spare or cache) or is the root,
234573ca77eSGeorge Wilson 	 * just return our own asize.
235573ca77eSGeorge Wilson 	 */
236573ca77eSGeorge Wilson 	if (pvd == NULL)
237573ca77eSGeorge Wilson 		return (vd->vdev_asize);
2382a79c5feSlling 
2392a79c5feSlling 	/*
240573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
241573ca77eSGeorge Wilson 	 * to the nearest metaslab.
2422a79c5feSlling 	 */
243573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
244573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
2452a79c5feSlling 
246573ca77eSGeorge Wilson 	/*
247573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
248573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
249573ca77eSGeorge Wilson 	 */
250573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
251c040c10cSSteven Hartland 		return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
252c040c10cSSteven Hartland 		    pvd->vdev_children);
2532a79c5feSlling 
254573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
255573ca77eSGeorge Wilson }
2562a79c5feSlling 
257573ca77eSGeorge Wilson void
258573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
259573ca77eSGeorge Wilson {
260573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
261573ca77eSGeorge Wilson 
262573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
263573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
2642a79c5feSlling }
2652a79c5feSlling 
266fa9e4066Sahrens vdev_t *
267fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
268fa9e4066Sahrens {
269fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
270fa9e4066Sahrens 
271e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
272e05725b1Sbonwick 
273088f3894Sahrens 	if (vdev < rvd->vdev_children) {
274088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
275fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
276088f3894Sahrens 	}
277fa9e4066Sahrens 
278fa9e4066Sahrens 	return (NULL);
279fa9e4066Sahrens }
280fa9e4066Sahrens 
281fa9e4066Sahrens vdev_t *
282fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
283fa9e4066Sahrens {
284fa9e4066Sahrens 	vdev_t *mvd;
285fa9e4066Sahrens 
2860e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
287fa9e4066Sahrens 		return (vd);
288fa9e4066Sahrens 
289573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
290fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
291fa9e4066Sahrens 		    NULL)
292fa9e4066Sahrens 			return (mvd);
293fa9e4066Sahrens 
294fa9e4066Sahrens 	return (NULL);
295fa9e4066Sahrens }
296fa9e4066Sahrens 
29712380e1eSArne Jansen static int
29812380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd)
29912380e1eSArne Jansen {
30012380e1eSArne Jansen 	int n = 0;
30112380e1eSArne Jansen 
30212380e1eSArne Jansen 	if (vd->vdev_ops->vdev_op_leaf)
30312380e1eSArne Jansen 		return (1);
30412380e1eSArne Jansen 
30512380e1eSArne Jansen 	for (int c = 0; c < vd->vdev_children; c++)
30612380e1eSArne Jansen 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
30712380e1eSArne Jansen 
30812380e1eSArne Jansen 	return (n);
30912380e1eSArne Jansen }
31012380e1eSArne Jansen 
31112380e1eSArne Jansen int
31212380e1eSArne Jansen vdev_count_leaves(spa_t *spa)
31312380e1eSArne Jansen {
31412380e1eSArne Jansen 	return (vdev_count_leaves_impl(spa->spa_root_vdev));
31512380e1eSArne Jansen }
31612380e1eSArne Jansen 
317fa9e4066Sahrens void
318fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
319fa9e4066Sahrens {
320fa9e4066Sahrens 	size_t oldsize, newsize;
321fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
322fa9e4066Sahrens 	vdev_t **newchild;
32381cd5c55SMatthew Ahrens 	spa_t *spa = cvd->vdev_spa;
324fa9e4066Sahrens 
32581cd5c55SMatthew Ahrens 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
326fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
327fa9e4066Sahrens 
328fa9e4066Sahrens 	cvd->vdev_parent = pvd;
329fa9e4066Sahrens 
330fa9e4066Sahrens 	if (pvd == NULL)
331fa9e4066Sahrens 		return;
332fa9e4066Sahrens 
333fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
334fa9e4066Sahrens 
335fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
336fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
337fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
338fa9e4066Sahrens 
339fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
340fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
341fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
342fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
343fa9e4066Sahrens 	}
344fa9e4066Sahrens 
345fa9e4066Sahrens 	pvd->vdev_child = newchild;
346fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
347fa9e4066Sahrens 
348fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
349fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
350fa9e4066Sahrens 
351fa9e4066Sahrens 	/*
352fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
353fa9e4066Sahrens 	 */
354fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
355fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
356*e0f1c0afSOlaf Faaland 
357*e0f1c0afSOlaf Faaland 	if (cvd->vdev_ops->vdev_op_leaf) {
358*e0f1c0afSOlaf Faaland 		list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
359*e0f1c0afSOlaf Faaland 		cvd->vdev_spa->spa_leaf_list_gen++;
360*e0f1c0afSOlaf Faaland 	}
361fa9e4066Sahrens }
362fa9e4066Sahrens 
363fa9e4066Sahrens void
364fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
365fa9e4066Sahrens {
366fa9e4066Sahrens 	int c;
367fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
368fa9e4066Sahrens 
369fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
370fa9e4066Sahrens 
371fa9e4066Sahrens 	if (pvd == NULL)
372fa9e4066Sahrens 		return;
373fa9e4066Sahrens 
374fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
375fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
376fa9e4066Sahrens 
377fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
378fa9e4066Sahrens 	cvd->vdev_parent = NULL;
379fa9e4066Sahrens 
380fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
381fa9e4066Sahrens 		if (pvd->vdev_child[c])
382fa9e4066Sahrens 			break;
383fa9e4066Sahrens 
384fa9e4066Sahrens 	if (c == pvd->vdev_children) {
385fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
386fa9e4066Sahrens 		pvd->vdev_child = NULL;
387fa9e4066Sahrens 		pvd->vdev_children = 0;
388fa9e4066Sahrens 	}
389fa9e4066Sahrens 
390*e0f1c0afSOlaf Faaland 	if (cvd->vdev_ops->vdev_op_leaf) {
391*e0f1c0afSOlaf Faaland 		spa_t *spa = cvd->vdev_spa;
392*e0f1c0afSOlaf Faaland 		list_remove(&spa->spa_leaf_list, cvd);
393*e0f1c0afSOlaf Faaland 		spa->spa_leaf_list_gen++;
394*e0f1c0afSOlaf Faaland 	}
395*e0f1c0afSOlaf Faaland 
396fa9e4066Sahrens 	/*
397fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
398fa9e4066Sahrens 	 */
399fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
400fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
401fa9e4066Sahrens }
402fa9e4066Sahrens 
403fa9e4066Sahrens /*
404fa9e4066Sahrens  * Remove any holes in the child array.
405fa9e4066Sahrens  */
406fa9e4066Sahrens void
407fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
408fa9e4066Sahrens {
409fa9e4066Sahrens 	vdev_t **newchild, *cvd;
410fa9e4066Sahrens 	int oldc = pvd->vdev_children;
411573ca77eSGeorge Wilson 	int newc;
412fa9e4066Sahrens 
413e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
414fa9e4066Sahrens 
415573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
416fa9e4066Sahrens 		if (pvd->vdev_child[c])
417fa9e4066Sahrens 			newc++;
418fa9e4066Sahrens 
419fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
420fa9e4066Sahrens 
421573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
422fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
423fa9e4066Sahrens 			newchild[newc] = cvd;
424fa9e4066Sahrens 			cvd->vdev_id = newc++;
425fa9e4066Sahrens 		}
426fa9e4066Sahrens 	}
427fa9e4066Sahrens 
428fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
429fa9e4066Sahrens 	pvd->vdev_child = newchild;
430fa9e4066Sahrens 	pvd->vdev_children = newc;
431fa9e4066Sahrens }
432fa9e4066Sahrens 
433fa9e4066Sahrens /*
434fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
435fa9e4066Sahrens  */
43688ecc943SGeorge Wilson vdev_t *
437fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
438fa9e4066Sahrens {
439fa9e4066Sahrens 	vdev_t *vd;
4405cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
441fa9e4066Sahrens 
442fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
4435cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
444fa9e4066Sahrens 
4450e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
4460e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
4470e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
448e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
4490e34b6a7Sbonwick 	}
4500e34b6a7Sbonwick 
45188ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
4520e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
4530e34b6a7Sbonwick 			/*
4540e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
4550e34b6a7Sbonwick 			 * which must be unique among all pools.
4560e34b6a7Sbonwick 			 */
4571195e687SMark J Musante 			guid = spa_generate_guid(NULL);
4580e34b6a7Sbonwick 		} else {
4590e34b6a7Sbonwick 			/*
4600e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
4610e34b6a7Sbonwick 			 */
4621195e687SMark J Musante 			guid = spa_generate_guid(spa);
4630e34b6a7Sbonwick 		}
4640e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
4650e34b6a7Sbonwick 	}
4660e34b6a7Sbonwick 
467fa9e4066Sahrens 	vd->vdev_spa = spa;
468fa9e4066Sahrens 	vd->vdev_id = id;
469fa9e4066Sahrens 	vd->vdev_guid = guid;
470fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
471fa9e4066Sahrens 	vd->vdev_ops = ops;
472fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
47388ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
4745cabbc6bSPrashanth Sreenivasa 	vic->vic_prev_indirect_vdev = UINT64_MAX;
4755cabbc6bSPrashanth Sreenivasa 
4765cabbc6bSPrashanth Sreenivasa 	rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
4775cabbc6bSPrashanth Sreenivasa 	mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
4785cabbc6bSPrashanth Sreenivasa 	vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
479fa9e4066Sahrens 
480*e0f1c0afSOlaf Faaland 	list_link_init(&vd->vdev_leaf_node);
481fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
4825ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
483e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
4840f7643c7SGeorge Wilson 	mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
485094e47e9SGeorge Wilson 	mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
486094e47e9SGeorge Wilson 	mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
487094e47e9SGeorge Wilson 	cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
488094e47e9SGeorge Wilson 	cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
489094e47e9SGeorge Wilson 
4908ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
4915cabbc6bSPrashanth Sreenivasa 		vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
4928ad4d6ddSJeff Bonwick 	}
493b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_ms_list, spa,
494fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
495b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_dtl_list, spa,
496fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
497fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
4983d7072f8Seschrock 	vdev_queue_init(vd);
4993d7072f8Seschrock 	vdev_cache_init(vd);
500fa9e4066Sahrens 
501fa9e4066Sahrens 	return (vd);
502fa9e4066Sahrens }
503fa9e4066Sahrens 
504fa9e4066Sahrens /*
505fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
506fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
507fa9e4066Sahrens  * different for each case.
508fa9e4066Sahrens  */
50999653d4eSeschrock int
51099653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
51199653d4eSeschrock     int alloctype)
512fa9e4066Sahrens {
513fa9e4066Sahrens 	vdev_ops_t *ops;
514fa9e4066Sahrens 	char *type;
5158654d025Sperrin 	uint64_t guid = 0, islog, nparity;
516fa9e4066Sahrens 	vdev_t *vd;
5175cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
518fa9e4066Sahrens 
519e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
520fa9e4066Sahrens 
521fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
522be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
523fa9e4066Sahrens 
524fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
525be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
526fa9e4066Sahrens 
527fa9e4066Sahrens 	/*
528fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
529fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
530fa9e4066Sahrens 	 */
531fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
532fa9e4066Sahrens 		uint64_t label_id;
533fa9e4066Sahrens 
534fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
535fa9e4066Sahrens 		    label_id != id)
536be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
537fa9e4066Sahrens 
538fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
539be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
54099653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
54199653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
542be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
543fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
544fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
545be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
54621ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
54721ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
548be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
549fa9e4066Sahrens 	}
550fa9e4066Sahrens 
55199653d4eSeschrock 	/*
55299653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
55399653d4eSeschrock 	 */
55499653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
555be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
55699653d4eSeschrock 
5578654d025Sperrin 	/*
5588654d025Sperrin 	 * Determine whether we're a log vdev.
5598654d025Sperrin 	 */
5608654d025Sperrin 	islog = 0;
5618654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
562990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
563be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
564fa9e4066Sahrens 
56588ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
566be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
56788ecc943SGeorge Wilson 
56899653d4eSeschrock 	/*
5698654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
57099653d4eSeschrock 	 */
5718654d025Sperrin 	nparity = -1ULL;
57299653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
57399653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
5748654d025Sperrin 		    &nparity) == 0) {
575b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
576be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
57799653d4eSeschrock 			/*
578f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
579f94275ceSAdam Leventhal 			 * device.
58099653d4eSeschrock 			 */
581f94275ceSAdam Leventhal 			if (nparity > 1 &&
582f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
583be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
584f94275ceSAdam Leventhal 			if (nparity > 2 &&
585f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
586be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
58799653d4eSeschrock 		} else {
58899653d4eSeschrock 			/*
58999653d4eSeschrock 			 * We require the parity to be specified for SPAs that
59099653d4eSeschrock 			 * support multiple parity levels.
59199653d4eSeschrock 			 */
592f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
593be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
59499653d4eSeschrock 			/*
59599653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
59699653d4eSeschrock 			 */
5978654d025Sperrin 			nparity = 1;
59899653d4eSeschrock 		}
59999653d4eSeschrock 	} else {
6008654d025Sperrin 		nparity = 0;
60199653d4eSeschrock 	}
6028654d025Sperrin 	ASSERT(nparity != -1ULL);
6038654d025Sperrin 
6048654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
6055cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
6068654d025Sperrin 
6078654d025Sperrin 	vd->vdev_islog = islog;
6088654d025Sperrin 	vd->vdev_nparity = nparity;
6098654d025Sperrin 
6108654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
6118654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
6128654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
6138654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
6148654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
6158654d025Sperrin 	    &vd->vdev_physpath) == 0)
6168654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
6176809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
6186809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
61999653d4eSeschrock 
620afefbcddSeschrock 	/*
621afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
622afefbcddSeschrock 	 * as -1.
623afefbcddSeschrock 	 */
624afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
625afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
626afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
627afefbcddSeschrock 
6285cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_mapping_object);
6295cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
6305cabbc6bSPrashanth Sreenivasa 	    &vic->vic_mapping_object);
6315cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_births_object);
6325cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
6335cabbc6bSPrashanth Sreenivasa 	    &vic->vic_births_object);
6345cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
6355cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
6365cabbc6bSPrashanth Sreenivasa 	    &vic->vic_prev_indirect_vdev);
6375cabbc6bSPrashanth Sreenivasa 
638ea8dc4b6Seschrock 	/*
639ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
640ea8dc4b6Seschrock 	 * was not present at the time of import.
641ea8dc4b6Seschrock 	 */
6426809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
6436809eb4eSEric Schrock 	    &vd->vdev_not_present);
644ea8dc4b6Seschrock 
645ecc2d604Sbonwick 	/*
646ecc2d604Sbonwick 	 * Get the alignment requirement.
647ecc2d604Sbonwick 	 */
648ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
649ecc2d604Sbonwick 
65088ecc943SGeorge Wilson 	/*
65188ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
65288ecc943SGeorge Wilson 	 */
65388ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
65488ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
65588ecc943SGeorge Wilson 
656fa9e4066Sahrens 	/*
657fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
658fa9e4066Sahrens 	 */
6591195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
6601195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
661fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
662fa9e4066Sahrens 		    &vd->vdev_ms_array);
663fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
664fa9e4066Sahrens 		    &vd->vdev_ms_shift);
665fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
666fa9e4066Sahrens 		    &vd->vdev_asize);
6673f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
6683f9d6ad7SLin Ling 		    &vd->vdev_removing);
669215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
670215198a6SJoe Stein 		    &vd->vdev_top_zap);
671215198a6SJoe Stein 	} else {
672215198a6SJoe Stein 		ASSERT0(vd->vdev_top_zap);
673fa9e4066Sahrens 	}
674fa9e4066Sahrens 
675cd0837ccSGeorge Wilson 	if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
676a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
6779f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
6781195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
6799f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
680a1521560SJeff Bonwick 		vd->vdev_mg = metaslab_group_create(islog ?
681f78cdc34SPaul Dagnelie 		    spa_log_class(spa) : spa_normal_class(spa), vd,
682f78cdc34SPaul Dagnelie 		    spa->spa_alloc_count);
683a1521560SJeff Bonwick 	}
684a1521560SJeff Bonwick 
685215198a6SJoe Stein 	if (vd->vdev_ops->vdev_op_leaf &&
686215198a6SJoe Stein 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
687215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv,
688215198a6SJoe Stein 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
689215198a6SJoe Stein 	} else {
690215198a6SJoe Stein 		ASSERT0(vd->vdev_leaf_zap);
691215198a6SJoe Stein 	}
692215198a6SJoe Stein 
693fa9e4066Sahrens 	/*
6943d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
695fa9e4066Sahrens 	 */
696215198a6SJoe Stein 
697c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
69821ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
69921ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
700c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
701c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
7020713e232SGeorge Wilson 			    &vd->vdev_dtl_object);
703c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
704c5904d13Seschrock 			    &vd->vdev_unspare);
705c5904d13Seschrock 		}
70621ecdf64SLin Ling 
70721ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
70821ecdf64SLin Ling 			uint64_t spare = 0;
70921ecdf64SLin Ling 
71021ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
71121ecdf64SLin Ling 			    &spare) == 0 && spare)
71221ecdf64SLin Ling 				spa_spare_add(vd);
71321ecdf64SLin Ling 		}
71421ecdf64SLin Ling 
715ecc2d604Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
716ecc2d604Sbonwick 		    &vd->vdev_offline);
717c5904d13Seschrock 
718b4952e17SGeorge Wilson 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
719b4952e17SGeorge Wilson 		    &vd->vdev_resilver_txg);
720cb04b873SMark J Musante 
7213d7072f8Seschrock 		/*
7223d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
7233d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
724069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
725069f55e2SEric Schrock 		 * remain in the faulted state.
7263d7072f8Seschrock 		 */
727b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
7283d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
7293d7072f8Seschrock 			    &vd->vdev_faulted);
7303d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
7313d7072f8Seschrock 			    &vd->vdev_degraded);
7323d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
7333d7072f8Seschrock 			    &vd->vdev_removed);
734069f55e2SEric Schrock 
735069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
736069f55e2SEric Schrock 				char *aux;
737069f55e2SEric Schrock 
738069f55e2SEric Schrock 				vd->vdev_label_aux =
739069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
740069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
741069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
742069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
743069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
744069f55e2SEric Schrock 			}
7453d7072f8Seschrock 		}
746fa9e4066Sahrens 	}
747fa9e4066Sahrens 
748fa9e4066Sahrens 	/*
749fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
750fa9e4066Sahrens 	 */
751fa9e4066Sahrens 	vdev_add_child(parent, vd);
752fa9e4066Sahrens 
75399653d4eSeschrock 	*vdp = vd;
75499653d4eSeschrock 
75599653d4eSeschrock 	return (0);
756fa9e4066Sahrens }
757fa9e4066Sahrens 
758fa9e4066Sahrens void
759fa9e4066Sahrens vdev_free(vdev_t *vd)
760fa9e4066Sahrens {
7613d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
762094e47e9SGeorge Wilson 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
763fa9e4066Sahrens 
764fa9e4066Sahrens 	/*
765fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
766fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
767fa9e4066Sahrens 	 */
768fa9e4066Sahrens 	vdev_close(vd);
769fa9e4066Sahrens 
770e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
771b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
772fa9e4066Sahrens 
773fa9e4066Sahrens 	/*
774fa9e4066Sahrens 	 * Free all children.
775fa9e4066Sahrens 	 */
776573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
777fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
778fa9e4066Sahrens 
779fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
780fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
781094e47e9SGeorge Wilson 	ASSERT(vd->vdev_initialize_thread == NULL);
782fa9e4066Sahrens 
783fa9e4066Sahrens 	/*
784fa9e4066Sahrens 	 * Discard allocation state.
785fa9e4066Sahrens 	 */
786a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
787fa9e4066Sahrens 		vdev_metaslab_fini(vd);
788a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
789a1521560SJeff Bonwick 	}
790fa9e4066Sahrens 
791fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_space);
792fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_dspace);
793fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
794fa9e4066Sahrens 
795fa9e4066Sahrens 	/*
796fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
797fa9e4066Sahrens 	 */
798fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
799fa9e4066Sahrens 
800fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
801*e0f1c0afSOlaf Faaland 	ASSERT(!list_link_active(&vd->vdev_leaf_node));
802fa9e4066Sahrens 
8033d7072f8Seschrock 	/*
8043d7072f8Seschrock 	 * Clean up vdev structure.
8053d7072f8Seschrock 	 */
8063d7072f8Seschrock 	vdev_queue_fini(vd);
8073d7072f8Seschrock 	vdev_cache_fini(vd);
8083d7072f8Seschrock 
8093d7072f8Seschrock 	if (vd->vdev_path)
8103d7072f8Seschrock 		spa_strfree(vd->vdev_path);
8113d7072f8Seschrock 	if (vd->vdev_devid)
8123d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
8133d7072f8Seschrock 	if (vd->vdev_physpath)
8143d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
8156809eb4eSEric Schrock 	if (vd->vdev_fru)
8166809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
8173d7072f8Seschrock 
8183d7072f8Seschrock 	if (vd->vdev_isspare)
8193d7072f8Seschrock 		spa_spare_remove(vd);
820fa94a07fSbrendan 	if (vd->vdev_isl2cache)
821fa94a07fSbrendan 		spa_l2cache_remove(vd);
8223d7072f8Seschrock 
8233d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
8243d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
8258ad4d6ddSJeff Bonwick 
8263d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
8270713e232SGeorge Wilson 	space_map_close(vd->vdev_dtl_sm);
8288ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
8290713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
8300713e232SGeorge Wilson 		range_tree_destroy(vd->vdev_dtl[t]);
8318ad4d6ddSJeff Bonwick 	}
8323d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
8338ad4d6ddSJeff Bonwick 
8345cabbc6bSPrashanth Sreenivasa 	EQUIV(vd->vdev_indirect_births != NULL,
8355cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping != NULL);
8365cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_indirect_births != NULL) {
8375cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
8385cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_close(vd->vdev_indirect_births);
8395cabbc6bSPrashanth Sreenivasa 	}
8405cabbc6bSPrashanth Sreenivasa 
8415cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
8425cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
8435cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
8445cabbc6bSPrashanth Sreenivasa 		space_map_close(vd->vdev_obsolete_sm);
8455cabbc6bSPrashanth Sreenivasa 		vd->vdev_obsolete_sm = NULL;
8465cabbc6bSPrashanth Sreenivasa 	}
8475cabbc6bSPrashanth Sreenivasa 	range_tree_destroy(vd->vdev_obsolete_segments);
8485cabbc6bSPrashanth Sreenivasa 	rw_destroy(&vd->vdev_indirect_rwlock);
8495cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&vd->vdev_obsolete_lock);
8505cabbc6bSPrashanth Sreenivasa 
8510f7643c7SGeorge Wilson 	mutex_destroy(&vd->vdev_queue_lock);
8523d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
8533d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
854e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
855094e47e9SGeorge Wilson 	mutex_destroy(&vd->vdev_initialize_lock);
856094e47e9SGeorge Wilson 	mutex_destroy(&vd->vdev_initialize_io_lock);
857094e47e9SGeorge Wilson 	cv_destroy(&vd->vdev_initialize_io_cv);
858094e47e9SGeorge Wilson 	cv_destroy(&vd->vdev_initialize_cv);
8593d7072f8Seschrock 
8603d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
8613d7072f8Seschrock 		spa->spa_root_vdev = NULL;
8623d7072f8Seschrock 
8633d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
864fa9e4066Sahrens }
865fa9e4066Sahrens 
866fa9e4066Sahrens /*
867fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
868fa9e4066Sahrens  */
869fa9e4066Sahrens static void
870fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
871fa9e4066Sahrens {
872fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
873fa9e4066Sahrens 	metaslab_t *msp;
874fa9e4066Sahrens 	vdev_t *vd;
875fa9e4066Sahrens 	int t;
876fa9e4066Sahrens 
877fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
878fa9e4066Sahrens 
879fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
880fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
881fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
882215198a6SJoe Stein 	tvd->vdev_top_zap = svd->vdev_top_zap;
883fa9e4066Sahrens 
884fa9e4066Sahrens 	svd->vdev_ms_array = 0;
885fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
886fa9e4066Sahrens 	svd->vdev_ms_count = 0;
887215198a6SJoe Stein 	svd->vdev_top_zap = 0;
888fa9e4066Sahrens 
889cd0837ccSGeorge Wilson 	if (tvd->vdev_mg)
890cd0837ccSGeorge Wilson 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
891fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
892fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
893fa9e4066Sahrens 
894fa9e4066Sahrens 	svd->vdev_mg = NULL;
895fa9e4066Sahrens 	svd->vdev_ms = NULL;
896ecc2d604Sbonwick 
897ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
898ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
899fa9e4066Sahrens 
90086714001SSerapheim Dimitropoulos 	tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
90186714001SSerapheim Dimitropoulos 	svd->vdev_checkpoint_sm = NULL;
90286714001SSerapheim Dimitropoulos 
903fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
904fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
90599653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
906fa9e4066Sahrens 
907fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
908fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
90999653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
910fa9e4066Sahrens 
9113a4b1be9SMatthew Ahrens 	/*
9123a4b1be9SMatthew Ahrens 	 * State which may be set on a top-level vdev that's in the
9133a4b1be9SMatthew Ahrens 	 * process of being removed.
9143a4b1be9SMatthew Ahrens 	 */
9153a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_indirect_config.vic_births_object);
9163a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
9173a4b1be9SMatthew Ahrens 	ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
9183a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
9193a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
9203a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
9213a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_removing);
9223a4b1be9SMatthew Ahrens 	tvd->vdev_removing = svd->vdev_removing;
9233a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_config = svd->vdev_indirect_config;
9243a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
9253a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_births = svd->vdev_indirect_births;
9263a4b1be9SMatthew Ahrens 	range_tree_swap(&svd->vdev_obsolete_segments,
9273a4b1be9SMatthew Ahrens 	    &tvd->vdev_obsolete_segments);
9283a4b1be9SMatthew Ahrens 	tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
9293a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_mapping_object = 0;
9303a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_births_object = 0;
9313a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
9323a4b1be9SMatthew Ahrens 	svd->vdev_indirect_mapping = NULL;
9333a4b1be9SMatthew Ahrens 	svd->vdev_indirect_births = NULL;
9343a4b1be9SMatthew Ahrens 	svd->vdev_obsolete_sm = NULL;
9353a4b1be9SMatthew Ahrens 	svd->vdev_removing = 0;
9363a4b1be9SMatthew Ahrens 
937fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
938fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
939fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
940fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
941fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
942fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
943fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
944fa9e4066Sahrens 	}
945fa9e4066Sahrens 
946e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
947fa9e4066Sahrens 		vdev_config_clean(svd);
948fa9e4066Sahrens 		vdev_config_dirty(tvd);
949fa9e4066Sahrens 	}
950fa9e4066Sahrens 
951e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
952e14bb325SJeff Bonwick 		vdev_state_clean(svd);
953e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
954e14bb325SJeff Bonwick 	}
955e14bb325SJeff Bonwick 
95699653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
95799653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
9588654d025Sperrin 
9598654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
9608654d025Sperrin 	svd->vdev_islog = 0;
961fa9e4066Sahrens }
962fa9e4066Sahrens 
963fa9e4066Sahrens static void
964fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
965fa9e4066Sahrens {
966fa9e4066Sahrens 	if (vd == NULL)
967fa9e4066Sahrens 		return;
968fa9e4066Sahrens 
969fa9e4066Sahrens 	vd->vdev_top = tvd;
970fa9e4066Sahrens 
971573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
972fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
973fa9e4066Sahrens }
974fa9e4066Sahrens 
975fa9e4066Sahrens /*
976fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
977fa9e4066Sahrens  */
978fa9e4066Sahrens vdev_t *
979fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
980fa9e4066Sahrens {
981fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
982fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
983fa9e4066Sahrens 	vdev_t *mvd;
984fa9e4066Sahrens 
985e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
986fa9e4066Sahrens 
987fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
988ecc2d604Sbonwick 
989ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
990573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
9914263d13fSGeorge Wilson 	mvd->vdev_max_asize = cvd->vdev_max_asize;
9925cabbc6bSPrashanth Sreenivasa 	mvd->vdev_psize = cvd->vdev_psize;
993ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
994ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
99588ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
996ecc2d604Sbonwick 
997fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
998fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
999fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
1000fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
1001fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1002fa9e4066Sahrens 
1003fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
1004fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
1005fa9e4066Sahrens 
1006fa9e4066Sahrens 	return (mvd);
1007fa9e4066Sahrens }
1008fa9e4066Sahrens 
1009fa9e4066Sahrens /*
1010fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
1011fa9e4066Sahrens  */
1012fa9e4066Sahrens void
1013fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
1014fa9e4066Sahrens {
1015fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
1016fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
1017fa9e4066Sahrens 
1018e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1019fa9e4066Sahrens 
1020fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
1021fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
102299653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
102399653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
1024ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
1025fa9e4066Sahrens 
1026fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
1027fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
10288ad4d6ddSJeff Bonwick 
102999653d4eSeschrock 	/*
1030e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1031e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
1032e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
1033e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
103499653d4eSeschrock 	 */
10358ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
10368ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
10371195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
10388ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
10398ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
10408ad4d6ddSJeff Bonwick 	}
1041e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
1042e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
1043fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1044fa9e4066Sahrens 
1045fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
1046fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
1047fa9e4066Sahrens 
1048fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
1049fa9e4066Sahrens 	vdev_free(mvd);
1050fa9e4066Sahrens }
1051fa9e4066Sahrens 
1052ea8dc4b6Seschrock int
1053fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1054fa9e4066Sahrens {
1055fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
1056ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1057ecc2d604Sbonwick 	uint64_t m;
1058fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
1059fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1060ecc2d604Sbonwick 	metaslab_t **mspp;
1061ecc2d604Sbonwick 	int error;
1062fa9e4066Sahrens 
1063a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1064a1521560SJeff Bonwick 
106588ecc943SGeorge Wilson 	/*
106688ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
106788ecc943SGeorge Wilson 	 */
106888ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
10690e34b6a7Sbonwick 		return (0);
10700e34b6a7Sbonwick 
107188ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
107288ecc943SGeorge Wilson 
1073fa9e4066Sahrens 	ASSERT(oldc <= newc);
1074fa9e4066Sahrens 
1075ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1076fa9e4066Sahrens 
1077ecc2d604Sbonwick 	if (oldc != 0) {
1078ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1079ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1080ecc2d604Sbonwick 	}
1081fa9e4066Sahrens 
1082ecc2d604Sbonwick 	vd->vdev_ms = mspp;
1083ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
1084ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
10850713e232SGeorge Wilson 		uint64_t object = 0;
10860713e232SGeorge Wilson 
10875cabbc6bSPrashanth Sreenivasa 		/*
10885cabbc6bSPrashanth Sreenivasa 		 * vdev_ms_array may be 0 if we are creating the "fake"
10895cabbc6bSPrashanth Sreenivasa 		 * metaslabs for an indirect vdev for zdb's leak detection.
10905cabbc6bSPrashanth Sreenivasa 		 * See zdb_leak_init().
10915cabbc6bSPrashanth Sreenivasa 		 */
10925cabbc6bSPrashanth Sreenivasa 		if (txg == 0 && vd->vdev_ms_array != 0) {
1093ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
10947bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
10957bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
10963ee8c80cSPavel Zakharov 			if (error != 0) {
10973ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "unable to read the metaslab "
10983ee8c80cSPavel Zakharov 				    "array [error=%d]", error);
1099ecc2d604Sbonwick 				return (error);
11003ee8c80cSPavel Zakharov 			}
1101fa9e4066Sahrens 		}
11021e9bd7ecSPrakash Surya 
11031e9bd7ecSPrakash Surya 		error = metaslab_init(vd->vdev_mg, m, object, txg,
11041e9bd7ecSPrakash Surya 		    &(vd->vdev_ms[m]));
11053ee8c80cSPavel Zakharov 		if (error != 0) {
11063ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
11073ee8c80cSPavel Zakharov 			    error);
11081e9bd7ecSPrakash Surya 			return (error);
11093ee8c80cSPavel Zakharov 		}
1110fa9e4066Sahrens 	}
1111fa9e4066Sahrens 
1112a1521560SJeff Bonwick 	if (txg == 0)
1113a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1114a1521560SJeff Bonwick 
11153f9d6ad7SLin Ling 	/*
11163f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
11173f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
11183f9d6ad7SLin Ling 	 * allocations are performed on this device.
11193f9d6ad7SLin Ling 	 */
11203f9d6ad7SLin Ling 	if (oldc == 0 && !vd->vdev_removing)
1121a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
1122a1521560SJeff Bonwick 
1123a1521560SJeff Bonwick 	if (txg == 0)
1124a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
1125a1521560SJeff Bonwick 
1126ea8dc4b6Seschrock 	return (0);
1127fa9e4066Sahrens }
1128fa9e4066Sahrens 
1129fa9e4066Sahrens void
1130fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
1131fa9e4066Sahrens {
113286714001SSerapheim Dimitropoulos 	if (vd->vdev_checkpoint_sm != NULL) {
113386714001SSerapheim Dimitropoulos 		ASSERT(spa_feature_is_active(vd->vdev_spa,
113486714001SSerapheim Dimitropoulos 		    SPA_FEATURE_POOL_CHECKPOINT));
113586714001SSerapheim Dimitropoulos 		space_map_close(vd->vdev_checkpoint_sm);
113686714001SSerapheim Dimitropoulos 		/*
113786714001SSerapheim Dimitropoulos 		 * Even though we close the space map, we need to set its
113886714001SSerapheim Dimitropoulos 		 * pointer to NULL. The reason is that vdev_metaslab_fini()
113986714001SSerapheim Dimitropoulos 		 * may be called multiple times for certain operations
114086714001SSerapheim Dimitropoulos 		 * (i.e. when destroying a pool) so we need to ensure that
114186714001SSerapheim Dimitropoulos 		 * this clause never executes twice. This logic is similar
114286714001SSerapheim Dimitropoulos 		 * to the one used for the vdev_ms clause below.
114386714001SSerapheim Dimitropoulos 		 */
114486714001SSerapheim Dimitropoulos 		vd->vdev_checkpoint_sm = NULL;
114586714001SSerapheim Dimitropoulos 	}
114686714001SSerapheim Dimitropoulos 
1147fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
11485cabbc6bSPrashanth Sreenivasa 		uint64_t count = vd->vdev_ms_count;
11495cabbc6bSPrashanth Sreenivasa 
1150a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
11515cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < count; m++) {
11520713e232SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
11530713e232SGeorge Wilson 
11540713e232SGeorge Wilson 			if (msp != NULL)
11550713e232SGeorge Wilson 				metaslab_fini(msp);
11560713e232SGeorge Wilson 		}
1157fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1158fa9e4066Sahrens 		vd->vdev_ms = NULL;
11595cabbc6bSPrashanth Sreenivasa 
11605cabbc6bSPrashanth Sreenivasa 		vd->vdev_ms_count = 0;
1161fa9e4066Sahrens 	}
11625cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_ms_count);
1163fa9e4066Sahrens }
1164fa9e4066Sahrens 
1165e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
1166e14bb325SJeff Bonwick 	boolean_t	vps_readable;
1167e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
1168e14bb325SJeff Bonwick 	int		vps_flags;
1169e14bb325SJeff Bonwick } vdev_probe_stats_t;
1170e14bb325SJeff Bonwick 
1171e14bb325SJeff Bonwick static void
1172e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
11730a4e9518Sgw {
11748ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
1175a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
1176e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
1177a3f829aeSBill Moore 
1178a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
1179e14bb325SJeff Bonwick 
1180e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
1181e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1182e14bb325SJeff Bonwick 			vps->vps_readable = 1;
11838ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
1184a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1185770499e1SDan Kimmel 			    zio->io_offset, zio->io_size, zio->io_abd,
1186e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1187e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1188e14bb325SJeff Bonwick 		} else {
1189770499e1SDan Kimmel 			abd_free(zio->io_abd);
1190e14bb325SJeff Bonwick 		}
1191e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
1192e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1193e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
1194770499e1SDan Kimmel 		abd_free(zio->io_abd);
1195e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
1196a3f829aeSBill Moore 		zio_t *pio;
1197e14bb325SJeff Bonwick 
1198e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
1199e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
1200e14bb325SJeff Bonwick 
1201e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
12028ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
1203e14bb325SJeff Bonwick 			zio->io_error = 0;
1204e14bb325SJeff Bonwick 		} else {
1205e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
12063ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "failed probe");
1207e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
12088ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
1209be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
1210e14bb325SJeff Bonwick 		}
1211a3f829aeSBill Moore 
1212a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
1213a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
1214a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
1215a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
1216a3f829aeSBill Moore 
12170f7643c7SGeorge Wilson 		zio_link_t *zl = NULL;
12180f7643c7SGeorge Wilson 		while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1219a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
1220be6fd75aSMatthew Ahrens 				pio->io_error = SET_ERROR(ENXIO);
1221a3f829aeSBill Moore 
1222e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
1223e14bb325SJeff Bonwick 	}
1224e14bb325SJeff Bonwick }
12250a4e9518Sgw 
1226e14bb325SJeff Bonwick /*
1227f7170741SWill Andrews  * Determine whether this device is accessible.
1228f7170741SWill Andrews  *
1229f7170741SWill Andrews  * Read and write to several known locations: the pad regions of each
1230f7170741SWill Andrews  * vdev label but the first, which we leave alone in case it contains
1231f7170741SWill Andrews  * a VTOC.
1232e14bb325SJeff Bonwick  */
1233e14bb325SJeff Bonwick zio_t *
1234a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
1235e14bb325SJeff Bonwick {
1236e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1237a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
1238a3f829aeSBill Moore 	zio_t *pio;
1239a3f829aeSBill Moore 
1240a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
12410a4e9518Sgw 
1242a3f829aeSBill Moore 	/*
1243a3f829aeSBill Moore 	 * Don't probe the probe.
1244a3f829aeSBill Moore 	 */
1245a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1246a3f829aeSBill Moore 		return (NULL);
1247e14bb325SJeff Bonwick 
1248a3f829aeSBill Moore 	/*
1249a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
1250a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
1251a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
1252a3f829aeSBill Moore 	 */
1253a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
1254e14bb325SJeff Bonwick 
1255a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
1256a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1257a3f829aeSBill Moore 
1258a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1259a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
12608956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
1261a3f829aeSBill Moore 
1262a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1263a3f829aeSBill Moore 			/*
1264a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
1265a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
1266a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
1267a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
1268a3f829aeSBill Moore 			 * any zio looking at these values can assume that
1269a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
1270a3f829aeSBill Moore 			 * important because when a device has intermittent
1271a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
1272a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1273a3f829aeSBill Moore 			 * the zio (EIO).
1274a3f829aeSBill Moore 			 *
1275a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1276a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1277a3f829aeSBill Moore 			 * principles.
1278a3f829aeSBill Moore 			 */
1279a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1280a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1281a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1282a3f829aeSBill Moore 		}
1283a3f829aeSBill Moore 
1284a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1285a3f829aeSBill Moore 		    vdev_probe_done, vps,
1286a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1287a3f829aeSBill Moore 
128898d1cbfeSGeorge Wilson 		/*
128998d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
129098d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
129198d1cbfeSGeorge Wilson 		 */
1292a3f829aeSBill Moore 		if (zio != NULL) {
1293a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1294a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1295a3f829aeSBill Moore 		}
1296e14bb325SJeff Bonwick 	}
1297e14bb325SJeff Bonwick 
1298a3f829aeSBill Moore 	if (zio != NULL)
1299a3f829aeSBill Moore 		zio_add_child(zio, pio);
1300e14bb325SJeff Bonwick 
1301a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1302e14bb325SJeff Bonwick 
1303a3f829aeSBill Moore 	if (vps == NULL) {
1304a3f829aeSBill Moore 		ASSERT(zio != NULL);
1305a3f829aeSBill Moore 		return (NULL);
1306a3f829aeSBill Moore 	}
1307e14bb325SJeff Bonwick 
1308e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1309a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1310e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1311770499e1SDan Kimmel 		    offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1312770499e1SDan Kimmel 		    abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1313e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1314e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1315e14bb325SJeff Bonwick 	}
1316e14bb325SJeff Bonwick 
1317a3f829aeSBill Moore 	if (zio == NULL)
1318a3f829aeSBill Moore 		return (pio);
1319a3f829aeSBill Moore 
1320a3f829aeSBill Moore 	zio_nowait(pio);
1321a3f829aeSBill Moore 	return (NULL);
13220a4e9518Sgw }
13230a4e9518Sgw 
1324f64c0e34SEric Taylor static void
1325f64c0e34SEric Taylor vdev_open_child(void *arg)
1326f64c0e34SEric Taylor {
1327f64c0e34SEric Taylor 	vdev_t *vd = arg;
1328f64c0e34SEric Taylor 
1329f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1330f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1331f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1332f64c0e34SEric Taylor }
1333f64c0e34SEric Taylor 
1334681d9761SEric Taylor boolean_t
1335681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1336681d9761SEric Taylor {
1337681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1338681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1339681d9761SEric Taylor 		return (B_TRUE);
1340681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1341681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1342681d9761SEric Taylor 			return (B_TRUE);
1343681d9761SEric Taylor 	return (B_FALSE);
1344681d9761SEric Taylor }
1345681d9761SEric Taylor 
1346f64c0e34SEric Taylor void
1347f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1348f64c0e34SEric Taylor {
1349f64c0e34SEric Taylor 	taskq_t *tq;
1350f64c0e34SEric Taylor 	int children = vd->vdev_children;
1351f64c0e34SEric Taylor 
1352681d9761SEric Taylor 	/*
1353681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1354681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1355681d9761SEric Taylor 	 * spa_namespace_lock
1356681d9761SEric Taylor 	 */
1357681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1358681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1359681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1360681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1361681d9761SEric Taylor 		return;
1362681d9761SEric Taylor 	}
1363f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1364f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1365f64c0e34SEric Taylor 
1366f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1367f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1368fc8ae2ecSToomas Soome 		    TQ_SLEEP) != TASKQID_INVALID);
1369f64c0e34SEric Taylor 
1370f64c0e34SEric Taylor 	taskq_destroy(tq);
1371f64c0e34SEric Taylor }
1372f64c0e34SEric Taylor 
13735cabbc6bSPrashanth Sreenivasa /*
13745cabbc6bSPrashanth Sreenivasa  * Compute the raidz-deflation ratio.  Note, we hard-code
13755cabbc6bSPrashanth Sreenivasa  * in 128k (1 << 17) because it is the "typical" blocksize.
13765cabbc6bSPrashanth Sreenivasa  * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
13775cabbc6bSPrashanth Sreenivasa  * otherwise it would inconsistently account for existing bp's.
13785cabbc6bSPrashanth Sreenivasa  */
13795cabbc6bSPrashanth Sreenivasa static void
13805cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd)
13815cabbc6bSPrashanth Sreenivasa {
13825cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
13835cabbc6bSPrashanth Sreenivasa 		vd->vdev_deflate_ratio = (1 << 17) /
13845cabbc6bSPrashanth Sreenivasa 		    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
13855cabbc6bSPrashanth Sreenivasa 	}
13865cabbc6bSPrashanth Sreenivasa }
13875cabbc6bSPrashanth Sreenivasa 
1388fa9e4066Sahrens /*
1389fa9e4066Sahrens  * Prepare a virtual device for access.
1390fa9e4066Sahrens  */
1391fa9e4066Sahrens int
1392fa9e4066Sahrens vdev_open(vdev_t *vd)
1393fa9e4066Sahrens {
13948ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1395fa9e4066Sahrens 	int error;
1396fa9e4066Sahrens 	uint64_t osize = 0;
13974263d13fSGeorge Wilson 	uint64_t max_osize = 0;
13984263d13fSGeorge Wilson 	uint64_t asize, max_asize, psize;
1399ecc2d604Sbonwick 	uint64_t ashift = 0;
1400fa9e4066Sahrens 
1401f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1402f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1403fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1404fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1405fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1406fa9e4066Sahrens 
1407fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1408e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1409e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1410573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1411fa9e4066Sahrens 
1412069f55e2SEric Schrock 	/*
1413069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1414069f55e2SEric Schrock 	 * faulted, bail out of the open.
1415069f55e2SEric Schrock 	 */
14163d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
14173d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1418069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1419069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
14203d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1421069f55e2SEric Schrock 		    vd->vdev_label_aux);
1422be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
14233d7072f8Seschrock 	} else if (vd->vdev_offline) {
1424fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1425ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1426be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1427fa9e4066Sahrens 	}
1428fa9e4066Sahrens 
14294263d13fSGeorge Wilson 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1430fa9e4066Sahrens 
1431095bcd66SGeorge Wilson 	/*
1432095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1433095bcd66SGeorge Wilson 	 * the vdev on error.
1434095bcd66SGeorge Wilson 	 */
1435095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1436ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
14378956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1438ea8dc4b6Seschrock 
1439fa9e4066Sahrens 	if (error) {
14403d7072f8Seschrock 		if (vd->vdev_removed &&
14413d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
14423d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
14433d7072f8Seschrock 
14446f793812SPavel Zakharov 		if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
14456f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
14466f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
14476f793812SPavel Zakharov 		} else {
14486f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
14496f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
14506f793812SPavel Zakharov 		}
1451fa9e4066Sahrens 		return (error);
1452fa9e4066Sahrens 	}
1453fa9e4066Sahrens 
14543d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
14553d7072f8Seschrock 
1456096d22d4SEric Schrock 	/*
1457096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1458096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1459096d22d4SEric Schrock 	 */
1460096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1461096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1462096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1463096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1464096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1465096d22d4SEric Schrock 		    vd->vdev_label_aux);
1466be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1467096d22d4SEric Schrock 	}
1468096d22d4SEric Schrock 
14693d7072f8Seschrock 	if (vd->vdev_degraded) {
14703d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
14713d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
14723d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
14733d7072f8Seschrock 	} else {
1474069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
14753d7072f8Seschrock 	}
1476fa9e4066Sahrens 
147788ecc943SGeorge Wilson 	/*
147888ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
147988ecc943SGeorge Wilson 	 */
148088ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
148188ecc943SGeorge Wilson 		return (0);
148288ecc943SGeorge Wilson 
1483573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1484ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1485ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1486ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1487ea8dc4b6Seschrock 			break;
1488ea8dc4b6Seschrock 		}
1489573ca77eSGeorge Wilson 	}
1490fa9e4066Sahrens 
1491fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
14924263d13fSGeorge Wilson 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1493fa9e4066Sahrens 
1494fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1495fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1496ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1497ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1498be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1499fa9e4066Sahrens 		}
1500fa9e4066Sahrens 		psize = osize;
1501fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
15024263d13fSGeorge Wilson 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
15034263d13fSGeorge Wilson 		    VDEV_LABEL_END_SIZE);
1504fa9e4066Sahrens 	} else {
1505ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1506fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1507ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1508ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1509be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1510fa9e4066Sahrens 		}
1511fa9e4066Sahrens 		psize = 0;
1512fa9e4066Sahrens 		asize = osize;
15134263d13fSGeorge Wilson 		max_asize = max_osize;
1514fa9e4066Sahrens 	}
1515fa9e4066Sahrens 
1516fa9e4066Sahrens 	vd->vdev_psize = psize;
1517fa9e4066Sahrens 
1518573ca77eSGeorge Wilson 	/*
1519c040c10cSSteven Hartland 	 * Make sure the allocatable size hasn't shrunk too much.
1520573ca77eSGeorge Wilson 	 */
1521573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1522573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1523573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1524be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1525573ca77eSGeorge Wilson 	}
1526573ca77eSGeorge Wilson 
1527fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1528fa9e4066Sahrens 		/*
1529fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1530ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1531fa9e4066Sahrens 		 */
1532fa9e4066Sahrens 		vd->vdev_asize = asize;
15334263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1534ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
153593a1902eSMatthew Ahrens 		vd->vdev_ashift = MAX(zfs_ashift_min, vd->vdev_ashift);
1536fa9e4066Sahrens 	} else {
1537fa9e4066Sahrens 		/*
15382384d9f8SGeorge Wilson 		 * Detect if the alignment requirement has increased.
15392384d9f8SGeorge Wilson 		 * We don't want to make the pool unavailable, just
15402384d9f8SGeorge Wilson 		 * issue a warning instead.
1541fa9e4066Sahrens 		 */
15422384d9f8SGeorge Wilson 		if (ashift > vd->vdev_top->vdev_ashift &&
15432384d9f8SGeorge Wilson 		    vd->vdev_ops->vdev_op_leaf) {
15442384d9f8SGeorge Wilson 			cmn_err(CE_WARN,
15452384d9f8SGeorge Wilson 			    "Disk, '%s', has a block alignment that is "
15462384d9f8SGeorge Wilson 			    "larger than the pool's alignment\n",
15472384d9f8SGeorge Wilson 			    vd->vdev_path);
1548fa9e4066Sahrens 		}
15494263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1550573ca77eSGeorge Wilson 	}
1551fa9e4066Sahrens 
1552573ca77eSGeorge Wilson 	/*
1553c040c10cSSteven Hartland 	 * If all children are healthy we update asize if either:
1554c040c10cSSteven Hartland 	 * The asize has increased, due to a device expansion caused by dynamic
1555c040c10cSSteven Hartland 	 * LUN growth or vdev replacement, and automatic expansion is enabled;
1556c040c10cSSteven Hartland 	 * making the additional space available.
1557c040c10cSSteven Hartland 	 *
1558c040c10cSSteven Hartland 	 * The asize has decreased, due to a device shrink usually caused by a
1559c040c10cSSteven Hartland 	 * vdev replace with a smaller device. This ensures that calculations
1560c040c10cSSteven Hartland 	 * based of max_asize and asize e.g. esize are always valid. It's safe
1561c040c10cSSteven Hartland 	 * to do this as we've already validated that asize is greater than
1562c040c10cSSteven Hartland 	 * vdev_min_asize.
1563573ca77eSGeorge Wilson 	 */
1564c040c10cSSteven Hartland 	if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1565c040c10cSSteven Hartland 	    ((asize > vd->vdev_asize &&
1566c040c10cSSteven Hartland 	    (vd->vdev_expanding || spa->spa_autoexpand)) ||
1567c040c10cSSteven Hartland 	    (asize < vd->vdev_asize)))
1568573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1569fa9e4066Sahrens 
1570573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1571fa9e4066Sahrens 
15720a4e9518Sgw 	/*
15730a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
15740a4e9518Sgw 	 * vdev open for business.
15750a4e9518Sgw 	 */
1576e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1577e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
157898d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
157998d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
15800a4e9518Sgw 		return (error);
15810a4e9518Sgw 	}
15820a4e9518Sgw 
158381cd5c55SMatthew Ahrens 	/*
158481cd5c55SMatthew Ahrens 	 * Track the min and max ashift values for normal data devices.
158581cd5c55SMatthew Ahrens 	 */
158681cd5c55SMatthew Ahrens 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
158781cd5c55SMatthew Ahrens 	    !vd->vdev_islog && vd->vdev_aux == NULL) {
158881cd5c55SMatthew Ahrens 		if (vd->vdev_ashift > spa->spa_max_ashift)
158981cd5c55SMatthew Ahrens 			spa->spa_max_ashift = vd->vdev_ashift;
159081cd5c55SMatthew Ahrens 		if (vd->vdev_ashift < spa->spa_min_ashift)
159181cd5c55SMatthew Ahrens 			spa->spa_min_ashift = vd->vdev_ashift;
159281cd5c55SMatthew Ahrens 	}
159381cd5c55SMatthew Ahrens 
1594088f3894Sahrens 	/*
1595088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
15968ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
15978ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1598088f3894Sahrens 	 */
15998ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
16008ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
16018ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1602088f3894Sahrens 
1603fa9e4066Sahrens 	return (0);
1604fa9e4066Sahrens }
1605fa9e4066Sahrens 
1606560e6e96Seschrock /*
1607560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
16086f793812SPavel Zakharov  * contents. This needs to be done before vdev_load() so that we don't
16093d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1610560e6e96Seschrock  *
1611560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1612560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1613560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1614560e6e96Seschrock  * will be updated but the function will return 0.
1615560e6e96Seschrock  */
1616560e6e96Seschrock int
16176f793812SPavel Zakharov vdev_validate(vdev_t *vd)
1618560e6e96Seschrock {
1619560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1620560e6e96Seschrock 	nvlist_t *label;
16216f793812SPavel Zakharov 	uint64_t guid = 0, aux_guid = 0, top_guid;
1622560e6e96Seschrock 	uint64_t state;
16236f793812SPavel Zakharov 	nvlist_t *nvl;
16246f793812SPavel Zakharov 	uint64_t txg;
1625560e6e96Seschrock 
16266f793812SPavel Zakharov 	if (vdev_validate_skip)
16276f793812SPavel Zakharov 		return (0);
16286f793812SPavel Zakharov 
16296f793812SPavel Zakharov 	for (uint64_t c = 0; c < vd->vdev_children; c++)
16306f793812SPavel Zakharov 		if (vdev_validate(vd->vdev_child[c]) != 0)
1631be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1632560e6e96Seschrock 
1633b5989ec7Seschrock 	/*
1634b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1635b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1636b5989ec7Seschrock 	 * overwrite the previous state.
1637b5989ec7Seschrock 	 */
16386f793812SPavel Zakharov 	if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
16396f793812SPavel Zakharov 		return (0);
1640560e6e96Seschrock 
16416f793812SPavel Zakharov 	/*
16426f793812SPavel Zakharov 	 * If we are performing an extreme rewind, we allow for a label that
16436f793812SPavel Zakharov 	 * was modified at a point after the current txg.
1644d1de72cfSPavel Zakharov 	 * If config lock is not held do not check for the txg. spa_sync could
1645d1de72cfSPavel Zakharov 	 * be updating the vdev's label before updating spa_last_synced_txg.
16466f793812SPavel Zakharov 	 */
1647d1de72cfSPavel Zakharov 	if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
1648d1de72cfSPavel Zakharov 	    spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
16496f793812SPavel Zakharov 		txg = UINT64_MAX;
16506f793812SPavel Zakharov 	else
16516f793812SPavel Zakharov 		txg = spa_last_synced_txg(spa);
1652560e6e96Seschrock 
16536f793812SPavel Zakharov 	if ((label = vdev_label_read_config(vd, txg)) == NULL) {
16546f793812SPavel Zakharov 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
16556f793812SPavel Zakharov 		    VDEV_AUX_BAD_LABEL);
1656b6bf6e15SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
1657b6bf6e15SPavel Zakharov 		    "txg %llu", (u_longlong_t)txg);
16586f793812SPavel Zakharov 		return (0);
16596f793812SPavel Zakharov 	}
16601195e687SMark J Musante 
16616f793812SPavel Zakharov 	/*
16626f793812SPavel Zakharov 	 * Determine if this vdev has been split off into another
16636f793812SPavel Zakharov 	 * pool.  If so, then refuse to open it.
16646f793812SPavel Zakharov 	 */
16656f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
16666f793812SPavel Zakharov 	    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
16676f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16686f793812SPavel Zakharov 		    VDEV_AUX_SPLIT_POOL);
16696f793812SPavel Zakharov 		nvlist_free(label);
16706f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
16716f793812SPavel Zakharov 		return (0);
16726f793812SPavel Zakharov 	}
1673560e6e96Seschrock 
16746f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
16756f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16766f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16776f793812SPavel Zakharov 		nvlist_free(label);
16786f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
16796f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_GUID);
16806f793812SPavel Zakharov 		return (0);
16816f793812SPavel Zakharov 	}
16821195e687SMark J Musante 
16836f793812SPavel Zakharov 	/*
16846f793812SPavel Zakharov 	 * If config is not trusted then ignore the spa guid check. This is
16856f793812SPavel Zakharov 	 * necessary because if the machine crashed during a re-guid the new
16866f793812SPavel Zakharov 	 * guid might have been written to all of the vdev labels, but not the
16876f793812SPavel Zakharov 	 * cached config. The check will be performed again once we have the
16886f793812SPavel Zakharov 	 * trusted config from the MOS.
16896f793812SPavel Zakharov 	 */
16906f793812SPavel Zakharov 	if (spa->spa_trust_config && guid != spa_guid(spa)) {
16916f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16926f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16936f793812SPavel Zakharov 		nvlist_free(label);
16946f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
16956f793812SPavel Zakharov 		    "match config (%llu != %llu)", (u_longlong_t)guid,
16966f793812SPavel Zakharov 		    (u_longlong_t)spa_guid(spa));
16976f793812SPavel Zakharov 		return (0);
16986f793812SPavel Zakharov 	}
16996f793812SPavel Zakharov 
17006f793812SPavel Zakharov 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
17016f793812SPavel Zakharov 	    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
17026f793812SPavel Zakharov 	    &aux_guid) != 0)
17036f793812SPavel Zakharov 		aux_guid = 0;
17046f793812SPavel Zakharov 
17056f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
17066f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17076f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
17086f793812SPavel Zakharov 		nvlist_free(label);
17096f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
17106f793812SPavel Zakharov 		    ZPOOL_CONFIG_GUID);
17116f793812SPavel Zakharov 		return (0);
17126f793812SPavel Zakharov 	}
17136f793812SPavel Zakharov 
17146f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
17156f793812SPavel Zakharov 	    != 0) {
17166f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17176f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
17186f793812SPavel Zakharov 		nvlist_free(label);
17196f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
17206f793812SPavel Zakharov 		    ZPOOL_CONFIG_TOP_GUID);
17216f793812SPavel Zakharov 		return (0);
17226f793812SPavel Zakharov 	}
17236f793812SPavel Zakharov 
17246f793812SPavel Zakharov 	/*
17256f793812SPavel Zakharov 	 * If this vdev just became a top-level vdev because its sibling was
17266f793812SPavel Zakharov 	 * detached, it will have adopted the parent's vdev guid -- but the
17276f793812SPavel Zakharov 	 * label may or may not be on disk yet. Fortunately, either version
17286f793812SPavel Zakharov 	 * of the label will have the same top guid, so if we're a top-level
17296f793812SPavel Zakharov 	 * vdev, we can safely compare to that instead.
17306f793812SPavel Zakharov 	 * However, if the config comes from a cachefile that failed to update
17316f793812SPavel Zakharov 	 * after the detach, a top-level vdev will appear as a non top-level
17326f793812SPavel Zakharov 	 * vdev in the config. Also relax the constraints if we perform an
17336f793812SPavel Zakharov 	 * extreme rewind.
17346f793812SPavel Zakharov 	 *
17356f793812SPavel Zakharov 	 * If we split this vdev off instead, then we also check the
17366f793812SPavel Zakharov 	 * original pool's guid. We don't want to consider the vdev
17376f793812SPavel Zakharov 	 * corrupt if it is partway through a split operation.
17386f793812SPavel Zakharov 	 */
17396f793812SPavel Zakharov 	if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
17406f793812SPavel Zakharov 		boolean_t mismatch = B_FALSE;
17416f793812SPavel Zakharov 		if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
17426f793812SPavel Zakharov 			if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
17436f793812SPavel Zakharov 				mismatch = B_TRUE;
17446f793812SPavel Zakharov 		} else {
17456f793812SPavel Zakharov 			if (vd->vdev_guid != top_guid &&
17466f793812SPavel Zakharov 			    vd->vdev_top->vdev_guid != guid)
17476f793812SPavel Zakharov 				mismatch = B_TRUE;
1748560e6e96Seschrock 		}
1749560e6e96Seschrock 
17506f793812SPavel Zakharov 		if (mismatch) {
1751560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1752560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1753560e6e96Seschrock 			nvlist_free(label);
17546f793812SPavel Zakharov 			vdev_dbgmsg(vd, "vdev_validate: config guid "
17556f793812SPavel Zakharov 			    "doesn't match label guid");
17566f793812SPavel Zakharov 			vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
17576f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_guid,
17586f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_top->vdev_guid);
17596f793812SPavel Zakharov 			vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
17606f793812SPavel Zakharov 			    "aux_guid %llu", (u_longlong_t)guid,
17616f793812SPavel Zakharov 			    (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1762560e6e96Seschrock 			return (0);
1763560e6e96Seschrock 		}
17646f793812SPavel Zakharov 	}
1765560e6e96Seschrock 
17666f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
17676f793812SPavel Zakharov 	    &state) != 0) {
17686f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17696f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1770560e6e96Seschrock 		nvlist_free(label);
17716f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
17726f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_STATE);
17736f793812SPavel Zakharov 		return (0);
17746f793812SPavel Zakharov 	}
1775560e6e96Seschrock 
17766f793812SPavel Zakharov 	nvlist_free(label);
17776f793812SPavel Zakharov 
17786f793812SPavel Zakharov 	/*
17796f793812SPavel Zakharov 	 * If this is a verbatim import, no need to check the
17806f793812SPavel Zakharov 	 * state of the pool.
17816f793812SPavel Zakharov 	 */
17826f793812SPavel Zakharov 	if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
17836f793812SPavel Zakharov 	    spa_load_state(spa) == SPA_LOAD_OPEN &&
17846f793812SPavel Zakharov 	    state != POOL_STATE_ACTIVE) {
17856f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
17866f793812SPavel Zakharov 		    "for spa %s", (u_longlong_t)state, spa->spa_name);
17876f793812SPavel Zakharov 		return (SET_ERROR(EBADF));
17886f793812SPavel Zakharov 	}
17896f793812SPavel Zakharov 
17906f793812SPavel Zakharov 	/*
17916f793812SPavel Zakharov 	 * If we were able to open and validate a vdev that was
17926f793812SPavel Zakharov 	 * previously marked permanently unavailable, clear that state
17936f793812SPavel Zakharov 	 * now.
17946f793812SPavel Zakharov 	 */
17956f793812SPavel Zakharov 	if (vd->vdev_not_present)
17966f793812SPavel Zakharov 		vd->vdev_not_present = 0;
17976f793812SPavel Zakharov 
17986f793812SPavel Zakharov 	return (0);
17996f793812SPavel Zakharov }
18006f793812SPavel Zakharov 
18016f793812SPavel Zakharov static void
18026f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
18036f793812SPavel Zakharov {
18046f793812SPavel Zakharov 	if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
18056f793812SPavel Zakharov 		if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
18066f793812SPavel Zakharov 			zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
18076f793812SPavel Zakharov 			    "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
18086f793812SPavel Zakharov 			    dvd->vdev_path, svd->vdev_path);
18096f793812SPavel Zakharov 			spa_strfree(dvd->vdev_path);
18106f793812SPavel Zakharov 			dvd->vdev_path = spa_strdup(svd->vdev_path);
18113ee8c80cSPavel Zakharov 		}
18126f793812SPavel Zakharov 	} else if (svd->vdev_path != NULL) {
18136f793812SPavel Zakharov 		dvd->vdev_path = spa_strdup(svd->vdev_path);
18146f793812SPavel Zakharov 		zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
18156f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
18166f793812SPavel Zakharov 	}
18176f793812SPavel Zakharov }
1818560e6e96Seschrock 
18196f793812SPavel Zakharov /*
18206f793812SPavel Zakharov  * Recursively copy vdev paths from one vdev to another. Source and destination
18216f793812SPavel Zakharov  * vdev trees must have same geometry otherwise return error. Intended to copy
18226f793812SPavel Zakharov  * paths from userland config into MOS config.
18236f793812SPavel Zakharov  */
18246f793812SPavel Zakharov int
18256f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
18266f793812SPavel Zakharov {
18276f793812SPavel Zakharov 	if ((svd->vdev_ops == &vdev_missing_ops) ||
18286f793812SPavel Zakharov 	    (svd->vdev_ishole && dvd->vdev_ishole) ||
18296f793812SPavel Zakharov 	    (dvd->vdev_ops == &vdev_indirect_ops))
18306f793812SPavel Zakharov 		return (0);
18316f793812SPavel Zakharov 
18326f793812SPavel Zakharov 	if (svd->vdev_ops != dvd->vdev_ops) {
18336f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
18346f793812SPavel Zakharov 		    svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
18356f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
18366f793812SPavel Zakharov 	}
18376f793812SPavel Zakharov 
18386f793812SPavel Zakharov 	if (svd->vdev_guid != dvd->vdev_guid) {
18396f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
18406f793812SPavel Zakharov 		    "%llu)", (u_longlong_t)svd->vdev_guid,
18416f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid);
18426f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
18436f793812SPavel Zakharov 	}
18446f793812SPavel Zakharov 
18456f793812SPavel Zakharov 	if (svd->vdev_children != dvd->vdev_children) {
18466f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
18476f793812SPavel Zakharov 		    "%llu != %llu", (u_longlong_t)svd->vdev_children,
18486f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_children);
18496f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
185051ece835Seschrock 	}
1851560e6e96Seschrock 
18526f793812SPavel Zakharov 	for (uint64_t i = 0; i < svd->vdev_children; i++) {
18536f793812SPavel Zakharov 		int error = vdev_copy_path_strict(svd->vdev_child[i],
18546f793812SPavel Zakharov 		    dvd->vdev_child[i]);
18556f793812SPavel Zakharov 		if (error != 0)
18566f793812SPavel Zakharov 			return (error);
18576f793812SPavel Zakharov 	}
18586f793812SPavel Zakharov 
18596f793812SPavel Zakharov 	if (svd->vdev_ops->vdev_op_leaf)
18606f793812SPavel Zakharov 		vdev_copy_path_impl(svd, dvd);
18616f793812SPavel Zakharov 
1862560e6e96Seschrock 	return (0);
1863560e6e96Seschrock }
1864560e6e96Seschrock 
18656f793812SPavel Zakharov static void
18666f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
18676f793812SPavel Zakharov {
18686f793812SPavel Zakharov 	ASSERT(stvd->vdev_top == stvd);
18696f793812SPavel Zakharov 	ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
18706f793812SPavel Zakharov 
18716f793812SPavel Zakharov 	for (uint64_t i = 0; i < dvd->vdev_children; i++) {
18726f793812SPavel Zakharov 		vdev_copy_path_search(stvd, dvd->vdev_child[i]);
18736f793812SPavel Zakharov 	}
18746f793812SPavel Zakharov 
18756f793812SPavel Zakharov 	if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
18766f793812SPavel Zakharov 		return;
18776f793812SPavel Zakharov 
18786f793812SPavel Zakharov 	/*
18796f793812SPavel Zakharov 	 * The idea here is that while a vdev can shift positions within
18806f793812SPavel Zakharov 	 * a top vdev (when replacing, attaching mirror, etc.) it cannot
18816f793812SPavel Zakharov 	 * step outside of it.
18826f793812SPavel Zakharov 	 */
18836f793812SPavel Zakharov 	vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
18846f793812SPavel Zakharov 
18856f793812SPavel Zakharov 	if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
18866f793812SPavel Zakharov 		return;
18876f793812SPavel Zakharov 
18886f793812SPavel Zakharov 	ASSERT(vd->vdev_ops->vdev_op_leaf);
18896f793812SPavel Zakharov 
18906f793812SPavel Zakharov 	vdev_copy_path_impl(vd, dvd);
18916f793812SPavel Zakharov }
18926f793812SPavel Zakharov 
18936f793812SPavel Zakharov /*
18946f793812SPavel Zakharov  * Recursively copy vdev paths from one root vdev to another. Source and
18956f793812SPavel Zakharov  * destination vdev trees may differ in geometry. For each destination leaf
18966f793812SPavel Zakharov  * vdev, search a vdev with the same guid and top vdev id in the source.
18976f793812SPavel Zakharov  * Intended to copy paths from userland config into MOS config.
18986f793812SPavel Zakharov  */
18996f793812SPavel Zakharov void
19006f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
19016f793812SPavel Zakharov {
19026f793812SPavel Zakharov 	uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
19036f793812SPavel Zakharov 	ASSERT(srvd->vdev_ops == &vdev_root_ops);
19046f793812SPavel Zakharov 	ASSERT(drvd->vdev_ops == &vdev_root_ops);
19056f793812SPavel Zakharov 
19066f793812SPavel Zakharov 	for (uint64_t i = 0; i < children; i++) {
19076f793812SPavel Zakharov 		vdev_copy_path_search(srvd->vdev_child[i],
19086f793812SPavel Zakharov 		    drvd->vdev_child[i]);
19096f793812SPavel Zakharov 	}
19106f793812SPavel Zakharov }
19116f793812SPavel Zakharov 
1912fa9e4066Sahrens /*
1913fa9e4066Sahrens  * Close a virtual device.
1914fa9e4066Sahrens  */
1915fa9e4066Sahrens void
1916fa9e4066Sahrens vdev_close(vdev_t *vd)
1917fa9e4066Sahrens {
19188ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1919095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
19208ad4d6ddSJeff Bonwick 
19218ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
19228ad4d6ddSJeff Bonwick 
19231195e687SMark J Musante 	/*
19241195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
19251195e687SMark J Musante 	 * going offline.
19261195e687SMark J Musante 	 */
1927095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
19281195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1929095bcd66SGeorge Wilson 
1930fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1931fa9e4066Sahrens 
19323d7072f8Seschrock 	vdev_cache_purge(vd);
1933fa9e4066Sahrens 
1934560e6e96Seschrock 	/*
1935573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1936560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1937560e6e96Seschrock 	 * it's still faulted.
1938560e6e96Seschrock 	 */
1939560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1940560e6e96Seschrock 
1941fa9e4066Sahrens 	if (vd->vdev_offline)
1942fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1943fa9e4066Sahrens 	else
1944fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1945ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1946fa9e4066Sahrens }
1947fa9e4066Sahrens 
1948dcba9f3fSGeorge Wilson void
1949dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1950dcba9f3fSGeorge Wilson {
1951dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1952dcba9f3fSGeorge Wilson 
1953dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1954dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1955dcba9f3fSGeorge Wilson 		return;
1956dcba9f3fSGeorge Wilson 
1957dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1958dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1959dcba9f3fSGeorge Wilson 
1960dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1961dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1962dcba9f3fSGeorge Wilson }
1963dcba9f3fSGeorge Wilson 
1964dcba9f3fSGeorge Wilson void
1965dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1966dcba9f3fSGeorge Wilson {
1967dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1968dcba9f3fSGeorge Wilson 
1969dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1970dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1971dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1972dcba9f3fSGeorge Wilson 
1973dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1974dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1975dcba9f3fSGeorge Wilson }
1976dcba9f3fSGeorge Wilson 
1977095bcd66SGeorge Wilson /*
1978095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1979095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1980095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1981095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1982095bcd66SGeorge Wilson  */
1983fa9e4066Sahrens void
1984ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1985fa9e4066Sahrens {
1986ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1987fa9e4066Sahrens 
1988e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1989ea8dc4b6Seschrock 
19901195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
19911195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1992fa9e4066Sahrens 	vdev_close(vd);
1993fa9e4066Sahrens 	(void) vdev_open(vd);
1994fa9e4066Sahrens 
199539c23413Seschrock 	/*
199639c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
199739c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
199839c23413Seschrock 	 * opened in response to vdev_reopen().
199939c23413Seschrock 	 */
2000c5904d13Seschrock 	if (vd->vdev_aux) {
2001c5904d13Seschrock 		(void) vdev_validate_aux(vd);
2002e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
20036809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
2004573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
2005573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
2006c5904d13Seschrock 	} else {
20076f793812SPavel Zakharov 		(void) vdev_validate(vd);
2008c5904d13Seschrock 	}
200939c23413Seschrock 
2010fa9e4066Sahrens 	/*
20113d7072f8Seschrock 	 * Reassess parent vdev's health.
2012fa9e4066Sahrens 	 */
20133d7072f8Seschrock 	vdev_propagate_state(vd);
2014fa9e4066Sahrens }
2015fa9e4066Sahrens 
2016fa9e4066Sahrens int
201799653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2018fa9e4066Sahrens {
2019fa9e4066Sahrens 	int error;
2020fa9e4066Sahrens 
2021fa9e4066Sahrens 	/*
2022fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
2023fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
2024fa9e4066Sahrens 	 * there are any components we can't open.
2025fa9e4066Sahrens 	 */
2026fa9e4066Sahrens 	error = vdev_open(vd);
2027fa9e4066Sahrens 
2028fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2029fa9e4066Sahrens 		vdev_close(vd);
2030fa9e4066Sahrens 		return (error ? error : ENXIO);
2031fa9e4066Sahrens 	}
2032fa9e4066Sahrens 
2033fa9e4066Sahrens 	/*
20340713e232SGeorge Wilson 	 * Recursively load DTLs and initialize all labels.
2035fa9e4066Sahrens 	 */
20360713e232SGeorge Wilson 	if ((error = vdev_dtl_load(vd)) != 0 ||
20370713e232SGeorge Wilson 	    (error = vdev_label_init(vd, txg, isreplacing ?
203839c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2039fa9e4066Sahrens 		vdev_close(vd);
2040fa9e4066Sahrens 		return (error);
2041fa9e4066Sahrens 	}
2042fa9e4066Sahrens 
2043fa9e4066Sahrens 	return (0);
2044fa9e4066Sahrens }
2045fa9e4066Sahrens 
20460e34b6a7Sbonwick void
2047573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
2048fa9e4066Sahrens {
204986714001SSerapheim Dimitropoulos 	uint64_t asize = vd->vdev_asize;
2050a0b03b16SSerapheim Dimitropoulos 	uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2051b4bf0cf0SDon Brady 	uint64_t ms_shift;
205286714001SSerapheim Dimitropoulos 
2053fa9e4066Sahrens 	/*
2054b4bf0cf0SDon Brady 	 * There are two dimensions to the metaslab sizing calculation:
2055b4bf0cf0SDon Brady 	 * the size of the metaslab and the count of metaslabs per vdev.
2056b4bf0cf0SDon Brady 	 *
2057a0b03b16SSerapheim Dimitropoulos 	 * The default values used below are a good balance between memory
2058a0b03b16SSerapheim Dimitropoulos 	 * usage (larger metaslab size means more memory needed for loaded
2059a0b03b16SSerapheim Dimitropoulos 	 * metaslabs; more metaslabs means more memory needed for the
2060a0b03b16SSerapheim Dimitropoulos 	 * metaslab_t structs), metaslab load time (larger metaslabs take
2061a0b03b16SSerapheim Dimitropoulos 	 * longer to load), and metaslab sync time (more metaslabs means
2062a0b03b16SSerapheim Dimitropoulos 	 * more time spent syncing all of them).
2063a0b03b16SSerapheim Dimitropoulos 	 *
2064a0b03b16SSerapheim Dimitropoulos 	 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2065a0b03b16SSerapheim Dimitropoulos 	 * The range of the dimensions are as follows:
2066a0b03b16SSerapheim Dimitropoulos 	 *
2067a0b03b16SSerapheim Dimitropoulos 	 *	2^29 <= ms_size  <= 2^34
2068b4bf0cf0SDon Brady 	 *	  16 <= ms_count <= 131,072
2069b4bf0cf0SDon Brady 	 *
2070b4bf0cf0SDon Brady 	 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2071b4bf0cf0SDon Brady 	 * at least 512MB (2^29) to minimize fragmentation effects when
2072b4bf0cf0SDon Brady 	 * testing with smaller devices.  However, the count constraint
2073b4bf0cf0SDon Brady 	 * of at least 16 metaslabs will override this minimum size goal.
2074b4bf0cf0SDon Brady 	 *
2075b4bf0cf0SDon Brady 	 * On the upper end of vdev sizes, we aim for a maximum metaslab
2076a0b03b16SSerapheim Dimitropoulos 	 * size of 16GB.  However, we will cap the total count to 2^17
2077a0b03b16SSerapheim Dimitropoulos 	 * metaslabs to keep our memory footprint in check and let the
2078a0b03b16SSerapheim Dimitropoulos 	 * metaslab size grow from there if that limit is hit.
2079b4bf0cf0SDon Brady 	 *
2080b4bf0cf0SDon Brady 	 * The net effect of applying above constrains is summarized below.
2081b4bf0cf0SDon Brady 	 *
2082a0b03b16SSerapheim Dimitropoulos 	 *   vdev size       metaslab count
2083a0b03b16SSerapheim Dimitropoulos 	 *  --------------|-----------------
2084a0b03b16SSerapheim Dimitropoulos 	 *      < 8GB        ~16
2085a0b03b16SSerapheim Dimitropoulos 	 *  8GB   - 100GB   one per 512MB
2086a0b03b16SSerapheim Dimitropoulos 	 *  100GB - 3TB     ~200
2087a0b03b16SSerapheim Dimitropoulos 	 *  3TB   - 2PB     one per 16GB
2088a0b03b16SSerapheim Dimitropoulos 	 *      > 2PB       ~131,072
2089a0b03b16SSerapheim Dimitropoulos 	 *  --------------------------------
2090a0b03b16SSerapheim Dimitropoulos 	 *
2091a0b03b16SSerapheim Dimitropoulos 	 *  Finally, note that all of the above calculate the initial
2092a0b03b16SSerapheim Dimitropoulos 	 *  number of metaslabs. Expanding a top-level vdev will result
2093a0b03b16SSerapheim Dimitropoulos 	 *  in additional metaslabs being allocated making it possible
2094a0b03b16SSerapheim Dimitropoulos 	 *  to exceed the zfs_vdev_ms_count_limit.
2095a0b03b16SSerapheim Dimitropoulos 	 */
2096a0b03b16SSerapheim Dimitropoulos 
2097a0b03b16SSerapheim Dimitropoulos 	if (ms_count < zfs_vdev_min_ms_count)
2098a0b03b16SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2099a0b03b16SSerapheim Dimitropoulos 	else if (ms_count > zfs_vdev_default_ms_count)
2100a0b03b16SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2101b4bf0cf0SDon Brady 	else
2102a0b03b16SSerapheim Dimitropoulos 		ms_shift = zfs_vdev_default_ms_shift;
2103b4bf0cf0SDon Brady 
2104b4bf0cf0SDon Brady 	if (ms_shift < SPA_MAXBLOCKSHIFT) {
2105b4bf0cf0SDon Brady 		ms_shift = SPA_MAXBLOCKSHIFT;
2106a0b03b16SSerapheim Dimitropoulos 	} else if (ms_shift > zfs_vdev_max_ms_shift) {
2107a0b03b16SSerapheim Dimitropoulos 		ms_shift = zfs_vdev_max_ms_shift;
2108b4bf0cf0SDon Brady 		/* cap the total count to constrain memory footprint */
2109a0b03b16SSerapheim Dimitropoulos 		if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2110a0b03b16SSerapheim Dimitropoulos 			ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
211186714001SSerapheim Dimitropoulos 	}
211286714001SSerapheim Dimitropoulos 
211386714001SSerapheim Dimitropoulos 	vd->vdev_ms_shift = ms_shift;
211486714001SSerapheim Dimitropoulos 	ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2115fa9e4066Sahrens }
2116fa9e4066Sahrens 
2117fa9e4066Sahrens void
2118ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2119fa9e4066Sahrens {
2120ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
21215cabbc6bSPrashanth Sreenivasa 	/* indirect vdevs don't have metaslabs or dtls */
21225cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd) || flags == 0);
2123ecc2d604Sbonwick 	ASSERT(ISP2(flags));
2124f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
2125fa9e4066Sahrens 
2126ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
2127ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2128ecc2d604Sbonwick 
2129ecc2d604Sbonwick 	if (flags & VDD_DTL)
2130ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2131ecc2d604Sbonwick 
2132ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2133fa9e4066Sahrens }
2134fa9e4066Sahrens 
21350713e232SGeorge Wilson void
21360713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
21370713e232SGeorge Wilson {
21380713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
21390713e232SGeorge Wilson 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
21400713e232SGeorge Wilson 
21410713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
21420713e232SGeorge Wilson 		vdev_dirty(vd->vdev_top, flags, vd, txg);
21430713e232SGeorge Wilson }
21440713e232SGeorge Wilson 
21458ad4d6ddSJeff Bonwick /*
21468ad4d6ddSJeff Bonwick  * DTLs.
21478ad4d6ddSJeff Bonwick  *
21488ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
21499fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
21508ad4d6ddSJeff Bonwick  *
21518ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
21528ad4d6ddSJeff Bonwick  *
21538ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
21548ad4d6ddSJeff Bonwick  *
21558ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
21568ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
21578ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
21588ad4d6ddSJeff Bonwick  *
21598ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
21608ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
21618ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
21628ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
21638ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
21648ad4d6ddSJeff Bonwick  *
21658ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
21668ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
21678ad4d6ddSJeff Bonwick  *
21688ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
21698ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
21708ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
21718ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
21728ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
21738ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
21748ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
21758ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
21768ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
21778ad4d6ddSJeff Bonwick  *
21788ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
21798ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
21808ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
21818ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
21828ad4d6ddSJeff Bonwick  */
2183fa9e4066Sahrens void
21848ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2185fa9e4066Sahrens {
21860713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
21878ad4d6ddSJeff Bonwick 
21888ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
21898ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2190f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
21918ad4d6ddSJeff Bonwick 
21925cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
21930713e232SGeorge Wilson 	if (!range_tree_contains(rt, txg, size))
21940713e232SGeorge Wilson 		range_tree_add(rt, txg, size);
21955cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2196fa9e4066Sahrens }
2197fa9e4066Sahrens 
21988ad4d6ddSJeff Bonwick boolean_t
21998ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2200fa9e4066Sahrens {
22010713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
22028ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
2203fa9e4066Sahrens 
22048ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
22058ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2206fa9e4066Sahrens 
22075cabbc6bSPrashanth Sreenivasa 	/*
22085cabbc6bSPrashanth Sreenivasa 	 * While we are loading the pool, the DTLs have not been loaded yet.
22095cabbc6bSPrashanth Sreenivasa 	 * Ignore the DTLs and try all devices.  This avoids a recursive
22105cabbc6bSPrashanth Sreenivasa 	 * mutex enter on the vdev_dtl_lock, and also makes us try hard
22115cabbc6bSPrashanth Sreenivasa 	 * when loading the pool (relying on the checksum to ensure that
22125cabbc6bSPrashanth Sreenivasa 	 * we get the right data -- note that we while loading, we are
22135cabbc6bSPrashanth Sreenivasa 	 * only reading the MOS, which is always checksummed).
22145cabbc6bSPrashanth Sreenivasa 	 */
22155cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
22165cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
22175cabbc6bSPrashanth Sreenivasa 
22185cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
221986714001SSerapheim Dimitropoulos 	if (!range_tree_is_empty(rt))
22200713e232SGeorge Wilson 		dirty = range_tree_contains(rt, txg, size);
22215cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2222fa9e4066Sahrens 
2223fa9e4066Sahrens 	return (dirty);
2224fa9e4066Sahrens }
2225fa9e4066Sahrens 
22268ad4d6ddSJeff Bonwick boolean_t
22278ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
22288ad4d6ddSJeff Bonwick {
22290713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
22308ad4d6ddSJeff Bonwick 	boolean_t empty;
22318ad4d6ddSJeff Bonwick 
22325cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
223386714001SSerapheim Dimitropoulos 	empty = range_tree_is_empty(rt);
22345cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
22358ad4d6ddSJeff Bonwick 
22368ad4d6ddSJeff Bonwick 	return (empty);
22378ad4d6ddSJeff Bonwick }
22388ad4d6ddSJeff Bonwick 
2239b4952e17SGeorge Wilson /*
2240b4952e17SGeorge Wilson  * Returns the lowest txg in the DTL range.
2241b4952e17SGeorge Wilson  */
2242b4952e17SGeorge Wilson static uint64_t
2243b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd)
2244b4952e17SGeorge Wilson {
22450713e232SGeorge Wilson 	range_seg_t *rs;
2246b4952e17SGeorge Wilson 
2247b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
22480713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2249b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2250b4952e17SGeorge Wilson 
22510713e232SGeorge Wilson 	rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
22520713e232SGeorge Wilson 	return (rs->rs_start - 1);
2253b4952e17SGeorge Wilson }
2254b4952e17SGeorge Wilson 
2255b4952e17SGeorge Wilson /*
2256b4952e17SGeorge Wilson  * Returns the highest txg in the DTL.
2257b4952e17SGeorge Wilson  */
2258b4952e17SGeorge Wilson static uint64_t
2259b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd)
2260b4952e17SGeorge Wilson {
22610713e232SGeorge Wilson 	range_seg_t *rs;
2262b4952e17SGeorge Wilson 
2263b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
22640713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2265b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2266b4952e17SGeorge Wilson 
22670713e232SGeorge Wilson 	rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
22680713e232SGeorge Wilson 	return (rs->rs_end);
2269b4952e17SGeorge Wilson }
2270b4952e17SGeorge Wilson 
2271b4952e17SGeorge Wilson /*
2272b4952e17SGeorge Wilson  * Determine if a resilvering vdev should remove any DTL entries from
2273b4952e17SGeorge Wilson  * its range. If the vdev was resilvering for the entire duration of the
2274b4952e17SGeorge Wilson  * scan then it should excise that range from its DTLs. Otherwise, this
2275b4952e17SGeorge Wilson  * vdev is considered partially resilvered and should leave its DTL
2276b4952e17SGeorge Wilson  * entries intact. The comment in vdev_dtl_reassess() describes how we
2277b4952e17SGeorge Wilson  * excise the DTLs.
2278b4952e17SGeorge Wilson  */
2279b4952e17SGeorge Wilson static boolean_t
2280b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd)
2281b4952e17SGeorge Wilson {
2282b4952e17SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2283b4952e17SGeorge Wilson 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2284b4952e17SGeorge Wilson 
2285b4952e17SGeorge Wilson 	ASSERT0(scn->scn_phys.scn_errors);
2286b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2287b4952e17SGeorge Wilson 
22882d2f193aSMatthew Ahrens 	if (vd->vdev_state < VDEV_STATE_DEGRADED)
22892d2f193aSMatthew Ahrens 		return (B_FALSE);
22902d2f193aSMatthew Ahrens 
2291b4952e17SGeorge Wilson 	if (vd->vdev_resilver_txg == 0 ||
229286714001SSerapheim Dimitropoulos 	    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2293b4952e17SGeorge Wilson 		return (B_TRUE);
2294b4952e17SGeorge Wilson 
2295b4952e17SGeorge Wilson 	/*
2296b4952e17SGeorge Wilson 	 * When a resilver is initiated the scan will assign the scn_max_txg
2297b4952e17SGeorge Wilson 	 * value to the highest txg value that exists in all DTLs. If this
2298b4952e17SGeorge Wilson 	 * device's max DTL is not part of this scan (i.e. it is not in
2299b4952e17SGeorge Wilson 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2300b4952e17SGeorge Wilson 	 * for excision.
2301b4952e17SGeorge Wilson 	 */
2302b4952e17SGeorge Wilson 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2303b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2304b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2305b4952e17SGeorge Wilson 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2306b4952e17SGeorge Wilson 		return (B_TRUE);
2307b4952e17SGeorge Wilson 	}
2308b4952e17SGeorge Wilson 	return (B_FALSE);
2309b4952e17SGeorge Wilson }
2310b4952e17SGeorge Wilson 
2311fa9e4066Sahrens /*
2312fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
2313fa9e4066Sahrens  */
2314fa9e4066Sahrens void
2315fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2316fa9e4066Sahrens {
2317ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
23188ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
23198ad4d6ddSJeff Bonwick 	int minref;
2320fa9e4066Sahrens 
23218ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2322fa9e4066Sahrens 
23238ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
23248ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
23258ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
23268ad4d6ddSJeff Bonwick 
23275cabbc6bSPrashanth Sreenivasa 	if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
23288ad4d6ddSJeff Bonwick 		return;
23298ad4d6ddSJeff Bonwick 
23308ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
23313f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
23323f9d6ad7SLin Ling 
2333fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
2334b4952e17SGeorge Wilson 
2335b4952e17SGeorge Wilson 		/*
2336b4952e17SGeorge Wilson 		 * If we've completed a scan cleanly then determine
2337b4952e17SGeorge Wilson 		 * if this vdev should remove any DTLs. We only want to
2338b4952e17SGeorge Wilson 		 * excise regions on vdevs that were available during
2339b4952e17SGeorge Wilson 		 * the entire duration of this scan.
2340b4952e17SGeorge Wilson 		 */
2341088f3894Sahrens 		if (scrub_txg != 0 &&
23423f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
2343b4952e17SGeorge Wilson 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2344b4952e17SGeorge Wilson 		    vdev_dtl_should_excise(vd)) {
2345088f3894Sahrens 			/*
2346088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
2347088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
2348088f3894Sahrens 			 * will be valid, so excise the old region and
2349088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
2350088f3894Sahrens 			 * dtl as-is if there was an error.
23518ad4d6ddSJeff Bonwick 			 *
23528ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
23538ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
23548ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
23558ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
23568ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
23578ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
23588ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
23598ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
23608ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
2361088f3894Sahrens 			 */
23620713e232SGeorge Wilson 			space_reftree_create(&reftree);
23630713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
23640713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
23650713e232SGeorge Wilson 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
23660713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
23670713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_SCRUB], 2);
23680713e232SGeorge Wilson 			space_reftree_generate_map(&reftree,
23690713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
23700713e232SGeorge Wilson 			space_reftree_destroy(&reftree);
2371fa9e4066Sahrens 		}
23720713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
23730713e232SGeorge Wilson 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
23740713e232SGeorge Wilson 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2375fa9e4066Sahrens 		if (scrub_done)
23760713e232SGeorge Wilson 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
23770713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
23788ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
23790713e232SGeorge Wilson 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
23808ad4d6ddSJeff Bonwick 		else
23810713e232SGeorge Wilson 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
23820713e232SGeorge Wilson 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2383b4952e17SGeorge Wilson 
2384b4952e17SGeorge Wilson 		/*
2385b4952e17SGeorge Wilson 		 * If the vdev was resilvering and no longer has any
2386b4952e17SGeorge Wilson 		 * DTLs then reset its resilvering flag.
2387b4952e17SGeorge Wilson 		 */
2388b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0 &&
238986714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
239086714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE]))
2391b4952e17SGeorge Wilson 			vd->vdev_resilver_txg = 0;
2392b4952e17SGeorge Wilson 
2393fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2394088f3894Sahrens 
2395ecc2d604Sbonwick 		if (txg != 0)
2396ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2397fa9e4066Sahrens 		return;
2398fa9e4066Sahrens 	}
2399fa9e4066Sahrens 
2400fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
24018ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
240299bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
240399bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
24048ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
24058ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
24068ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
24078ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
24088ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
24098ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
24108ad4d6ddSJeff Bonwick 		else
24118ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
24120713e232SGeorge Wilson 		space_reftree_create(&reftree);
24138ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
24148ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
24158ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
24160713e232SGeorge Wilson 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
24178ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
24188ad4d6ddSJeff Bonwick 		}
24190713e232SGeorge Wilson 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
24200713e232SGeorge Wilson 		space_reftree_destroy(&reftree);
2421fa9e4066Sahrens 	}
24228ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
2423fa9e4066Sahrens }
2424fa9e4066Sahrens 
24250713e232SGeorge Wilson int
2426fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
2427fa9e4066Sahrens {
2428fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2429ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
24300713e232SGeorge Wilson 	int error = 0;
2431fa9e4066Sahrens 
24320713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
24335cabbc6bSPrashanth Sreenivasa 		ASSERT(vdev_is_concrete(vd));
2434fa9e4066Sahrens 
24350713e232SGeorge Wilson 		error = space_map_open(&vd->vdev_dtl_sm, mos,
24365cabbc6bSPrashanth Sreenivasa 		    vd->vdev_dtl_object, 0, -1ULL, 0);
24370713e232SGeorge Wilson 		if (error)
24380713e232SGeorge Wilson 			return (error);
24390713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2440fa9e4066Sahrens 
24410713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
244288ecc943SGeorge Wilson 
24430713e232SGeorge Wilson 		/*
24440713e232SGeorge Wilson 		 * Now that we've opened the space_map we need to update
24450713e232SGeorge Wilson 		 * the in-core DTL.
24460713e232SGeorge Wilson 		 */
24470713e232SGeorge Wilson 		space_map_update(vd->vdev_dtl_sm);
2448ecc2d604Sbonwick 
24490713e232SGeorge Wilson 		error = space_map_load(vd->vdev_dtl_sm,
24500713e232SGeorge Wilson 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
24510713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2452fa9e4066Sahrens 
24530713e232SGeorge Wilson 		return (error);
24540713e232SGeorge Wilson 	}
24550713e232SGeorge Wilson 
24560713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
24570713e232SGeorge Wilson 		error = vdev_dtl_load(vd->vdev_child[c]);
24580713e232SGeorge Wilson 		if (error != 0)
24590713e232SGeorge Wilson 			break;
24600713e232SGeorge Wilson 	}
2461fa9e4066Sahrens 
2462fa9e4066Sahrens 	return (error);
2463fa9e4066Sahrens }
2464fa9e4066Sahrens 
2465215198a6SJoe Stein void
2466215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2467215198a6SJoe Stein {
2468215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2469215198a6SJoe Stein 
2470215198a6SJoe Stein 	VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2471215198a6SJoe Stein 	VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2472215198a6SJoe Stein 	    zapobj, tx));
2473215198a6SJoe Stein }
2474215198a6SJoe Stein 
2475215198a6SJoe Stein uint64_t
2476215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2477215198a6SJoe Stein {
2478215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2479215198a6SJoe Stein 	uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2480215198a6SJoe Stein 	    DMU_OT_NONE, 0, tx);
2481215198a6SJoe Stein 
2482215198a6SJoe Stein 	ASSERT(zap != 0);
2483215198a6SJoe Stein 	VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2484215198a6SJoe Stein 	    zap, tx));
2485215198a6SJoe Stein 
2486215198a6SJoe Stein 	return (zap);
2487215198a6SJoe Stein }
2488215198a6SJoe Stein 
2489215198a6SJoe Stein void
2490215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2491215198a6SJoe Stein {
2492215198a6SJoe Stein 	if (vd->vdev_ops != &vdev_hole_ops &&
2493215198a6SJoe Stein 	    vd->vdev_ops != &vdev_missing_ops &&
2494215198a6SJoe Stein 	    vd->vdev_ops != &vdev_root_ops &&
2495215198a6SJoe Stein 	    !vd->vdev_top->vdev_removing) {
2496215198a6SJoe Stein 		if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2497215198a6SJoe Stein 			vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2498215198a6SJoe Stein 		}
2499215198a6SJoe Stein 		if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2500215198a6SJoe Stein 			vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2501215198a6SJoe Stein 		}
2502215198a6SJoe Stein 	}
2503215198a6SJoe Stein 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
2504215198a6SJoe Stein 		vdev_construct_zaps(vd->vdev_child[i], tx);
2505215198a6SJoe Stein 	}
2506215198a6SJoe Stein }
2507215198a6SJoe Stein 
2508fa9e4066Sahrens void
2509fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2510fa9e4066Sahrens {
2511fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
25120713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2513ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
25140713e232SGeorge Wilson 	range_tree_t *rtsync;
2515fa9e4066Sahrens 	dmu_tx_t *tx;
25160713e232SGeorge Wilson 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
2517fa9e4066Sahrens 
25185cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
25190713e232SGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
252088ecc943SGeorge Wilson 
2521fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2522fa9e4066Sahrens 
25230713e232SGeorge Wilson 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
25240713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
25250713e232SGeorge Wilson 		space_map_free(vd->vdev_dtl_sm, tx);
25260713e232SGeorge Wilson 		space_map_close(vd->vdev_dtl_sm);
25270713e232SGeorge Wilson 		vd->vdev_dtl_sm = NULL;
25280713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2529215198a6SJoe Stein 
2530215198a6SJoe Stein 		/*
2531215198a6SJoe Stein 		 * We only destroy the leaf ZAP for detached leaves or for
2532215198a6SJoe Stein 		 * removed log devices. Removed data devices handle leaf ZAP
2533215198a6SJoe Stein 		 * cleanup later, once cancellation is no longer possible.
2534215198a6SJoe Stein 		 */
2535215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2536215198a6SJoe Stein 		    vd->vdev_top->vdev_islog)) {
2537215198a6SJoe Stein 			vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2538215198a6SJoe Stein 			vd->vdev_leaf_zap = 0;
2539215198a6SJoe Stein 		}
2540215198a6SJoe Stein 
2541fa9e4066Sahrens 		dmu_tx_commit(tx);
2542fa9e4066Sahrens 		return;
2543fa9e4066Sahrens 	}
2544fa9e4066Sahrens 
25450713e232SGeorge Wilson 	if (vd->vdev_dtl_sm == NULL) {
25460713e232SGeorge Wilson 		uint64_t new_object;
25470713e232SGeorge Wilson 
254886714001SSerapheim Dimitropoulos 		new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx);
25490713e232SGeorge Wilson 		VERIFY3U(new_object, !=, 0);
25500713e232SGeorge Wilson 
25510713e232SGeorge Wilson 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
25525cabbc6bSPrashanth Sreenivasa 		    0, -1ULL, 0));
25530713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2554fa9e4066Sahrens 	}
2555fa9e4066Sahrens 
25565cabbc6bSPrashanth Sreenivasa 	rtsync = range_tree_create(NULL, NULL);
2557fa9e4066Sahrens 
2558fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
25590713e232SGeorge Wilson 	range_tree_walk(rt, range_tree_add, rtsync);
2560fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
2561fa9e4066Sahrens 
256286714001SSerapheim Dimitropoulos 	space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx);
256317f11284SSerapheim Dimitropoulos 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
25640713e232SGeorge Wilson 	range_tree_vacate(rtsync, NULL, NULL);
2565fa9e4066Sahrens 
25660713e232SGeorge Wilson 	range_tree_destroy(rtsync);
2567fa9e4066Sahrens 
25680713e232SGeorge Wilson 	/*
25690713e232SGeorge Wilson 	 * If the object for the space map has changed then dirty
25700713e232SGeorge Wilson 	 * the top level so that we update the config.
25710713e232SGeorge Wilson 	 */
25720713e232SGeorge Wilson 	if (object != space_map_object(vd->vdev_dtl_sm)) {
25733ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
25743ee8c80cSPavel Zakharov 		    "new object %llu", (u_longlong_t)txg, spa_name(spa),
25753ee8c80cSPavel Zakharov 		    (u_longlong_t)object,
25763ee8c80cSPavel Zakharov 		    (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
25770713e232SGeorge Wilson 		vdev_config_dirty(vd->vdev_top);
25780713e232SGeorge Wilson 	}
2579fa9e4066Sahrens 
2580fa9e4066Sahrens 	dmu_tx_commit(tx);
25810713e232SGeorge Wilson 
25820713e232SGeorge Wilson 	mutex_enter(&vd->vdev_dtl_lock);
25830713e232SGeorge Wilson 	space_map_update(vd->vdev_dtl_sm);
25840713e232SGeorge Wilson 	mutex_exit(&vd->vdev_dtl_lock);
2585fa9e4066Sahrens }
2586fa9e4066Sahrens 
25878ad4d6ddSJeff Bonwick /*
25888ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
25898ad4d6ddSJeff Bonwick  * without losing data.
25908ad4d6ddSJeff Bonwick  */
25918ad4d6ddSJeff Bonwick boolean_t
25928ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
25938ad4d6ddSJeff Bonwick {
25948ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
25958ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
25968ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
25978ad4d6ddSJeff Bonwick 	boolean_t required;
25988ad4d6ddSJeff Bonwick 
25998ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
26008ad4d6ddSJeff Bonwick 
26018ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
26028ad4d6ddSJeff Bonwick 		return (B_TRUE);
26038ad4d6ddSJeff Bonwick 
26048ad4d6ddSJeff Bonwick 	/*
26058ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
26068ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
26078ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
26088ad4d6ddSJeff Bonwick 	 */
26098ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
26108ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
26118ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
26128ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
26138ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
26148ad4d6ddSJeff Bonwick 
2615cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
2616cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2617cb04b873SMark J Musante 
26188ad4d6ddSJeff Bonwick 	return (required);
26198ad4d6ddSJeff Bonwick }
26208ad4d6ddSJeff Bonwick 
2621088f3894Sahrens /*
2622088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
2623088f3894Sahrens  */
2624088f3894Sahrens boolean_t
2625088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2626088f3894Sahrens {
2627088f3894Sahrens 	boolean_t needed = B_FALSE;
2628088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
2629088f3894Sahrens 	uint64_t thismax = 0;
2630088f3894Sahrens 
2631088f3894Sahrens 	if (vd->vdev_children == 0) {
2632088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
263386714001SSerapheim Dimitropoulos 		if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
26348ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
2635088f3894Sahrens 
2636b4952e17SGeorge Wilson 			thismin = vdev_dtl_min(vd);
2637b4952e17SGeorge Wilson 			thismax = vdev_dtl_max(vd);
2638088f3894Sahrens 			needed = B_TRUE;
2639088f3894Sahrens 		}
2640088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2641088f3894Sahrens 	} else {
26428ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2643088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
2644088f3894Sahrens 			uint64_t cmin, cmax;
2645088f3894Sahrens 
2646088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2647088f3894Sahrens 				thismin = MIN(thismin, cmin);
2648088f3894Sahrens 				thismax = MAX(thismax, cmax);
2649088f3894Sahrens 				needed = B_TRUE;
2650088f3894Sahrens 			}
2651088f3894Sahrens 		}
2652088f3894Sahrens 	}
2653088f3894Sahrens 
2654088f3894Sahrens 	if (needed && minp) {
2655088f3894Sahrens 		*minp = thismin;
2656088f3894Sahrens 		*maxp = thismax;
2657088f3894Sahrens 	}
2658088f3894Sahrens 	return (needed);
2659088f3894Sahrens }
2660088f3894Sahrens 
266186714001SSerapheim Dimitropoulos /*
266286714001SSerapheim Dimitropoulos  * Gets the checkpoint space map object from the vdev's ZAP.
266386714001SSerapheim Dimitropoulos  * Returns the spacemap object, or 0 if it wasn't in the ZAP
266486714001SSerapheim Dimitropoulos  * or the ZAP doesn't exist yet.
266586714001SSerapheim Dimitropoulos  */
266686714001SSerapheim Dimitropoulos int
266786714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd)
266886714001SSerapheim Dimitropoulos {
266986714001SSerapheim Dimitropoulos 	ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
267086714001SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0) {
267186714001SSerapheim Dimitropoulos 		return (0);
267286714001SSerapheim Dimitropoulos 	}
267386714001SSerapheim Dimitropoulos 
267486714001SSerapheim Dimitropoulos 	uint64_t sm_obj = 0;
267586714001SSerapheim Dimitropoulos 	int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
267686714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj);
267786714001SSerapheim Dimitropoulos 
267886714001SSerapheim Dimitropoulos 	ASSERT(err == 0 || err == ENOENT);
267986714001SSerapheim Dimitropoulos 
268086714001SSerapheim Dimitropoulos 	return (sm_obj);
268186714001SSerapheim Dimitropoulos }
268286714001SSerapheim Dimitropoulos 
26835cabbc6bSPrashanth Sreenivasa int
2684ea8dc4b6Seschrock vdev_load(vdev_t *vd)
2685fa9e4066Sahrens {
26865cabbc6bSPrashanth Sreenivasa 	int error = 0;
2687fa9e4066Sahrens 	/*
2688fa9e4066Sahrens 	 * Recursively load all children.
2689fa9e4066Sahrens 	 */
26905cabbc6bSPrashanth Sreenivasa 	for (int c = 0; c < vd->vdev_children; c++) {
26915cabbc6bSPrashanth Sreenivasa 		error = vdev_load(vd->vdev_child[c]);
26925cabbc6bSPrashanth Sreenivasa 		if (error != 0) {
26935cabbc6bSPrashanth Sreenivasa 			return (error);
26945cabbc6bSPrashanth Sreenivasa 		}
26955cabbc6bSPrashanth Sreenivasa 	}
26965cabbc6bSPrashanth Sreenivasa 
26975cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
2698fa9e4066Sahrens 
2699fa9e4066Sahrens 	/*
27000e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
2701fa9e4066Sahrens 	 */
27025cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
27035cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
27045cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
27055cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
27063ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
27073ee8c80cSPavel Zakharov 			    "asize=%llu", (u_longlong_t)vd->vdev_ashift,
27083ee8c80cSPavel Zakharov 			    (u_longlong_t)vd->vdev_asize);
27095cabbc6bSPrashanth Sreenivasa 			return (SET_ERROR(ENXIO));
27105cabbc6bSPrashanth Sreenivasa 		} else if ((error = vdev_metaslab_init(vd, 0)) != 0) {
27113ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
27123ee8c80cSPavel Zakharov 			    "[error=%d]", error);
27135cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
27145cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
27155cabbc6bSPrashanth Sreenivasa 			return (error);
27165cabbc6bSPrashanth Sreenivasa 		}
271786714001SSerapheim Dimitropoulos 
271886714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd);
271986714001SSerapheim Dimitropoulos 		if (checkpoint_sm_obj != 0) {
272086714001SSerapheim Dimitropoulos 			objset_t *mos = spa_meta_objset(vd->vdev_spa);
272186714001SSerapheim Dimitropoulos 			ASSERT(vd->vdev_asize != 0);
272286714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
272386714001SSerapheim Dimitropoulos 
272486714001SSerapheim Dimitropoulos 			if ((error = space_map_open(&vd->vdev_checkpoint_sm,
272586714001SSerapheim Dimitropoulos 			    mos, checkpoint_sm_obj, 0, vd->vdev_asize,
272686714001SSerapheim Dimitropoulos 			    vd->vdev_ashift))) {
272786714001SSerapheim Dimitropoulos 				vdev_dbgmsg(vd, "vdev_load: space_map_open "
272886714001SSerapheim Dimitropoulos 				    "failed for checkpoint spacemap (obj %llu) "
272986714001SSerapheim Dimitropoulos 				    "[error=%d]",
273086714001SSerapheim Dimitropoulos 				    (u_longlong_t)checkpoint_sm_obj, error);
273186714001SSerapheim Dimitropoulos 				return (error);
273286714001SSerapheim Dimitropoulos 			}
273386714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
273486714001SSerapheim Dimitropoulos 			space_map_update(vd->vdev_checkpoint_sm);
273586714001SSerapheim Dimitropoulos 
273686714001SSerapheim Dimitropoulos 			/*
273786714001SSerapheim Dimitropoulos 			 * Since the checkpoint_sm contains free entries
273886714001SSerapheim Dimitropoulos 			 * exclusively we can use sm_alloc to indicate the
273986714001SSerapheim Dimitropoulos 			 * culmulative checkpointed space that has been freed.
274086714001SSerapheim Dimitropoulos 			 */
274186714001SSerapheim Dimitropoulos 			vd->vdev_stat.vs_checkpoint_space =
274286714001SSerapheim Dimitropoulos 			    -vd->vdev_checkpoint_sm->sm_alloc;
274386714001SSerapheim Dimitropoulos 			vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
274486714001SSerapheim Dimitropoulos 			    vd->vdev_stat.vs_checkpoint_space;
274586714001SSerapheim Dimitropoulos 		}
27465cabbc6bSPrashanth Sreenivasa 	}
2747fa9e4066Sahrens 
2748fa9e4066Sahrens 	/*
2749fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
2750fa9e4066Sahrens 	 */
27515cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
2752560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2753560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
27543ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
27553ee8c80cSPavel Zakharov 		    "[error=%d]", error);
27565cabbc6bSPrashanth Sreenivasa 		return (error);
27575cabbc6bSPrashanth Sreenivasa 	}
27585cabbc6bSPrashanth Sreenivasa 
27595cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
27605cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
27615cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
27625cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_asize != 0);
276386714001SSerapheim Dimitropoulos 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
27645cabbc6bSPrashanth Sreenivasa 
27655cabbc6bSPrashanth Sreenivasa 		if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
27665cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object, 0, vd->vdev_asize, 0))) {
27675cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
27685cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
27693ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
27703ee8c80cSPavel Zakharov 			    "obsolete spacemap (obj %llu) [error=%d]",
27713ee8c80cSPavel Zakharov 			    (u_longlong_t)obsolete_sm_object, error);
27725cabbc6bSPrashanth Sreenivasa 			return (error);
27735cabbc6bSPrashanth Sreenivasa 		}
27745cabbc6bSPrashanth Sreenivasa 		space_map_update(vd->vdev_obsolete_sm);
27755cabbc6bSPrashanth Sreenivasa 	}
27765cabbc6bSPrashanth Sreenivasa 
27775cabbc6bSPrashanth Sreenivasa 	return (0);
2778fa9e4066Sahrens }
2779fa9e4066Sahrens 
278099653d4eSeschrock /*
2781fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
2782fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
2783fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
2784fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
2785fa94a07fSbrendan  * repurposed to another pool.
278699653d4eSeschrock  */
278799653d4eSeschrock int
2788fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
278999653d4eSeschrock {
279099653d4eSeschrock 	nvlist_t *label;
279199653d4eSeschrock 	uint64_t guid, version;
279299653d4eSeschrock 	uint64_t state;
279399653d4eSeschrock 
2794e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
2795c5904d13Seschrock 		return (0);
2796c5904d13Seschrock 
2797dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
279899653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
279999653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
280099653d4eSeschrock 		return (-1);
280199653d4eSeschrock 	}
280299653d4eSeschrock 
280399653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2804ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(version) ||
280599653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
280699653d4eSeschrock 	    guid != vd->vdev_guid ||
280799653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
280899653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
280999653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
281099653d4eSeschrock 		nvlist_free(label);
281199653d4eSeschrock 		return (-1);
281299653d4eSeschrock 	}
281399653d4eSeschrock 
281499653d4eSeschrock 	/*
281599653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
281699653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
281799653d4eSeschrock 	 */
281899653d4eSeschrock 	nvlist_free(label);
281999653d4eSeschrock 	return (0);
282099653d4eSeschrock }
282199653d4eSeschrock 
28225cabbc6bSPrashanth Sreenivasa /*
28235cabbc6bSPrashanth Sreenivasa  * Free the objects used to store this vdev's spacemaps, and the array
28245cabbc6bSPrashanth Sreenivasa  * that points to them.
28255cabbc6bSPrashanth Sreenivasa  */
282688ecc943SGeorge Wilson void
28275cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
28285cabbc6bSPrashanth Sreenivasa {
28295cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0)
28305cabbc6bSPrashanth Sreenivasa 		return;
28315cabbc6bSPrashanth Sreenivasa 
28325cabbc6bSPrashanth Sreenivasa 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
28335cabbc6bSPrashanth Sreenivasa 	uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
28345cabbc6bSPrashanth Sreenivasa 	size_t array_bytes = array_count * sizeof (uint64_t);
28355cabbc6bSPrashanth Sreenivasa 	uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
28365cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
28375cabbc6bSPrashanth Sreenivasa 	    array_bytes, smobj_array, 0));
28385cabbc6bSPrashanth Sreenivasa 
28395cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < array_count; i++) {
28405cabbc6bSPrashanth Sreenivasa 		uint64_t smobj = smobj_array[i];
28415cabbc6bSPrashanth Sreenivasa 		if (smobj == 0)
28425cabbc6bSPrashanth Sreenivasa 			continue;
28435cabbc6bSPrashanth Sreenivasa 
28445cabbc6bSPrashanth Sreenivasa 		space_map_free_obj(mos, smobj, tx);
28455cabbc6bSPrashanth Sreenivasa 	}
28465cabbc6bSPrashanth Sreenivasa 
28475cabbc6bSPrashanth Sreenivasa 	kmem_free(smobj_array, array_bytes);
28485cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
28495cabbc6bSPrashanth Sreenivasa 	vd->vdev_ms_array = 0;
28505cabbc6bSPrashanth Sreenivasa }
28515cabbc6bSPrashanth Sreenivasa 
28525cabbc6bSPrashanth Sreenivasa static void
28534e75ba68SSerapheim Dimitropoulos vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
285488ecc943SGeorge Wilson {
285588ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
285688ecc943SGeorge Wilson 
28574e75ba68SSerapheim Dimitropoulos 	ASSERT(vd->vdev_islog);
2858215198a6SJoe Stein 	ASSERT(vd == vd->vdev_top);
2859215198a6SJoe Stein 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
286088ecc943SGeorge Wilson 
286188ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
28622e4c9986SGeorge Wilson 		metaslab_group_t *mg = vd->vdev_mg;
28632e4c9986SGeorge Wilson 
28642e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
28652e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
28662e4c9986SGeorge Wilson 
286788ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
286888ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
286988ecc943SGeorge Wilson 
28700713e232SGeorge Wilson 			if (msp == NULL || msp->ms_sm == NULL)
287188ecc943SGeorge Wilson 				continue;
287288ecc943SGeorge Wilson 
28730713e232SGeorge Wilson 			mutex_enter(&msp->ms_lock);
28742e4c9986SGeorge Wilson 			/*
28752e4c9986SGeorge Wilson 			 * If the metaslab was not loaded when the vdev
28762e4c9986SGeorge Wilson 			 * was removed then the histogram accounting may
28772e4c9986SGeorge Wilson 			 * not be accurate. Update the histogram information
28782e4c9986SGeorge Wilson 			 * here so that we ensure that the metaslab group
28792e4c9986SGeorge Wilson 			 * and metaslab class are up-to-date.
28802e4c9986SGeorge Wilson 			 */
28812e4c9986SGeorge Wilson 			metaslab_group_histogram_remove(mg, msp);
28822e4c9986SGeorge Wilson 
28830713e232SGeorge Wilson 			VERIFY0(space_map_allocated(msp->ms_sm));
28840713e232SGeorge Wilson 			space_map_close(msp->ms_sm);
28850713e232SGeorge Wilson 			msp->ms_sm = NULL;
28860713e232SGeorge Wilson 			mutex_exit(&msp->ms_lock);
288788ecc943SGeorge Wilson 		}
28882e4c9986SGeorge Wilson 
288986714001SSerapheim Dimitropoulos 		if (vd->vdev_checkpoint_sm != NULL) {
289086714001SSerapheim Dimitropoulos 			ASSERT(spa_has_checkpoint(spa));
289186714001SSerapheim Dimitropoulos 			space_map_close(vd->vdev_checkpoint_sm);
289286714001SSerapheim Dimitropoulos 			vd->vdev_checkpoint_sm = NULL;
289386714001SSerapheim Dimitropoulos 		}
289486714001SSerapheim Dimitropoulos 
28952e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
28962e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
28972e4c9986SGeorge Wilson 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
28982e4c9986SGeorge Wilson 			ASSERT0(mg->mg_histogram[i]);
289988ecc943SGeorge Wilson 	}
290088ecc943SGeorge Wilson 
29014e75ba68SSerapheim Dimitropoulos 	dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2902215198a6SJoe Stein 
29034e75ba68SSerapheim Dimitropoulos 	vdev_destroy_spacemaps(vd, tx);
29044e75ba68SSerapheim Dimitropoulos 	if (vd->vdev_top_zap != 0) {
2905215198a6SJoe Stein 		vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2906215198a6SJoe Stein 		vd->vdev_top_zap = 0;
2907215198a6SJoe Stein 	}
29084e75ba68SSerapheim Dimitropoulos 
290988ecc943SGeorge Wilson 	dmu_tx_commit(tx);
291088ecc943SGeorge Wilson }
291188ecc943SGeorge Wilson 
2912fa9e4066Sahrens void
2913fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2914fa9e4066Sahrens {
2915fa9e4066Sahrens 	metaslab_t *msp;
291680eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2917fa9e4066Sahrens 
29185cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
291988ecc943SGeorge Wilson 
2920094e47e9SGeorge Wilson 	while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2921094e47e9SGeorge Wilson 	    != NULL)
2922fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
292380eb36f2SGeorge Wilson 
292480eb36f2SGeorge Wilson 	if (reassess)
292580eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2926fa9e4066Sahrens }
2927fa9e4066Sahrens 
2928fa9e4066Sahrens void
2929fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2930fa9e4066Sahrens {
2931fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2932fa9e4066Sahrens 	vdev_t *lvd;
2933fa9e4066Sahrens 	metaslab_t *msp;
2934ecc2d604Sbonwick 	dmu_tx_t *tx;
2935fa9e4066Sahrens 
29365cabbc6bSPrashanth Sreenivasa 	if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
29375cabbc6bSPrashanth Sreenivasa 		dmu_tx_t *tx;
29385cabbc6bSPrashanth Sreenivasa 
29395cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
29405cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
294188ecc943SGeorge Wilson 
29425cabbc6bSPrashanth Sreenivasa 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
29435cabbc6bSPrashanth Sreenivasa 		vdev_indirect_sync_obsolete(vd, tx);
29445cabbc6bSPrashanth Sreenivasa 		dmu_tx_commit(tx);
29455cabbc6bSPrashanth Sreenivasa 
29465cabbc6bSPrashanth Sreenivasa 		/*
29475cabbc6bSPrashanth Sreenivasa 		 * If the vdev is indirect, it can't have dirty
29485cabbc6bSPrashanth Sreenivasa 		 * metaslabs or DTLs.
29495cabbc6bSPrashanth Sreenivasa 		 */
29505cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ops == &vdev_indirect_ops) {
29515cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
29525cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
29535cabbc6bSPrashanth Sreenivasa 			return;
29545cabbc6bSPrashanth Sreenivasa 		}
29555cabbc6bSPrashanth Sreenivasa 	}
29565cabbc6bSPrashanth Sreenivasa 
29575cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
29585cabbc6bSPrashanth Sreenivasa 
29595cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
29605cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_removing) {
2961ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
29625cabbc6bSPrashanth Sreenivasa 		ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
2963ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2964ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2965ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2966ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2967ecc2d604Sbonwick 		vdev_config_dirty(vd);
2968ecc2d604Sbonwick 		dmu_tx_commit(tx);
2969ecc2d604Sbonwick 	}
2970fa9e4066Sahrens 
2971ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2972fa9e4066Sahrens 		metaslab_sync(msp, txg);
2973ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2974ecc2d604Sbonwick 	}
2975fa9e4066Sahrens 
2976fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2977fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2978fa9e4066Sahrens 
29795cabbc6bSPrashanth Sreenivasa 	/*
29804e75ba68SSerapheim Dimitropoulos 	 * If this is an empty log device being removed, destroy the
29814e75ba68SSerapheim Dimitropoulos 	 * metadata associated with it.
29825cabbc6bSPrashanth Sreenivasa 	 */
29834e75ba68SSerapheim Dimitropoulos 	if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
29844e75ba68SSerapheim Dimitropoulos 		vdev_remove_empty_log(vd, txg);
29855cabbc6bSPrashanth Sreenivasa 
2986fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2987fa9e4066Sahrens }
2988fa9e4066Sahrens 
2989fa9e4066Sahrens uint64_t
2990fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2991fa9e4066Sahrens {
2992fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2993fa9e4066Sahrens }
2994fa9e4066Sahrens 
29953d7072f8Seschrock /*
29963d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
29973d7072f8Seschrock  * not be opened, and no I/O is attempted.
29983d7072f8Seschrock  */
2999fa9e4066Sahrens int
3000069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3001fa9e4066Sahrens {
30024b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
3003fa9e4066Sahrens 
30048f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
3005fa9e4066Sahrens 
3006c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3007e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3008e14bb325SJeff Bonwick 
30093d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
3010e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3011fa9e4066Sahrens 
30124b964adaSGeorge Wilson 	tvd = vd->vdev_top;
30134b964adaSGeorge Wilson 
3014069f55e2SEric Schrock 	/*
3015069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
3016069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
3017069f55e2SEric Schrock 	 * were faulted.
3018069f55e2SEric Schrock 	 */
3019069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
3020069f55e2SEric Schrock 
30213d7072f8Seschrock 	/*
30223d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
30233d7072f8Seschrock 	 */
302498d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
30253d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
30263d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
3027069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
30283d7072f8Seschrock 
30293d7072f8Seschrock 	/*
3030c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
3031c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
30323d7072f8Seschrock 	 */
30334b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
30343d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
30353d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
30363d7072f8Seschrock 
30373d7072f8Seschrock 		/*
30383d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
30393d7072f8Seschrock 		 * mark it degraded.
30403d7072f8Seschrock 		 */
30414b964adaSGeorge Wilson 		vdev_reopen(tvd);
30423d7072f8Seschrock 
3043069f55e2SEric Schrock 		if (vdev_readable(vd))
3044069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
30453d7072f8Seschrock 	}
30463d7072f8Seschrock 
3047e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
30483d7072f8Seschrock }
30493d7072f8Seschrock 
30503d7072f8Seschrock /*
30513d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
30523d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
30533d7072f8Seschrock  * as I/O is concerned.
30543d7072f8Seschrock  */
30553d7072f8Seschrock int
3056069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
30573d7072f8Seschrock {
3058c5904d13Seschrock 	vdev_t *vd;
30590a4e9518Sgw 
30608f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
30613d7072f8Seschrock 
3062c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3063e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3064e14bb325SJeff Bonwick 
30650e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
3066e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
30670e34b6a7Sbonwick 
30683d7072f8Seschrock 	/*
30693d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
30703d7072f8Seschrock 	 */
3071e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
3072e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
30733d7072f8Seschrock 
30743d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
30753d7072f8Seschrock 	if (!vdev_is_dead(vd))
30763d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3077069f55e2SEric Schrock 		    aux);
30783d7072f8Seschrock 
3079e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
30803d7072f8Seschrock }
30813d7072f8Seschrock 
30823d7072f8Seschrock /*
3083f7170741SWill Andrews  * Online the given vdev.
3084f7170741SWill Andrews  *
3085f7170741SWill Andrews  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
3086f7170741SWill Andrews  * spare device should be detached when the device finishes resilvering.
3087f7170741SWill Andrews  * Second, the online should be treated like a 'test' online case, so no FMA
3088f7170741SWill Andrews  * events are generated if the device fails to open.
30893d7072f8Seschrock  */
30903d7072f8Seschrock int
3091e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
30923d7072f8Seschrock {
3093573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
30945f368aefSYuri Pankov 	boolean_t wasoffline;
30955f368aefSYuri Pankov 	vdev_state_t oldstate;
30963d7072f8Seschrock 
30978f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
30983d7072f8Seschrock 
3099c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3100e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
31013d7072f8Seschrock 
31023d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
3103e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3104fa9e4066Sahrens 
31055f368aefSYuri Pankov 	wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
31065f368aefSYuri Pankov 	oldstate = vd->vdev_state;
310714372834SHans Rosenfeld 
3108573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
3109fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
3110441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
3111e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3112e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3113573ca77eSGeorge Wilson 
3114573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
3115573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3116573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3117573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
3118573ca77eSGeorge Wilson 	}
3119573ca77eSGeorge Wilson 
3120573ca77eSGeorge Wilson 	vdev_reopen(tvd);
31213d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
31223d7072f8Seschrock 
3123573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3124573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3125573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
3126573ca77eSGeorge Wilson 	}
3127573ca77eSGeorge Wilson 
31283d7072f8Seschrock 	if (newstate)
31293d7072f8Seschrock 		*newstate = vd->vdev_state;
31303d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
31313d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
31323d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
31333d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
31343d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
3135fa9e4066Sahrens 
3136573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3137573ca77eSGeorge Wilson 
3138573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
3139573ca77eSGeorge Wilson 		if (vd->vdev_aux)
3140573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3141573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3142573ca77eSGeorge Wilson 	}
314314372834SHans Rosenfeld 
3144094e47e9SGeorge Wilson 	/* Restart initializing if necessary */
3145094e47e9SGeorge Wilson 	mutex_enter(&vd->vdev_initialize_lock);
3146094e47e9SGeorge Wilson 	if (vdev_writeable(vd) &&
3147094e47e9SGeorge Wilson 	    vd->vdev_initialize_thread == NULL &&
3148094e47e9SGeorge Wilson 	    vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
3149094e47e9SGeorge Wilson 		(void) vdev_initialize(vd);
3150094e47e9SGeorge Wilson 	}
3151094e47e9SGeorge Wilson 	mutex_exit(&vd->vdev_initialize_lock);
3152094e47e9SGeorge Wilson 
31535f368aefSYuri Pankov 	if (wasoffline ||
31545f368aefSYuri Pankov 	    (oldstate < VDEV_STATE_DEGRADED &&
31555f368aefSYuri Pankov 	    vd->vdev_state >= VDEV_STATE_DEGRADED))
3156ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
315714372834SHans Rosenfeld 
31588ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
3159fa9e4066Sahrens }
3160fa9e4066Sahrens 
3161a1521560SJeff Bonwick static int
3162a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3163fa9e4066Sahrens {
3164e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
31658f18d1faSGeorge Wilson 	int error = 0;
31668f18d1faSGeorge Wilson 	uint64_t generation;
31678f18d1faSGeorge Wilson 	metaslab_group_t *mg;
31680a4e9518Sgw 
31698f18d1faSGeorge Wilson top:
31708f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
3171fa9e4066Sahrens 
3172c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3173e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3174fa9e4066Sahrens 
31750e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
3176e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
31770e34b6a7Sbonwick 
3178e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
31798f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
31808f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
3181e6ca193dSGeorge Wilson 
3182fa9e4066Sahrens 	/*
3183ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
3184fa9e4066Sahrens 	 */
3185ecc2d604Sbonwick 	if (!vd->vdev_offline) {
3186ecc2d604Sbonwick 		/*
31878ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
3188e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
3189e6ca193dSGeorge Wilson 		 * expendable.
3190ecc2d604Sbonwick 		 */
3191e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3192e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
3193e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3194fa9e4066Sahrens 
31958f18d1faSGeorge Wilson 		/*
3196b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
3197b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
3198b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
3199b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
32008f18d1faSGeorge Wilson 		 */
32018f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
32028f18d1faSGeorge Wilson 			/*
32038f18d1faSGeorge Wilson 			 * Prevent any future allocations.
32048f18d1faSGeorge Wilson 			 */
3205a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
32068f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
32078f18d1faSGeorge Wilson 
32085cabbc6bSPrashanth Sreenivasa 			error = spa_reset_logs(spa);
32098f18d1faSGeorge Wilson 
321086714001SSerapheim Dimitropoulos 			/*
321186714001SSerapheim Dimitropoulos 			 * If the log device was successfully reset but has
321286714001SSerapheim Dimitropoulos 			 * checkpointed data, do not offline it.
321386714001SSerapheim Dimitropoulos 			 */
321486714001SSerapheim Dimitropoulos 			if (error == 0 &&
321586714001SSerapheim Dimitropoulos 			    tvd->vdev_checkpoint_sm != NULL) {
321686714001SSerapheim Dimitropoulos 				ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc,
321786714001SSerapheim Dimitropoulos 				    !=, 0);
321886714001SSerapheim Dimitropoulos 				error = ZFS_ERR_CHECKPOINT_EXISTS;
321986714001SSerapheim Dimitropoulos 			}
322086714001SSerapheim Dimitropoulos 
32218f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
32228f18d1faSGeorge Wilson 
32238f18d1faSGeorge Wilson 			/*
32248f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
32258f18d1faSGeorge Wilson 			 */
32268f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
3227a1521560SJeff Bonwick 				metaslab_group_activate(mg);
32288f18d1faSGeorge Wilson 				if (error)
32298f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
32308f18d1faSGeorge Wilson 					    vd, error));
32318f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
32328f18d1faSGeorge Wilson 				goto top;
32338f18d1faSGeorge Wilson 			}
3234fb09f5aaSMadhav Suresh 			ASSERT0(tvd->vdev_stat.vs_alloc);
32358f18d1faSGeorge Wilson 		}
32368f18d1faSGeorge Wilson 
3237ecc2d604Sbonwick 		/*
3238ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
3239e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
3240e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
3241e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
3242ecc2d604Sbonwick 		 */
3243ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
3244e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
3245e6ca193dSGeorge Wilson 
3246e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3247e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
3248ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
3249e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
3250e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3251ecc2d604Sbonwick 		}
32528f18d1faSGeorge Wilson 
32538f18d1faSGeorge Wilson 		/*
32548f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
32558f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
32568f18d1faSGeorge Wilson 		 */
32578f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
3258a1521560SJeff Bonwick 			metaslab_group_activate(mg);
3259fa9e4066Sahrens 	}
3260fa9e4066Sahrens 
3261e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3262ecc2d604Sbonwick 
32638f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
3264fa9e4066Sahrens }
3265fa9e4066Sahrens 
3266a1521560SJeff Bonwick int
3267a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3268a1521560SJeff Bonwick {
3269a1521560SJeff Bonwick 	int error;
3270a1521560SJeff Bonwick 
3271a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
3272a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
3273a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
3274a1521560SJeff Bonwick 
3275a1521560SJeff Bonwick 	return (error);
3276a1521560SJeff Bonwick }
3277a1521560SJeff Bonwick 
3278ea8dc4b6Seschrock /*
3279ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
3280ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
3281ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
3282ea8dc4b6Seschrock  */
3283ea8dc4b6Seschrock void
3284e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
3285fa9e4066Sahrens {
3286e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3287e14bb325SJeff Bonwick 
3288e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3289fa9e4066Sahrens 
3290ea8dc4b6Seschrock 	if (vd == NULL)
3291e14bb325SJeff Bonwick 		vd = rvd;
3292fa9e4066Sahrens 
3293ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
3294ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
3295ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
3296fa9e4066Sahrens 
3297e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
3298e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
32993d7072f8Seschrock 
33005cabbc6bSPrashanth Sreenivasa 	/*
33015cabbc6bSPrashanth Sreenivasa 	 * It makes no sense to "clear" an indirect vdev.
33025cabbc6bSPrashanth Sreenivasa 	 */
33035cabbc6bSPrashanth Sreenivasa 	if (!vdev_is_concrete(vd))
33045cabbc6bSPrashanth Sreenivasa 		return;
33055cabbc6bSPrashanth Sreenivasa 
33063d7072f8Seschrock 	/*
33078a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
33088a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
33098a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
33108a79c1b5Sek 	 * written out to disk.
33113d7072f8Seschrock 	 */
3312e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
3313e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
33148a79c1b5Sek 
3315096d22d4SEric Schrock 		/*
3316096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
3317096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
3318096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
3319096d22d4SEric Schrock 		 */
3320096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
3321096d22d4SEric Schrock 
33224b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3323e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
3324e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
3325e14bb325SJeff Bonwick 
3326f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
33273d7072f8Seschrock 
3328096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
3329096d22d4SEric Schrock 
3330f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
3331e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
3332e14bb325SJeff Bonwick 
3333e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
3334bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
33353d7072f8Seschrock 
3336ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
33373d7072f8Seschrock 	}
3338096d22d4SEric Schrock 
3339096d22d4SEric Schrock 	/*
3340096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
3341096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
3342096d22d4SEric Schrock 	 * done in response to the FMA fault.
3343096d22d4SEric Schrock 	 */
3344096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3345096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3346096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
3347096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
3348fa9e4066Sahrens }
3349fa9e4066Sahrens 
3350e14bb325SJeff Bonwick boolean_t
3351e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
33520a4e9518Sgw {
335388ecc943SGeorge Wilson 	/*
335488ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
335588ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
335688ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
335788ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
335888ecc943SGeorge Wilson 	 * before issuing I/O to them.
335988ecc943SGeorge Wilson 	 */
33605cabbc6bSPrashanth Sreenivasa 	return (vd->vdev_state < VDEV_STATE_DEGRADED ||
33615cabbc6bSPrashanth Sreenivasa 	    vd->vdev_ops == &vdev_hole_ops ||
336288ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
33630a4e9518Sgw }
33640a4e9518Sgw 
3365e14bb325SJeff Bonwick boolean_t
3366e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
33670a4e9518Sgw {
3368e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
33690a4e9518Sgw }
33700a4e9518Sgw 
3371e14bb325SJeff Bonwick boolean_t
3372e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
3373fa9e4066Sahrens {
33745cabbc6bSPrashanth Sreenivasa 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
33755cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd));
3376fa9e4066Sahrens }
3377fa9e4066Sahrens 
3378a31e6787SGeorge Wilson boolean_t
3379a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
3380a31e6787SGeorge Wilson {
33818ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
33828ad4d6ddSJeff Bonwick 
3383a31e6787SGeorge Wilson 	/*
33848ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
3385a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3386a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
33878ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
33888ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
33898ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
3390a31e6787SGeorge Wilson 	 */
33918ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
33925cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_cant_write && vdev_is_concrete(vd) &&
33930f7643c7SGeorge Wilson 	    vd->vdev_mg->mg_initialized);
3394a31e6787SGeorge Wilson }
3395a31e6787SGeorge Wilson 
3396e14bb325SJeff Bonwick boolean_t
3397e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
3398fa9e4066Sahrens {
3399e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
3400fa9e4066Sahrens 
3401e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3402e14bb325SJeff Bonwick 		return (B_FALSE);
3403fa9e4066Sahrens 
3404e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
3405e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
3406fa9e4066Sahrens 
3407e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
3408e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
3409fa9e4066Sahrens 
3410e14bb325SJeff Bonwick 	return (B_TRUE);
3411fa9e4066Sahrens }
3412fa9e4066Sahrens 
341386714001SSerapheim Dimitropoulos boolean_t
341486714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd)
341586714001SSerapheim Dimitropoulos {
3416a0b03b16SSerapheim Dimitropoulos 	if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
3417a0b03b16SSerapheim Dimitropoulos 		return (B_TRUE);
3418a0b03b16SSerapheim Dimitropoulos 
341986714001SSerapheim Dimitropoulos 	/*
3420a0b03b16SSerapheim Dimitropoulos 	 * If double-word space map entries are not enabled we assume
3421a0b03b16SSerapheim Dimitropoulos 	 * 47 bits of the space map entry are dedicated to the entry's
3422a0b03b16SSerapheim Dimitropoulos 	 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
3423a0b03b16SSerapheim Dimitropoulos 	 * to calculate the maximum address that can be described by a
3424a0b03b16SSerapheim Dimitropoulos 	 * space map entry for the given device.
342586714001SSerapheim Dimitropoulos 	 */
3426a0b03b16SSerapheim Dimitropoulos 	uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
342786714001SSerapheim Dimitropoulos 
342886714001SSerapheim Dimitropoulos 	if (shift >= 63) /* detect potential overflow */
342986714001SSerapheim Dimitropoulos 		return (B_TRUE);
343086714001SSerapheim Dimitropoulos 
343186714001SSerapheim Dimitropoulos 	return (vd->vdev_asize < (1ULL << shift));
343286714001SSerapheim Dimitropoulos }
343386714001SSerapheim Dimitropoulos 
3434fa9e4066Sahrens /*
3435fa9e4066Sahrens  * Get statistics for the given vdev.
3436fa9e4066Sahrens  */
3437fa9e4066Sahrens void
3438fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3439fa9e4066Sahrens {
34402e4c9986SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
34412e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
3442c39a2aaeSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
34432e4c9986SGeorge Wilson 
34442e4c9986SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3445fa9e4066Sahrens 
3446fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
3447fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3448fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3449fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
3450573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
3451094e47e9SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3452573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
3453094e47e9SGeorge Wilson 		/*
3454094e47e9SGeorge Wilson 		 * Report intializing progress. Since we don't have the
3455094e47e9SGeorge Wilson 		 * initializing locks held, this is only an estimate (although a
3456094e47e9SGeorge Wilson 		 * fairly accurate one).
3457094e47e9SGeorge Wilson 		 */
3458094e47e9SGeorge Wilson 		vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done;
3459094e47e9SGeorge Wilson 		vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est;
3460094e47e9SGeorge Wilson 		vs->vs_initialize_state = vd->vdev_initialize_state;
3461094e47e9SGeorge Wilson 		vs->vs_initialize_action_time = vd->vdev_initialize_action_time;
3462094e47e9SGeorge Wilson 	}
3463c39a2aaeSGeorge Wilson 	/*
3464c39a2aaeSGeorge Wilson 	 * Report expandable space on top-level, non-auxillary devices only.
3465c39a2aaeSGeorge Wilson 	 * The expandable space is reported in terms of metaslab sized units
3466c39a2aaeSGeorge Wilson 	 * since that determines how much space the pool can expand.
3467c39a2aaeSGeorge Wilson 	 */
3468c39a2aaeSGeorge Wilson 	if (vd->vdev_aux == NULL && tvd != NULL) {
34697855d95bSToomas Soome 		vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize -
34707855d95bSToomas Soome 		    spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3471c39a2aaeSGeorge Wilson 	}
34725cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
34735cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd)) {
34742e4c9986SGeorge Wilson 		vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
34752986efa8SAlex Reece 	}
3476fa9e4066Sahrens 
3477fa9e4066Sahrens 	/*
3478fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
3479fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
3480fa9e4066Sahrens 	 */
3481fa9e4066Sahrens 	if (vd == rvd) {
3482e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
3483fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
3484fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
3485fa9e4066Sahrens 
3486e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
3487fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
3488fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
3489fa9e4066Sahrens 			}
34903f9d6ad7SLin Ling 			cvs->vs_scan_removing = cvd->vdev_removing;
3491fa9e4066Sahrens 		}
3492fa9e4066Sahrens 	}
34932e4c9986SGeorge Wilson 	mutex_exit(&vd->vdev_stat_lock);
3494fa9e4066Sahrens }
3495fa9e4066Sahrens 
3496fa94a07fSbrendan void
3497fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
3498fa94a07fSbrendan {
3499fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
3500fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
3501fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
3502fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
3503fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
3504fa94a07fSbrendan }
3505fa94a07fSbrendan 
35063f9d6ad7SLin Ling void
35073f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
35083f9d6ad7SLin Ling {
35093f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
35103f9d6ad7SLin Ling 
35113f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
35123f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
35133f9d6ad7SLin Ling 
35143f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
35153f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
35163f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
35173f9d6ad7SLin Ling }
35183f9d6ad7SLin Ling 
3519fa9e4066Sahrens void
3520e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
3521fa9e4066Sahrens {
35228ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
35238ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3524e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3525fa9e4066Sahrens 	vdev_t *pvd;
3526fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
3527fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
3528fa9e4066Sahrens 	zio_type_t type = zio->io_type;
3529fa9e4066Sahrens 	int flags = zio->io_flags;
3530fa9e4066Sahrens 
3531e14bb325SJeff Bonwick 	/*
3532e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
3533e14bb325SJeff Bonwick 	 */
3534e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
3535e14bb325SJeff Bonwick 		return;
3536e14bb325SJeff Bonwick 
3537fa9e4066Sahrens 	if (zio->io_error == 0) {
3538e14bb325SJeff Bonwick 		/*
3539e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
3540e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
3541e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
3542e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
3543e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
3544e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
3545e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
3546e14bb325SJeff Bonwick 		 *
3547e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
3548e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
3549e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
3550e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
3551e14bb325SJeff Bonwick 		 */
3552e14bb325SJeff Bonwick 		if (vd == rvd)
3553e14bb325SJeff Bonwick 			return;
3554e14bb325SJeff Bonwick 
3555e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
35568ad4d6ddSJeff Bonwick 
35578ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
35588ad4d6ddSJeff Bonwick 			return;
35598ad4d6ddSJeff Bonwick 
35608ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
35618ad4d6ddSJeff Bonwick 
3562e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
356344ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
35643f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
35653f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
35663f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
35673f9d6ad7SLin Ling 
35683f9d6ad7SLin Ling 				/* XXX cleanup? */
35693f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
35703f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
35713f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
35723f9d6ad7SLin Ling 			}
35733f9d6ad7SLin Ling 
35748ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
3575e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
3576fa9e4066Sahrens 		}
35778ad4d6ddSJeff Bonwick 
35788ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
35798ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
35808ad4d6ddSJeff Bonwick 
35818ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
3582fa9e4066Sahrens 		return;
3583fa9e4066Sahrens 	}
3584fa9e4066Sahrens 
3585fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
3586fa9e4066Sahrens 		return;
3587fa9e4066Sahrens 
35888956713aSEric Schrock 	/*
35898956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
35908956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
35918956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
35928956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
35938956713aSEric Schrock 	 */
35948956713aSEric Schrock 	if (zio->io_error == EIO &&
35958956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
35968956713aSEric Schrock 		return;
35978956713aSEric Schrock 
35988f18d1faSGeorge Wilson 	/*
35998f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
36008f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
36018f18d1faSGeorge Wilson 	 * errors.
36028f18d1faSGeorge Wilson 	 */
36038f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
36048f18d1faSGeorge Wilson 		return;
36058f18d1faSGeorge Wilson 
3606e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
3607b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3608e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
3609e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
3610e14bb325SJeff Bonwick 		else
3611e14bb325SJeff Bonwick 			vs->vs_read_errors++;
3612fa9e4066Sahrens 	}
3613b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3614e14bb325SJeff Bonwick 		vs->vs_write_errors++;
3615e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
3616fa9e4066Sahrens 
36175cabbc6bSPrashanth Sreenivasa 	if (spa->spa_load_state == SPA_LOAD_NONE &&
36185cabbc6bSPrashanth Sreenivasa 	    type == ZIO_TYPE_WRITE && txg != 0 &&
36198ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
362044ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
3621b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
36228ad4d6ddSJeff Bonwick 		/*
3623b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
3624b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
3625b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
3626b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
3627b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
3628b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
3629b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
3630b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
36318ad4d6ddSJeff Bonwick 		 *
36328ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
36338ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
36348ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
36358ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
36368ad4d6ddSJeff Bonwick 		 */
36378ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
36388ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
363944ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
36408ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
36418ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
36428ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3643b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
3644b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
3645b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3646b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
36478ad4d6ddSJeff Bonwick 			}
3648b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
36498ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3650fa9e4066Sahrens 				return;
36518ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
36528ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
36538ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3654fa9e4066Sahrens 		}
36558ad4d6ddSJeff Bonwick 		if (vd != rvd)
36568ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3657fa9e4066Sahrens 	}
3658fa9e4066Sahrens }
3659fa9e4066Sahrens 
3660fa9e4066Sahrens /*
3661b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
3662b24ab676SJeff Bonwick  * and the root vdev.
3663fa9e4066Sahrens  */
3664fa9e4066Sahrens void
3665b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3666b24ab676SJeff Bonwick     int64_t space_delta)
3667fa9e4066Sahrens {
366899653d4eSeschrock 	int64_t dspace_delta = space_delta;
36698654d025Sperrin 	spa_t *spa = vd->vdev_spa;
36708654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
3671b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
3672b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3673fa9e4066Sahrens 
36748654d025Sperrin 	ASSERT(vd == vd->vdev_top);
367599653d4eSeschrock 
36768654d025Sperrin 	/*
36778654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
36788654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
36798654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
36808654d025Sperrin 	 * childrens', thus not accurate enough for us.
36818654d025Sperrin 	 */
36828654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3683e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
36848654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
36858654d025Sperrin 	    vd->vdev_deflate_ratio;
36868654d025Sperrin 
36878654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
36888654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
3689b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
36908654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
36918654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
36928654d025Sperrin 
3693b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
3694fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
3695fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
3696b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
3697fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
3698fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
3699fa94a07fSbrendan 	}
3700b24ab676SJeff Bonwick 
3701b24ab676SJeff Bonwick 	if (mc != NULL) {
3702b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
3703b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
3704b24ab676SJeff Bonwick 
3705b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
3706b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
3707b24ab676SJeff Bonwick 	}
3708fa9e4066Sahrens }
3709fa9e4066Sahrens 
3710fa9e4066Sahrens /*
3711fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
3712fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
3713fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3714fa9e4066Sahrens  */
3715fa9e4066Sahrens void
3716fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
3717fa9e4066Sahrens {
3718fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
3719fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3720fa9e4066Sahrens 	int c;
3721fa9e4066Sahrens 
3722f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3723f9af39baSGeorge Wilson 
3724c5904d13Seschrock 	/*
37256809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
37266809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
3727c5904d13Seschrock 	 */
3728c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
3729c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
3730c5904d13Seschrock 		nvlist_t **aux;
3731c5904d13Seschrock 		uint_t naux;
3732c5904d13Seschrock 
3733c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
3734c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
3735c5904d13Seschrock 				break;
3736c5904d13Seschrock 		}
3737c5904d13Seschrock 
3738e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
3739e14bb325SJeff Bonwick 			/*
3740e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
3741e14bb325SJeff Bonwick 			 */
3742e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
3743e14bb325SJeff Bonwick 			return;
3744e14bb325SJeff Bonwick 		}
3745e14bb325SJeff Bonwick 
3746c5904d13Seschrock 		sav->sav_sync = B_TRUE;
3747c5904d13Seschrock 
37486809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
37496809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
37506809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
37516809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
37526809eb4eSEric Schrock 		}
3753c5904d13Seschrock 
3754c5904d13Seschrock 		ASSERT(c < naux);
3755c5904d13Seschrock 
3756c5904d13Seschrock 		/*
3757c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
3758c5904d13Seschrock 		 * sketchy, but it will work.
3759c5904d13Seschrock 		 */
3760c5904d13Seschrock 		nvlist_free(aux[c]);
37613f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3762c5904d13Seschrock 
3763c5904d13Seschrock 		return;
3764c5904d13Seschrock 	}
3765c5904d13Seschrock 
37665dabedeeSbonwick 	/*
3767e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
3768e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
3769e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
37705dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
37715dabedeeSbonwick 	 */
3772e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3773e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3774e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
37755dabedeeSbonwick 
3776fa9e4066Sahrens 	if (vd == rvd) {
3777fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
3778fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
3779fa9e4066Sahrens 	} else {
3780fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
3781fa9e4066Sahrens 
378288ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
37835cabbc6bSPrashanth Sreenivasa 		    vdev_is_concrete(vd)) {
3784e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
37855cabbc6bSPrashanth Sreenivasa 		}
3786fa9e4066Sahrens 	}
3787fa9e4066Sahrens }
3788fa9e4066Sahrens 
3789fa9e4066Sahrens void
3790fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
3791fa9e4066Sahrens {
37925dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
37935dabedeeSbonwick 
3794e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3795e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3796e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
37975dabedeeSbonwick 
3798e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3799e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
3800e14bb325SJeff Bonwick }
3801e14bb325SJeff Bonwick 
3802e14bb325SJeff Bonwick /*
3803e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
3804e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
3805e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
3806e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
3807e14bb325SJeff Bonwick  */
3808e14bb325SJeff Bonwick void
3809e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
3810e14bb325SJeff Bonwick {
3811e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3812e14bb325SJeff Bonwick 
3813f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3814e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
3815e14bb325SJeff Bonwick 
3816e14bb325SJeff Bonwick 	/*
3817e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
3818e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
3819e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
3820e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
3821e14bb325SJeff Bonwick 	 */
3822e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3823e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3824e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3825e14bb325SJeff Bonwick 
38265cabbc6bSPrashanth Sreenivasa 	if (!list_link_active(&vd->vdev_state_dirty_node) &&
38275cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd))
3828e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
3829e14bb325SJeff Bonwick }
3830e14bb325SJeff Bonwick 
3831e14bb325SJeff Bonwick void
3832e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
3833e14bb325SJeff Bonwick {
3834e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3835e14bb325SJeff Bonwick 
3836e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3837e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3838e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3839e14bb325SJeff Bonwick 
3840e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3841e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
3842fa9e4066Sahrens }
3843fa9e4066Sahrens 
384432b87932Sek /*
384532b87932Sek  * Propagate vdev state up from children to parent.
384632b87932Sek  */
384744cd46caSbillm void
384844cd46caSbillm vdev_propagate_state(vdev_t *vd)
384944cd46caSbillm {
38508ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
38518ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
385244cd46caSbillm 	int degraded = 0, faulted = 0;
385344cd46caSbillm 	int corrupted = 0;
385444cd46caSbillm 	vdev_t *child;
385544cd46caSbillm 
38563d7072f8Seschrock 	if (vd->vdev_children > 0) {
3857573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
38583d7072f8Seschrock 			child = vd->vdev_child[c];
385951ece835Seschrock 
386088ecc943SGeorge Wilson 			/*
38615cabbc6bSPrashanth Sreenivasa 			 * Don't factor holes or indirect vdevs into the
38625cabbc6bSPrashanth Sreenivasa 			 * decision.
386388ecc943SGeorge Wilson 			 */
38645cabbc6bSPrashanth Sreenivasa 			if (!vdev_is_concrete(child))
386588ecc943SGeorge Wilson 				continue;
386688ecc943SGeorge Wilson 
3867e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
38688ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
386951ece835Seschrock 				/*
387051ece835Seschrock 				 * Root special: if there is a top-level log
387151ece835Seschrock 				 * device, treat the root vdev as if it were
387251ece835Seschrock 				 * degraded.
387351ece835Seschrock 				 */
387451ece835Seschrock 				if (child->vdev_islog && vd == rvd)
387551ece835Seschrock 					degraded++;
387651ece835Seschrock 				else
387751ece835Seschrock 					faulted++;
387851ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
38793d7072f8Seschrock 				degraded++;
388051ece835Seschrock 			}
388144cd46caSbillm 
38823d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
38833d7072f8Seschrock 				corrupted++;
38843d7072f8Seschrock 		}
388544cd46caSbillm 
38863d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
38873d7072f8Seschrock 
38883d7072f8Seschrock 		/*
3889e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
38903d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
38913d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
38923d7072f8Seschrock 		 * replicas'.
38933d7072f8Seschrock 		 */
38943d7072f8Seschrock 		if (corrupted && vd == rvd &&
38953d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
38963d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
38973d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
38983d7072f8Seschrock 	}
38993d7072f8Seschrock 
390051ece835Seschrock 	if (vd->vdev_parent)
39013d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
390244cd46caSbillm }
390344cd46caSbillm 
3904fa9e4066Sahrens /*
3905ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
3906ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
3907ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
3908ea8dc4b6Seschrock  *
3909ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
3910ea8dc4b6Seschrock  * generated.
3911fa9e4066Sahrens  */
3912fa9e4066Sahrens void
3913ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3914fa9e4066Sahrens {
3915560e6e96Seschrock 	uint64_t save_state;
3916c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
3917ea8dc4b6Seschrock 
3918ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
3919ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
3920fa9e4066Sahrens 		return;
3921ea8dc4b6Seschrock 	}
3922ea8dc4b6Seschrock 
3923560e6e96Seschrock 	save_state = vd->vdev_state;
3924fa9e4066Sahrens 
3925fa9e4066Sahrens 	vd->vdev_state = state;
3926fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
3927fa9e4066Sahrens 
39283d7072f8Seschrock 	/*
39293d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
393098d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
393198d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
393298d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
393398d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
393498d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
393598d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
393698d1cbfeSGeorge Wilson 	 * children.
393798d1cbfeSGeorge Wilson 	 */
393898d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
393998d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
39403d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
39413d7072f8Seschrock 
3942069f55e2SEric Schrock 	/*
3943069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
3944069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
3945069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
3946069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
3947069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
3948069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
3949069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
3950069f55e2SEric Schrock 	 */
3951069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3952069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
3953069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
3954069f55e2SEric Schrock 
39553d7072f8Seschrock 	if (vd->vdev_removed &&
39563d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
39573d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
39583d7072f8Seschrock 		/*
39593d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
39603d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
39613d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
39623d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
39633d7072f8Seschrock 		 * it is one of our special test online cases, which is only
39643d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
39653d7072f8Seschrock 		 * fault.
39663d7072f8Seschrock 		 */
39673d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
39683d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
39693d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
39703d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
39713d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
3972ea8dc4b6Seschrock 		/*
3973cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
3974cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
3975cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
3976cb04b873SMark J Musante 		 * is not considered an error.
3977ea8dc4b6Seschrock 		 */
3978cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3979cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3980560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
3981560e6e96Seschrock 			vd->vdev_not_present = 1;
3982560e6e96Seschrock 
3983560e6e96Seschrock 		/*
3984560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
3985560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3986560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
3987560e6e96Seschrock 		 * want to post the ereport if the device was already in the
3988560e6e96Seschrock 		 * CANT_OPEN state beforehand.
39893d7072f8Seschrock 		 *
39903d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
39913d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
39923d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
39933d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
39943d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
39953d7072f8Seschrock 		 * about to replace the device, or attempt an online with
39963d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
3997560e6e96Seschrock 		 */
39983d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
39993d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
4000c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
4001ea8dc4b6Seschrock 			const char *class;
4002ea8dc4b6Seschrock 
4003ea8dc4b6Seschrock 			switch (aux) {
4004ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
4005ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
4006ea8dc4b6Seschrock 				break;
4007ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
4008ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
4009ea8dc4b6Seschrock 				break;
4010ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
4011ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
4012ea8dc4b6Seschrock 				break;
4013ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
4014ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
4015ea8dc4b6Seschrock 				break;
4016ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
4017ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
4018ea8dc4b6Seschrock 				break;
4019ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
4020ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
4021ea8dc4b6Seschrock 				break;
4022ea8dc4b6Seschrock 			default:
4023ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
4024ea8dc4b6Seschrock 			}
4025ea8dc4b6Seschrock 
4026c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
4027ea8dc4b6Seschrock 		}
4028ea8dc4b6Seschrock 
40293d7072f8Seschrock 		/* Erase any notion of persistent removed state */
40303d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
40313d7072f8Seschrock 	} else {
40323d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
40333d7072f8Seschrock 	}
4034ea8dc4b6Seschrock 
40358b33d774STim Haley 	if (!isopen && vd->vdev_parent)
40368b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
4037fa9e4066Sahrens }
403815e6edf1Sgw 
40396f793812SPavel Zakharov boolean_t
40406f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd)
40416f793812SPavel Zakharov {
40426f793812SPavel Zakharov 	ASSERT(!vd->vdev_ops->vdev_op_leaf);
40436f793812SPavel Zakharov 
40446f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
40456f793812SPavel Zakharov 		if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
40466f793812SPavel Zakharov 			return (B_FALSE);
40476f793812SPavel Zakharov 	}
40486f793812SPavel Zakharov 
40496f793812SPavel Zakharov 	return (B_TRUE);
40506f793812SPavel Zakharov }
40516f793812SPavel Zakharov 
405215e6edf1Sgw /*
405315e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
4054c8811bd3SToomas Soome  * a root pool. We do not support partial configuration.
4055c8811bd3SToomas Soome  * In addition, only a single top-level vdev is allowed.
405615e6edf1Sgw  */
405715e6edf1Sgw boolean_t
405815e6edf1Sgw vdev_is_bootable(vdev_t *vd)
405915e6edf1Sgw {
406015e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
406115e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
406215e6edf1Sgw 
406315e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
406415e6edf1Sgw 		    vd->vdev_children > 1) {
406515e6edf1Sgw 			return (B_FALSE);
40665cabbc6bSPrashanth Sreenivasa 		} else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
40675cabbc6bSPrashanth Sreenivasa 		    strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
406815e6edf1Sgw 			return (B_FALSE);
406915e6edf1Sgw 		}
407015e6edf1Sgw 	}
407115e6edf1Sgw 
4072573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
407315e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
407415e6edf1Sgw 			return (B_FALSE);
407515e6edf1Sgw 	}
407615e6edf1Sgw 	return (B_TRUE);
407715e6edf1Sgw }
4078e6ca193dSGeorge Wilson 
40795cabbc6bSPrashanth Sreenivasa boolean_t
40805cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd)
40815cabbc6bSPrashanth Sreenivasa {
40825cabbc6bSPrashanth Sreenivasa 	vdev_ops_t *ops = vd->vdev_ops;
40835cabbc6bSPrashanth Sreenivasa 	if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
40845cabbc6bSPrashanth Sreenivasa 	    ops == &vdev_missing_ops || ops == &vdev_root_ops) {
40855cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
40865cabbc6bSPrashanth Sreenivasa 	} else {
40875cabbc6bSPrashanth Sreenivasa 		return (B_TRUE);
40885cabbc6bSPrashanth Sreenivasa 	}
40895cabbc6bSPrashanth Sreenivasa }
40905cabbc6bSPrashanth Sreenivasa 
40914b964adaSGeorge Wilson /*
40924b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
40934b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
40944b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
40954b964adaSGeorge Wilson  */
40964b964adaSGeorge Wilson boolean_t
40974b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
40984b964adaSGeorge Wilson {
40994b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
41004b964adaSGeorge Wilson 	    !vd->vdev_removed)
41014b964adaSGeorge Wilson 		return (B_TRUE);
41024b964adaSGeorge Wilson 
41034b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
41044b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
41054b964adaSGeorge Wilson 			return (B_TRUE);
41064b964adaSGeorge Wilson 
41074b964adaSGeorge Wilson 	return (B_FALSE);
41084b964adaSGeorge Wilson }
41094b964adaSGeorge Wilson 
4110573ca77eSGeorge Wilson /*
4111573ca77eSGeorge Wilson  * Expand a vdev if possible.
4112573ca77eSGeorge Wilson  */
4113573ca77eSGeorge Wilson void
4114573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
4115573ca77eSGeorge Wilson {
4116573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
4117573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
411811f6a968SSerapheim Dimitropoulos 	ASSERT(vdev_is_concrete(vd));
4119573ca77eSGeorge Wilson 
41205cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
41215cabbc6bSPrashanth Sreenivasa 
412211f6a968SSerapheim Dimitropoulos 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
4123573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
4124573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
4125573ca77eSGeorge Wilson 	}
4126573ca77eSGeorge Wilson }
41271195e687SMark J Musante 
41281195e687SMark J Musante /*
41291195e687SMark J Musante  * Split a vdev.
41301195e687SMark J Musante  */
41311195e687SMark J Musante void
41321195e687SMark J Musante vdev_split(vdev_t *vd)
41331195e687SMark J Musante {
41341195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
41351195e687SMark J Musante 
41361195e687SMark J Musante 	vdev_remove_child(pvd, vd);
41371195e687SMark J Musante 	vdev_compact_children(pvd);
41381195e687SMark J Musante 
41391195e687SMark J Musante 	cvd = pvd->vdev_child[0];
41401195e687SMark J Musante 	if (pvd->vdev_children == 1) {
41411195e687SMark J Musante 		vdev_remove_parent(cvd);
41421195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
41431195e687SMark J Musante 	}
41441195e687SMark J Musante 	vdev_propagate_state(cvd);
41451195e687SMark J Musante }
4146283b8460SGeorge.Wilson 
4147283b8460SGeorge.Wilson void
4148283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd)
4149283b8460SGeorge.Wilson {
4150283b8460SGeorge.Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
4151283b8460SGeorge.Wilson 		vdev_t *cvd = vd->vdev_child[c];
4152283b8460SGeorge.Wilson 
4153283b8460SGeorge.Wilson 		vdev_deadman(cvd);
4154283b8460SGeorge.Wilson 	}
4155283b8460SGeorge.Wilson 
4156283b8460SGeorge.Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
4157283b8460SGeorge.Wilson 		vdev_queue_t *vq = &vd->vdev_queue;
4158283b8460SGeorge.Wilson 
4159283b8460SGeorge.Wilson 		mutex_enter(&vq->vq_lock);
416069962b56SMatthew Ahrens 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
4161283b8460SGeorge.Wilson 			spa_t *spa = vd->vdev_spa;
4162283b8460SGeorge.Wilson 			zio_t *fio;
4163283b8460SGeorge.Wilson 			uint64_t delta;
4164283b8460SGeorge.Wilson 
4165283b8460SGeorge.Wilson 			/*
4166283b8460SGeorge.Wilson 			 * Look at the head of all the pending queues,
4167283b8460SGeorge.Wilson 			 * if any I/O has been outstanding for longer than
4168283b8460SGeorge.Wilson 			 * the spa_deadman_synctime we panic the system.
4169283b8460SGeorge.Wilson 			 */
417069962b56SMatthew Ahrens 			fio = avl_first(&vq->vq_active_tree);
4171c55e05cbSMatthew Ahrens 			delta = gethrtime() - fio->io_timestamp;
4172c55e05cbSMatthew Ahrens 			if (delta > spa_deadman_synctime(spa)) {
41733ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
41743ee8c80cSPavel Zakharov 				    "%lluns, delta %lluns, last io %lluns",
41753ee8c80cSPavel Zakharov 				    fio->io_timestamp, (u_longlong_t)delta,
4176283b8460SGeorge.Wilson 				    vq->vq_io_complete_ts);
4177283b8460SGeorge.Wilson 				fm_panic("I/O to pool '%s' appears to be "
4178283b8460SGeorge.Wilson 				    "hung.", spa_name(spa));
4179283b8460SGeorge.Wilson 			}
4180283b8460SGeorge.Wilson 		}
4181283b8460SGeorge.Wilson 		mutex_exit(&vq->vq_lock);
4182283b8460SGeorge.Wilson 	}
4183283b8460SGeorge.Wilson }
4184