xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 094e47e9)
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>
52*094e47e9SGeorge 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 
7586714001SSerapheim Dimitropoulos /* maximum number of metaslabs per top-level vdev */
7686714001SSerapheim Dimitropoulos int vdev_max_ms_count = 200;
7786714001SSerapheim Dimitropoulos 
7886714001SSerapheim Dimitropoulos /* minimum amount of metaslabs per top-level vdev */
7986714001SSerapheim Dimitropoulos int vdev_min_ms_count = 16;
8086714001SSerapheim Dimitropoulos 
8186714001SSerapheim Dimitropoulos /* see comment in vdev_metaslab_set_size() */
8286714001SSerapheim Dimitropoulos int vdev_default_ms_shift = 29;
8386714001SSerapheim Dimitropoulos 
8486714001SSerapheim Dimitropoulos boolean_t vdev_validate_skip = B_FALSE;
8586714001SSerapheim Dimitropoulos 
86bf3e216cSMatthew Ahrens /*
8786714001SSerapheim Dimitropoulos  * Since the DTL space map of a vdev is not expected to have a lot of
8886714001SSerapheim Dimitropoulos  * entries, we default its block size to 4K.
89bf3e216cSMatthew Ahrens  */
9086714001SSerapheim Dimitropoulos int vdev_dtl_sm_blksz = (1 << 12);
91bf3e216cSMatthew Ahrens 
9286714001SSerapheim Dimitropoulos /*
9386714001SSerapheim Dimitropoulos  * vdev-wide space maps that have lots of entries written to them at
9486714001SSerapheim Dimitropoulos  * the end of each transaction can benefit from a higher I/O bandwidth
9586714001SSerapheim Dimitropoulos  * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
9686714001SSerapheim Dimitropoulos  */
9786714001SSerapheim Dimitropoulos int vdev_standard_sm_blksz = (1 << 17);
986f793812SPavel Zakharov 
993ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
1003ee8c80cSPavel Zakharov void
1013ee8c80cSPavel Zakharov vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
1023ee8c80cSPavel Zakharov {
1033ee8c80cSPavel Zakharov 	va_list adx;
1043ee8c80cSPavel Zakharov 	char buf[256];
1053ee8c80cSPavel Zakharov 
1063ee8c80cSPavel Zakharov 	va_start(adx, fmt);
1073ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
1083ee8c80cSPavel Zakharov 	va_end(adx);
1093ee8c80cSPavel Zakharov 
1103ee8c80cSPavel Zakharov 	if (vd->vdev_path != NULL) {
1113ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
1123ee8c80cSPavel Zakharov 		    vd->vdev_path, buf);
1133ee8c80cSPavel Zakharov 	} else {
1143ee8c80cSPavel Zakharov 		zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
1153ee8c80cSPavel Zakharov 		    vd->vdev_ops->vdev_op_type,
1163ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_id,
1173ee8c80cSPavel Zakharov 		    (u_longlong_t)vd->vdev_guid, buf);
1183ee8c80cSPavel Zakharov 	}
1193ee8c80cSPavel Zakharov }
1203ee8c80cSPavel Zakharov 
1216f793812SPavel Zakharov void
1226f793812SPavel Zakharov vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
1236f793812SPavel Zakharov {
1246f793812SPavel Zakharov 	char state[20];
1256f793812SPavel Zakharov 
1266f793812SPavel Zakharov 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
1276f793812SPavel Zakharov 		zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
1286f793812SPavel Zakharov 		    vd->vdev_ops->vdev_op_type);
1296f793812SPavel Zakharov 		return;
1306f793812SPavel Zakharov 	}
1316f793812SPavel Zakharov 
1326f793812SPavel Zakharov 	switch (vd->vdev_state) {
1336f793812SPavel Zakharov 	case VDEV_STATE_UNKNOWN:
1346f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "unknown");
1356f793812SPavel Zakharov 		break;
1366f793812SPavel Zakharov 	case VDEV_STATE_CLOSED:
1376f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "closed");
1386f793812SPavel Zakharov 		break;
1396f793812SPavel Zakharov 	case VDEV_STATE_OFFLINE:
1406f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "offline");
1416f793812SPavel Zakharov 		break;
1426f793812SPavel Zakharov 	case VDEV_STATE_REMOVED:
1436f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "removed");
1446f793812SPavel Zakharov 		break;
1456f793812SPavel Zakharov 	case VDEV_STATE_CANT_OPEN:
1466f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "can't open");
1476f793812SPavel Zakharov 		break;
1486f793812SPavel Zakharov 	case VDEV_STATE_FAULTED:
1496f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "faulted");
1506f793812SPavel Zakharov 		break;
1516f793812SPavel Zakharov 	case VDEV_STATE_DEGRADED:
1526f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "degraded");
1536f793812SPavel Zakharov 		break;
1546f793812SPavel Zakharov 	case VDEV_STATE_HEALTHY:
1556f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "healthy");
1566f793812SPavel Zakharov 		break;
1576f793812SPavel Zakharov 	default:
1586f793812SPavel Zakharov 		(void) snprintf(state, sizeof (state), "<state %u>",
1596f793812SPavel Zakharov 		    (uint_t)vd->vdev_state);
1606f793812SPavel Zakharov 	}
1616f793812SPavel Zakharov 
1626f793812SPavel Zakharov 	zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
1636f793812SPavel Zakharov 	    "", vd->vdev_id, vd->vdev_ops->vdev_op_type,
1646f793812SPavel Zakharov 	    vd->vdev_islog ? " (log)" : "",
1656f793812SPavel Zakharov 	    (u_longlong_t)vd->vdev_guid,
1666f793812SPavel Zakharov 	    vd->vdev_path ? vd->vdev_path : "N/A", state);
1676f793812SPavel Zakharov 
1686f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++)
1696f793812SPavel Zakharov 		vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
1706f793812SPavel Zakharov }
1716f793812SPavel Zakharov 
172fa9e4066Sahrens /*
173fa9e4066Sahrens  * Given a vdev type, return the appropriate ops vector.
174fa9e4066Sahrens  */
175fa9e4066Sahrens static vdev_ops_t *
176fa9e4066Sahrens vdev_getops(const char *type)
177fa9e4066Sahrens {
178fa9e4066Sahrens 	vdev_ops_t *ops, **opspp;
179fa9e4066Sahrens 
180fa9e4066Sahrens 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
181fa9e4066Sahrens 		if (strcmp(ops->vdev_op_type, type) == 0)
182fa9e4066Sahrens 			break;
183fa9e4066Sahrens 
184fa9e4066Sahrens 	return (ops);
185fa9e4066Sahrens }
186fa9e4066Sahrens 
187*094e47e9SGeorge Wilson /* ARGSUSED */
188*094e47e9SGeorge Wilson void
189*094e47e9SGeorge Wilson vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res)
190*094e47e9SGeorge Wilson {
191*094e47e9SGeorge Wilson 	res->rs_start = in->rs_start;
192*094e47e9SGeorge Wilson 	res->rs_end = in->rs_end;
193*094e47e9SGeorge Wilson }
194*094e47e9SGeorge Wilson 
195fa9e4066Sahrens /*
196fa9e4066Sahrens  * Default asize function: return the MAX of psize with the asize of
197fa9e4066Sahrens  * all children.  This is what's used by anything other than RAID-Z.
198fa9e4066Sahrens  */
199fa9e4066Sahrens uint64_t
200fa9e4066Sahrens vdev_default_asize(vdev_t *vd, uint64_t psize)
201fa9e4066Sahrens {
202ecc2d604Sbonwick 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
203fa9e4066Sahrens 	uint64_t csize;
204fa9e4066Sahrens 
205573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
206fa9e4066Sahrens 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
207fa9e4066Sahrens 		asize = MAX(asize, csize);
208fa9e4066Sahrens 	}
209fa9e4066Sahrens 
210fa9e4066Sahrens 	return (asize);
211fa9e4066Sahrens }
212fa9e4066Sahrens 
2132a79c5feSlling /*
214573ca77eSGeorge Wilson  * Get the minimum allocatable size. We define the allocatable size as
215573ca77eSGeorge Wilson  * the vdev's asize rounded to the nearest metaslab. This allows us to
216573ca77eSGeorge Wilson  * replace or attach devices which don't have the same physical size but
217573ca77eSGeorge Wilson  * can still satisfy the same number of allocations.
2182a79c5feSlling  */
2192a79c5feSlling uint64_t
220573ca77eSGeorge Wilson vdev_get_min_asize(vdev_t *vd)
2212a79c5feSlling {
222573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
2232a79c5feSlling 
224573ca77eSGeorge Wilson 	/*
2254263d13fSGeorge Wilson 	 * If our parent is NULL (inactive spare or cache) or is the root,
226573ca77eSGeorge Wilson 	 * just return our own asize.
227573ca77eSGeorge Wilson 	 */
228573ca77eSGeorge Wilson 	if (pvd == NULL)
229573ca77eSGeorge Wilson 		return (vd->vdev_asize);
2302a79c5feSlling 
2312a79c5feSlling 	/*
232573ca77eSGeorge Wilson 	 * The top-level vdev just returns the allocatable size rounded
233573ca77eSGeorge Wilson 	 * to the nearest metaslab.
2342a79c5feSlling 	 */
235573ca77eSGeorge Wilson 	if (vd == vd->vdev_top)
236573ca77eSGeorge Wilson 		return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
2372a79c5feSlling 
238573ca77eSGeorge Wilson 	/*
239573ca77eSGeorge Wilson 	 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
240573ca77eSGeorge Wilson 	 * so each child must provide at least 1/Nth of its asize.
241573ca77eSGeorge Wilson 	 */
242573ca77eSGeorge Wilson 	if (pvd->vdev_ops == &vdev_raidz_ops)
243c040c10cSSteven Hartland 		return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
244c040c10cSSteven Hartland 		    pvd->vdev_children);
2452a79c5feSlling 
246573ca77eSGeorge Wilson 	return (pvd->vdev_min_asize);
247573ca77eSGeorge Wilson }
2482a79c5feSlling 
249573ca77eSGeorge Wilson void
250573ca77eSGeorge Wilson vdev_set_min_asize(vdev_t *vd)
251573ca77eSGeorge Wilson {
252573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
253573ca77eSGeorge Wilson 
254573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
255573ca77eSGeorge Wilson 		vdev_set_min_asize(vd->vdev_child[c]);
2562a79c5feSlling }
2572a79c5feSlling 
258fa9e4066Sahrens vdev_t *
259fa9e4066Sahrens vdev_lookup_top(spa_t *spa, uint64_t vdev)
260fa9e4066Sahrens {
261fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
262fa9e4066Sahrens 
263e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
264e05725b1Sbonwick 
265088f3894Sahrens 	if (vdev < rvd->vdev_children) {
266088f3894Sahrens 		ASSERT(rvd->vdev_child[vdev] != NULL);
267fa9e4066Sahrens 		return (rvd->vdev_child[vdev]);
268088f3894Sahrens 	}
269fa9e4066Sahrens 
270fa9e4066Sahrens 	return (NULL);
271fa9e4066Sahrens }
272fa9e4066Sahrens 
273fa9e4066Sahrens vdev_t *
274fa9e4066Sahrens vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
275fa9e4066Sahrens {
276fa9e4066Sahrens 	vdev_t *mvd;
277fa9e4066Sahrens 
2780e34b6a7Sbonwick 	if (vd->vdev_guid == guid)
279fa9e4066Sahrens 		return (vd);
280fa9e4066Sahrens 
281573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
282fa9e4066Sahrens 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
283fa9e4066Sahrens 		    NULL)
284fa9e4066Sahrens 			return (mvd);
285fa9e4066Sahrens 
286fa9e4066Sahrens 	return (NULL);
287fa9e4066Sahrens }
288fa9e4066Sahrens 
28912380e1eSArne Jansen static int
29012380e1eSArne Jansen vdev_count_leaves_impl(vdev_t *vd)
29112380e1eSArne Jansen {
29212380e1eSArne Jansen 	int n = 0;
29312380e1eSArne Jansen 
29412380e1eSArne Jansen 	if (vd->vdev_ops->vdev_op_leaf)
29512380e1eSArne Jansen 		return (1);
29612380e1eSArne Jansen 
29712380e1eSArne Jansen 	for (int c = 0; c < vd->vdev_children; c++)
29812380e1eSArne Jansen 		n += vdev_count_leaves_impl(vd->vdev_child[c]);
29912380e1eSArne Jansen 
30012380e1eSArne Jansen 	return (n);
30112380e1eSArne Jansen }
30212380e1eSArne Jansen 
30312380e1eSArne Jansen int
30412380e1eSArne Jansen vdev_count_leaves(spa_t *spa)
30512380e1eSArne Jansen {
30612380e1eSArne Jansen 	return (vdev_count_leaves_impl(spa->spa_root_vdev));
30712380e1eSArne Jansen }
30812380e1eSArne Jansen 
309fa9e4066Sahrens void
310fa9e4066Sahrens vdev_add_child(vdev_t *pvd, vdev_t *cvd)
311fa9e4066Sahrens {
312fa9e4066Sahrens 	size_t oldsize, newsize;
313fa9e4066Sahrens 	uint64_t id = cvd->vdev_id;
314fa9e4066Sahrens 	vdev_t **newchild;
31581cd5c55SMatthew Ahrens 	spa_t *spa = cvd->vdev_spa;
316fa9e4066Sahrens 
31781cd5c55SMatthew Ahrens 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
318fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == NULL);
319fa9e4066Sahrens 
320fa9e4066Sahrens 	cvd->vdev_parent = pvd;
321fa9e4066Sahrens 
322fa9e4066Sahrens 	if (pvd == NULL)
323fa9e4066Sahrens 		return;
324fa9e4066Sahrens 
325fa9e4066Sahrens 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
326fa9e4066Sahrens 
327fa9e4066Sahrens 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
328fa9e4066Sahrens 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
329fa9e4066Sahrens 	newsize = pvd->vdev_children * sizeof (vdev_t *);
330fa9e4066Sahrens 
331fa9e4066Sahrens 	newchild = kmem_zalloc(newsize, KM_SLEEP);
332fa9e4066Sahrens 	if (pvd->vdev_child != NULL) {
333fa9e4066Sahrens 		bcopy(pvd->vdev_child, newchild, oldsize);
334fa9e4066Sahrens 		kmem_free(pvd->vdev_child, oldsize);
335fa9e4066Sahrens 	}
336fa9e4066Sahrens 
337fa9e4066Sahrens 	pvd->vdev_child = newchild;
338fa9e4066Sahrens 	pvd->vdev_child[id] = cvd;
339fa9e4066Sahrens 
340fa9e4066Sahrens 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
341fa9e4066Sahrens 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
342fa9e4066Sahrens 
343fa9e4066Sahrens 	/*
344fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
345fa9e4066Sahrens 	 */
346fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
347fa9e4066Sahrens 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
348fa9e4066Sahrens }
349fa9e4066Sahrens 
350fa9e4066Sahrens void
351fa9e4066Sahrens vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
352fa9e4066Sahrens {
353fa9e4066Sahrens 	int c;
354fa9e4066Sahrens 	uint_t id = cvd->vdev_id;
355fa9e4066Sahrens 
356fa9e4066Sahrens 	ASSERT(cvd->vdev_parent == pvd);
357fa9e4066Sahrens 
358fa9e4066Sahrens 	if (pvd == NULL)
359fa9e4066Sahrens 		return;
360fa9e4066Sahrens 
361fa9e4066Sahrens 	ASSERT(id < pvd->vdev_children);
362fa9e4066Sahrens 	ASSERT(pvd->vdev_child[id] == cvd);
363fa9e4066Sahrens 
364fa9e4066Sahrens 	pvd->vdev_child[id] = NULL;
365fa9e4066Sahrens 	cvd->vdev_parent = NULL;
366fa9e4066Sahrens 
367fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++)
368fa9e4066Sahrens 		if (pvd->vdev_child[c])
369fa9e4066Sahrens 			break;
370fa9e4066Sahrens 
371fa9e4066Sahrens 	if (c == pvd->vdev_children) {
372fa9e4066Sahrens 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
373fa9e4066Sahrens 		pvd->vdev_child = NULL;
374fa9e4066Sahrens 		pvd->vdev_children = 0;
375fa9e4066Sahrens 	}
376fa9e4066Sahrens 
377fa9e4066Sahrens 	/*
378fa9e4066Sahrens 	 * Walk up all ancestors to update guid sum.
379fa9e4066Sahrens 	 */
380fa9e4066Sahrens 	for (; pvd != NULL; pvd = pvd->vdev_parent)
381fa9e4066Sahrens 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
382fa9e4066Sahrens }
383fa9e4066Sahrens 
384fa9e4066Sahrens /*
385fa9e4066Sahrens  * Remove any holes in the child array.
386fa9e4066Sahrens  */
387fa9e4066Sahrens void
388fa9e4066Sahrens vdev_compact_children(vdev_t *pvd)
389fa9e4066Sahrens {
390fa9e4066Sahrens 	vdev_t **newchild, *cvd;
391fa9e4066Sahrens 	int oldc = pvd->vdev_children;
392573ca77eSGeorge Wilson 	int newc;
393fa9e4066Sahrens 
394e14bb325SJeff Bonwick 	ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
395fa9e4066Sahrens 
396573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++)
397fa9e4066Sahrens 		if (pvd->vdev_child[c])
398fa9e4066Sahrens 			newc++;
399fa9e4066Sahrens 
400fa9e4066Sahrens 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
401fa9e4066Sahrens 
402573ca77eSGeorge Wilson 	for (int c = newc = 0; c < oldc; c++) {
403fa9e4066Sahrens 		if ((cvd = pvd->vdev_child[c]) != NULL) {
404fa9e4066Sahrens 			newchild[newc] = cvd;
405fa9e4066Sahrens 			cvd->vdev_id = newc++;
406fa9e4066Sahrens 		}
407fa9e4066Sahrens 	}
408fa9e4066Sahrens 
409fa9e4066Sahrens 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
410fa9e4066Sahrens 	pvd->vdev_child = newchild;
411fa9e4066Sahrens 	pvd->vdev_children = newc;
412fa9e4066Sahrens }
413fa9e4066Sahrens 
414fa9e4066Sahrens /*
415fa9e4066Sahrens  * Allocate and minimally initialize a vdev_t.
416fa9e4066Sahrens  */
41788ecc943SGeorge Wilson vdev_t *
418fa9e4066Sahrens vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	vdev_t *vd;
4215cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
422fa9e4066Sahrens 
423fa9e4066Sahrens 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
4245cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
425fa9e4066Sahrens 
4260e34b6a7Sbonwick 	if (spa->spa_root_vdev == NULL) {
4270e34b6a7Sbonwick 		ASSERT(ops == &vdev_root_ops);
4280e34b6a7Sbonwick 		spa->spa_root_vdev = vd;
429e9103aaeSGarrett D'Amore 		spa->spa_load_guid = spa_generate_guid(NULL);
4300e34b6a7Sbonwick 	}
4310e34b6a7Sbonwick 
43288ecc943SGeorge Wilson 	if (guid == 0 && ops != &vdev_hole_ops) {
4330e34b6a7Sbonwick 		if (spa->spa_root_vdev == vd) {
4340e34b6a7Sbonwick 			/*
4350e34b6a7Sbonwick 			 * The root vdev's guid will also be the pool guid,
4360e34b6a7Sbonwick 			 * which must be unique among all pools.
4370e34b6a7Sbonwick 			 */
4381195e687SMark J Musante 			guid = spa_generate_guid(NULL);
4390e34b6a7Sbonwick 		} else {
4400e34b6a7Sbonwick 			/*
4410e34b6a7Sbonwick 			 * Any other vdev's guid must be unique within the pool.
4420e34b6a7Sbonwick 			 */
4431195e687SMark J Musante 			guid = spa_generate_guid(spa);
4440e34b6a7Sbonwick 		}
4450e34b6a7Sbonwick 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
4460e34b6a7Sbonwick 	}
4470e34b6a7Sbonwick 
448fa9e4066Sahrens 	vd->vdev_spa = spa;
449fa9e4066Sahrens 	vd->vdev_id = id;
450fa9e4066Sahrens 	vd->vdev_guid = guid;
451fa9e4066Sahrens 	vd->vdev_guid_sum = guid;
452fa9e4066Sahrens 	vd->vdev_ops = ops;
453fa9e4066Sahrens 	vd->vdev_state = VDEV_STATE_CLOSED;
45488ecc943SGeorge Wilson 	vd->vdev_ishole = (ops == &vdev_hole_ops);
4555cabbc6bSPrashanth Sreenivasa 	vic->vic_prev_indirect_vdev = UINT64_MAX;
4565cabbc6bSPrashanth Sreenivasa 
4575cabbc6bSPrashanth Sreenivasa 	rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
4585cabbc6bSPrashanth Sreenivasa 	mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
4595cabbc6bSPrashanth Sreenivasa 	vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
460fa9e4066Sahrens 
461fa9e4066Sahrens 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
4625ad82045Snd 	mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
463e14bb325SJeff Bonwick 	mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
4640f7643c7SGeorge Wilson 	mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
465*094e47e9SGeorge Wilson 	mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
466*094e47e9SGeorge Wilson 	mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
467*094e47e9SGeorge Wilson 	cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
468*094e47e9SGeorge Wilson 	cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
469*094e47e9SGeorge Wilson 
4708ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
4715cabbc6bSPrashanth Sreenivasa 		vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
4728ad4d6ddSJeff Bonwick 	}
473b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_ms_list, spa,
474fa9e4066Sahrens 	    offsetof(struct metaslab, ms_txg_node));
475b7b2590dSMatthew Ahrens 	txg_list_create(&vd->vdev_dtl_list, spa,
476fa9e4066Sahrens 	    offsetof(struct vdev, vdev_dtl_node));
477fa9e4066Sahrens 	vd->vdev_stat.vs_timestamp = gethrtime();
4783d7072f8Seschrock 	vdev_queue_init(vd);
4793d7072f8Seschrock 	vdev_cache_init(vd);
480fa9e4066Sahrens 
481fa9e4066Sahrens 	return (vd);
482fa9e4066Sahrens }
483fa9e4066Sahrens 
484fa9e4066Sahrens /*
485fa9e4066Sahrens  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
486fa9e4066Sahrens  * creating a new vdev or loading an existing one - the behavior is slightly
487fa9e4066Sahrens  * different for each case.
488fa9e4066Sahrens  */
48999653d4eSeschrock int
49099653d4eSeschrock vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
49199653d4eSeschrock     int alloctype)
492fa9e4066Sahrens {
493fa9e4066Sahrens 	vdev_ops_t *ops;
494fa9e4066Sahrens 	char *type;
4958654d025Sperrin 	uint64_t guid = 0, islog, nparity;
496fa9e4066Sahrens 	vdev_t *vd;
4975cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic;
498fa9e4066Sahrens 
499e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
500fa9e4066Sahrens 
501fa9e4066Sahrens 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
502be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
503fa9e4066Sahrens 
504fa9e4066Sahrens 	if ((ops = vdev_getops(type)) == NULL)
505be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
506fa9e4066Sahrens 
507fa9e4066Sahrens 	/*
508fa9e4066Sahrens 	 * If this is a load, get the vdev guid from the nvlist.
509fa9e4066Sahrens 	 * Otherwise, vdev_alloc_common() will generate one for us.
510fa9e4066Sahrens 	 */
511fa9e4066Sahrens 	if (alloctype == VDEV_ALLOC_LOAD) {
512fa9e4066Sahrens 		uint64_t label_id;
513fa9e4066Sahrens 
514fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
515fa9e4066Sahrens 		    label_id != id)
516be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
517fa9e4066Sahrens 
518fa9e4066Sahrens 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
519be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
52099653d4eSeschrock 	} else if (alloctype == VDEV_ALLOC_SPARE) {
52199653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
522be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
523fa94a07fSbrendan 	} else if (alloctype == VDEV_ALLOC_L2CACHE) {
524fa94a07fSbrendan 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
525be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
52621ecdf64SLin Ling 	} else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
52721ecdf64SLin Ling 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
528be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
529fa9e4066Sahrens 	}
530fa9e4066Sahrens 
53199653d4eSeschrock 	/*
53299653d4eSeschrock 	 * The first allocated vdev must be of type 'root'.
53399653d4eSeschrock 	 */
53499653d4eSeschrock 	if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
535be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
53699653d4eSeschrock 
5378654d025Sperrin 	/*
5388654d025Sperrin 	 * Determine whether we're a log vdev.
5398654d025Sperrin 	 */
5408654d025Sperrin 	islog = 0;
5418654d025Sperrin 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
542990b4856Slling 	if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
543be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
544fa9e4066Sahrens 
54588ecc943SGeorge Wilson 	if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
546be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
54788ecc943SGeorge Wilson 
54899653d4eSeschrock 	/*
5498654d025Sperrin 	 * Set the nparity property for RAID-Z vdevs.
55099653d4eSeschrock 	 */
5518654d025Sperrin 	nparity = -1ULL;
55299653d4eSeschrock 	if (ops == &vdev_raidz_ops) {
55399653d4eSeschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
5548654d025Sperrin 		    &nparity) == 0) {
555b24ab676SJeff Bonwick 			if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
556be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
55799653d4eSeschrock 			/*
558f94275ceSAdam Leventhal 			 * Previous versions could only support 1 or 2 parity
559f94275ceSAdam Leventhal 			 * device.
56099653d4eSeschrock 			 */
561f94275ceSAdam Leventhal 			if (nparity > 1 &&
562f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ2)
563be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
564f94275ceSAdam Leventhal 			if (nparity > 2 &&
565f94275ceSAdam Leventhal 			    spa_version(spa) < SPA_VERSION_RAIDZ3)
566be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
56799653d4eSeschrock 		} else {
56899653d4eSeschrock 			/*
56999653d4eSeschrock 			 * We require the parity to be specified for SPAs that
57099653d4eSeschrock 			 * support multiple parity levels.
57199653d4eSeschrock 			 */
572f94275ceSAdam Leventhal 			if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
573be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
57499653d4eSeschrock 			/*
57599653d4eSeschrock 			 * Otherwise, we default to 1 parity device for RAID-Z.
57699653d4eSeschrock 			 */
5778654d025Sperrin 			nparity = 1;
57899653d4eSeschrock 		}
57999653d4eSeschrock 	} else {
5808654d025Sperrin 		nparity = 0;
58199653d4eSeschrock 	}
5828654d025Sperrin 	ASSERT(nparity != -1ULL);
5838654d025Sperrin 
5848654d025Sperrin 	vd = vdev_alloc_common(spa, id, guid, ops);
5855cabbc6bSPrashanth Sreenivasa 	vic = &vd->vdev_indirect_config;
5868654d025Sperrin 
5878654d025Sperrin 	vd->vdev_islog = islog;
5888654d025Sperrin 	vd->vdev_nparity = nparity;
5898654d025Sperrin 
5908654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
5918654d025Sperrin 		vd->vdev_path = spa_strdup(vd->vdev_path);
5928654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
5938654d025Sperrin 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
5948654d025Sperrin 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
5958654d025Sperrin 	    &vd->vdev_physpath) == 0)
5968654d025Sperrin 		vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
5976809eb4eSEric Schrock 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
5986809eb4eSEric Schrock 		vd->vdev_fru = spa_strdup(vd->vdev_fru);
59999653d4eSeschrock 
600afefbcddSeschrock 	/*
601afefbcddSeschrock 	 * Set the whole_disk property.  If it's not specified, leave the value
602afefbcddSeschrock 	 * as -1.
603afefbcddSeschrock 	 */
604afefbcddSeschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
605afefbcddSeschrock 	    &vd->vdev_wholedisk) != 0)
606afefbcddSeschrock 		vd->vdev_wholedisk = -1ULL;
607afefbcddSeschrock 
6085cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_mapping_object);
6095cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
6105cabbc6bSPrashanth Sreenivasa 	    &vic->vic_mapping_object);
6115cabbc6bSPrashanth Sreenivasa 	ASSERT0(vic->vic_births_object);
6125cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
6135cabbc6bSPrashanth Sreenivasa 	    &vic->vic_births_object);
6145cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
6155cabbc6bSPrashanth Sreenivasa 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
6165cabbc6bSPrashanth Sreenivasa 	    &vic->vic_prev_indirect_vdev);
6175cabbc6bSPrashanth Sreenivasa 
618ea8dc4b6Seschrock 	/*
619ea8dc4b6Seschrock 	 * Look for the 'not present' flag.  This will only be set if the device
620ea8dc4b6Seschrock 	 * was not present at the time of import.
621ea8dc4b6Seschrock 	 */
6226809eb4eSEric Schrock 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
6236809eb4eSEric Schrock 	    &vd->vdev_not_present);
624ea8dc4b6Seschrock 
625ecc2d604Sbonwick 	/*
626ecc2d604Sbonwick 	 * Get the alignment requirement.
627ecc2d604Sbonwick 	 */
628ecc2d604Sbonwick 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
629ecc2d604Sbonwick 
63088ecc943SGeorge Wilson 	/*
63188ecc943SGeorge Wilson 	 * Retrieve the vdev creation time.
63288ecc943SGeorge Wilson 	 */
63388ecc943SGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
63488ecc943SGeorge Wilson 	    &vd->vdev_crtxg);
63588ecc943SGeorge Wilson 
636fa9e4066Sahrens 	/*
637fa9e4066Sahrens 	 * If we're a top-level vdev, try to load the allocation parameters.
638fa9e4066Sahrens 	 */
6391195e687SMark J Musante 	if (parent && !parent->vdev_parent &&
6401195e687SMark J Musante 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
641fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
642fa9e4066Sahrens 		    &vd->vdev_ms_array);
643fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
644fa9e4066Sahrens 		    &vd->vdev_ms_shift);
645fa9e4066Sahrens 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
646fa9e4066Sahrens 		    &vd->vdev_asize);
6473f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
6483f9d6ad7SLin Ling 		    &vd->vdev_removing);
649215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
650215198a6SJoe Stein 		    &vd->vdev_top_zap);
651215198a6SJoe Stein 	} else {
652215198a6SJoe Stein 		ASSERT0(vd->vdev_top_zap);
653fa9e4066Sahrens 	}
654fa9e4066Sahrens 
655cd0837ccSGeorge Wilson 	if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
656a1521560SJeff Bonwick 		ASSERT(alloctype == VDEV_ALLOC_LOAD ||
6579f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ADD ||
6581195e687SMark J Musante 		    alloctype == VDEV_ALLOC_SPLIT ||
6599f4ab4d8SGeorge Wilson 		    alloctype == VDEV_ALLOC_ROOTPOOL);
660a1521560SJeff Bonwick 		vd->vdev_mg = metaslab_group_create(islog ?
661f78cdc34SPaul Dagnelie 		    spa_log_class(spa) : spa_normal_class(spa), vd,
662f78cdc34SPaul Dagnelie 		    spa->spa_alloc_count);
663a1521560SJeff Bonwick 	}
664a1521560SJeff Bonwick 
665215198a6SJoe Stein 	if (vd->vdev_ops->vdev_op_leaf &&
666215198a6SJoe Stein 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
667215198a6SJoe Stein 		(void) nvlist_lookup_uint64(nv,
668215198a6SJoe Stein 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
669215198a6SJoe Stein 	} else {
670215198a6SJoe Stein 		ASSERT0(vd->vdev_leaf_zap);
671215198a6SJoe Stein 	}
672215198a6SJoe Stein 
673fa9e4066Sahrens 	/*
6743d7072f8Seschrock 	 * If we're a leaf vdev, try to load the DTL object and other state.
675fa9e4066Sahrens 	 */
676215198a6SJoe Stein 
677c5904d13Seschrock 	if (vd->vdev_ops->vdev_op_leaf &&
67821ecdf64SLin Ling 	    (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
67921ecdf64SLin Ling 	    alloctype == VDEV_ALLOC_ROOTPOOL)) {
680c5904d13Seschrock 		if (alloctype == VDEV_ALLOC_LOAD) {
681c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
6820713e232SGeorge Wilson 			    &vd->vdev_dtl_object);
683c5904d13Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
684c5904d13Seschrock 			    &vd->vdev_unspare);
685c5904d13Seschrock 		}
68621ecdf64SLin Ling 
68721ecdf64SLin Ling 		if (alloctype == VDEV_ALLOC_ROOTPOOL) {
68821ecdf64SLin Ling 			uint64_t spare = 0;
68921ecdf64SLin Ling 
69021ecdf64SLin Ling 			if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
69121ecdf64SLin Ling 			    &spare) == 0 && spare)
69221ecdf64SLin Ling 				spa_spare_add(vd);
69321ecdf64SLin Ling 		}
69421ecdf64SLin Ling 
695ecc2d604Sbonwick 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
696ecc2d604Sbonwick 		    &vd->vdev_offline);
697c5904d13Seschrock 
698b4952e17SGeorge Wilson 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
699b4952e17SGeorge Wilson 		    &vd->vdev_resilver_txg);
700cb04b873SMark J Musante 
7013d7072f8Seschrock 		/*
7023d7072f8Seschrock 		 * When importing a pool, we want to ignore the persistent fault
7033d7072f8Seschrock 		 * state, as the diagnosis made on another system may not be
704069f55e2SEric Schrock 		 * valid in the current context.  Local vdevs will
705069f55e2SEric Schrock 		 * remain in the faulted state.
7063d7072f8Seschrock 		 */
707b16da2e2SGeorge Wilson 		if (spa_load_state(spa) == SPA_LOAD_OPEN) {
7083d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
7093d7072f8Seschrock 			    &vd->vdev_faulted);
7103d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
7113d7072f8Seschrock 			    &vd->vdev_degraded);
7123d7072f8Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
7133d7072f8Seschrock 			    &vd->vdev_removed);
714069f55e2SEric Schrock 
715069f55e2SEric Schrock 			if (vd->vdev_faulted || vd->vdev_degraded) {
716069f55e2SEric Schrock 				char *aux;
717069f55e2SEric Schrock 
718069f55e2SEric Schrock 				vd->vdev_label_aux =
719069f55e2SEric Schrock 				    VDEV_AUX_ERR_EXCEEDED;
720069f55e2SEric Schrock 				if (nvlist_lookup_string(nv,
721069f55e2SEric Schrock 				    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
722069f55e2SEric Schrock 				    strcmp(aux, "external") == 0)
723069f55e2SEric Schrock 					vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
724069f55e2SEric Schrock 			}
7253d7072f8Seschrock 		}
726fa9e4066Sahrens 	}
727fa9e4066Sahrens 
728fa9e4066Sahrens 	/*
729fa9e4066Sahrens 	 * Add ourselves to the parent's list of children.
730fa9e4066Sahrens 	 */
731fa9e4066Sahrens 	vdev_add_child(parent, vd);
732fa9e4066Sahrens 
73399653d4eSeschrock 	*vdp = vd;
73499653d4eSeschrock 
73599653d4eSeschrock 	return (0);
736fa9e4066Sahrens }
737fa9e4066Sahrens 
738fa9e4066Sahrens void
739fa9e4066Sahrens vdev_free(vdev_t *vd)
740fa9e4066Sahrens {
7413d7072f8Seschrock 	spa_t *spa = vd->vdev_spa;
742*094e47e9SGeorge Wilson 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
743fa9e4066Sahrens 
744fa9e4066Sahrens 	/*
745fa9e4066Sahrens 	 * vdev_free() implies closing the vdev first.  This is simpler than
746fa9e4066Sahrens 	 * trying to ensure complicated semantics for all callers.
747fa9e4066Sahrens 	 */
748fa9e4066Sahrens 	vdev_close(vd);
749fa9e4066Sahrens 
750e14bb325SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
751b24ab676SJeff Bonwick 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
752fa9e4066Sahrens 
753fa9e4066Sahrens 	/*
754fa9e4066Sahrens 	 * Free all children.
755fa9e4066Sahrens 	 */
756573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
757fa9e4066Sahrens 		vdev_free(vd->vdev_child[c]);
758fa9e4066Sahrens 
759fa9e4066Sahrens 	ASSERT(vd->vdev_child == NULL);
760fa9e4066Sahrens 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
761*094e47e9SGeorge Wilson 	ASSERT(vd->vdev_initialize_thread == NULL);
762fa9e4066Sahrens 
763fa9e4066Sahrens 	/*
764fa9e4066Sahrens 	 * Discard allocation state.
765fa9e4066Sahrens 	 */
766a1521560SJeff Bonwick 	if (vd->vdev_mg != NULL) {
767fa9e4066Sahrens 		vdev_metaslab_fini(vd);
768a1521560SJeff Bonwick 		metaslab_group_destroy(vd->vdev_mg);
769a1521560SJeff Bonwick 	}
770fa9e4066Sahrens 
771fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_space);
772fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_dspace);
773fb09f5aaSMadhav Suresh 	ASSERT0(vd->vdev_stat.vs_alloc);
774fa9e4066Sahrens 
775fa9e4066Sahrens 	/*
776fa9e4066Sahrens 	 * Remove this vdev from its parent's child list.
777fa9e4066Sahrens 	 */
778fa9e4066Sahrens 	vdev_remove_child(vd->vdev_parent, vd);
779fa9e4066Sahrens 
780fa9e4066Sahrens 	ASSERT(vd->vdev_parent == NULL);
781fa9e4066Sahrens 
7823d7072f8Seschrock 	/*
7833d7072f8Seschrock 	 * Clean up vdev structure.
7843d7072f8Seschrock 	 */
7853d7072f8Seschrock 	vdev_queue_fini(vd);
7863d7072f8Seschrock 	vdev_cache_fini(vd);
7873d7072f8Seschrock 
7883d7072f8Seschrock 	if (vd->vdev_path)
7893d7072f8Seschrock 		spa_strfree(vd->vdev_path);
7903d7072f8Seschrock 	if (vd->vdev_devid)
7913d7072f8Seschrock 		spa_strfree(vd->vdev_devid);
7923d7072f8Seschrock 	if (vd->vdev_physpath)
7933d7072f8Seschrock 		spa_strfree(vd->vdev_physpath);
7946809eb4eSEric Schrock 	if (vd->vdev_fru)
7956809eb4eSEric Schrock 		spa_strfree(vd->vdev_fru);
7963d7072f8Seschrock 
7973d7072f8Seschrock 	if (vd->vdev_isspare)
7983d7072f8Seschrock 		spa_spare_remove(vd);
799fa94a07fSbrendan 	if (vd->vdev_isl2cache)
800fa94a07fSbrendan 		spa_l2cache_remove(vd);
8013d7072f8Seschrock 
8023d7072f8Seschrock 	txg_list_destroy(&vd->vdev_ms_list);
8033d7072f8Seschrock 	txg_list_destroy(&vd->vdev_dtl_list);
8048ad4d6ddSJeff Bonwick 
8053d7072f8Seschrock 	mutex_enter(&vd->vdev_dtl_lock);
8060713e232SGeorge Wilson 	space_map_close(vd->vdev_dtl_sm);
8078ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
8080713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
8090713e232SGeorge Wilson 		range_tree_destroy(vd->vdev_dtl[t]);
8108ad4d6ddSJeff Bonwick 	}
8113d7072f8Seschrock 	mutex_exit(&vd->vdev_dtl_lock);
8128ad4d6ddSJeff Bonwick 
8135cabbc6bSPrashanth Sreenivasa 	EQUIV(vd->vdev_indirect_births != NULL,
8145cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping != NULL);
8155cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_indirect_births != NULL) {
8165cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
8175cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_close(vd->vdev_indirect_births);
8185cabbc6bSPrashanth Sreenivasa 	}
8195cabbc6bSPrashanth Sreenivasa 
8205cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
8215cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
8225cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
8235cabbc6bSPrashanth Sreenivasa 		space_map_close(vd->vdev_obsolete_sm);
8245cabbc6bSPrashanth Sreenivasa 		vd->vdev_obsolete_sm = NULL;
8255cabbc6bSPrashanth Sreenivasa 	}
8265cabbc6bSPrashanth Sreenivasa 	range_tree_destroy(vd->vdev_obsolete_segments);
8275cabbc6bSPrashanth Sreenivasa 	rw_destroy(&vd->vdev_indirect_rwlock);
8285cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&vd->vdev_obsolete_lock);
8295cabbc6bSPrashanth Sreenivasa 
8300f7643c7SGeorge Wilson 	mutex_destroy(&vd->vdev_queue_lock);
8313d7072f8Seschrock 	mutex_destroy(&vd->vdev_dtl_lock);
8323d7072f8Seschrock 	mutex_destroy(&vd->vdev_stat_lock);
833e14bb325SJeff Bonwick 	mutex_destroy(&vd->vdev_probe_lock);
834*094e47e9SGeorge Wilson 	mutex_destroy(&vd->vdev_initialize_lock);
835*094e47e9SGeorge Wilson 	mutex_destroy(&vd->vdev_initialize_io_lock);
836*094e47e9SGeorge Wilson 	cv_destroy(&vd->vdev_initialize_io_cv);
837*094e47e9SGeorge Wilson 	cv_destroy(&vd->vdev_initialize_cv);
8383d7072f8Seschrock 
8393d7072f8Seschrock 	if (vd == spa->spa_root_vdev)
8403d7072f8Seschrock 		spa->spa_root_vdev = NULL;
8413d7072f8Seschrock 
8423d7072f8Seschrock 	kmem_free(vd, sizeof (vdev_t));
843fa9e4066Sahrens }
844fa9e4066Sahrens 
845fa9e4066Sahrens /*
846fa9e4066Sahrens  * Transfer top-level vdev state from svd to tvd.
847fa9e4066Sahrens  */
848fa9e4066Sahrens static void
849fa9e4066Sahrens vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
850fa9e4066Sahrens {
851fa9e4066Sahrens 	spa_t *spa = svd->vdev_spa;
852fa9e4066Sahrens 	metaslab_t *msp;
853fa9e4066Sahrens 	vdev_t *vd;
854fa9e4066Sahrens 	int t;
855fa9e4066Sahrens 
856fa9e4066Sahrens 	ASSERT(tvd == tvd->vdev_top);
857fa9e4066Sahrens 
858fa9e4066Sahrens 	tvd->vdev_ms_array = svd->vdev_ms_array;
859fa9e4066Sahrens 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
860fa9e4066Sahrens 	tvd->vdev_ms_count = svd->vdev_ms_count;
861215198a6SJoe Stein 	tvd->vdev_top_zap = svd->vdev_top_zap;
862fa9e4066Sahrens 
863fa9e4066Sahrens 	svd->vdev_ms_array = 0;
864fa9e4066Sahrens 	svd->vdev_ms_shift = 0;
865fa9e4066Sahrens 	svd->vdev_ms_count = 0;
866215198a6SJoe Stein 	svd->vdev_top_zap = 0;
867fa9e4066Sahrens 
868cd0837ccSGeorge Wilson 	if (tvd->vdev_mg)
869cd0837ccSGeorge Wilson 		ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
870fa9e4066Sahrens 	tvd->vdev_mg = svd->vdev_mg;
871fa9e4066Sahrens 	tvd->vdev_ms = svd->vdev_ms;
872fa9e4066Sahrens 
873fa9e4066Sahrens 	svd->vdev_mg = NULL;
874fa9e4066Sahrens 	svd->vdev_ms = NULL;
875ecc2d604Sbonwick 
876ecc2d604Sbonwick 	if (tvd->vdev_mg != NULL)
877ecc2d604Sbonwick 		tvd->vdev_mg->mg_vd = tvd;
878fa9e4066Sahrens 
87986714001SSerapheim Dimitropoulos 	tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
88086714001SSerapheim Dimitropoulos 	svd->vdev_checkpoint_sm = NULL;
88186714001SSerapheim Dimitropoulos 
882fa9e4066Sahrens 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
883fa9e4066Sahrens 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
88499653d4eSeschrock 	tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
885fa9e4066Sahrens 
886fa9e4066Sahrens 	svd->vdev_stat.vs_alloc = 0;
887fa9e4066Sahrens 	svd->vdev_stat.vs_space = 0;
88899653d4eSeschrock 	svd->vdev_stat.vs_dspace = 0;
889fa9e4066Sahrens 
8903a4b1be9SMatthew Ahrens 	/*
8913a4b1be9SMatthew Ahrens 	 * State which may be set on a top-level vdev that's in the
8923a4b1be9SMatthew Ahrens 	 * process of being removed.
8933a4b1be9SMatthew Ahrens 	 */
8943a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_indirect_config.vic_births_object);
8953a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
8963a4b1be9SMatthew Ahrens 	ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
8973a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
8983a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
8993a4b1be9SMatthew Ahrens 	ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
9003a4b1be9SMatthew Ahrens 	ASSERT0(tvd->vdev_removing);
9013a4b1be9SMatthew Ahrens 	tvd->vdev_removing = svd->vdev_removing;
9023a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_config = svd->vdev_indirect_config;
9033a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
9043a4b1be9SMatthew Ahrens 	tvd->vdev_indirect_births = svd->vdev_indirect_births;
9053a4b1be9SMatthew Ahrens 	range_tree_swap(&svd->vdev_obsolete_segments,
9063a4b1be9SMatthew Ahrens 	    &tvd->vdev_obsolete_segments);
9073a4b1be9SMatthew Ahrens 	tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
9083a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_mapping_object = 0;
9093a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_births_object = 0;
9103a4b1be9SMatthew Ahrens 	svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
9113a4b1be9SMatthew Ahrens 	svd->vdev_indirect_mapping = NULL;
9123a4b1be9SMatthew Ahrens 	svd->vdev_indirect_births = NULL;
9133a4b1be9SMatthew Ahrens 	svd->vdev_obsolete_sm = NULL;
9143a4b1be9SMatthew Ahrens 	svd->vdev_removing = 0;
9153a4b1be9SMatthew Ahrens 
916fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
917fa9e4066Sahrens 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
918fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
919fa9e4066Sahrens 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
920fa9e4066Sahrens 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
921fa9e4066Sahrens 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
922fa9e4066Sahrens 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
923fa9e4066Sahrens 	}
924fa9e4066Sahrens 
925e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_config_dirty_node)) {
926fa9e4066Sahrens 		vdev_config_clean(svd);
927fa9e4066Sahrens 		vdev_config_dirty(tvd);
928fa9e4066Sahrens 	}
929fa9e4066Sahrens 
930e14bb325SJeff Bonwick 	if (list_link_active(&svd->vdev_state_dirty_node)) {
931e14bb325SJeff Bonwick 		vdev_state_clean(svd);
932e14bb325SJeff Bonwick 		vdev_state_dirty(tvd);
933e14bb325SJeff Bonwick 	}
934e14bb325SJeff Bonwick 
93599653d4eSeschrock 	tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
93699653d4eSeschrock 	svd->vdev_deflate_ratio = 0;
9378654d025Sperrin 
9388654d025Sperrin 	tvd->vdev_islog = svd->vdev_islog;
9398654d025Sperrin 	svd->vdev_islog = 0;
940fa9e4066Sahrens }
941fa9e4066Sahrens 
942fa9e4066Sahrens static void
943fa9e4066Sahrens vdev_top_update(vdev_t *tvd, vdev_t *vd)
944fa9e4066Sahrens {
945fa9e4066Sahrens 	if (vd == NULL)
946fa9e4066Sahrens 		return;
947fa9e4066Sahrens 
948fa9e4066Sahrens 	vd->vdev_top = tvd;
949fa9e4066Sahrens 
950573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
951fa9e4066Sahrens 		vdev_top_update(tvd, vd->vdev_child[c]);
952fa9e4066Sahrens }
953fa9e4066Sahrens 
954fa9e4066Sahrens /*
955fa9e4066Sahrens  * Add a mirror/replacing vdev above an existing vdev.
956fa9e4066Sahrens  */
957fa9e4066Sahrens vdev_t *
958fa9e4066Sahrens vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
959fa9e4066Sahrens {
960fa9e4066Sahrens 	spa_t *spa = cvd->vdev_spa;
961fa9e4066Sahrens 	vdev_t *pvd = cvd->vdev_parent;
962fa9e4066Sahrens 	vdev_t *mvd;
963fa9e4066Sahrens 
964e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
965fa9e4066Sahrens 
966fa9e4066Sahrens 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
967ecc2d604Sbonwick 
968ecc2d604Sbonwick 	mvd->vdev_asize = cvd->vdev_asize;
969573ca77eSGeorge Wilson 	mvd->vdev_min_asize = cvd->vdev_min_asize;
9704263d13fSGeorge Wilson 	mvd->vdev_max_asize = cvd->vdev_max_asize;
9715cabbc6bSPrashanth Sreenivasa 	mvd->vdev_psize = cvd->vdev_psize;
972ecc2d604Sbonwick 	mvd->vdev_ashift = cvd->vdev_ashift;
973ecc2d604Sbonwick 	mvd->vdev_state = cvd->vdev_state;
97488ecc943SGeorge Wilson 	mvd->vdev_crtxg = cvd->vdev_crtxg;
975ecc2d604Sbonwick 
976fa9e4066Sahrens 	vdev_remove_child(pvd, cvd);
977fa9e4066Sahrens 	vdev_add_child(pvd, mvd);
978fa9e4066Sahrens 	cvd->vdev_id = mvd->vdev_children;
979fa9e4066Sahrens 	vdev_add_child(mvd, cvd);
980fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
981fa9e4066Sahrens 
982fa9e4066Sahrens 	if (mvd == mvd->vdev_top)
983fa9e4066Sahrens 		vdev_top_transfer(cvd, mvd);
984fa9e4066Sahrens 
985fa9e4066Sahrens 	return (mvd);
986fa9e4066Sahrens }
987fa9e4066Sahrens 
988fa9e4066Sahrens /*
989fa9e4066Sahrens  * Remove a 1-way mirror/replacing vdev from the tree.
990fa9e4066Sahrens  */
991fa9e4066Sahrens void
992fa9e4066Sahrens vdev_remove_parent(vdev_t *cvd)
993fa9e4066Sahrens {
994fa9e4066Sahrens 	vdev_t *mvd = cvd->vdev_parent;
995fa9e4066Sahrens 	vdev_t *pvd = mvd->vdev_parent;
996fa9e4066Sahrens 
997e14bb325SJeff Bonwick 	ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
998fa9e4066Sahrens 
999fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 1);
1000fa9e4066Sahrens 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
100199653d4eSeschrock 	    mvd->vdev_ops == &vdev_replacing_ops ||
100299653d4eSeschrock 	    mvd->vdev_ops == &vdev_spare_ops);
1003ecc2d604Sbonwick 	cvd->vdev_ashift = mvd->vdev_ashift;
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	vdev_remove_child(mvd, cvd);
1006fa9e4066Sahrens 	vdev_remove_child(pvd, mvd);
10078ad4d6ddSJeff Bonwick 
100899653d4eSeschrock 	/*
1009e14bb325SJeff Bonwick 	 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1010e14bb325SJeff Bonwick 	 * Otherwise, we could have detached an offline device, and when we
1011e14bb325SJeff Bonwick 	 * go to import the pool we'll think we have two top-level vdevs,
1012e14bb325SJeff Bonwick 	 * instead of a different version of the same top-level vdev.
101399653d4eSeschrock 	 */
10148ad4d6ddSJeff Bonwick 	if (mvd->vdev_top == mvd) {
10158ad4d6ddSJeff Bonwick 		uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
10161195e687SMark J Musante 		cvd->vdev_orig_guid = cvd->vdev_guid;
10178ad4d6ddSJeff Bonwick 		cvd->vdev_guid += guid_delta;
10188ad4d6ddSJeff Bonwick 		cvd->vdev_guid_sum += guid_delta;
10198ad4d6ddSJeff Bonwick 	}
1020e14bb325SJeff Bonwick 	cvd->vdev_id = mvd->vdev_id;
1021e14bb325SJeff Bonwick 	vdev_add_child(pvd, cvd);
1022fa9e4066Sahrens 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1023fa9e4066Sahrens 
1024fa9e4066Sahrens 	if (cvd == cvd->vdev_top)
1025fa9e4066Sahrens 		vdev_top_transfer(mvd, cvd);
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	ASSERT(mvd->vdev_children == 0);
1028fa9e4066Sahrens 	vdev_free(mvd);
1029fa9e4066Sahrens }
1030fa9e4066Sahrens 
1031ea8dc4b6Seschrock int
1032fa9e4066Sahrens vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1033fa9e4066Sahrens {
1034fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
1035ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
1036ecc2d604Sbonwick 	uint64_t m;
1037fa9e4066Sahrens 	uint64_t oldc = vd->vdev_ms_count;
1038fa9e4066Sahrens 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1039ecc2d604Sbonwick 	metaslab_t **mspp;
1040ecc2d604Sbonwick 	int error;
1041fa9e4066Sahrens 
1042a1521560SJeff Bonwick 	ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1043a1521560SJeff Bonwick 
104488ecc943SGeorge Wilson 	/*
104588ecc943SGeorge Wilson 	 * This vdev is not being allocated from yet or is a hole.
104688ecc943SGeorge Wilson 	 */
104788ecc943SGeorge Wilson 	if (vd->vdev_ms_shift == 0)
10480e34b6a7Sbonwick 		return (0);
10490e34b6a7Sbonwick 
105088ecc943SGeorge Wilson 	ASSERT(!vd->vdev_ishole);
105188ecc943SGeorge Wilson 
1052fa9e4066Sahrens 	ASSERT(oldc <= newc);
1053fa9e4066Sahrens 
1054ecc2d604Sbonwick 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1055fa9e4066Sahrens 
1056ecc2d604Sbonwick 	if (oldc != 0) {
1057ecc2d604Sbonwick 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1058ecc2d604Sbonwick 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1059ecc2d604Sbonwick 	}
1060fa9e4066Sahrens 
1061ecc2d604Sbonwick 	vd->vdev_ms = mspp;
1062ecc2d604Sbonwick 	vd->vdev_ms_count = newc;
1063ecc2d604Sbonwick 	for (m = oldc; m < newc; m++) {
10640713e232SGeorge Wilson 		uint64_t object = 0;
10650713e232SGeorge Wilson 
10665cabbc6bSPrashanth Sreenivasa 		/*
10675cabbc6bSPrashanth Sreenivasa 		 * vdev_ms_array may be 0 if we are creating the "fake"
10685cabbc6bSPrashanth Sreenivasa 		 * metaslabs for an indirect vdev for zdb's leak detection.
10695cabbc6bSPrashanth Sreenivasa 		 * See zdb_leak_init().
10705cabbc6bSPrashanth Sreenivasa 		 */
10715cabbc6bSPrashanth Sreenivasa 		if (txg == 0 && vd->vdev_ms_array != 0) {
1072ecc2d604Sbonwick 			error = dmu_read(mos, vd->vdev_ms_array,
10737bfdf011SNeil Perrin 			    m * sizeof (uint64_t), sizeof (uint64_t), &object,
10747bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
10753ee8c80cSPavel Zakharov 			if (error != 0) {
10763ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "unable to read the metaslab "
10773ee8c80cSPavel Zakharov 				    "array [error=%d]", error);
1078ecc2d604Sbonwick 				return (error);
10793ee8c80cSPavel Zakharov 			}
1080fa9e4066Sahrens 		}
10811e9bd7ecSPrakash Surya 
10821e9bd7ecSPrakash Surya 		error = metaslab_init(vd->vdev_mg, m, object, txg,
10831e9bd7ecSPrakash Surya 		    &(vd->vdev_ms[m]));
10843ee8c80cSPavel Zakharov 		if (error != 0) {
10853ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
10863ee8c80cSPavel Zakharov 			    error);
10871e9bd7ecSPrakash Surya 			return (error);
10883ee8c80cSPavel Zakharov 		}
1089fa9e4066Sahrens 	}
1090fa9e4066Sahrens 
1091a1521560SJeff Bonwick 	if (txg == 0)
1092a1521560SJeff Bonwick 		spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1093a1521560SJeff Bonwick 
10943f9d6ad7SLin Ling 	/*
10953f9d6ad7SLin Ling 	 * If the vdev is being removed we don't activate
10963f9d6ad7SLin Ling 	 * the metaslabs since we want to ensure that no new
10973f9d6ad7SLin Ling 	 * allocations are performed on this device.
10983f9d6ad7SLin Ling 	 */
10993f9d6ad7SLin Ling 	if (oldc == 0 && !vd->vdev_removing)
1100a1521560SJeff Bonwick 		metaslab_group_activate(vd->vdev_mg);
1101a1521560SJeff Bonwick 
1102a1521560SJeff Bonwick 	if (txg == 0)
1103a1521560SJeff Bonwick 		spa_config_exit(spa, SCL_ALLOC, FTAG);
1104a1521560SJeff Bonwick 
1105ea8dc4b6Seschrock 	return (0);
1106fa9e4066Sahrens }
1107fa9e4066Sahrens 
1108fa9e4066Sahrens void
1109fa9e4066Sahrens vdev_metaslab_fini(vdev_t *vd)
1110fa9e4066Sahrens {
111186714001SSerapheim Dimitropoulos 	if (vd->vdev_checkpoint_sm != NULL) {
111286714001SSerapheim Dimitropoulos 		ASSERT(spa_feature_is_active(vd->vdev_spa,
111386714001SSerapheim Dimitropoulos 		    SPA_FEATURE_POOL_CHECKPOINT));
111486714001SSerapheim Dimitropoulos 		space_map_close(vd->vdev_checkpoint_sm);
111586714001SSerapheim Dimitropoulos 		/*
111686714001SSerapheim Dimitropoulos 		 * Even though we close the space map, we need to set its
111786714001SSerapheim Dimitropoulos 		 * pointer to NULL. The reason is that vdev_metaslab_fini()
111886714001SSerapheim Dimitropoulos 		 * may be called multiple times for certain operations
111986714001SSerapheim Dimitropoulos 		 * (i.e. when destroying a pool) so we need to ensure that
112086714001SSerapheim Dimitropoulos 		 * this clause never executes twice. This logic is similar
112186714001SSerapheim Dimitropoulos 		 * to the one used for the vdev_ms clause below.
112286714001SSerapheim Dimitropoulos 		 */
112386714001SSerapheim Dimitropoulos 		vd->vdev_checkpoint_sm = NULL;
112486714001SSerapheim Dimitropoulos 	}
112586714001SSerapheim Dimitropoulos 
1126fa9e4066Sahrens 	if (vd->vdev_ms != NULL) {
11275cabbc6bSPrashanth Sreenivasa 		uint64_t count = vd->vdev_ms_count;
11285cabbc6bSPrashanth Sreenivasa 
1129a1521560SJeff Bonwick 		metaslab_group_passivate(vd->vdev_mg);
11305cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < count; m++) {
11310713e232SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
11320713e232SGeorge Wilson 
11330713e232SGeorge Wilson 			if (msp != NULL)
11340713e232SGeorge Wilson 				metaslab_fini(msp);
11350713e232SGeorge Wilson 		}
1136fa9e4066Sahrens 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1137fa9e4066Sahrens 		vd->vdev_ms = NULL;
11385cabbc6bSPrashanth Sreenivasa 
11395cabbc6bSPrashanth Sreenivasa 		vd->vdev_ms_count = 0;
1140fa9e4066Sahrens 	}
11415cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_ms_count);
1142fa9e4066Sahrens }
1143fa9e4066Sahrens 
1144e14bb325SJeff Bonwick typedef struct vdev_probe_stats {
1145e14bb325SJeff Bonwick 	boolean_t	vps_readable;
1146e14bb325SJeff Bonwick 	boolean_t	vps_writeable;
1147e14bb325SJeff Bonwick 	int		vps_flags;
1148e14bb325SJeff Bonwick } vdev_probe_stats_t;
1149e14bb325SJeff Bonwick 
1150e14bb325SJeff Bonwick static void
1151e14bb325SJeff Bonwick vdev_probe_done(zio_t *zio)
11520a4e9518Sgw {
11538ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
1154a3f829aeSBill Moore 	vdev_t *vd = zio->io_vd;
1155e14bb325SJeff Bonwick 	vdev_probe_stats_t *vps = zio->io_private;
1156a3f829aeSBill Moore 
1157a3f829aeSBill Moore 	ASSERT(vd->vdev_probe_zio != NULL);
1158e14bb325SJeff Bonwick 
1159e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ) {
1160e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1161e14bb325SJeff Bonwick 			vps->vps_readable = 1;
11628ad4d6ddSJeff Bonwick 		if (zio->io_error == 0 && spa_writeable(spa)) {
1163a3f829aeSBill Moore 			zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1164770499e1SDan Kimmel 			    zio->io_offset, zio->io_size, zio->io_abd,
1165e14bb325SJeff Bonwick 			    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1166e14bb325SJeff Bonwick 			    ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1167e14bb325SJeff Bonwick 		} else {
1168770499e1SDan Kimmel 			abd_free(zio->io_abd);
1169e14bb325SJeff Bonwick 		}
1170e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
1171e14bb325SJeff Bonwick 		if (zio->io_error == 0)
1172e14bb325SJeff Bonwick 			vps->vps_writeable = 1;
1173770499e1SDan Kimmel 		abd_free(zio->io_abd);
1174e14bb325SJeff Bonwick 	} else if (zio->io_type == ZIO_TYPE_NULL) {
1175a3f829aeSBill Moore 		zio_t *pio;
1176e14bb325SJeff Bonwick 
1177e14bb325SJeff Bonwick 		vd->vdev_cant_read |= !vps->vps_readable;
1178e14bb325SJeff Bonwick 		vd->vdev_cant_write |= !vps->vps_writeable;
1179e14bb325SJeff Bonwick 
1180e14bb325SJeff Bonwick 		if (vdev_readable(vd) &&
11818ad4d6ddSJeff Bonwick 		    (vdev_writeable(vd) || !spa_writeable(spa))) {
1182e14bb325SJeff Bonwick 			zio->io_error = 0;
1183e14bb325SJeff Bonwick 		} else {
1184e14bb325SJeff Bonwick 			ASSERT(zio->io_error != 0);
11853ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "failed probe");
1186e14bb325SJeff Bonwick 			zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
11878ad4d6ddSJeff Bonwick 			    spa, vd, NULL, 0, 0);
1188be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
1189e14bb325SJeff Bonwick 		}
1190a3f829aeSBill Moore 
1191a3f829aeSBill Moore 		mutex_enter(&vd->vdev_probe_lock);
1192a3f829aeSBill Moore 		ASSERT(vd->vdev_probe_zio == zio);
1193a3f829aeSBill Moore 		vd->vdev_probe_zio = NULL;
1194a3f829aeSBill Moore 		mutex_exit(&vd->vdev_probe_lock);
1195a3f829aeSBill Moore 
11960f7643c7SGeorge Wilson 		zio_link_t *zl = NULL;
11970f7643c7SGeorge Wilson 		while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1198a3f829aeSBill Moore 			if (!vdev_accessible(vd, pio))
1199be6fd75aSMatthew Ahrens 				pio->io_error = SET_ERROR(ENXIO);
1200a3f829aeSBill Moore 
1201e14bb325SJeff Bonwick 		kmem_free(vps, sizeof (*vps));
1202e14bb325SJeff Bonwick 	}
1203e14bb325SJeff Bonwick }
12040a4e9518Sgw 
1205e14bb325SJeff Bonwick /*
1206f7170741SWill Andrews  * Determine whether this device is accessible.
1207f7170741SWill Andrews  *
1208f7170741SWill Andrews  * Read and write to several known locations: the pad regions of each
1209f7170741SWill Andrews  * vdev label but the first, which we leave alone in case it contains
1210f7170741SWill Andrews  * a VTOC.
1211e14bb325SJeff Bonwick  */
1212e14bb325SJeff Bonwick zio_t *
1213a3f829aeSBill Moore vdev_probe(vdev_t *vd, zio_t *zio)
1214e14bb325SJeff Bonwick {
1215e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1216a3f829aeSBill Moore 	vdev_probe_stats_t *vps = NULL;
1217a3f829aeSBill Moore 	zio_t *pio;
1218a3f829aeSBill Moore 
1219a3f829aeSBill Moore 	ASSERT(vd->vdev_ops->vdev_op_leaf);
12200a4e9518Sgw 
1221a3f829aeSBill Moore 	/*
1222a3f829aeSBill Moore 	 * Don't probe the probe.
1223a3f829aeSBill Moore 	 */
1224a3f829aeSBill Moore 	if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1225a3f829aeSBill Moore 		return (NULL);
1226e14bb325SJeff Bonwick 
1227a3f829aeSBill Moore 	/*
1228a3f829aeSBill Moore 	 * To prevent 'probe storms' when a device fails, we create
1229a3f829aeSBill Moore 	 * just one probe i/o at a time.  All zios that want to probe
1230a3f829aeSBill Moore 	 * this vdev will become parents of the probe io.
1231a3f829aeSBill Moore 	 */
1232a3f829aeSBill Moore 	mutex_enter(&vd->vdev_probe_lock);
1233e14bb325SJeff Bonwick 
1234a3f829aeSBill Moore 	if ((pio = vd->vdev_probe_zio) == NULL) {
1235a3f829aeSBill Moore 		vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1236a3f829aeSBill Moore 
1237a3f829aeSBill Moore 		vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1238a3f829aeSBill Moore 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
12398956713aSEric Schrock 		    ZIO_FLAG_TRYHARD;
1240a3f829aeSBill Moore 
1241a3f829aeSBill Moore 		if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1242a3f829aeSBill Moore 			/*
1243a3f829aeSBill Moore 			 * vdev_cant_read and vdev_cant_write can only
1244a3f829aeSBill Moore 			 * transition from TRUE to FALSE when we have the
1245a3f829aeSBill Moore 			 * SCL_ZIO lock as writer; otherwise they can only
1246a3f829aeSBill Moore 			 * transition from FALSE to TRUE.  This ensures that
1247a3f829aeSBill Moore 			 * any zio looking at these values can assume that
1248a3f829aeSBill Moore 			 * failures persist for the life of the I/O.  That's
1249a3f829aeSBill Moore 			 * important because when a device has intermittent
1250a3f829aeSBill Moore 			 * connectivity problems, we want to ensure that
1251a3f829aeSBill Moore 			 * they're ascribed to the device (ENXIO) and not
1252a3f829aeSBill Moore 			 * the zio (EIO).
1253a3f829aeSBill Moore 			 *
1254a3f829aeSBill Moore 			 * Since we hold SCL_ZIO as writer here, clear both
1255a3f829aeSBill Moore 			 * values so the probe can reevaluate from first
1256a3f829aeSBill Moore 			 * principles.
1257a3f829aeSBill Moore 			 */
1258a3f829aeSBill Moore 			vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1259a3f829aeSBill Moore 			vd->vdev_cant_read = B_FALSE;
1260a3f829aeSBill Moore 			vd->vdev_cant_write = B_FALSE;
1261a3f829aeSBill Moore 		}
1262a3f829aeSBill Moore 
1263a3f829aeSBill Moore 		vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1264a3f829aeSBill Moore 		    vdev_probe_done, vps,
1265a3f829aeSBill Moore 		    vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1266a3f829aeSBill Moore 
126798d1cbfeSGeorge Wilson 		/*
126898d1cbfeSGeorge Wilson 		 * We can't change the vdev state in this context, so we
126998d1cbfeSGeorge Wilson 		 * kick off an async task to do it on our behalf.
127098d1cbfeSGeorge Wilson 		 */
1271a3f829aeSBill Moore 		if (zio != NULL) {
1272a3f829aeSBill Moore 			vd->vdev_probe_wanted = B_TRUE;
1273a3f829aeSBill Moore 			spa_async_request(spa, SPA_ASYNC_PROBE);
1274a3f829aeSBill Moore 		}
1275e14bb325SJeff Bonwick 	}
1276e14bb325SJeff Bonwick 
1277a3f829aeSBill Moore 	if (zio != NULL)
1278a3f829aeSBill Moore 		zio_add_child(zio, pio);
1279e14bb325SJeff Bonwick 
1280a3f829aeSBill Moore 	mutex_exit(&vd->vdev_probe_lock);
1281e14bb325SJeff Bonwick 
1282a3f829aeSBill Moore 	if (vps == NULL) {
1283a3f829aeSBill Moore 		ASSERT(zio != NULL);
1284a3f829aeSBill Moore 		return (NULL);
1285a3f829aeSBill Moore 	}
1286e14bb325SJeff Bonwick 
1287e14bb325SJeff Bonwick 	for (int l = 1; l < VDEV_LABELS; l++) {
1288a3f829aeSBill Moore 		zio_nowait(zio_read_phys(pio, vd,
1289e14bb325SJeff Bonwick 		    vdev_label_offset(vd->vdev_psize, l,
1290770499e1SDan Kimmel 		    offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1291770499e1SDan Kimmel 		    abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1292e14bb325SJeff Bonwick 		    ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1293e14bb325SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1294e14bb325SJeff Bonwick 	}
1295e14bb325SJeff Bonwick 
1296a3f829aeSBill Moore 	if (zio == NULL)
1297a3f829aeSBill Moore 		return (pio);
1298a3f829aeSBill Moore 
1299a3f829aeSBill Moore 	zio_nowait(pio);
1300a3f829aeSBill Moore 	return (NULL);
13010a4e9518Sgw }
13020a4e9518Sgw 
1303f64c0e34SEric Taylor static void
1304f64c0e34SEric Taylor vdev_open_child(void *arg)
1305f64c0e34SEric Taylor {
1306f64c0e34SEric Taylor 	vdev_t *vd = arg;
1307f64c0e34SEric Taylor 
1308f64c0e34SEric Taylor 	vd->vdev_open_thread = curthread;
1309f64c0e34SEric Taylor 	vd->vdev_open_error = vdev_open(vd);
1310f64c0e34SEric Taylor 	vd->vdev_open_thread = NULL;
1311f64c0e34SEric Taylor }
1312f64c0e34SEric Taylor 
1313681d9761SEric Taylor boolean_t
1314681d9761SEric Taylor vdev_uses_zvols(vdev_t *vd)
1315681d9761SEric Taylor {
1316681d9761SEric Taylor 	if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1317681d9761SEric Taylor 	    strlen(ZVOL_DIR)) == 0)
1318681d9761SEric Taylor 		return (B_TRUE);
1319681d9761SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
1320681d9761SEric Taylor 		if (vdev_uses_zvols(vd->vdev_child[c]))
1321681d9761SEric Taylor 			return (B_TRUE);
1322681d9761SEric Taylor 	return (B_FALSE);
1323681d9761SEric Taylor }
1324681d9761SEric Taylor 
1325f64c0e34SEric Taylor void
1326f64c0e34SEric Taylor vdev_open_children(vdev_t *vd)
1327f64c0e34SEric Taylor {
1328f64c0e34SEric Taylor 	taskq_t *tq;
1329f64c0e34SEric Taylor 	int children = vd->vdev_children;
1330f64c0e34SEric Taylor 
1331681d9761SEric Taylor 	/*
1332681d9761SEric Taylor 	 * in order to handle pools on top of zvols, do the opens
1333681d9761SEric Taylor 	 * in a single thread so that the same thread holds the
1334681d9761SEric Taylor 	 * spa_namespace_lock
1335681d9761SEric Taylor 	 */
1336681d9761SEric Taylor 	if (vdev_uses_zvols(vd)) {
1337681d9761SEric Taylor 		for (int c = 0; c < children; c++)
1338681d9761SEric Taylor 			vd->vdev_child[c]->vdev_open_error =
1339681d9761SEric Taylor 			    vdev_open(vd->vdev_child[c]);
1340681d9761SEric Taylor 		return;
1341681d9761SEric Taylor 	}
1342f64c0e34SEric Taylor 	tq = taskq_create("vdev_open", children, minclsyspri,
1343f64c0e34SEric Taylor 	    children, children, TASKQ_PREPOPULATE);
1344f64c0e34SEric Taylor 
1345f64c0e34SEric Taylor 	for (int c = 0; c < children; c++)
1346f64c0e34SEric Taylor 		VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1347f64c0e34SEric Taylor 		    TQ_SLEEP) != NULL);
1348f64c0e34SEric Taylor 
1349f64c0e34SEric Taylor 	taskq_destroy(tq);
1350f64c0e34SEric Taylor }
1351f64c0e34SEric Taylor 
13525cabbc6bSPrashanth Sreenivasa /*
13535cabbc6bSPrashanth Sreenivasa  * Compute the raidz-deflation ratio.  Note, we hard-code
13545cabbc6bSPrashanth Sreenivasa  * in 128k (1 << 17) because it is the "typical" blocksize.
13555cabbc6bSPrashanth Sreenivasa  * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
13565cabbc6bSPrashanth Sreenivasa  * otherwise it would inconsistently account for existing bp's.
13575cabbc6bSPrashanth Sreenivasa  */
13585cabbc6bSPrashanth Sreenivasa static void
13595cabbc6bSPrashanth Sreenivasa vdev_set_deflate_ratio(vdev_t *vd)
13605cabbc6bSPrashanth Sreenivasa {
13615cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
13625cabbc6bSPrashanth Sreenivasa 		vd->vdev_deflate_ratio = (1 << 17) /
13635cabbc6bSPrashanth Sreenivasa 		    (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
13645cabbc6bSPrashanth Sreenivasa 	}
13655cabbc6bSPrashanth Sreenivasa }
13665cabbc6bSPrashanth Sreenivasa 
1367fa9e4066Sahrens /*
1368fa9e4066Sahrens  * Prepare a virtual device for access.
1369fa9e4066Sahrens  */
1370fa9e4066Sahrens int
1371fa9e4066Sahrens vdev_open(vdev_t *vd)
1372fa9e4066Sahrens {
13738ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1374fa9e4066Sahrens 	int error;
1375fa9e4066Sahrens 	uint64_t osize = 0;
13764263d13fSGeorge Wilson 	uint64_t max_osize = 0;
13774263d13fSGeorge Wilson 	uint64_t asize, max_asize, psize;
1378ecc2d604Sbonwick 	uint64_t ashift = 0;
1379fa9e4066Sahrens 
1380f64c0e34SEric Taylor 	ASSERT(vd->vdev_open_thread == curthread ||
1381f64c0e34SEric Taylor 	    spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1382fa9e4066Sahrens 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1383fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1384fa9e4066Sahrens 	    vd->vdev_state == VDEV_STATE_OFFLINE);
1385fa9e4066Sahrens 
1386fa9e4066Sahrens 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1387e6ca193dSGeorge Wilson 	vd->vdev_cant_read = B_FALSE;
1388e6ca193dSGeorge Wilson 	vd->vdev_cant_write = B_FALSE;
1389573ca77eSGeorge Wilson 	vd->vdev_min_asize = vdev_get_min_asize(vd);
1390fa9e4066Sahrens 
1391069f55e2SEric Schrock 	/*
1392069f55e2SEric Schrock 	 * If this vdev is not removed, check its fault status.  If it's
1393069f55e2SEric Schrock 	 * faulted, bail out of the open.
1394069f55e2SEric Schrock 	 */
13953d7072f8Seschrock 	if (!vd->vdev_removed && vd->vdev_faulted) {
13963d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
1397069f55e2SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1398069f55e2SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
13993d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1400069f55e2SEric Schrock 		    vd->vdev_label_aux);
1401be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
14023d7072f8Seschrock 	} else if (vd->vdev_offline) {
1403fa9e4066Sahrens 		ASSERT(vd->vdev_children == 0);
1404ea8dc4b6Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1405be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1406fa9e4066Sahrens 	}
1407fa9e4066Sahrens 
14084263d13fSGeorge Wilson 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1409fa9e4066Sahrens 
1410095bcd66SGeorge Wilson 	/*
1411095bcd66SGeorge Wilson 	 * Reset the vdev_reopening flag so that we actually close
1412095bcd66SGeorge Wilson 	 * the vdev on error.
1413095bcd66SGeorge Wilson 	 */
1414095bcd66SGeorge Wilson 	vd->vdev_reopening = B_FALSE;
1415ea8dc4b6Seschrock 	if (zio_injection_enabled && error == 0)
14168956713aSEric Schrock 		error = zio_handle_device_injection(vd, NULL, ENXIO);
1417ea8dc4b6Seschrock 
1418fa9e4066Sahrens 	if (error) {
14193d7072f8Seschrock 		if (vd->vdev_removed &&
14203d7072f8Seschrock 		    vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
14213d7072f8Seschrock 			vd->vdev_removed = B_FALSE;
14223d7072f8Seschrock 
14236f793812SPavel Zakharov 		if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
14246f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
14256f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
14266f793812SPavel Zakharov 		} else {
14276f793812SPavel Zakharov 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
14286f793812SPavel Zakharov 			    vd->vdev_stat.vs_aux);
14296f793812SPavel Zakharov 		}
1430fa9e4066Sahrens 		return (error);
1431fa9e4066Sahrens 	}
1432fa9e4066Sahrens 
14333d7072f8Seschrock 	vd->vdev_removed = B_FALSE;
14343d7072f8Seschrock 
1435096d22d4SEric Schrock 	/*
1436096d22d4SEric Schrock 	 * Recheck the faulted flag now that we have confirmed that
1437096d22d4SEric Schrock 	 * the vdev is accessible.  If we're faulted, bail.
1438096d22d4SEric Schrock 	 */
1439096d22d4SEric Schrock 	if (vd->vdev_faulted) {
1440096d22d4SEric Schrock 		ASSERT(vd->vdev_children == 0);
1441096d22d4SEric Schrock 		ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1442096d22d4SEric Schrock 		    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1443096d22d4SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1444096d22d4SEric Schrock 		    vd->vdev_label_aux);
1445be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1446096d22d4SEric Schrock 	}
1447096d22d4SEric Schrock 
14483d7072f8Seschrock 	if (vd->vdev_degraded) {
14493d7072f8Seschrock 		ASSERT(vd->vdev_children == 0);
14503d7072f8Seschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
14513d7072f8Seschrock 		    VDEV_AUX_ERR_EXCEEDED);
14523d7072f8Seschrock 	} else {
1453069f55e2SEric Schrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
14543d7072f8Seschrock 	}
1455fa9e4066Sahrens 
145688ecc943SGeorge Wilson 	/*
145788ecc943SGeorge Wilson 	 * For hole or missing vdevs we just return success.
145888ecc943SGeorge Wilson 	 */
145988ecc943SGeorge Wilson 	if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
146088ecc943SGeorge Wilson 		return (0);
146188ecc943SGeorge Wilson 
1462573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
1463ea8dc4b6Seschrock 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1464ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1465ea8dc4b6Seschrock 			    VDEV_AUX_NONE);
1466ea8dc4b6Seschrock 			break;
1467ea8dc4b6Seschrock 		}
1468573ca77eSGeorge Wilson 	}
1469fa9e4066Sahrens 
1470fa9e4066Sahrens 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
14714263d13fSGeorge Wilson 	max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1472fa9e4066Sahrens 
1473fa9e4066Sahrens 	if (vd->vdev_children == 0) {
1474fa9e4066Sahrens 		if (osize < SPA_MINDEVSIZE) {
1475ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1476ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1477be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1478fa9e4066Sahrens 		}
1479fa9e4066Sahrens 		psize = osize;
1480fa9e4066Sahrens 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
14814263d13fSGeorge Wilson 		max_asize = max_osize - (VDEV_LABEL_START_SIZE +
14824263d13fSGeorge Wilson 		    VDEV_LABEL_END_SIZE);
1483fa9e4066Sahrens 	} else {
1484ecc2d604Sbonwick 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1485fa9e4066Sahrens 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1486ea8dc4b6Seschrock 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1487ea8dc4b6Seschrock 			    VDEV_AUX_TOO_SMALL);
1488be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
1489fa9e4066Sahrens 		}
1490fa9e4066Sahrens 		psize = 0;
1491fa9e4066Sahrens 		asize = osize;
14924263d13fSGeorge Wilson 		max_asize = max_osize;
1493fa9e4066Sahrens 	}
1494fa9e4066Sahrens 
1495fa9e4066Sahrens 	vd->vdev_psize = psize;
1496fa9e4066Sahrens 
1497573ca77eSGeorge Wilson 	/*
1498c040c10cSSteven Hartland 	 * Make sure the allocatable size hasn't shrunk too much.
1499573ca77eSGeorge Wilson 	 */
1500573ca77eSGeorge Wilson 	if (asize < vd->vdev_min_asize) {
1501573ca77eSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1502573ca77eSGeorge Wilson 		    VDEV_AUX_BAD_LABEL);
1503be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1504573ca77eSGeorge Wilson 	}
1505573ca77eSGeorge Wilson 
1506fa9e4066Sahrens 	if (vd->vdev_asize == 0) {
1507fa9e4066Sahrens 		/*
1508fa9e4066Sahrens 		 * This is the first-ever open, so use the computed values.
1509ecc2d604Sbonwick 		 * For testing purposes, a higher ashift can be requested.
1510fa9e4066Sahrens 		 */
1511fa9e4066Sahrens 		vd->vdev_asize = asize;
15124263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1513ecc2d604Sbonwick 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1514fa9e4066Sahrens 	} else {
1515fa9e4066Sahrens 		/*
15162384d9f8SGeorge Wilson 		 * Detect if the alignment requirement has increased.
15172384d9f8SGeorge Wilson 		 * We don't want to make the pool unavailable, just
15182384d9f8SGeorge Wilson 		 * issue a warning instead.
1519fa9e4066Sahrens 		 */
15202384d9f8SGeorge Wilson 		if (ashift > vd->vdev_top->vdev_ashift &&
15212384d9f8SGeorge Wilson 		    vd->vdev_ops->vdev_op_leaf) {
15222384d9f8SGeorge Wilson 			cmn_err(CE_WARN,
15232384d9f8SGeorge Wilson 			    "Disk, '%s', has a block alignment that is "
15242384d9f8SGeorge Wilson 			    "larger than the pool's alignment\n",
15252384d9f8SGeorge Wilson 			    vd->vdev_path);
1526fa9e4066Sahrens 		}
15274263d13fSGeorge Wilson 		vd->vdev_max_asize = max_asize;
1528573ca77eSGeorge Wilson 	}
1529fa9e4066Sahrens 
1530573ca77eSGeorge Wilson 	/*
1531c040c10cSSteven Hartland 	 * If all children are healthy we update asize if either:
1532c040c10cSSteven Hartland 	 * The asize has increased, due to a device expansion caused by dynamic
1533c040c10cSSteven Hartland 	 * LUN growth or vdev replacement, and automatic expansion is enabled;
1534c040c10cSSteven Hartland 	 * making the additional space available.
1535c040c10cSSteven Hartland 	 *
1536c040c10cSSteven Hartland 	 * The asize has decreased, due to a device shrink usually caused by a
1537c040c10cSSteven Hartland 	 * vdev replace with a smaller device. This ensures that calculations
1538c040c10cSSteven Hartland 	 * based of max_asize and asize e.g. esize are always valid. It's safe
1539c040c10cSSteven Hartland 	 * to do this as we've already validated that asize is greater than
1540c040c10cSSteven Hartland 	 * vdev_min_asize.
1541573ca77eSGeorge Wilson 	 */
1542c040c10cSSteven Hartland 	if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1543c040c10cSSteven Hartland 	    ((asize > vd->vdev_asize &&
1544c040c10cSSteven Hartland 	    (vd->vdev_expanding || spa->spa_autoexpand)) ||
1545c040c10cSSteven Hartland 	    (asize < vd->vdev_asize)))
1546573ca77eSGeorge Wilson 		vd->vdev_asize = asize;
1547fa9e4066Sahrens 
1548573ca77eSGeorge Wilson 	vdev_set_min_asize(vd);
1549fa9e4066Sahrens 
15500a4e9518Sgw 	/*
15510a4e9518Sgw 	 * Ensure we can issue some IO before declaring the
15520a4e9518Sgw 	 * vdev open for business.
15530a4e9518Sgw 	 */
1554e14bb325SJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf &&
1555e14bb325SJeff Bonwick 	    (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
155698d1cbfeSGeorge Wilson 		vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
155798d1cbfeSGeorge Wilson 		    VDEV_AUX_ERR_EXCEEDED);
15580a4e9518Sgw 		return (error);
15590a4e9518Sgw 	}
15600a4e9518Sgw 
156181cd5c55SMatthew Ahrens 	/*
156281cd5c55SMatthew Ahrens 	 * Track the min and max ashift values for normal data devices.
156381cd5c55SMatthew Ahrens 	 */
156481cd5c55SMatthew Ahrens 	if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
156581cd5c55SMatthew Ahrens 	    !vd->vdev_islog && vd->vdev_aux == NULL) {
156681cd5c55SMatthew Ahrens 		if (vd->vdev_ashift > spa->spa_max_ashift)
156781cd5c55SMatthew Ahrens 			spa->spa_max_ashift = vd->vdev_ashift;
156881cd5c55SMatthew Ahrens 		if (vd->vdev_ashift < spa->spa_min_ashift)
156981cd5c55SMatthew Ahrens 			spa->spa_min_ashift = vd->vdev_ashift;
157081cd5c55SMatthew Ahrens 	}
157181cd5c55SMatthew Ahrens 
1572088f3894Sahrens 	/*
1573088f3894Sahrens 	 * If a leaf vdev has a DTL, and seems healthy, then kick off a
15748ad4d6ddSJeff Bonwick 	 * resilver.  But don't do this if we are doing a reopen for a scrub,
15758ad4d6ddSJeff Bonwick 	 * since this would just restart the scrub we are already doing.
1576088f3894Sahrens 	 */
15778ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
15788ad4d6ddSJeff Bonwick 	    vdev_resilver_needed(vd, NULL, NULL))
15798ad4d6ddSJeff Bonwick 		spa_async_request(spa, SPA_ASYNC_RESILVER);
1580088f3894Sahrens 
1581fa9e4066Sahrens 	return (0);
1582fa9e4066Sahrens }
1583fa9e4066Sahrens 
1584560e6e96Seschrock /*
1585560e6e96Seschrock  * Called once the vdevs are all opened, this routine validates the label
15866f793812SPavel Zakharov  * contents. This needs to be done before vdev_load() so that we don't
15873d7072f8Seschrock  * inadvertently do repair I/Os to the wrong device.
1588560e6e96Seschrock  *
1589560e6e96Seschrock  * This function will only return failure if one of the vdevs indicates that it
1590560e6e96Seschrock  * has since been destroyed or exported.  This is only possible if
1591560e6e96Seschrock  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
1592560e6e96Seschrock  * will be updated but the function will return 0.
1593560e6e96Seschrock  */
1594560e6e96Seschrock int
15956f793812SPavel Zakharov vdev_validate(vdev_t *vd)
1596560e6e96Seschrock {
1597560e6e96Seschrock 	spa_t *spa = vd->vdev_spa;
1598560e6e96Seschrock 	nvlist_t *label;
15996f793812SPavel Zakharov 	uint64_t guid = 0, aux_guid = 0, top_guid;
1600560e6e96Seschrock 	uint64_t state;
16016f793812SPavel Zakharov 	nvlist_t *nvl;
16026f793812SPavel Zakharov 	uint64_t txg;
1603560e6e96Seschrock 
16046f793812SPavel Zakharov 	if (vdev_validate_skip)
16056f793812SPavel Zakharov 		return (0);
16066f793812SPavel Zakharov 
16076f793812SPavel Zakharov 	for (uint64_t c = 0; c < vd->vdev_children; c++)
16086f793812SPavel Zakharov 		if (vdev_validate(vd->vdev_child[c]) != 0)
1609be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
1610560e6e96Seschrock 
1611b5989ec7Seschrock 	/*
1612b5989ec7Seschrock 	 * If the device has already failed, or was marked offline, don't do
1613b5989ec7Seschrock 	 * any further validation.  Otherwise, label I/O will fail and we will
1614b5989ec7Seschrock 	 * overwrite the previous state.
1615b5989ec7Seschrock 	 */
16166f793812SPavel Zakharov 	if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
16176f793812SPavel Zakharov 		return (0);
1618560e6e96Seschrock 
16196f793812SPavel Zakharov 	/*
16206f793812SPavel Zakharov 	 * If we are performing an extreme rewind, we allow for a label that
16216f793812SPavel Zakharov 	 * was modified at a point after the current txg.
1622d1de72cfSPavel Zakharov 	 * If config lock is not held do not check for the txg. spa_sync could
1623d1de72cfSPavel Zakharov 	 * be updating the vdev's label before updating spa_last_synced_txg.
16246f793812SPavel Zakharov 	 */
1625d1de72cfSPavel Zakharov 	if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
1626d1de72cfSPavel Zakharov 	    spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
16276f793812SPavel Zakharov 		txg = UINT64_MAX;
16286f793812SPavel Zakharov 	else
16296f793812SPavel Zakharov 		txg = spa_last_synced_txg(spa);
1630560e6e96Seschrock 
16316f793812SPavel Zakharov 	if ((label = vdev_label_read_config(vd, txg)) == NULL) {
16326f793812SPavel Zakharov 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
16336f793812SPavel Zakharov 		    VDEV_AUX_BAD_LABEL);
1634b6bf6e15SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
1635b6bf6e15SPavel Zakharov 		    "txg %llu", (u_longlong_t)txg);
16366f793812SPavel Zakharov 		return (0);
16376f793812SPavel Zakharov 	}
16381195e687SMark J Musante 
16396f793812SPavel Zakharov 	/*
16406f793812SPavel Zakharov 	 * Determine if this vdev has been split off into another
16416f793812SPavel Zakharov 	 * pool.  If so, then refuse to open it.
16426f793812SPavel Zakharov 	 */
16436f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
16446f793812SPavel Zakharov 	    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
16456f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16466f793812SPavel Zakharov 		    VDEV_AUX_SPLIT_POOL);
16476f793812SPavel Zakharov 		nvlist_free(label);
16486f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
16496f793812SPavel Zakharov 		return (0);
16506f793812SPavel Zakharov 	}
1651560e6e96Seschrock 
16526f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
16536f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16546f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16556f793812SPavel Zakharov 		nvlist_free(label);
16566f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
16576f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_GUID);
16586f793812SPavel Zakharov 		return (0);
16596f793812SPavel Zakharov 	}
16601195e687SMark J Musante 
16616f793812SPavel Zakharov 	/*
16626f793812SPavel Zakharov 	 * If config is not trusted then ignore the spa guid check. This is
16636f793812SPavel Zakharov 	 * necessary because if the machine crashed during a re-guid the new
16646f793812SPavel Zakharov 	 * guid might have been written to all of the vdev labels, but not the
16656f793812SPavel Zakharov 	 * cached config. The check will be performed again once we have the
16666f793812SPavel Zakharov 	 * trusted config from the MOS.
16676f793812SPavel Zakharov 	 */
16686f793812SPavel Zakharov 	if (spa->spa_trust_config && guid != spa_guid(spa)) {
16696f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16706f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16716f793812SPavel Zakharov 		nvlist_free(label);
16726f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
16736f793812SPavel Zakharov 		    "match config (%llu != %llu)", (u_longlong_t)guid,
16746f793812SPavel Zakharov 		    (u_longlong_t)spa_guid(spa));
16756f793812SPavel Zakharov 		return (0);
16766f793812SPavel Zakharov 	}
16776f793812SPavel Zakharov 
16786f793812SPavel Zakharov 	if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
16796f793812SPavel Zakharov 	    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
16806f793812SPavel Zakharov 	    &aux_guid) != 0)
16816f793812SPavel Zakharov 		aux_guid = 0;
16826f793812SPavel Zakharov 
16836f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
16846f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16856f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16866f793812SPavel Zakharov 		nvlist_free(label);
16876f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
16886f793812SPavel Zakharov 		    ZPOOL_CONFIG_GUID);
16896f793812SPavel Zakharov 		return (0);
16906f793812SPavel Zakharov 	}
16916f793812SPavel Zakharov 
16926f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
16936f793812SPavel Zakharov 	    != 0) {
16946f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
16956f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
16966f793812SPavel Zakharov 		nvlist_free(label);
16976f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
16986f793812SPavel Zakharov 		    ZPOOL_CONFIG_TOP_GUID);
16996f793812SPavel Zakharov 		return (0);
17006f793812SPavel Zakharov 	}
17016f793812SPavel Zakharov 
17026f793812SPavel Zakharov 	/*
17036f793812SPavel Zakharov 	 * If this vdev just became a top-level vdev because its sibling was
17046f793812SPavel Zakharov 	 * detached, it will have adopted the parent's vdev guid -- but the
17056f793812SPavel Zakharov 	 * label may or may not be on disk yet. Fortunately, either version
17066f793812SPavel Zakharov 	 * of the label will have the same top guid, so if we're a top-level
17076f793812SPavel Zakharov 	 * vdev, we can safely compare to that instead.
17086f793812SPavel Zakharov 	 * However, if the config comes from a cachefile that failed to update
17096f793812SPavel Zakharov 	 * after the detach, a top-level vdev will appear as a non top-level
17106f793812SPavel Zakharov 	 * vdev in the config. Also relax the constraints if we perform an
17116f793812SPavel Zakharov 	 * extreme rewind.
17126f793812SPavel Zakharov 	 *
17136f793812SPavel Zakharov 	 * If we split this vdev off instead, then we also check the
17146f793812SPavel Zakharov 	 * original pool's guid. We don't want to consider the vdev
17156f793812SPavel Zakharov 	 * corrupt if it is partway through a split operation.
17166f793812SPavel Zakharov 	 */
17176f793812SPavel Zakharov 	if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
17186f793812SPavel Zakharov 		boolean_t mismatch = B_FALSE;
17196f793812SPavel Zakharov 		if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
17206f793812SPavel Zakharov 			if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
17216f793812SPavel Zakharov 				mismatch = B_TRUE;
17226f793812SPavel Zakharov 		} else {
17236f793812SPavel Zakharov 			if (vd->vdev_guid != top_guid &&
17246f793812SPavel Zakharov 			    vd->vdev_top->vdev_guid != guid)
17256f793812SPavel Zakharov 				mismatch = B_TRUE;
1726560e6e96Seschrock 		}
1727560e6e96Seschrock 
17286f793812SPavel Zakharov 		if (mismatch) {
1729560e6e96Seschrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1730560e6e96Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1731560e6e96Seschrock 			nvlist_free(label);
17326f793812SPavel Zakharov 			vdev_dbgmsg(vd, "vdev_validate: config guid "
17336f793812SPavel Zakharov 			    "doesn't match label guid");
17346f793812SPavel Zakharov 			vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
17356f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_guid,
17366f793812SPavel Zakharov 			    (u_longlong_t)vd->vdev_top->vdev_guid);
17376f793812SPavel Zakharov 			vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
17386f793812SPavel Zakharov 			    "aux_guid %llu", (u_longlong_t)guid,
17396f793812SPavel Zakharov 			    (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1740560e6e96Seschrock 			return (0);
1741560e6e96Seschrock 		}
17426f793812SPavel Zakharov 	}
1743560e6e96Seschrock 
17446f793812SPavel Zakharov 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
17456f793812SPavel Zakharov 	    &state) != 0) {
17466f793812SPavel Zakharov 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
17476f793812SPavel Zakharov 		    VDEV_AUX_CORRUPT_DATA);
1748560e6e96Seschrock 		nvlist_free(label);
17496f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
17506f793812SPavel Zakharov 		    ZPOOL_CONFIG_POOL_STATE);
17516f793812SPavel Zakharov 		return (0);
17526f793812SPavel Zakharov 	}
1753560e6e96Seschrock 
17546f793812SPavel Zakharov 	nvlist_free(label);
17556f793812SPavel Zakharov 
17566f793812SPavel Zakharov 	/*
17576f793812SPavel Zakharov 	 * If this is a verbatim import, no need to check the
17586f793812SPavel Zakharov 	 * state of the pool.
17596f793812SPavel Zakharov 	 */
17606f793812SPavel Zakharov 	if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
17616f793812SPavel Zakharov 	    spa_load_state(spa) == SPA_LOAD_OPEN &&
17626f793812SPavel Zakharov 	    state != POOL_STATE_ACTIVE) {
17636f793812SPavel Zakharov 		vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
17646f793812SPavel Zakharov 		    "for spa %s", (u_longlong_t)state, spa->spa_name);
17656f793812SPavel Zakharov 		return (SET_ERROR(EBADF));
17666f793812SPavel Zakharov 	}
17676f793812SPavel Zakharov 
17686f793812SPavel Zakharov 	/*
17696f793812SPavel Zakharov 	 * If we were able to open and validate a vdev that was
17706f793812SPavel Zakharov 	 * previously marked permanently unavailable, clear that state
17716f793812SPavel Zakharov 	 * now.
17726f793812SPavel Zakharov 	 */
17736f793812SPavel Zakharov 	if (vd->vdev_not_present)
17746f793812SPavel Zakharov 		vd->vdev_not_present = 0;
17756f793812SPavel Zakharov 
17766f793812SPavel Zakharov 	return (0);
17776f793812SPavel Zakharov }
17786f793812SPavel Zakharov 
17796f793812SPavel Zakharov static void
17806f793812SPavel Zakharov vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
17816f793812SPavel Zakharov {
17826f793812SPavel Zakharov 	if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
17836f793812SPavel Zakharov 		if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
17846f793812SPavel Zakharov 			zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
17856f793812SPavel Zakharov 			    "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
17866f793812SPavel Zakharov 			    dvd->vdev_path, svd->vdev_path);
17876f793812SPavel Zakharov 			spa_strfree(dvd->vdev_path);
17886f793812SPavel Zakharov 			dvd->vdev_path = spa_strdup(svd->vdev_path);
17893ee8c80cSPavel Zakharov 		}
17906f793812SPavel Zakharov 	} else if (svd->vdev_path != NULL) {
17916f793812SPavel Zakharov 		dvd->vdev_path = spa_strdup(svd->vdev_path);
17926f793812SPavel Zakharov 		zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
17936f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
17946f793812SPavel Zakharov 	}
17956f793812SPavel Zakharov }
1796560e6e96Seschrock 
17976f793812SPavel Zakharov /*
17986f793812SPavel Zakharov  * Recursively copy vdev paths from one vdev to another. Source and destination
17996f793812SPavel Zakharov  * vdev trees must have same geometry otherwise return error. Intended to copy
18006f793812SPavel Zakharov  * paths from userland config into MOS config.
18016f793812SPavel Zakharov  */
18026f793812SPavel Zakharov int
18036f793812SPavel Zakharov vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
18046f793812SPavel Zakharov {
18056f793812SPavel Zakharov 	if ((svd->vdev_ops == &vdev_missing_ops) ||
18066f793812SPavel Zakharov 	    (svd->vdev_ishole && dvd->vdev_ishole) ||
18076f793812SPavel Zakharov 	    (dvd->vdev_ops == &vdev_indirect_ops))
18086f793812SPavel Zakharov 		return (0);
18096f793812SPavel Zakharov 
18106f793812SPavel Zakharov 	if (svd->vdev_ops != dvd->vdev_ops) {
18116f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
18126f793812SPavel Zakharov 		    svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
18136f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
18146f793812SPavel Zakharov 	}
18156f793812SPavel Zakharov 
18166f793812SPavel Zakharov 	if (svd->vdev_guid != dvd->vdev_guid) {
18176f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
18186f793812SPavel Zakharov 		    "%llu)", (u_longlong_t)svd->vdev_guid,
18196f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_guid);
18206f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
18216f793812SPavel Zakharov 	}
18226f793812SPavel Zakharov 
18236f793812SPavel Zakharov 	if (svd->vdev_children != dvd->vdev_children) {
18246f793812SPavel Zakharov 		vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
18256f793812SPavel Zakharov 		    "%llu != %llu", (u_longlong_t)svd->vdev_children,
18266f793812SPavel Zakharov 		    (u_longlong_t)dvd->vdev_children);
18276f793812SPavel Zakharov 		return (SET_ERROR(EINVAL));
182851ece835Seschrock 	}
1829560e6e96Seschrock 
18306f793812SPavel Zakharov 	for (uint64_t i = 0; i < svd->vdev_children; i++) {
18316f793812SPavel Zakharov 		int error = vdev_copy_path_strict(svd->vdev_child[i],
18326f793812SPavel Zakharov 		    dvd->vdev_child[i]);
18336f793812SPavel Zakharov 		if (error != 0)
18346f793812SPavel Zakharov 			return (error);
18356f793812SPavel Zakharov 	}
18366f793812SPavel Zakharov 
18376f793812SPavel Zakharov 	if (svd->vdev_ops->vdev_op_leaf)
18386f793812SPavel Zakharov 		vdev_copy_path_impl(svd, dvd);
18396f793812SPavel Zakharov 
1840560e6e96Seschrock 	return (0);
1841560e6e96Seschrock }
1842560e6e96Seschrock 
18436f793812SPavel Zakharov static void
18446f793812SPavel Zakharov vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
18456f793812SPavel Zakharov {
18466f793812SPavel Zakharov 	ASSERT(stvd->vdev_top == stvd);
18476f793812SPavel Zakharov 	ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
18486f793812SPavel Zakharov 
18496f793812SPavel Zakharov 	for (uint64_t i = 0; i < dvd->vdev_children; i++) {
18506f793812SPavel Zakharov 		vdev_copy_path_search(stvd, dvd->vdev_child[i]);
18516f793812SPavel Zakharov 	}
18526f793812SPavel Zakharov 
18536f793812SPavel Zakharov 	if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
18546f793812SPavel Zakharov 		return;
18556f793812SPavel Zakharov 
18566f793812SPavel Zakharov 	/*
18576f793812SPavel Zakharov 	 * The idea here is that while a vdev can shift positions within
18586f793812SPavel Zakharov 	 * a top vdev (when replacing, attaching mirror, etc.) it cannot
18596f793812SPavel Zakharov 	 * step outside of it.
18606f793812SPavel Zakharov 	 */
18616f793812SPavel Zakharov 	vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
18626f793812SPavel Zakharov 
18636f793812SPavel Zakharov 	if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
18646f793812SPavel Zakharov 		return;
18656f793812SPavel Zakharov 
18666f793812SPavel Zakharov 	ASSERT(vd->vdev_ops->vdev_op_leaf);
18676f793812SPavel Zakharov 
18686f793812SPavel Zakharov 	vdev_copy_path_impl(vd, dvd);
18696f793812SPavel Zakharov }
18706f793812SPavel Zakharov 
18716f793812SPavel Zakharov /*
18726f793812SPavel Zakharov  * Recursively copy vdev paths from one root vdev to another. Source and
18736f793812SPavel Zakharov  * destination vdev trees may differ in geometry. For each destination leaf
18746f793812SPavel Zakharov  * vdev, search a vdev with the same guid and top vdev id in the source.
18756f793812SPavel Zakharov  * Intended to copy paths from userland config into MOS config.
18766f793812SPavel Zakharov  */
18776f793812SPavel Zakharov void
18786f793812SPavel Zakharov vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
18796f793812SPavel Zakharov {
18806f793812SPavel Zakharov 	uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
18816f793812SPavel Zakharov 	ASSERT(srvd->vdev_ops == &vdev_root_ops);
18826f793812SPavel Zakharov 	ASSERT(drvd->vdev_ops == &vdev_root_ops);
18836f793812SPavel Zakharov 
18846f793812SPavel Zakharov 	for (uint64_t i = 0; i < children; i++) {
18856f793812SPavel Zakharov 		vdev_copy_path_search(srvd->vdev_child[i],
18866f793812SPavel Zakharov 		    drvd->vdev_child[i]);
18876f793812SPavel Zakharov 	}
18886f793812SPavel Zakharov }
18896f793812SPavel Zakharov 
1890fa9e4066Sahrens /*
1891fa9e4066Sahrens  * Close a virtual device.
1892fa9e4066Sahrens  */
1893fa9e4066Sahrens void
1894fa9e4066Sahrens vdev_close(vdev_t *vd)
1895fa9e4066Sahrens {
18968ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
1897095bcd66SGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
18988ad4d6ddSJeff Bonwick 
18998ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
19008ad4d6ddSJeff Bonwick 
19011195e687SMark J Musante 	/*
19021195e687SMark J Musante 	 * If our parent is reopening, then we are as well, unless we are
19031195e687SMark J Musante 	 * going offline.
19041195e687SMark J Musante 	 */
1905095bcd66SGeorge Wilson 	if (pvd != NULL && pvd->vdev_reopening)
19061195e687SMark J Musante 		vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1907095bcd66SGeorge Wilson 
1908fa9e4066Sahrens 	vd->vdev_ops->vdev_op_close(vd);
1909fa9e4066Sahrens 
19103d7072f8Seschrock 	vdev_cache_purge(vd);
1911fa9e4066Sahrens 
1912560e6e96Seschrock 	/*
1913573ca77eSGeorge Wilson 	 * We record the previous state before we close it, so that if we are
1914560e6e96Seschrock 	 * doing a reopen(), we don't generate FMA ereports if we notice that
1915560e6e96Seschrock 	 * it's still faulted.
1916560e6e96Seschrock 	 */
1917560e6e96Seschrock 	vd->vdev_prevstate = vd->vdev_state;
1918560e6e96Seschrock 
1919fa9e4066Sahrens 	if (vd->vdev_offline)
1920fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_OFFLINE;
1921fa9e4066Sahrens 	else
1922fa9e4066Sahrens 		vd->vdev_state = VDEV_STATE_CLOSED;
1923ea8dc4b6Seschrock 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1924fa9e4066Sahrens }
1925fa9e4066Sahrens 
1926dcba9f3fSGeorge Wilson void
1927dcba9f3fSGeorge Wilson vdev_hold(vdev_t *vd)
1928dcba9f3fSGeorge Wilson {
1929dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1930dcba9f3fSGeorge Wilson 
1931dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1932dcba9f3fSGeorge Wilson 	if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1933dcba9f3fSGeorge Wilson 		return;
1934dcba9f3fSGeorge Wilson 
1935dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1936dcba9f3fSGeorge Wilson 		vdev_hold(vd->vdev_child[c]);
1937dcba9f3fSGeorge Wilson 
1938dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1939dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_hold(vd);
1940dcba9f3fSGeorge Wilson }
1941dcba9f3fSGeorge Wilson 
1942dcba9f3fSGeorge Wilson void
1943dcba9f3fSGeorge Wilson vdev_rele(vdev_t *vd)
1944dcba9f3fSGeorge Wilson {
1945dcba9f3fSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1946dcba9f3fSGeorge Wilson 
1947dcba9f3fSGeorge Wilson 	ASSERT(spa_is_root(spa));
1948dcba9f3fSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
1949dcba9f3fSGeorge Wilson 		vdev_rele(vd->vdev_child[c]);
1950dcba9f3fSGeorge Wilson 
1951dcba9f3fSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
1952dcba9f3fSGeorge Wilson 		vd->vdev_ops->vdev_op_rele(vd);
1953dcba9f3fSGeorge Wilson }
1954dcba9f3fSGeorge Wilson 
1955095bcd66SGeorge Wilson /*
1956095bcd66SGeorge Wilson  * Reopen all interior vdevs and any unopened leaves.  We don't actually
1957095bcd66SGeorge Wilson  * reopen leaf vdevs which had previously been opened as they might deadlock
1958095bcd66SGeorge Wilson  * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
1959095bcd66SGeorge Wilson  * If the leaf has never been opened then open it, as usual.
1960095bcd66SGeorge Wilson  */
1961fa9e4066Sahrens void
1962ea8dc4b6Seschrock vdev_reopen(vdev_t *vd)
1963fa9e4066Sahrens {
1964ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
1965fa9e4066Sahrens 
1966e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1967ea8dc4b6Seschrock 
19681195e687SMark J Musante 	/* set the reopening flag unless we're taking the vdev offline */
19691195e687SMark J Musante 	vd->vdev_reopening = !vd->vdev_offline;
1970fa9e4066Sahrens 	vdev_close(vd);
1971fa9e4066Sahrens 	(void) vdev_open(vd);
1972fa9e4066Sahrens 
197339c23413Seschrock 	/*
197439c23413Seschrock 	 * Call vdev_validate() here to make sure we have the same device.
197539c23413Seschrock 	 * Otherwise, a device with an invalid label could be successfully
197639c23413Seschrock 	 * opened in response to vdev_reopen().
197739c23413Seschrock 	 */
1978c5904d13Seschrock 	if (vd->vdev_aux) {
1979c5904d13Seschrock 		(void) vdev_validate_aux(vd);
1980e14bb325SJeff Bonwick 		if (vdev_readable(vd) && vdev_writeable(vd) &&
19816809eb4eSEric Schrock 		    vd->vdev_aux == &spa->spa_l2cache &&
1982573ca77eSGeorge Wilson 		    !l2arc_vdev_present(vd))
1983573ca77eSGeorge Wilson 			l2arc_add_vdev(spa, vd);
1984c5904d13Seschrock 	} else {
19856f793812SPavel Zakharov 		(void) vdev_validate(vd);
1986c5904d13Seschrock 	}
198739c23413Seschrock 
1988fa9e4066Sahrens 	/*
19893d7072f8Seschrock 	 * Reassess parent vdev's health.
1990fa9e4066Sahrens 	 */
19913d7072f8Seschrock 	vdev_propagate_state(vd);
1992fa9e4066Sahrens }
1993fa9e4066Sahrens 
1994fa9e4066Sahrens int
199599653d4eSeschrock vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1996fa9e4066Sahrens {
1997fa9e4066Sahrens 	int error;
1998fa9e4066Sahrens 
1999fa9e4066Sahrens 	/*
2000fa9e4066Sahrens 	 * Normally, partial opens (e.g. of a mirror) are allowed.
2001fa9e4066Sahrens 	 * For a create, however, we want to fail the request if
2002fa9e4066Sahrens 	 * there are any components we can't open.
2003fa9e4066Sahrens 	 */
2004fa9e4066Sahrens 	error = vdev_open(vd);
2005fa9e4066Sahrens 
2006fa9e4066Sahrens 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2007fa9e4066Sahrens 		vdev_close(vd);
2008fa9e4066Sahrens 		return (error ? error : ENXIO);
2009fa9e4066Sahrens 	}
2010fa9e4066Sahrens 
2011fa9e4066Sahrens 	/*
20120713e232SGeorge Wilson 	 * Recursively load DTLs and initialize all labels.
2013fa9e4066Sahrens 	 */
20140713e232SGeorge Wilson 	if ((error = vdev_dtl_load(vd)) != 0 ||
20150713e232SGeorge Wilson 	    (error = vdev_label_init(vd, txg, isreplacing ?
201639c23413Seschrock 	    VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2017fa9e4066Sahrens 		vdev_close(vd);
2018fa9e4066Sahrens 		return (error);
2019fa9e4066Sahrens 	}
2020fa9e4066Sahrens 
2021fa9e4066Sahrens 	return (0);
2022fa9e4066Sahrens }
2023fa9e4066Sahrens 
20240e34b6a7Sbonwick void
2025573ca77eSGeorge Wilson vdev_metaslab_set_size(vdev_t *vd)
2026fa9e4066Sahrens {
202786714001SSerapheim Dimitropoulos 	uint64_t asize = vd->vdev_asize;
202886714001SSerapheim Dimitropoulos 	uint64_t ms_shift = 0;
202986714001SSerapheim Dimitropoulos 
2030fa9e4066Sahrens 	/*
203186714001SSerapheim Dimitropoulos 	 * For vdevs that are bigger than 8G the metaslab size varies in
203286714001SSerapheim Dimitropoulos 	 * a way that the number of metaslabs increases in powers of two,
203386714001SSerapheim Dimitropoulos 	 * linearly in terms of vdev_asize, starting from 16 metaslabs.
203486714001SSerapheim Dimitropoulos 	 * So for vdev_asize of 8G we get 16 metaslabs, for 16G, we get 32,
203586714001SSerapheim Dimitropoulos 	 * and so on, until we hit the maximum metaslab count limit
203686714001SSerapheim Dimitropoulos 	 * [vdev_max_ms_count] from which point the metaslab count stays
203786714001SSerapheim Dimitropoulos 	 * the same.
2038fa9e4066Sahrens 	 */
203986714001SSerapheim Dimitropoulos 	ms_shift = vdev_default_ms_shift;
204086714001SSerapheim Dimitropoulos 
204186714001SSerapheim Dimitropoulos 	if ((asize >> ms_shift) < vdev_min_ms_count) {
204286714001SSerapheim Dimitropoulos 		/*
204386714001SSerapheim Dimitropoulos 		 * For devices that are less than 8G we want to have
204486714001SSerapheim Dimitropoulos 		 * exactly 16 metaslabs. We don't want less as integer
204586714001SSerapheim Dimitropoulos 		 * division rounds down, so less metaslabs mean more
204686714001SSerapheim Dimitropoulos 		 * wasted space. We don't want more as these vdevs are
204786714001SSerapheim Dimitropoulos 		 * small and in the likely event that we are running
204886714001SSerapheim Dimitropoulos 		 * out of space, the SPA will have a hard time finding
204986714001SSerapheim Dimitropoulos 		 * space due to fragmentation.
205086714001SSerapheim Dimitropoulos 		 */
205186714001SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / vdev_min_ms_count);
205286714001SSerapheim Dimitropoulos 		ms_shift = MAX(ms_shift, SPA_MAXBLOCKSHIFT);
205386714001SSerapheim Dimitropoulos 
205486714001SSerapheim Dimitropoulos 	} else if ((asize >> ms_shift) > vdev_max_ms_count) {
205586714001SSerapheim Dimitropoulos 		ms_shift = highbit64(asize / vdev_max_ms_count);
205686714001SSerapheim Dimitropoulos 	}
205786714001SSerapheim Dimitropoulos 
205886714001SSerapheim Dimitropoulos 	vd->vdev_ms_shift = ms_shift;
205986714001SSerapheim Dimitropoulos 	ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2060fa9e4066Sahrens }
2061fa9e4066Sahrens 
2062fa9e4066Sahrens void
2063ecc2d604Sbonwick vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2064fa9e4066Sahrens {
2065ecc2d604Sbonwick 	ASSERT(vd == vd->vdev_top);
20665cabbc6bSPrashanth Sreenivasa 	/* indirect vdevs don't have metaslabs or dtls */
20675cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd) || flags == 0);
2068ecc2d604Sbonwick 	ASSERT(ISP2(flags));
2069f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
2070fa9e4066Sahrens 
2071ecc2d604Sbonwick 	if (flags & VDD_METASLAB)
2072ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2073ecc2d604Sbonwick 
2074ecc2d604Sbonwick 	if (flags & VDD_DTL)
2075ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2076ecc2d604Sbonwick 
2077ecc2d604Sbonwick 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2078fa9e4066Sahrens }
2079fa9e4066Sahrens 
20800713e232SGeorge Wilson void
20810713e232SGeorge Wilson vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
20820713e232SGeorge Wilson {
20830713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
20840713e232SGeorge Wilson 		vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
20850713e232SGeorge Wilson 
20860713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf)
20870713e232SGeorge Wilson 		vdev_dirty(vd->vdev_top, flags, vd, txg);
20880713e232SGeorge Wilson }
20890713e232SGeorge Wilson 
20908ad4d6ddSJeff Bonwick /*
20918ad4d6ddSJeff Bonwick  * DTLs.
20928ad4d6ddSJeff Bonwick  *
20938ad4d6ddSJeff Bonwick  * A vdev's DTL (dirty time log) is the set of transaction groups for which
20949fb35debSEric Taylor  * the vdev has less than perfect replication.  There are four kinds of DTL:
20958ad4d6ddSJeff Bonwick  *
20968ad4d6ddSJeff Bonwick  * DTL_MISSING: txgs for which the vdev has no valid copies of the data
20978ad4d6ddSJeff Bonwick  *
20988ad4d6ddSJeff Bonwick  * DTL_PARTIAL: txgs for which data is available, but not fully replicated
20998ad4d6ddSJeff Bonwick  *
21008ad4d6ddSJeff Bonwick  * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
21018ad4d6ddSJeff Bonwick  *	scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
21028ad4d6ddSJeff Bonwick  *	txgs that was scrubbed.
21038ad4d6ddSJeff Bonwick  *
21048ad4d6ddSJeff Bonwick  * DTL_OUTAGE: txgs which cannot currently be read, whether due to
21058ad4d6ddSJeff Bonwick  *	persistent errors or just some device being offline.
21068ad4d6ddSJeff Bonwick  *	Unlike the other three, the DTL_OUTAGE map is not generally
21078ad4d6ddSJeff Bonwick  *	maintained; it's only computed when needed, typically to
21088ad4d6ddSJeff Bonwick  *	determine whether a device can be detached.
21098ad4d6ddSJeff Bonwick  *
21108ad4d6ddSJeff Bonwick  * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
21118ad4d6ddSJeff Bonwick  * either has the data or it doesn't.
21128ad4d6ddSJeff Bonwick  *
21138ad4d6ddSJeff Bonwick  * For interior vdevs such as mirror and RAID-Z the picture is more complex.
21148ad4d6ddSJeff Bonwick  * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
21158ad4d6ddSJeff Bonwick  * if any child is less than fully replicated, then so is its parent.
21168ad4d6ddSJeff Bonwick  * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
21178ad4d6ddSJeff Bonwick  * comprising only those txgs which appear in 'maxfaults' or more children;
21188ad4d6ddSJeff Bonwick  * those are the txgs we don't have enough replication to read.  For example,
21198ad4d6ddSJeff Bonwick  * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
21208ad4d6ddSJeff Bonwick  * thus, its DTL_MISSING consists of the set of txgs that appear in more than
21218ad4d6ddSJeff Bonwick  * two child DTL_MISSING maps.
21228ad4d6ddSJeff Bonwick  *
21238ad4d6ddSJeff Bonwick  * It should be clear from the above that to compute the DTLs and outage maps
21248ad4d6ddSJeff Bonwick  * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
21258ad4d6ddSJeff Bonwick  * Therefore, that is all we keep on disk.  When loading the pool, or after
21268ad4d6ddSJeff Bonwick  * a configuration change, we generate all other DTLs from first principles.
21278ad4d6ddSJeff Bonwick  */
2128fa9e4066Sahrens void
21298ad4d6ddSJeff Bonwick vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2130fa9e4066Sahrens {
21310713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
21328ad4d6ddSJeff Bonwick 
21338ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
21348ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2135f9af39baSGeorge Wilson 	ASSERT(spa_writeable(vd->vdev_spa));
21368ad4d6ddSJeff Bonwick 
21375cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
21380713e232SGeorge Wilson 	if (!range_tree_contains(rt, txg, size))
21390713e232SGeorge Wilson 		range_tree_add(rt, txg, size);
21405cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2141fa9e4066Sahrens }
2142fa9e4066Sahrens 
21438ad4d6ddSJeff Bonwick boolean_t
21448ad4d6ddSJeff Bonwick vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2145fa9e4066Sahrens {
21460713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
21478ad4d6ddSJeff Bonwick 	boolean_t dirty = B_FALSE;
2148fa9e4066Sahrens 
21498ad4d6ddSJeff Bonwick 	ASSERT(t < DTL_TYPES);
21508ad4d6ddSJeff Bonwick 	ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2151fa9e4066Sahrens 
21525cabbc6bSPrashanth Sreenivasa 	/*
21535cabbc6bSPrashanth Sreenivasa 	 * While we are loading the pool, the DTLs have not been loaded yet.
21545cabbc6bSPrashanth Sreenivasa 	 * Ignore the DTLs and try all devices.  This avoids a recursive
21555cabbc6bSPrashanth Sreenivasa 	 * mutex enter on the vdev_dtl_lock, and also makes us try hard
21565cabbc6bSPrashanth Sreenivasa 	 * when loading the pool (relying on the checksum to ensure that
21575cabbc6bSPrashanth Sreenivasa 	 * we get the right data -- note that we while loading, we are
21585cabbc6bSPrashanth Sreenivasa 	 * only reading the MOS, which is always checksummed).
21595cabbc6bSPrashanth Sreenivasa 	 */
21605cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
21615cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
21625cabbc6bSPrashanth Sreenivasa 
21635cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
216486714001SSerapheim Dimitropoulos 	if (!range_tree_is_empty(rt))
21650713e232SGeorge Wilson 		dirty = range_tree_contains(rt, txg, size);
21665cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
2167fa9e4066Sahrens 
2168fa9e4066Sahrens 	return (dirty);
2169fa9e4066Sahrens }
2170fa9e4066Sahrens 
21718ad4d6ddSJeff Bonwick boolean_t
21728ad4d6ddSJeff Bonwick vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
21738ad4d6ddSJeff Bonwick {
21740713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[t];
21758ad4d6ddSJeff Bonwick 	boolean_t empty;
21768ad4d6ddSJeff Bonwick 
21775cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vd->vdev_dtl_lock);
217886714001SSerapheim Dimitropoulos 	empty = range_tree_is_empty(rt);
21795cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vd->vdev_dtl_lock);
21808ad4d6ddSJeff Bonwick 
21818ad4d6ddSJeff Bonwick 	return (empty);
21828ad4d6ddSJeff Bonwick }
21838ad4d6ddSJeff Bonwick 
2184b4952e17SGeorge Wilson /*
2185b4952e17SGeorge Wilson  * Returns the lowest txg in the DTL range.
2186b4952e17SGeorge Wilson  */
2187b4952e17SGeorge Wilson static uint64_t
2188b4952e17SGeorge Wilson vdev_dtl_min(vdev_t *vd)
2189b4952e17SGeorge Wilson {
21900713e232SGeorge Wilson 	range_seg_t *rs;
2191b4952e17SGeorge Wilson 
2192b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
21930713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2194b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2195b4952e17SGeorge Wilson 
21960713e232SGeorge Wilson 	rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
21970713e232SGeorge Wilson 	return (rs->rs_start - 1);
2198b4952e17SGeorge Wilson }
2199b4952e17SGeorge Wilson 
2200b4952e17SGeorge Wilson /*
2201b4952e17SGeorge Wilson  * Returns the highest txg in the DTL.
2202b4952e17SGeorge Wilson  */
2203b4952e17SGeorge Wilson static uint64_t
2204b4952e17SGeorge Wilson vdev_dtl_max(vdev_t *vd)
2205b4952e17SGeorge Wilson {
22060713e232SGeorge Wilson 	range_seg_t *rs;
2207b4952e17SGeorge Wilson 
2208b4952e17SGeorge Wilson 	ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
22090713e232SGeorge Wilson 	ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2210b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2211b4952e17SGeorge Wilson 
22120713e232SGeorge Wilson 	rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
22130713e232SGeorge Wilson 	return (rs->rs_end);
2214b4952e17SGeorge Wilson }
2215b4952e17SGeorge Wilson 
2216b4952e17SGeorge Wilson /*
2217b4952e17SGeorge Wilson  * Determine if a resilvering vdev should remove any DTL entries from
2218b4952e17SGeorge Wilson  * its range. If the vdev was resilvering for the entire duration of the
2219b4952e17SGeorge Wilson  * scan then it should excise that range from its DTLs. Otherwise, this
2220b4952e17SGeorge Wilson  * vdev is considered partially resilvered and should leave its DTL
2221b4952e17SGeorge Wilson  * entries intact. The comment in vdev_dtl_reassess() describes how we
2222b4952e17SGeorge Wilson  * excise the DTLs.
2223b4952e17SGeorge Wilson  */
2224b4952e17SGeorge Wilson static boolean_t
2225b4952e17SGeorge Wilson vdev_dtl_should_excise(vdev_t *vd)
2226b4952e17SGeorge Wilson {
2227b4952e17SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2228b4952e17SGeorge Wilson 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2229b4952e17SGeorge Wilson 
2230b4952e17SGeorge Wilson 	ASSERT0(scn->scn_phys.scn_errors);
2231b4952e17SGeorge Wilson 	ASSERT0(vd->vdev_children);
2232b4952e17SGeorge Wilson 
22332d2f193aSMatthew Ahrens 	if (vd->vdev_state < VDEV_STATE_DEGRADED)
22342d2f193aSMatthew Ahrens 		return (B_FALSE);
22352d2f193aSMatthew Ahrens 
2236b4952e17SGeorge Wilson 	if (vd->vdev_resilver_txg == 0 ||
223786714001SSerapheim Dimitropoulos 	    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2238b4952e17SGeorge Wilson 		return (B_TRUE);
2239b4952e17SGeorge Wilson 
2240b4952e17SGeorge Wilson 	/*
2241b4952e17SGeorge Wilson 	 * When a resilver is initiated the scan will assign the scn_max_txg
2242b4952e17SGeorge Wilson 	 * value to the highest txg value that exists in all DTLs. If this
2243b4952e17SGeorge Wilson 	 * device's max DTL is not part of this scan (i.e. it is not in
2244b4952e17SGeorge Wilson 	 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2245b4952e17SGeorge Wilson 	 * for excision.
2246b4952e17SGeorge Wilson 	 */
2247b4952e17SGeorge Wilson 	if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2248b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2249b4952e17SGeorge Wilson 		ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2250b4952e17SGeorge Wilson 		ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2251b4952e17SGeorge Wilson 		return (B_TRUE);
2252b4952e17SGeorge Wilson 	}
2253b4952e17SGeorge Wilson 	return (B_FALSE);
2254b4952e17SGeorge Wilson }
2255b4952e17SGeorge Wilson 
2256fa9e4066Sahrens /*
2257fa9e4066Sahrens  * Reassess DTLs after a config change or scrub completion.
2258fa9e4066Sahrens  */
2259fa9e4066Sahrens void
2260fa9e4066Sahrens vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2261fa9e4066Sahrens {
2262ea8dc4b6Seschrock 	spa_t *spa = vd->vdev_spa;
22638ad4d6ddSJeff Bonwick 	avl_tree_t reftree;
22648ad4d6ddSJeff Bonwick 	int minref;
2265fa9e4066Sahrens 
22668ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2267fa9e4066Sahrens 
22688ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
22698ad4d6ddSJeff Bonwick 		vdev_dtl_reassess(vd->vdev_child[c], txg,
22708ad4d6ddSJeff Bonwick 		    scrub_txg, scrub_done);
22718ad4d6ddSJeff Bonwick 
22725cabbc6bSPrashanth Sreenivasa 	if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
22738ad4d6ddSJeff Bonwick 		return;
22748ad4d6ddSJeff Bonwick 
22758ad4d6ddSJeff Bonwick 	if (vd->vdev_ops->vdev_op_leaf) {
22763f9d6ad7SLin Ling 		dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
22773f9d6ad7SLin Ling 
2278fa9e4066Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
2279b4952e17SGeorge Wilson 
2280b4952e17SGeorge Wilson 		/*
2281b4952e17SGeorge Wilson 		 * If we've completed a scan cleanly then determine
2282b4952e17SGeorge Wilson 		 * if this vdev should remove any DTLs. We only want to
2283b4952e17SGeorge Wilson 		 * excise regions on vdevs that were available during
2284b4952e17SGeorge Wilson 		 * the entire duration of this scan.
2285b4952e17SGeorge Wilson 		 */
2286088f3894Sahrens 		if (scrub_txg != 0 &&
22873f9d6ad7SLin Ling 		    (spa->spa_scrub_started ||
2288b4952e17SGeorge Wilson 		    (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2289b4952e17SGeorge Wilson 		    vdev_dtl_should_excise(vd)) {
2290088f3894Sahrens 			/*
2291088f3894Sahrens 			 * We completed a scrub up to scrub_txg.  If we
2292088f3894Sahrens 			 * did it without rebooting, then the scrub dtl
2293088f3894Sahrens 			 * will be valid, so excise the old region and
2294088f3894Sahrens 			 * fold in the scrub dtl.  Otherwise, leave the
2295088f3894Sahrens 			 * dtl as-is if there was an error.
22968ad4d6ddSJeff Bonwick 			 *
22978ad4d6ddSJeff Bonwick 			 * There's little trick here: to excise the beginning
22988ad4d6ddSJeff Bonwick 			 * of the DTL_MISSING map, we put it into a reference
22998ad4d6ddSJeff Bonwick 			 * tree and then add a segment with refcnt -1 that
23008ad4d6ddSJeff Bonwick 			 * covers the range [0, scrub_txg).  This means
23018ad4d6ddSJeff Bonwick 			 * that each txg in that range has refcnt -1 or 0.
23028ad4d6ddSJeff Bonwick 			 * We then add DTL_SCRUB with a refcnt of 2, so that
23038ad4d6ddSJeff Bonwick 			 * entries in the range [0, scrub_txg) will have a
23048ad4d6ddSJeff Bonwick 			 * positive refcnt -- either 1 or 2.  We then convert
23058ad4d6ddSJeff Bonwick 			 * the reference tree into the new DTL_MISSING map.
2306088f3894Sahrens 			 */
23070713e232SGeorge Wilson 			space_reftree_create(&reftree);
23080713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
23090713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
23100713e232SGeorge Wilson 			space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
23110713e232SGeorge Wilson 			space_reftree_add_map(&reftree,
23120713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_SCRUB], 2);
23130713e232SGeorge Wilson 			space_reftree_generate_map(&reftree,
23140713e232SGeorge Wilson 			    vd->vdev_dtl[DTL_MISSING], 1);
23150713e232SGeorge Wilson 			space_reftree_destroy(&reftree);
2316fa9e4066Sahrens 		}
23170713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
23180713e232SGeorge Wilson 		range_tree_walk(vd->vdev_dtl[DTL_MISSING],
23190713e232SGeorge Wilson 		    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2320fa9e4066Sahrens 		if (scrub_done)
23210713e232SGeorge Wilson 			range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
23220713e232SGeorge Wilson 		range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
23238ad4d6ddSJeff Bonwick 		if (!vdev_readable(vd))
23240713e232SGeorge Wilson 			range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
23258ad4d6ddSJeff Bonwick 		else
23260713e232SGeorge Wilson 			range_tree_walk(vd->vdev_dtl[DTL_MISSING],
23270713e232SGeorge Wilson 			    range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2328b4952e17SGeorge Wilson 
2329b4952e17SGeorge Wilson 		/*
2330b4952e17SGeorge Wilson 		 * If the vdev was resilvering and no longer has any
2331b4952e17SGeorge Wilson 		 * DTLs then reset its resilvering flag.
2332b4952e17SGeorge Wilson 		 */
2333b4952e17SGeorge Wilson 		if (vd->vdev_resilver_txg != 0 &&
233486714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
233586714001SSerapheim Dimitropoulos 		    range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE]))
2336b4952e17SGeorge Wilson 			vd->vdev_resilver_txg = 0;
2337b4952e17SGeorge Wilson 
2338fa9e4066Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2339088f3894Sahrens 
2340ecc2d604Sbonwick 		if (txg != 0)
2341ecc2d604Sbonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2342fa9e4066Sahrens 		return;
2343fa9e4066Sahrens 	}
2344fa9e4066Sahrens 
2345fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
23468ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
234799bb17e2SEric Taylor 		/* account for child's outage in parent's missing map */
234899bb17e2SEric Taylor 		int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
23498ad4d6ddSJeff Bonwick 		if (t == DTL_SCRUB)
23508ad4d6ddSJeff Bonwick 			continue;			/* leaf vdevs only */
23518ad4d6ddSJeff Bonwick 		if (t == DTL_PARTIAL)
23528ad4d6ddSJeff Bonwick 			minref = 1;			/* i.e. non-zero */
23538ad4d6ddSJeff Bonwick 		else if (vd->vdev_nparity != 0)
23548ad4d6ddSJeff Bonwick 			minref = vd->vdev_nparity + 1;	/* RAID-Z */
23558ad4d6ddSJeff Bonwick 		else
23568ad4d6ddSJeff Bonwick 			minref = vd->vdev_children;	/* any kind of mirror */
23570713e232SGeorge Wilson 		space_reftree_create(&reftree);
23588ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
23598ad4d6ddSJeff Bonwick 			vdev_t *cvd = vd->vdev_child[c];
23608ad4d6ddSJeff Bonwick 			mutex_enter(&cvd->vdev_dtl_lock);
23610713e232SGeorge Wilson 			space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
23628ad4d6ddSJeff Bonwick 			mutex_exit(&cvd->vdev_dtl_lock);
23638ad4d6ddSJeff Bonwick 		}
23640713e232SGeorge Wilson 		space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
23650713e232SGeorge Wilson 		space_reftree_destroy(&reftree);
2366fa9e4066Sahrens 	}
23678ad4d6ddSJeff Bonwick 	mutex_exit(&vd->vdev_dtl_lock);
2368fa9e4066Sahrens }
2369fa9e4066Sahrens 
23700713e232SGeorge Wilson int
2371fa9e4066Sahrens vdev_dtl_load(vdev_t *vd)
2372fa9e4066Sahrens {
2373fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2374ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
23750713e232SGeorge Wilson 	int error = 0;
2376fa9e4066Sahrens 
23770713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
23785cabbc6bSPrashanth Sreenivasa 		ASSERT(vdev_is_concrete(vd));
2379fa9e4066Sahrens 
23800713e232SGeorge Wilson 		error = space_map_open(&vd->vdev_dtl_sm, mos,
23815cabbc6bSPrashanth Sreenivasa 		    vd->vdev_dtl_object, 0, -1ULL, 0);
23820713e232SGeorge Wilson 		if (error)
23830713e232SGeorge Wilson 			return (error);
23840713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2385fa9e4066Sahrens 
23860713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
238788ecc943SGeorge Wilson 
23880713e232SGeorge Wilson 		/*
23890713e232SGeorge Wilson 		 * Now that we've opened the space_map we need to update
23900713e232SGeorge Wilson 		 * the in-core DTL.
23910713e232SGeorge Wilson 		 */
23920713e232SGeorge Wilson 		space_map_update(vd->vdev_dtl_sm);
2393ecc2d604Sbonwick 
23940713e232SGeorge Wilson 		error = space_map_load(vd->vdev_dtl_sm,
23950713e232SGeorge Wilson 		    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
23960713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2397fa9e4066Sahrens 
23980713e232SGeorge Wilson 		return (error);
23990713e232SGeorge Wilson 	}
24000713e232SGeorge Wilson 
24010713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
24020713e232SGeorge Wilson 		error = vdev_dtl_load(vd->vdev_child[c]);
24030713e232SGeorge Wilson 		if (error != 0)
24040713e232SGeorge Wilson 			break;
24050713e232SGeorge Wilson 	}
2406fa9e4066Sahrens 
2407fa9e4066Sahrens 	return (error);
2408fa9e4066Sahrens }
2409fa9e4066Sahrens 
2410215198a6SJoe Stein void
2411215198a6SJoe Stein vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2412215198a6SJoe Stein {
2413215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2414215198a6SJoe Stein 
2415215198a6SJoe Stein 	VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2416215198a6SJoe Stein 	VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2417215198a6SJoe Stein 	    zapobj, tx));
2418215198a6SJoe Stein }
2419215198a6SJoe Stein 
2420215198a6SJoe Stein uint64_t
2421215198a6SJoe Stein vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2422215198a6SJoe Stein {
2423215198a6SJoe Stein 	spa_t *spa = vd->vdev_spa;
2424215198a6SJoe Stein 	uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2425215198a6SJoe Stein 	    DMU_OT_NONE, 0, tx);
2426215198a6SJoe Stein 
2427215198a6SJoe Stein 	ASSERT(zap != 0);
2428215198a6SJoe Stein 	VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2429215198a6SJoe Stein 	    zap, tx));
2430215198a6SJoe Stein 
2431215198a6SJoe Stein 	return (zap);
2432215198a6SJoe Stein }
2433215198a6SJoe Stein 
2434215198a6SJoe Stein void
2435215198a6SJoe Stein vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2436215198a6SJoe Stein {
2437215198a6SJoe Stein 	if (vd->vdev_ops != &vdev_hole_ops &&
2438215198a6SJoe Stein 	    vd->vdev_ops != &vdev_missing_ops &&
2439215198a6SJoe Stein 	    vd->vdev_ops != &vdev_root_ops &&
2440215198a6SJoe Stein 	    !vd->vdev_top->vdev_removing) {
2441215198a6SJoe Stein 		if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2442215198a6SJoe Stein 			vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2443215198a6SJoe Stein 		}
2444215198a6SJoe Stein 		if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2445215198a6SJoe Stein 			vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2446215198a6SJoe Stein 		}
2447215198a6SJoe Stein 	}
2448215198a6SJoe Stein 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
2449215198a6SJoe Stein 		vdev_construct_zaps(vd->vdev_child[i], tx);
2450215198a6SJoe Stein 	}
2451215198a6SJoe Stein }
2452215198a6SJoe Stein 
2453fa9e4066Sahrens void
2454fa9e4066Sahrens vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2455fa9e4066Sahrens {
2456fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
24570713e232SGeorge Wilson 	range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2458ecc2d604Sbonwick 	objset_t *mos = spa->spa_meta_objset;
24590713e232SGeorge Wilson 	range_tree_t *rtsync;
2460fa9e4066Sahrens 	dmu_tx_t *tx;
24610713e232SGeorge Wilson 	uint64_t object = space_map_object(vd->vdev_dtl_sm);
2462fa9e4066Sahrens 
24635cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
24640713e232SGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
246588ecc943SGeorge Wilson 
2466fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2467fa9e4066Sahrens 
24680713e232SGeorge Wilson 	if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
24690713e232SGeorge Wilson 		mutex_enter(&vd->vdev_dtl_lock);
24700713e232SGeorge Wilson 		space_map_free(vd->vdev_dtl_sm, tx);
24710713e232SGeorge Wilson 		space_map_close(vd->vdev_dtl_sm);
24720713e232SGeorge Wilson 		vd->vdev_dtl_sm = NULL;
24730713e232SGeorge Wilson 		mutex_exit(&vd->vdev_dtl_lock);
2474215198a6SJoe Stein 
2475215198a6SJoe Stein 		/*
2476215198a6SJoe Stein 		 * We only destroy the leaf ZAP for detached leaves or for
2477215198a6SJoe Stein 		 * removed log devices. Removed data devices handle leaf ZAP
2478215198a6SJoe Stein 		 * cleanup later, once cancellation is no longer possible.
2479215198a6SJoe Stein 		 */
2480215198a6SJoe Stein 		if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2481215198a6SJoe Stein 		    vd->vdev_top->vdev_islog)) {
2482215198a6SJoe Stein 			vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2483215198a6SJoe Stein 			vd->vdev_leaf_zap = 0;
2484215198a6SJoe Stein 		}
2485215198a6SJoe Stein 
2486fa9e4066Sahrens 		dmu_tx_commit(tx);
2487fa9e4066Sahrens 		return;
2488fa9e4066Sahrens 	}
2489fa9e4066Sahrens 
24900713e232SGeorge Wilson 	if (vd->vdev_dtl_sm == NULL) {
24910713e232SGeorge Wilson 		uint64_t new_object;
24920713e232SGeorge Wilson 
249386714001SSerapheim Dimitropoulos 		new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx);
24940713e232SGeorge Wilson 		VERIFY3U(new_object, !=, 0);
24950713e232SGeorge Wilson 
24960713e232SGeorge Wilson 		VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
24975cabbc6bSPrashanth Sreenivasa 		    0, -1ULL, 0));
24980713e232SGeorge Wilson 		ASSERT(vd->vdev_dtl_sm != NULL);
2499fa9e4066Sahrens 	}
2500fa9e4066Sahrens 
25015cabbc6bSPrashanth Sreenivasa 	rtsync = range_tree_create(NULL, NULL);
2502fa9e4066Sahrens 
2503fa9e4066Sahrens 	mutex_enter(&vd->vdev_dtl_lock);
25040713e232SGeorge Wilson 	range_tree_walk(rt, range_tree_add, rtsync);
2505fa9e4066Sahrens 	mutex_exit(&vd->vdev_dtl_lock);
2506fa9e4066Sahrens 
250786714001SSerapheim Dimitropoulos 	space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx);
250817f11284SSerapheim Dimitropoulos 	space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
25090713e232SGeorge Wilson 	range_tree_vacate(rtsync, NULL, NULL);
2510fa9e4066Sahrens 
25110713e232SGeorge Wilson 	range_tree_destroy(rtsync);
2512fa9e4066Sahrens 
25130713e232SGeorge Wilson 	/*
25140713e232SGeorge Wilson 	 * If the object for the space map has changed then dirty
25150713e232SGeorge Wilson 	 * the top level so that we update the config.
25160713e232SGeorge Wilson 	 */
25170713e232SGeorge Wilson 	if (object != space_map_object(vd->vdev_dtl_sm)) {
25183ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
25193ee8c80cSPavel Zakharov 		    "new object %llu", (u_longlong_t)txg, spa_name(spa),
25203ee8c80cSPavel Zakharov 		    (u_longlong_t)object,
25213ee8c80cSPavel Zakharov 		    (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
25220713e232SGeorge Wilson 		vdev_config_dirty(vd->vdev_top);
25230713e232SGeorge Wilson 	}
2524fa9e4066Sahrens 
2525fa9e4066Sahrens 	dmu_tx_commit(tx);
25260713e232SGeorge Wilson 
25270713e232SGeorge Wilson 	mutex_enter(&vd->vdev_dtl_lock);
25280713e232SGeorge Wilson 	space_map_update(vd->vdev_dtl_sm);
25290713e232SGeorge Wilson 	mutex_exit(&vd->vdev_dtl_lock);
2530fa9e4066Sahrens }
2531fa9e4066Sahrens 
25328ad4d6ddSJeff Bonwick /*
25338ad4d6ddSJeff Bonwick  * Determine whether the specified vdev can be offlined/detached/removed
25348ad4d6ddSJeff Bonwick  * without losing data.
25358ad4d6ddSJeff Bonwick  */
25368ad4d6ddSJeff Bonwick boolean_t
25378ad4d6ddSJeff Bonwick vdev_dtl_required(vdev_t *vd)
25388ad4d6ddSJeff Bonwick {
25398ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
25408ad4d6ddSJeff Bonwick 	vdev_t *tvd = vd->vdev_top;
25418ad4d6ddSJeff Bonwick 	uint8_t cant_read = vd->vdev_cant_read;
25428ad4d6ddSJeff Bonwick 	boolean_t required;
25438ad4d6ddSJeff Bonwick 
25448ad4d6ddSJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
25458ad4d6ddSJeff Bonwick 
25468ad4d6ddSJeff Bonwick 	if (vd == spa->spa_root_vdev || vd == tvd)
25478ad4d6ddSJeff Bonwick 		return (B_TRUE);
25488ad4d6ddSJeff Bonwick 
25498ad4d6ddSJeff Bonwick 	/*
25508ad4d6ddSJeff Bonwick 	 * Temporarily mark the device as unreadable, and then determine
25518ad4d6ddSJeff Bonwick 	 * whether this results in any DTL outages in the top-level vdev.
25528ad4d6ddSJeff Bonwick 	 * If not, we can safely offline/detach/remove the device.
25538ad4d6ddSJeff Bonwick 	 */
25548ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = B_TRUE;
25558ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
25568ad4d6ddSJeff Bonwick 	required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
25578ad4d6ddSJeff Bonwick 	vd->vdev_cant_read = cant_read;
25588ad4d6ddSJeff Bonwick 	vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
25598ad4d6ddSJeff Bonwick 
2560cb04b873SMark J Musante 	if (!required && zio_injection_enabled)
2561cb04b873SMark J Musante 		required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2562cb04b873SMark J Musante 
25638ad4d6ddSJeff Bonwick 	return (required);
25648ad4d6ddSJeff Bonwick }
25658ad4d6ddSJeff Bonwick 
2566088f3894Sahrens /*
2567088f3894Sahrens  * Determine if resilver is needed, and if so the txg range.
2568088f3894Sahrens  */
2569088f3894Sahrens boolean_t
2570088f3894Sahrens vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2571088f3894Sahrens {
2572088f3894Sahrens 	boolean_t needed = B_FALSE;
2573088f3894Sahrens 	uint64_t thismin = UINT64_MAX;
2574088f3894Sahrens 	uint64_t thismax = 0;
2575088f3894Sahrens 
2576088f3894Sahrens 	if (vd->vdev_children == 0) {
2577088f3894Sahrens 		mutex_enter(&vd->vdev_dtl_lock);
257886714001SSerapheim Dimitropoulos 		if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
25798ad4d6ddSJeff Bonwick 		    vdev_writeable(vd)) {
2580088f3894Sahrens 
2581b4952e17SGeorge Wilson 			thismin = vdev_dtl_min(vd);
2582b4952e17SGeorge Wilson 			thismax = vdev_dtl_max(vd);
2583088f3894Sahrens 			needed = B_TRUE;
2584088f3894Sahrens 		}
2585088f3894Sahrens 		mutex_exit(&vd->vdev_dtl_lock);
2586088f3894Sahrens 	} else {
25878ad4d6ddSJeff Bonwick 		for (int c = 0; c < vd->vdev_children; c++) {
2588088f3894Sahrens 			vdev_t *cvd = vd->vdev_child[c];
2589088f3894Sahrens 			uint64_t cmin, cmax;
2590088f3894Sahrens 
2591088f3894Sahrens 			if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2592088f3894Sahrens 				thismin = MIN(thismin, cmin);
2593088f3894Sahrens 				thismax = MAX(thismax, cmax);
2594088f3894Sahrens 				needed = B_TRUE;
2595088f3894Sahrens 			}
2596088f3894Sahrens 		}
2597088f3894Sahrens 	}
2598088f3894Sahrens 
2599088f3894Sahrens 	if (needed && minp) {
2600088f3894Sahrens 		*minp = thismin;
2601088f3894Sahrens 		*maxp = thismax;
2602088f3894Sahrens 	}
2603088f3894Sahrens 	return (needed);
2604088f3894Sahrens }
2605088f3894Sahrens 
260686714001SSerapheim Dimitropoulos /*
260786714001SSerapheim Dimitropoulos  * Gets the checkpoint space map object from the vdev's ZAP.
260886714001SSerapheim Dimitropoulos  * Returns the spacemap object, or 0 if it wasn't in the ZAP
260986714001SSerapheim Dimitropoulos  * or the ZAP doesn't exist yet.
261086714001SSerapheim Dimitropoulos  */
261186714001SSerapheim Dimitropoulos int
261286714001SSerapheim Dimitropoulos vdev_checkpoint_sm_object(vdev_t *vd)
261386714001SSerapheim Dimitropoulos {
261486714001SSerapheim Dimitropoulos 	ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
261586714001SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0) {
261686714001SSerapheim Dimitropoulos 		return (0);
261786714001SSerapheim Dimitropoulos 	}
261886714001SSerapheim Dimitropoulos 
261986714001SSerapheim Dimitropoulos 	uint64_t sm_obj = 0;
262086714001SSerapheim Dimitropoulos 	int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
262186714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj);
262286714001SSerapheim Dimitropoulos 
262386714001SSerapheim Dimitropoulos 	ASSERT(err == 0 || err == ENOENT);
262486714001SSerapheim Dimitropoulos 
262586714001SSerapheim Dimitropoulos 	return (sm_obj);
262686714001SSerapheim Dimitropoulos }
262786714001SSerapheim Dimitropoulos 
26285cabbc6bSPrashanth Sreenivasa int
2629ea8dc4b6Seschrock vdev_load(vdev_t *vd)
2630fa9e4066Sahrens {
26315cabbc6bSPrashanth Sreenivasa 	int error = 0;
2632fa9e4066Sahrens 	/*
2633fa9e4066Sahrens 	 * Recursively load all children.
2634fa9e4066Sahrens 	 */
26355cabbc6bSPrashanth Sreenivasa 	for (int c = 0; c < vd->vdev_children; c++) {
26365cabbc6bSPrashanth Sreenivasa 		error = vdev_load(vd->vdev_child[c]);
26375cabbc6bSPrashanth Sreenivasa 		if (error != 0) {
26385cabbc6bSPrashanth Sreenivasa 			return (error);
26395cabbc6bSPrashanth Sreenivasa 		}
26405cabbc6bSPrashanth Sreenivasa 	}
26415cabbc6bSPrashanth Sreenivasa 
26425cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
2643fa9e4066Sahrens 
2644fa9e4066Sahrens 	/*
26450e34b6a7Sbonwick 	 * If this is a top-level vdev, initialize its metaslabs.
2646fa9e4066Sahrens 	 */
26475cabbc6bSPrashanth Sreenivasa 	if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
26485cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
26495cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
26505cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
26513ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
26523ee8c80cSPavel Zakharov 			    "asize=%llu", (u_longlong_t)vd->vdev_ashift,
26533ee8c80cSPavel Zakharov 			    (u_longlong_t)vd->vdev_asize);
26545cabbc6bSPrashanth Sreenivasa 			return (SET_ERROR(ENXIO));
26555cabbc6bSPrashanth Sreenivasa 		} else if ((error = vdev_metaslab_init(vd, 0)) != 0) {
26563ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
26573ee8c80cSPavel Zakharov 			    "[error=%d]", error);
26585cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
26595cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
26605cabbc6bSPrashanth Sreenivasa 			return (error);
26615cabbc6bSPrashanth Sreenivasa 		}
266286714001SSerapheim Dimitropoulos 
266386714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd);
266486714001SSerapheim Dimitropoulos 		if (checkpoint_sm_obj != 0) {
266586714001SSerapheim Dimitropoulos 			objset_t *mos = spa_meta_objset(vd->vdev_spa);
266686714001SSerapheim Dimitropoulos 			ASSERT(vd->vdev_asize != 0);
266786714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
266886714001SSerapheim Dimitropoulos 
266986714001SSerapheim Dimitropoulos 			if ((error = space_map_open(&vd->vdev_checkpoint_sm,
267086714001SSerapheim Dimitropoulos 			    mos, checkpoint_sm_obj, 0, vd->vdev_asize,
267186714001SSerapheim Dimitropoulos 			    vd->vdev_ashift))) {
267286714001SSerapheim Dimitropoulos 				vdev_dbgmsg(vd, "vdev_load: space_map_open "
267386714001SSerapheim Dimitropoulos 				    "failed for checkpoint spacemap (obj %llu) "
267486714001SSerapheim Dimitropoulos 				    "[error=%d]",
267586714001SSerapheim Dimitropoulos 				    (u_longlong_t)checkpoint_sm_obj, error);
267686714001SSerapheim Dimitropoulos 				return (error);
267786714001SSerapheim Dimitropoulos 			}
267886714001SSerapheim Dimitropoulos 			ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
267986714001SSerapheim Dimitropoulos 			space_map_update(vd->vdev_checkpoint_sm);
268086714001SSerapheim Dimitropoulos 
268186714001SSerapheim Dimitropoulos 			/*
268286714001SSerapheim Dimitropoulos 			 * Since the checkpoint_sm contains free entries
268386714001SSerapheim Dimitropoulos 			 * exclusively we can use sm_alloc to indicate the
268486714001SSerapheim Dimitropoulos 			 * culmulative checkpointed space that has been freed.
268586714001SSerapheim Dimitropoulos 			 */
268686714001SSerapheim Dimitropoulos 			vd->vdev_stat.vs_checkpoint_space =
268786714001SSerapheim Dimitropoulos 			    -vd->vdev_checkpoint_sm->sm_alloc;
268886714001SSerapheim Dimitropoulos 			vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
268986714001SSerapheim Dimitropoulos 			    vd->vdev_stat.vs_checkpoint_space;
269086714001SSerapheim Dimitropoulos 		}
26915cabbc6bSPrashanth Sreenivasa 	}
2692fa9e4066Sahrens 
2693fa9e4066Sahrens 	/*
2694fa9e4066Sahrens 	 * If this is a leaf vdev, load its DTL.
2695fa9e4066Sahrens 	 */
26965cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
2697560e6e96Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2698560e6e96Seschrock 		    VDEV_AUX_CORRUPT_DATA);
26993ee8c80cSPavel Zakharov 		vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
27003ee8c80cSPavel Zakharov 		    "[error=%d]", error);
27015cabbc6bSPrashanth Sreenivasa 		return (error);
27025cabbc6bSPrashanth Sreenivasa 	}
27035cabbc6bSPrashanth Sreenivasa 
27045cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
27055cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
27065cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
27075cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_asize != 0);
270886714001SSerapheim Dimitropoulos 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
27095cabbc6bSPrashanth Sreenivasa 
27105cabbc6bSPrashanth Sreenivasa 		if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
27115cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object, 0, vd->vdev_asize, 0))) {
27125cabbc6bSPrashanth Sreenivasa 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
27135cabbc6bSPrashanth Sreenivasa 			    VDEV_AUX_CORRUPT_DATA);
27143ee8c80cSPavel Zakharov 			vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
27153ee8c80cSPavel Zakharov 			    "obsolete spacemap (obj %llu) [error=%d]",
27163ee8c80cSPavel Zakharov 			    (u_longlong_t)obsolete_sm_object, error);
27175cabbc6bSPrashanth Sreenivasa 			return (error);
27185cabbc6bSPrashanth Sreenivasa 		}
27195cabbc6bSPrashanth Sreenivasa 		space_map_update(vd->vdev_obsolete_sm);
27205cabbc6bSPrashanth Sreenivasa 	}
27215cabbc6bSPrashanth Sreenivasa 
27225cabbc6bSPrashanth Sreenivasa 	return (0);
2723fa9e4066Sahrens }
2724fa9e4066Sahrens 
272599653d4eSeschrock /*
2726fa94a07fSbrendan  * The special vdev case is used for hot spares and l2cache devices.  Its
2727fa94a07fSbrendan  * sole purpose it to set the vdev state for the associated vdev.  To do this,
2728fa94a07fSbrendan  * we make sure that we can open the underlying device, then try to read the
2729fa94a07fSbrendan  * label, and make sure that the label is sane and that it hasn't been
2730fa94a07fSbrendan  * repurposed to another pool.
273199653d4eSeschrock  */
273299653d4eSeschrock int
2733fa94a07fSbrendan vdev_validate_aux(vdev_t *vd)
273499653d4eSeschrock {
273599653d4eSeschrock 	nvlist_t *label;
273699653d4eSeschrock 	uint64_t guid, version;
273799653d4eSeschrock 	uint64_t state;
273899653d4eSeschrock 
2739e14bb325SJeff Bonwick 	if (!vdev_readable(vd))
2740c5904d13Seschrock 		return (0);
2741c5904d13Seschrock 
2742dfbb9432SGeorge Wilson 	if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
274399653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
274499653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
274599653d4eSeschrock 		return (-1);
274699653d4eSeschrock 	}
274799653d4eSeschrock 
274899653d4eSeschrock 	if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2749ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(version) ||
275099653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
275199653d4eSeschrock 	    guid != vd->vdev_guid ||
275299653d4eSeschrock 	    nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
275399653d4eSeschrock 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
275499653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
275599653d4eSeschrock 		nvlist_free(label);
275699653d4eSeschrock 		return (-1);
275799653d4eSeschrock 	}
275899653d4eSeschrock 
275999653d4eSeschrock 	/*
276099653d4eSeschrock 	 * We don't actually check the pool state here.  If it's in fact in
276199653d4eSeschrock 	 * use by another pool, we update this fact on the fly when requested.
276299653d4eSeschrock 	 */
276399653d4eSeschrock 	nvlist_free(label);
276499653d4eSeschrock 	return (0);
276599653d4eSeschrock }
276699653d4eSeschrock 
27675cabbc6bSPrashanth Sreenivasa /*
27685cabbc6bSPrashanth Sreenivasa  * Free the objects used to store this vdev's spacemaps, and the array
27695cabbc6bSPrashanth Sreenivasa  * that points to them.
27705cabbc6bSPrashanth Sreenivasa  */
277188ecc943SGeorge Wilson void
27725cabbc6bSPrashanth Sreenivasa vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
27735cabbc6bSPrashanth Sreenivasa {
27745cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0)
27755cabbc6bSPrashanth Sreenivasa 		return;
27765cabbc6bSPrashanth Sreenivasa 
27775cabbc6bSPrashanth Sreenivasa 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
27785cabbc6bSPrashanth Sreenivasa 	uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
27795cabbc6bSPrashanth Sreenivasa 	size_t array_bytes = array_count * sizeof (uint64_t);
27805cabbc6bSPrashanth Sreenivasa 	uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
27815cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
27825cabbc6bSPrashanth Sreenivasa 	    array_bytes, smobj_array, 0));
27835cabbc6bSPrashanth Sreenivasa 
27845cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < array_count; i++) {
27855cabbc6bSPrashanth Sreenivasa 		uint64_t smobj = smobj_array[i];
27865cabbc6bSPrashanth Sreenivasa 		if (smobj == 0)
27875cabbc6bSPrashanth Sreenivasa 			continue;
27885cabbc6bSPrashanth Sreenivasa 
27895cabbc6bSPrashanth Sreenivasa 		space_map_free_obj(mos, smobj, tx);
27905cabbc6bSPrashanth Sreenivasa 	}
27915cabbc6bSPrashanth Sreenivasa 
27925cabbc6bSPrashanth Sreenivasa 	kmem_free(smobj_array, array_bytes);
27935cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
27945cabbc6bSPrashanth Sreenivasa 	vd->vdev_ms_array = 0;
27955cabbc6bSPrashanth Sreenivasa }
27965cabbc6bSPrashanth Sreenivasa 
27975cabbc6bSPrashanth Sreenivasa static void
27985cabbc6bSPrashanth Sreenivasa vdev_remove_empty(vdev_t *vd, uint64_t txg)
279988ecc943SGeorge Wilson {
280088ecc943SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
280188ecc943SGeorge Wilson 	dmu_tx_t *tx;
280288ecc943SGeorge Wilson 
2803215198a6SJoe Stein 	ASSERT(vd == vd->vdev_top);
2804215198a6SJoe Stein 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
280588ecc943SGeorge Wilson 
280688ecc943SGeorge Wilson 	if (vd->vdev_ms != NULL) {
28072e4c9986SGeorge Wilson 		metaslab_group_t *mg = vd->vdev_mg;
28082e4c9986SGeorge Wilson 
28092e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
28102e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
28112e4c9986SGeorge Wilson 
281288ecc943SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
281388ecc943SGeorge Wilson 			metaslab_t *msp = vd->vdev_ms[m];
281488ecc943SGeorge Wilson 
28150713e232SGeorge Wilson 			if (msp == NULL || msp->ms_sm == NULL)
281688ecc943SGeorge Wilson 				continue;
281788ecc943SGeorge Wilson 
28180713e232SGeorge Wilson 			mutex_enter(&msp->ms_lock);
28192e4c9986SGeorge Wilson 			/*
28202e4c9986SGeorge Wilson 			 * If the metaslab was not loaded when the vdev
28212e4c9986SGeorge Wilson 			 * was removed then the histogram accounting may
28222e4c9986SGeorge Wilson 			 * not be accurate. Update the histogram information
28232e4c9986SGeorge Wilson 			 * here so that we ensure that the metaslab group
28242e4c9986SGeorge Wilson 			 * and metaslab class are up-to-date.
28252e4c9986SGeorge Wilson 			 */
28262e4c9986SGeorge Wilson 			metaslab_group_histogram_remove(mg, msp);
28272e4c9986SGeorge Wilson 
28280713e232SGeorge Wilson 			VERIFY0(space_map_allocated(msp->ms_sm));
28290713e232SGeorge Wilson 			space_map_close(msp->ms_sm);
28300713e232SGeorge Wilson 			msp->ms_sm = NULL;
28310713e232SGeorge Wilson 			mutex_exit(&msp->ms_lock);
283288ecc943SGeorge Wilson 		}
28332e4c9986SGeorge Wilson 
283486714001SSerapheim Dimitropoulos 		if (vd->vdev_checkpoint_sm != NULL) {
283586714001SSerapheim Dimitropoulos 			ASSERT(spa_has_checkpoint(spa));
283686714001SSerapheim Dimitropoulos 			space_map_close(vd->vdev_checkpoint_sm);
283786714001SSerapheim Dimitropoulos 			vd->vdev_checkpoint_sm = NULL;
283886714001SSerapheim Dimitropoulos 		}
283986714001SSerapheim Dimitropoulos 
28402e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
28412e4c9986SGeorge Wilson 		metaslab_class_histogram_verify(mg->mg_class);
28422e4c9986SGeorge Wilson 		for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
28432e4c9986SGeorge Wilson 			ASSERT0(mg->mg_histogram[i]);
284488ecc943SGeorge Wilson 	}
284588ecc943SGeorge Wilson 
28465cabbc6bSPrashanth Sreenivasa 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
28475cabbc6bSPrashanth Sreenivasa 	vdev_destroy_spacemaps(vd, tx);
2848215198a6SJoe Stein 
2849215198a6SJoe Stein 	if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2850215198a6SJoe Stein 		vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2851215198a6SJoe Stein 		vd->vdev_top_zap = 0;
2852215198a6SJoe Stein 	}
285388ecc943SGeorge Wilson 	dmu_tx_commit(tx);
285488ecc943SGeorge Wilson }
285588ecc943SGeorge Wilson 
2856fa9e4066Sahrens void
2857fa9e4066Sahrens vdev_sync_done(vdev_t *vd, uint64_t txg)
2858fa9e4066Sahrens {
2859fa9e4066Sahrens 	metaslab_t *msp;
286080eb36f2SGeorge Wilson 	boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2861fa9e4066Sahrens 
28625cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
286388ecc943SGeorge Wilson 
2864*094e47e9SGeorge Wilson 	while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2865*094e47e9SGeorge Wilson 	    != NULL)
2866fa9e4066Sahrens 		metaslab_sync_done(msp, txg);
286780eb36f2SGeorge Wilson 
286880eb36f2SGeorge Wilson 	if (reassess)
286980eb36f2SGeorge Wilson 		metaslab_sync_reassess(vd->vdev_mg);
2870fa9e4066Sahrens }
2871fa9e4066Sahrens 
2872fa9e4066Sahrens void
2873fa9e4066Sahrens vdev_sync(vdev_t *vd, uint64_t txg)
2874fa9e4066Sahrens {
2875fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
2876fa9e4066Sahrens 	vdev_t *lvd;
2877fa9e4066Sahrens 	metaslab_t *msp;
2878ecc2d604Sbonwick 	dmu_tx_t *tx;
2879fa9e4066Sahrens 
28805cabbc6bSPrashanth Sreenivasa 	if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
28815cabbc6bSPrashanth Sreenivasa 		dmu_tx_t *tx;
28825cabbc6bSPrashanth Sreenivasa 
28835cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_removing ||
28845cabbc6bSPrashanth Sreenivasa 		    vd->vdev_ops == &vdev_indirect_ops);
288588ecc943SGeorge Wilson 
28865cabbc6bSPrashanth Sreenivasa 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
28875cabbc6bSPrashanth Sreenivasa 		vdev_indirect_sync_obsolete(vd, tx);
28885cabbc6bSPrashanth Sreenivasa 		dmu_tx_commit(tx);
28895cabbc6bSPrashanth Sreenivasa 
28905cabbc6bSPrashanth Sreenivasa 		/*
28915cabbc6bSPrashanth Sreenivasa 		 * If the vdev is indirect, it can't have dirty
28925cabbc6bSPrashanth Sreenivasa 		 * metaslabs or DTLs.
28935cabbc6bSPrashanth Sreenivasa 		 */
28945cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_ops == &vdev_indirect_ops) {
28955cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
28965cabbc6bSPrashanth Sreenivasa 			ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
28975cabbc6bSPrashanth Sreenivasa 			return;
28985cabbc6bSPrashanth Sreenivasa 		}
28995cabbc6bSPrashanth Sreenivasa 	}
29005cabbc6bSPrashanth Sreenivasa 
29015cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
29025cabbc6bSPrashanth Sreenivasa 
29035cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
29045cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_removing) {
2905ecc2d604Sbonwick 		ASSERT(vd == vd->vdev_top);
29065cabbc6bSPrashanth Sreenivasa 		ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
2907ecc2d604Sbonwick 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2908ecc2d604Sbonwick 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2909ecc2d604Sbonwick 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2910ecc2d604Sbonwick 		ASSERT(vd->vdev_ms_array != 0);
2911ecc2d604Sbonwick 		vdev_config_dirty(vd);
2912ecc2d604Sbonwick 		dmu_tx_commit(tx);
2913ecc2d604Sbonwick 	}
2914fa9e4066Sahrens 
2915ecc2d604Sbonwick 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2916fa9e4066Sahrens 		metaslab_sync(msp, txg);
2917ecc2d604Sbonwick 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2918ecc2d604Sbonwick 	}
2919fa9e4066Sahrens 
2920fa9e4066Sahrens 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2921fa9e4066Sahrens 		vdev_dtl_sync(lvd, txg);
2922fa9e4066Sahrens 
29235cabbc6bSPrashanth Sreenivasa 	/*
29245cabbc6bSPrashanth Sreenivasa 	 * Remove the metadata associated with this vdev once it's empty.
29255cabbc6bSPrashanth Sreenivasa 	 * Note that this is typically used for log/cache device removal;
29265cabbc6bSPrashanth Sreenivasa 	 * we don't empty toplevel vdevs when removing them.  But if
29275cabbc6bSPrashanth Sreenivasa 	 * a toplevel happens to be emptied, this is not harmful.
29285cabbc6bSPrashanth Sreenivasa 	 */
29295cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) {
29305cabbc6bSPrashanth Sreenivasa 		vdev_remove_empty(vd, txg);
29315cabbc6bSPrashanth Sreenivasa 	}
29325cabbc6bSPrashanth Sreenivasa 
2933fa9e4066Sahrens 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2934fa9e4066Sahrens }
2935fa9e4066Sahrens 
2936fa9e4066Sahrens uint64_t
2937fa9e4066Sahrens vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2938fa9e4066Sahrens {
2939fa9e4066Sahrens 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
2940fa9e4066Sahrens }
2941fa9e4066Sahrens 
29423d7072f8Seschrock /*
29433d7072f8Seschrock  * Mark the given vdev faulted.  A faulted vdev behaves as if the device could
29443d7072f8Seschrock  * not be opened, and no I/O is attempted.
29453d7072f8Seschrock  */
2946fa9e4066Sahrens int
2947069f55e2SEric Schrock vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2948fa9e4066Sahrens {
29494b964adaSGeorge Wilson 	vdev_t *vd, *tvd;
2950fa9e4066Sahrens 
29518f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
2952fa9e4066Sahrens 
2953c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2954e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
2955e14bb325SJeff Bonwick 
29563d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
2957e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2958fa9e4066Sahrens 
29594b964adaSGeorge Wilson 	tvd = vd->vdev_top;
29604b964adaSGeorge Wilson 
2961069f55e2SEric Schrock 	/*
2962069f55e2SEric Schrock 	 * We don't directly use the aux state here, but if we do a
2963069f55e2SEric Schrock 	 * vdev_reopen(), we need this value to be present to remember why we
2964069f55e2SEric Schrock 	 * were faulted.
2965069f55e2SEric Schrock 	 */
2966069f55e2SEric Schrock 	vd->vdev_label_aux = aux;
2967069f55e2SEric Schrock 
29683d7072f8Seschrock 	/*
29693d7072f8Seschrock 	 * Faulted state takes precedence over degraded.
29703d7072f8Seschrock 	 */
297198d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
29723d7072f8Seschrock 	vd->vdev_faulted = 1ULL;
29733d7072f8Seschrock 	vd->vdev_degraded = 0ULL;
2974069f55e2SEric Schrock 	vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
29753d7072f8Seschrock 
29763d7072f8Seschrock 	/*
2977c79790bcSGeorge Wilson 	 * If this device has the only valid copy of the data, then
2978c79790bcSGeorge Wilson 	 * back off and simply mark the vdev as degraded instead.
29793d7072f8Seschrock 	 */
29804b964adaSGeorge Wilson 	if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
29813d7072f8Seschrock 		vd->vdev_degraded = 1ULL;
29823d7072f8Seschrock 		vd->vdev_faulted = 0ULL;
29833d7072f8Seschrock 
29843d7072f8Seschrock 		/*
29853d7072f8Seschrock 		 * If we reopen the device and it's not dead, only then do we
29863d7072f8Seschrock 		 * mark it degraded.
29873d7072f8Seschrock 		 */
29884b964adaSGeorge Wilson 		vdev_reopen(tvd);
29893d7072f8Seschrock 
2990069f55e2SEric Schrock 		if (vdev_readable(vd))
2991069f55e2SEric Schrock 			vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
29923d7072f8Seschrock 	}
29933d7072f8Seschrock 
2994e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
29953d7072f8Seschrock }
29963d7072f8Seschrock 
29973d7072f8Seschrock /*
29983d7072f8Seschrock  * Mark the given vdev degraded.  A degraded vdev is purely an indication to the
29993d7072f8Seschrock  * user that something is wrong.  The vdev continues to operate as normal as far
30003d7072f8Seschrock  * as I/O is concerned.
30013d7072f8Seschrock  */
30023d7072f8Seschrock int
3003069f55e2SEric Schrock vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
30043d7072f8Seschrock {
3005c5904d13Seschrock 	vdev_t *vd;
30060a4e9518Sgw 
30078f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
30083d7072f8Seschrock 
3009c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3010e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3011e14bb325SJeff Bonwick 
30120e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
3013e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
30140e34b6a7Sbonwick 
30153d7072f8Seschrock 	/*
30163d7072f8Seschrock 	 * If the vdev is already faulted, then don't do anything.
30173d7072f8Seschrock 	 */
3018e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded)
3019e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, 0));
30203d7072f8Seschrock 
30213d7072f8Seschrock 	vd->vdev_degraded = 1ULL;
30223d7072f8Seschrock 	if (!vdev_is_dead(vd))
30233d7072f8Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3024069f55e2SEric Schrock 		    aux);
30253d7072f8Seschrock 
3026e14bb325SJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
30273d7072f8Seschrock }
30283d7072f8Seschrock 
30293d7072f8Seschrock /*
3030f7170741SWill Andrews  * Online the given vdev.
3031f7170741SWill Andrews  *
3032f7170741SWill Andrews  * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things.  First, any attached
3033f7170741SWill Andrews  * spare device should be detached when the device finishes resilvering.
3034f7170741SWill Andrews  * Second, the online should be treated like a 'test' online case, so no FMA
3035f7170741SWill Andrews  * events are generated if the device fails to open.
30363d7072f8Seschrock  */
30373d7072f8Seschrock int
3038e14bb325SJeff Bonwick vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
30393d7072f8Seschrock {
3040573ca77eSGeorge Wilson 	vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
30415f368aefSYuri Pankov 	boolean_t wasoffline;
30425f368aefSYuri Pankov 	vdev_state_t oldstate;
30433d7072f8Seschrock 
30448f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
30453d7072f8Seschrock 
3046c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3047e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
30483d7072f8Seschrock 
30493d7072f8Seschrock 	if (!vd->vdev_ops->vdev_op_leaf)
3050e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3051fa9e4066Sahrens 
30525f368aefSYuri Pankov 	wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
30535f368aefSYuri Pankov 	oldstate = vd->vdev_state;
305414372834SHans Rosenfeld 
3055573ca77eSGeorge Wilson 	tvd = vd->vdev_top;
3056fa9e4066Sahrens 	vd->vdev_offline = B_FALSE;
3057441d80aaSlling 	vd->vdev_tmpoffline = B_FALSE;
3058e14bb325SJeff Bonwick 	vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3059e14bb325SJeff Bonwick 	vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3060573ca77eSGeorge Wilson 
3061573ca77eSGeorge Wilson 	/* XXX - L2ARC 1.0 does not support expansion */
3062573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3063573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3064573ca77eSGeorge Wilson 			pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
3065573ca77eSGeorge Wilson 	}
3066573ca77eSGeorge Wilson 
3067573ca77eSGeorge Wilson 	vdev_reopen(tvd);
30683d7072f8Seschrock 	vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
30693d7072f8Seschrock 
3070573ca77eSGeorge Wilson 	if (!vd->vdev_aux) {
3071573ca77eSGeorge Wilson 		for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3072573ca77eSGeorge Wilson 			pvd->vdev_expanding = B_FALSE;
3073573ca77eSGeorge Wilson 	}
3074573ca77eSGeorge Wilson 
30753d7072f8Seschrock 	if (newstate)
30763d7072f8Seschrock 		*newstate = vd->vdev_state;
30773d7072f8Seschrock 	if ((flags & ZFS_ONLINE_UNSPARE) &&
30783d7072f8Seschrock 	    !vdev_is_dead(vd) && vd->vdev_parent &&
30793d7072f8Seschrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
30803d7072f8Seschrock 	    vd->vdev_parent->vdev_child[0] == vd)
30813d7072f8Seschrock 		vd->vdev_unspare = B_TRUE;
3082fa9e4066Sahrens 
3083573ca77eSGeorge Wilson 	if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3084573ca77eSGeorge Wilson 
3085573ca77eSGeorge Wilson 		/* XXX - L2ARC 1.0 does not support expansion */
3086573ca77eSGeorge Wilson 		if (vd->vdev_aux)
3087573ca77eSGeorge Wilson 			return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3088573ca77eSGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3089573ca77eSGeorge Wilson 	}
309014372834SHans Rosenfeld 
3091*094e47e9SGeorge Wilson 	/* Restart initializing if necessary */
3092*094e47e9SGeorge Wilson 	mutex_enter(&vd->vdev_initialize_lock);
3093*094e47e9SGeorge Wilson 	if (vdev_writeable(vd) &&
3094*094e47e9SGeorge Wilson 	    vd->vdev_initialize_thread == NULL &&
3095*094e47e9SGeorge Wilson 	    vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
3096*094e47e9SGeorge Wilson 		(void) vdev_initialize(vd);
3097*094e47e9SGeorge Wilson 	}
3098*094e47e9SGeorge Wilson 	mutex_exit(&vd->vdev_initialize_lock);
3099*094e47e9SGeorge Wilson 
31005f368aefSYuri Pankov 	if (wasoffline ||
31015f368aefSYuri Pankov 	    (oldstate < VDEV_STATE_DEGRADED &&
31025f368aefSYuri Pankov 	    vd->vdev_state >= VDEV_STATE_DEGRADED))
3103ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
310414372834SHans Rosenfeld 
31058ad4d6ddSJeff Bonwick 	return (spa_vdev_state_exit(spa, vd, 0));
3106fa9e4066Sahrens }
3107fa9e4066Sahrens 
3108a1521560SJeff Bonwick static int
3109a1521560SJeff Bonwick vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3110fa9e4066Sahrens {
3111e6ca193dSGeorge Wilson 	vdev_t *vd, *tvd;
31128f18d1faSGeorge Wilson 	int error = 0;
31138f18d1faSGeorge Wilson 	uint64_t generation;
31148f18d1faSGeorge Wilson 	metaslab_group_t *mg;
31150a4e9518Sgw 
31168f18d1faSGeorge Wilson top:
31178f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_ALLOC);
3118fa9e4066Sahrens 
3119c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3120e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENODEV));
3121fa9e4066Sahrens 
31220e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
3123e14bb325SJeff Bonwick 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
31240e34b6a7Sbonwick 
3125e6ca193dSGeorge Wilson 	tvd = vd->vdev_top;
31268f18d1faSGeorge Wilson 	mg = tvd->vdev_mg;
31278f18d1faSGeorge Wilson 	generation = spa->spa_config_generation + 1;
3128e6ca193dSGeorge Wilson 
3129fa9e4066Sahrens 	/*
3130ecc2d604Sbonwick 	 * If the device isn't already offline, try to offline it.
3131fa9e4066Sahrens 	 */
3132ecc2d604Sbonwick 	if (!vd->vdev_offline) {
3133ecc2d604Sbonwick 		/*
31348ad4d6ddSJeff Bonwick 		 * If this device has the only valid copy of some data,
3135e6ca193dSGeorge Wilson 		 * don't allow it to be offlined. Log devices are always
3136e6ca193dSGeorge Wilson 		 * expendable.
3137ecc2d604Sbonwick 		 */
3138e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3139e6ca193dSGeorge Wilson 		    vdev_dtl_required(vd))
3140e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3141fa9e4066Sahrens 
31428f18d1faSGeorge Wilson 		/*
3143b24ab676SJeff Bonwick 		 * If the top-level is a slog and it has had allocations
3144b24ab676SJeff Bonwick 		 * then proceed.  We check that the vdev's metaslab group
3145b24ab676SJeff Bonwick 		 * is not NULL since it's possible that we may have just
3146b24ab676SJeff Bonwick 		 * added this vdev but not yet initialized its metaslabs.
31478f18d1faSGeorge Wilson 		 */
31488f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL) {
31498f18d1faSGeorge Wilson 			/*
31508f18d1faSGeorge Wilson 			 * Prevent any future allocations.
31518f18d1faSGeorge Wilson 			 */
3152a1521560SJeff Bonwick 			metaslab_group_passivate(mg);
31538f18d1faSGeorge Wilson 			(void) spa_vdev_state_exit(spa, vd, 0);
31548f18d1faSGeorge Wilson 
31555cabbc6bSPrashanth Sreenivasa 			error = spa_reset_logs(spa);
31568f18d1faSGeorge Wilson 
315786714001SSerapheim Dimitropoulos 			/*
315886714001SSerapheim Dimitropoulos 			 * If the log device was successfully reset but has
315986714001SSerapheim Dimitropoulos 			 * checkpointed data, do not offline it.
316086714001SSerapheim Dimitropoulos 			 */
316186714001SSerapheim Dimitropoulos 			if (error == 0 &&
316286714001SSerapheim Dimitropoulos 			    tvd->vdev_checkpoint_sm != NULL) {
316386714001SSerapheim Dimitropoulos 				ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc,
316486714001SSerapheim Dimitropoulos 				    !=, 0);
316586714001SSerapheim Dimitropoulos 				error = ZFS_ERR_CHECKPOINT_EXISTS;
316686714001SSerapheim Dimitropoulos 			}
316786714001SSerapheim Dimitropoulos 
31688f18d1faSGeorge Wilson 			spa_vdev_state_enter(spa, SCL_ALLOC);
31698f18d1faSGeorge Wilson 
31708f18d1faSGeorge Wilson 			/*
31718f18d1faSGeorge Wilson 			 * Check to see if the config has changed.
31728f18d1faSGeorge Wilson 			 */
31738f18d1faSGeorge Wilson 			if (error || generation != spa->spa_config_generation) {
3174a1521560SJeff Bonwick 				metaslab_group_activate(mg);
31758f18d1faSGeorge Wilson 				if (error)
31768f18d1faSGeorge Wilson 					return (spa_vdev_state_exit(spa,
31778f18d1faSGeorge Wilson 					    vd, error));
31788f18d1faSGeorge Wilson 				(void) spa_vdev_state_exit(spa, vd, 0);
31798f18d1faSGeorge Wilson 				goto top;
31808f18d1faSGeorge Wilson 			}
3181fb09f5aaSMadhav Suresh 			ASSERT0(tvd->vdev_stat.vs_alloc);
31828f18d1faSGeorge Wilson 		}
31838f18d1faSGeorge Wilson 
3184ecc2d604Sbonwick 		/*
3185ecc2d604Sbonwick 		 * Offline this device and reopen its top-level vdev.
3186e6ca193dSGeorge Wilson 		 * If the top-level vdev is a log device then just offline
3187e6ca193dSGeorge Wilson 		 * it. Otherwise, if this action results in the top-level
3188e6ca193dSGeorge Wilson 		 * vdev becoming unusable, undo it and fail the request.
3189ecc2d604Sbonwick 		 */
3190ecc2d604Sbonwick 		vd->vdev_offline = B_TRUE;
3191e6ca193dSGeorge Wilson 		vdev_reopen(tvd);
3192e6ca193dSGeorge Wilson 
3193e6ca193dSGeorge Wilson 		if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3194e6ca193dSGeorge Wilson 		    vdev_is_dead(tvd)) {
3195ecc2d604Sbonwick 			vd->vdev_offline = B_FALSE;
3196e6ca193dSGeorge Wilson 			vdev_reopen(tvd);
3197e14bb325SJeff Bonwick 			return (spa_vdev_state_exit(spa, NULL, EBUSY));
3198ecc2d604Sbonwick 		}
31998f18d1faSGeorge Wilson 
32008f18d1faSGeorge Wilson 		/*
32018f18d1faSGeorge Wilson 		 * Add the device back into the metaslab rotor so that
32028f18d1faSGeorge Wilson 		 * once we online the device it's open for business.
32038f18d1faSGeorge Wilson 		 */
32048f18d1faSGeorge Wilson 		if (tvd->vdev_islog && mg != NULL)
3205a1521560SJeff Bonwick 			metaslab_group_activate(mg);
3206fa9e4066Sahrens 	}
3207fa9e4066Sahrens 
3208e14bb325SJeff Bonwick 	vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3209ecc2d604Sbonwick 
32108f18d1faSGeorge Wilson 	return (spa_vdev_state_exit(spa, vd, 0));
3211fa9e4066Sahrens }
3212fa9e4066Sahrens 
3213a1521560SJeff Bonwick int
3214a1521560SJeff Bonwick vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3215a1521560SJeff Bonwick {
3216a1521560SJeff Bonwick 	int error;
3217a1521560SJeff Bonwick 
3218a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
3219a1521560SJeff Bonwick 	error = vdev_offline_locked(spa, guid, flags);
3220a1521560SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
3221a1521560SJeff Bonwick 
3222a1521560SJeff Bonwick 	return (error);
3223a1521560SJeff Bonwick }
3224a1521560SJeff Bonwick 
3225ea8dc4b6Seschrock /*
3226ea8dc4b6Seschrock  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
3227ea8dc4b6Seschrock  * vdev_offline(), we assume the spa config is locked.  We also clear all
3228ea8dc4b6Seschrock  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
3229ea8dc4b6Seschrock  */
3230ea8dc4b6Seschrock void
3231e14bb325SJeff Bonwick vdev_clear(spa_t *spa, vdev_t *vd)
3232fa9e4066Sahrens {
3233e14bb325SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3234e14bb325SJeff Bonwick 
3235e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3236fa9e4066Sahrens 
3237ea8dc4b6Seschrock 	if (vd == NULL)
3238e14bb325SJeff Bonwick 		vd = rvd;
3239fa9e4066Sahrens 
3240ea8dc4b6Seschrock 	vd->vdev_stat.vs_read_errors = 0;
3241ea8dc4b6Seschrock 	vd->vdev_stat.vs_write_errors = 0;
3242ea8dc4b6Seschrock 	vd->vdev_stat.vs_checksum_errors = 0;
3243fa9e4066Sahrens 
3244e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
3245e14bb325SJeff Bonwick 		vdev_clear(spa, vd->vdev_child[c]);
32463d7072f8Seschrock 
32475cabbc6bSPrashanth Sreenivasa 	/*
32485cabbc6bSPrashanth Sreenivasa 	 * It makes no sense to "clear" an indirect vdev.
32495cabbc6bSPrashanth Sreenivasa 	 */
32505cabbc6bSPrashanth Sreenivasa 	if (!vdev_is_concrete(vd))
32515cabbc6bSPrashanth Sreenivasa 		return;
32525cabbc6bSPrashanth Sreenivasa 
32533d7072f8Seschrock 	/*
32548a79c1b5Sek 	 * If we're in the FAULTED state or have experienced failed I/O, then
32558a79c1b5Sek 	 * clear the persistent state and attempt to reopen the device.  We
32568a79c1b5Sek 	 * also mark the vdev config dirty, so that the new faulted state is
32578a79c1b5Sek 	 * written out to disk.
32583d7072f8Seschrock 	 */
3259e14bb325SJeff Bonwick 	if (vd->vdev_faulted || vd->vdev_degraded ||
3260e14bb325SJeff Bonwick 	    !vdev_readable(vd) || !vdev_writeable(vd)) {
32618a79c1b5Sek 
3262096d22d4SEric Schrock 		/*
3263096d22d4SEric Schrock 		 * When reopening in reponse to a clear event, it may be due to
3264096d22d4SEric Schrock 		 * a fmadm repair request.  In this case, if the device is
3265096d22d4SEric Schrock 		 * still broken, we want to still post the ereport again.
3266096d22d4SEric Schrock 		 */
3267096d22d4SEric Schrock 		vd->vdev_forcefault = B_TRUE;
3268096d22d4SEric Schrock 
32694b964adaSGeorge Wilson 		vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3270e14bb325SJeff Bonwick 		vd->vdev_cant_read = B_FALSE;
3271e14bb325SJeff Bonwick 		vd->vdev_cant_write = B_FALSE;
3272e14bb325SJeff Bonwick 
3273f9af39baSGeorge Wilson 		vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
32743d7072f8Seschrock 
3275096d22d4SEric Schrock 		vd->vdev_forcefault = B_FALSE;
3276096d22d4SEric Schrock 
3277f9af39baSGeorge Wilson 		if (vd != rvd && vdev_writeable(vd->vdev_top))
3278e14bb325SJeff Bonwick 			vdev_state_dirty(vd->vdev_top);
3279e14bb325SJeff Bonwick 
3280e14bb325SJeff Bonwick 		if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
3281bb8b5132Sek 			spa_async_request(spa, SPA_ASYNC_RESILVER);
32823d7072f8Seschrock 
3283ce1577b0SDave Eddy 		spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
32843d7072f8Seschrock 	}
3285096d22d4SEric Schrock 
3286096d22d4SEric Schrock 	/*
3287096d22d4SEric Schrock 	 * When clearing a FMA-diagnosed fault, we always want to
3288096d22d4SEric Schrock 	 * unspare the device, as we assume that the original spare was
3289096d22d4SEric Schrock 	 * done in response to the FMA fault.
3290096d22d4SEric Schrock 	 */
3291096d22d4SEric Schrock 	if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3292096d22d4SEric Schrock 	    vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3293096d22d4SEric Schrock 	    vd->vdev_parent->vdev_child[0] == vd)
3294096d22d4SEric Schrock 		vd->vdev_unspare = B_TRUE;
3295fa9e4066Sahrens }
3296fa9e4066Sahrens 
3297e14bb325SJeff Bonwick boolean_t
3298e14bb325SJeff Bonwick vdev_is_dead(vdev_t *vd)
32990a4e9518Sgw {
330088ecc943SGeorge Wilson 	/*
330188ecc943SGeorge Wilson 	 * Holes and missing devices are always considered "dead".
330288ecc943SGeorge Wilson 	 * This simplifies the code since we don't have to check for
330388ecc943SGeorge Wilson 	 * these types of devices in the various code paths.
330488ecc943SGeorge Wilson 	 * Instead we rely on the fact that we skip over dead devices
330588ecc943SGeorge Wilson 	 * before issuing I/O to them.
330688ecc943SGeorge Wilson 	 */
33075cabbc6bSPrashanth Sreenivasa 	return (vd->vdev_state < VDEV_STATE_DEGRADED ||
33085cabbc6bSPrashanth Sreenivasa 	    vd->vdev_ops == &vdev_hole_ops ||
330988ecc943SGeorge Wilson 	    vd->vdev_ops == &vdev_missing_ops);
33100a4e9518Sgw }
33110a4e9518Sgw 
3312e14bb325SJeff Bonwick boolean_t
3313e14bb325SJeff Bonwick vdev_readable(vdev_t *vd)
33140a4e9518Sgw {
3315e14bb325SJeff Bonwick 	return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
33160a4e9518Sgw }
33170a4e9518Sgw 
3318e14bb325SJeff Bonwick boolean_t
3319e14bb325SJeff Bonwick vdev_writeable(vdev_t *vd)
3320fa9e4066Sahrens {
33215cabbc6bSPrashanth Sreenivasa 	return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
33225cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd));
3323fa9e4066Sahrens }
3324fa9e4066Sahrens 
3325a31e6787SGeorge Wilson boolean_t
3326a31e6787SGeorge Wilson vdev_allocatable(vdev_t *vd)
3327a31e6787SGeorge Wilson {
33288ad4d6ddSJeff Bonwick 	uint64_t state = vd->vdev_state;
33298ad4d6ddSJeff Bonwick 
3330a31e6787SGeorge Wilson 	/*
33318ad4d6ddSJeff Bonwick 	 * We currently allow allocations from vdevs which may be in the
3332a31e6787SGeorge Wilson 	 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3333a31e6787SGeorge Wilson 	 * fails to reopen then we'll catch it later when we're holding
33348ad4d6ddSJeff Bonwick 	 * the proper locks.  Note that we have to get the vdev state
33358ad4d6ddSJeff Bonwick 	 * in a local variable because although it changes atomically,
33368ad4d6ddSJeff Bonwick 	 * we're asking two separate questions about it.
3337a31e6787SGeorge Wilson 	 */
33388ad4d6ddSJeff Bonwick 	return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
33395cabbc6bSPrashanth Sreenivasa 	    !vd->vdev_cant_write && vdev_is_concrete(vd) &&
33400f7643c7SGeorge Wilson 	    vd->vdev_mg->mg_initialized);
3341a31e6787SGeorge Wilson }
3342a31e6787SGeorge Wilson 
3343e14bb325SJeff Bonwick boolean_t
3344e14bb325SJeff Bonwick vdev_accessible(vdev_t *vd, zio_t *zio)
3345fa9e4066Sahrens {
3346e14bb325SJeff Bonwick 	ASSERT(zio->io_vd == vd);
3347fa9e4066Sahrens 
3348e14bb325SJeff Bonwick 	if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3349e14bb325SJeff Bonwick 		return (B_FALSE);
3350fa9e4066Sahrens 
3351e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_READ)
3352e14bb325SJeff Bonwick 		return (!vd->vdev_cant_read);
3353fa9e4066Sahrens 
3354e14bb325SJeff Bonwick 	if (zio->io_type == ZIO_TYPE_WRITE)
3355e14bb325SJeff Bonwick 		return (!vd->vdev_cant_write);
3356fa9e4066Sahrens 
3357e14bb325SJeff Bonwick 	return (B_TRUE);
3358fa9e4066Sahrens }
3359fa9e4066Sahrens 
336086714001SSerapheim Dimitropoulos boolean_t
336186714001SSerapheim Dimitropoulos vdev_is_spacemap_addressable(vdev_t *vd)
336286714001SSerapheim Dimitropoulos {
336386714001SSerapheim Dimitropoulos 	/*
336486714001SSerapheim Dimitropoulos 	 * Assuming 47 bits of the space map entry dedicated for the entry's
336586714001SSerapheim Dimitropoulos 	 * offset (see description in space_map.h), we calculate the maximum
336686714001SSerapheim Dimitropoulos 	 * address that can be described by a space map entry for the given
336786714001SSerapheim Dimitropoulos 	 * device.
336886714001SSerapheim Dimitropoulos 	 */
336986714001SSerapheim Dimitropoulos 	uint64_t shift = vd->vdev_ashift + 47;
337086714001SSerapheim Dimitropoulos 
337186714001SSerapheim Dimitropoulos 	if (shift >= 63) /* detect potential overflow */
337286714001SSerapheim Dimitropoulos 		return (B_TRUE);
337386714001SSerapheim Dimitropoulos 
337486714001SSerapheim Dimitropoulos 	return (vd->vdev_asize < (1ULL << shift));
337586714001SSerapheim Dimitropoulos }
337686714001SSerapheim Dimitropoulos 
3377fa9e4066Sahrens /*
3378fa9e4066Sahrens  * Get statistics for the given vdev.
3379fa9e4066Sahrens  */
3380fa9e4066Sahrens void
3381fa9e4066Sahrens vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3382fa9e4066Sahrens {
33832e4c9986SGeorge Wilson 	spa_t *spa = vd->vdev_spa;
33842e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
3385c39a2aaeSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
33862e4c9986SGeorge Wilson 
33872e4c9986SGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3388fa9e4066Sahrens 
3389fa9e4066Sahrens 	mutex_enter(&vd->vdev_stat_lock);
3390fa9e4066Sahrens 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3391fa9e4066Sahrens 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3392fa9e4066Sahrens 	vs->vs_state = vd->vdev_state;
3393573ca77eSGeorge Wilson 	vs->vs_rsize = vdev_get_min_asize(vd);
3394*094e47e9SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3395573ca77eSGeorge Wilson 		vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
3396*094e47e9SGeorge Wilson 		/*
3397*094e47e9SGeorge Wilson 		 * Report intializing progress. Since we don't have the
3398*094e47e9SGeorge Wilson 		 * initializing locks held, this is only an estimate (although a
3399*094e47e9SGeorge Wilson 		 * fairly accurate one).
3400*094e47e9SGeorge Wilson 		 */
3401*094e47e9SGeorge Wilson 		vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done;
3402*094e47e9SGeorge Wilson 		vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est;
3403*094e47e9SGeorge Wilson 		vs->vs_initialize_state = vd->vdev_initialize_state;
3404*094e47e9SGeorge Wilson 		vs->vs_initialize_action_time = vd->vdev_initialize_action_time;
3405*094e47e9SGeorge Wilson 	}
3406c39a2aaeSGeorge Wilson 	/*
3407c39a2aaeSGeorge Wilson 	 * Report expandable space on top-level, non-auxillary devices only.
3408c39a2aaeSGeorge Wilson 	 * The expandable space is reported in terms of metaslab sized units
3409c39a2aaeSGeorge Wilson 	 * since that determines how much space the pool can expand.
3410c39a2aaeSGeorge Wilson 	 */
3411c39a2aaeSGeorge Wilson 	if (vd->vdev_aux == NULL && tvd != NULL) {
34127855d95bSToomas Soome 		vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize -
34137855d95bSToomas Soome 		    spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3414c39a2aaeSGeorge Wilson 	}
34155cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
34165cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd)) {
34172e4c9986SGeorge Wilson 		vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
34182986efa8SAlex Reece 	}
3419fa9e4066Sahrens 
3420fa9e4066Sahrens 	/*
3421fa9e4066Sahrens 	 * If we're getting stats on the root vdev, aggregate the I/O counts
3422fa9e4066Sahrens 	 * over all top-level vdevs (i.e. the direct children of the root).
3423fa9e4066Sahrens 	 */
3424fa9e4066Sahrens 	if (vd == rvd) {
3425e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
3426fa9e4066Sahrens 			vdev_t *cvd = rvd->vdev_child[c];
3427fa9e4066Sahrens 			vdev_stat_t *cvs = &cvd->vdev_stat;
3428fa9e4066Sahrens 
3429e14bb325SJeff Bonwick 			for (int t = 0; t < ZIO_TYPES; t++) {
3430fa9e4066Sahrens 				vs->vs_ops[t] += cvs->vs_ops[t];
3431fa9e4066Sahrens 				vs->vs_bytes[t] += cvs->vs_bytes[t];
3432fa9e4066Sahrens 			}
34333f9d6ad7SLin Ling 			cvs->vs_scan_removing = cvd->vdev_removing;
3434fa9e4066Sahrens 		}
3435fa9e4066Sahrens 	}
34362e4c9986SGeorge Wilson 	mutex_exit(&vd->vdev_stat_lock);
3437fa9e4066Sahrens }
3438fa9e4066Sahrens 
3439fa94a07fSbrendan void
3440fa94a07fSbrendan vdev_clear_stats(vdev_t *vd)
3441fa94a07fSbrendan {
3442fa94a07fSbrendan 	mutex_enter(&vd->vdev_stat_lock);
3443fa94a07fSbrendan 	vd->vdev_stat.vs_space = 0;
3444fa94a07fSbrendan 	vd->vdev_stat.vs_dspace = 0;
3445fa94a07fSbrendan 	vd->vdev_stat.vs_alloc = 0;
3446fa94a07fSbrendan 	mutex_exit(&vd->vdev_stat_lock);
3447fa94a07fSbrendan }
3448fa94a07fSbrendan 
34493f9d6ad7SLin Ling void
34503f9d6ad7SLin Ling vdev_scan_stat_init(vdev_t *vd)
34513f9d6ad7SLin Ling {
34523f9d6ad7SLin Ling 	vdev_stat_t *vs = &vd->vdev_stat;
34533f9d6ad7SLin Ling 
34543f9d6ad7SLin Ling 	for (int c = 0; c < vd->vdev_children; c++)
34553f9d6ad7SLin Ling 		vdev_scan_stat_init(vd->vdev_child[c]);
34563f9d6ad7SLin Ling 
34573f9d6ad7SLin Ling 	mutex_enter(&vd->vdev_stat_lock);
34583f9d6ad7SLin Ling 	vs->vs_scan_processed = 0;
34593f9d6ad7SLin Ling 	mutex_exit(&vd->vdev_stat_lock);
34603f9d6ad7SLin Ling }
34613f9d6ad7SLin Ling 
3462fa9e4066Sahrens void
3463e14bb325SJeff Bonwick vdev_stat_update(zio_t *zio, uint64_t psize)
3464fa9e4066Sahrens {
34658ad4d6ddSJeff Bonwick 	spa_t *spa = zio->io_spa;
34668ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3467e14bb325SJeff Bonwick 	vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3468fa9e4066Sahrens 	vdev_t *pvd;
3469fa9e4066Sahrens 	uint64_t txg = zio->io_txg;
3470fa9e4066Sahrens 	vdev_stat_t *vs = &vd->vdev_stat;
3471fa9e4066Sahrens 	zio_type_t type = zio->io_type;
3472fa9e4066Sahrens 	int flags = zio->io_flags;
3473fa9e4066Sahrens 
3474e14bb325SJeff Bonwick 	/*
3475e14bb325SJeff Bonwick 	 * If this i/o is a gang leader, it didn't do any actual work.
3476e14bb325SJeff Bonwick 	 */
3477e14bb325SJeff Bonwick 	if (zio->io_gang_tree)
3478e14bb325SJeff Bonwick 		return;
3479e14bb325SJeff Bonwick 
3480fa9e4066Sahrens 	if (zio->io_error == 0) {
3481e14bb325SJeff Bonwick 		/*
3482e14bb325SJeff Bonwick 		 * If this is a root i/o, don't count it -- we've already
3483e14bb325SJeff Bonwick 		 * counted the top-level vdevs, and vdev_get_stats() will
3484e14bb325SJeff Bonwick 		 * aggregate them when asked.  This reduces contention on
3485e14bb325SJeff Bonwick 		 * the root vdev_stat_lock and implicitly handles blocks
3486e14bb325SJeff Bonwick 		 * that compress away to holes, for which there is no i/o.
3487e14bb325SJeff Bonwick 		 * (Holes never create vdev children, so all the counters
3488e14bb325SJeff Bonwick 		 * remain zero, which is what we want.)
3489e14bb325SJeff Bonwick 		 *
3490e14bb325SJeff Bonwick 		 * Note: this only applies to successful i/o (io_error == 0)
3491e14bb325SJeff Bonwick 		 * because unlike i/o counts, errors are not additive.
3492e14bb325SJeff Bonwick 		 * When reading a ditto block, for example, failure of
3493e14bb325SJeff Bonwick 		 * one top-level vdev does not imply a root-level error.
3494e14bb325SJeff Bonwick 		 */
3495e14bb325SJeff Bonwick 		if (vd == rvd)
3496e14bb325SJeff Bonwick 			return;
3497e14bb325SJeff Bonwick 
3498e14bb325SJeff Bonwick 		ASSERT(vd == zio->io_vd);
34998ad4d6ddSJeff Bonwick 
35008ad4d6ddSJeff Bonwick 		if (flags & ZIO_FLAG_IO_BYPASS)
35018ad4d6ddSJeff Bonwick 			return;
35028ad4d6ddSJeff Bonwick 
35038ad4d6ddSJeff Bonwick 		mutex_enter(&vd->vdev_stat_lock);
35048ad4d6ddSJeff Bonwick 
3505e14bb325SJeff Bonwick 		if (flags & ZIO_FLAG_IO_REPAIR) {
350644ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
35073f9d6ad7SLin Ling 				dsl_scan_phys_t *scn_phys =
35083f9d6ad7SLin Ling 				    &spa->spa_dsl_pool->dp_scan->scn_phys;
35093f9d6ad7SLin Ling 				uint64_t *processed = &scn_phys->scn_processed;
35103f9d6ad7SLin Ling 
35113f9d6ad7SLin Ling 				/* XXX cleanup? */
35123f9d6ad7SLin Ling 				if (vd->vdev_ops->vdev_op_leaf)
35133f9d6ad7SLin Ling 					atomic_add_64(processed, psize);
35143f9d6ad7SLin Ling 				vs->vs_scan_processed += psize;
35153f9d6ad7SLin Ling 			}
35163f9d6ad7SLin Ling 
35178ad4d6ddSJeff Bonwick 			if (flags & ZIO_FLAG_SELF_HEAL)
3518e14bb325SJeff Bonwick 				vs->vs_self_healed += psize;
3519fa9e4066Sahrens 		}
35208ad4d6ddSJeff Bonwick 
35218ad4d6ddSJeff Bonwick 		vs->vs_ops[type]++;
35228ad4d6ddSJeff Bonwick 		vs->vs_bytes[type] += psize;
35238ad4d6ddSJeff Bonwick 
35248ad4d6ddSJeff Bonwick 		mutex_exit(&vd->vdev_stat_lock);
3525fa9e4066Sahrens 		return;
3526fa9e4066Sahrens 	}
3527fa9e4066Sahrens 
3528fa9e4066Sahrens 	if (flags & ZIO_FLAG_SPECULATIVE)
3529fa9e4066Sahrens 		return;
3530fa9e4066Sahrens 
35318956713aSEric Schrock 	/*
35328956713aSEric Schrock 	 * If this is an I/O error that is going to be retried, then ignore the
35338956713aSEric Schrock 	 * error.  Otherwise, the user may interpret B_FAILFAST I/O errors as
35348956713aSEric Schrock 	 * hard errors, when in reality they can happen for any number of
35358956713aSEric Schrock 	 * innocuous reasons (bus resets, MPxIO link failure, etc).
35368956713aSEric Schrock 	 */
35378956713aSEric Schrock 	if (zio->io_error == EIO &&
35388956713aSEric Schrock 	    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
35398956713aSEric Schrock 		return;
35408956713aSEric Schrock 
35418f18d1faSGeorge Wilson 	/*
35428f18d1faSGeorge Wilson 	 * Intent logs writes won't propagate their error to the root
35438f18d1faSGeorge Wilson 	 * I/O so don't mark these types of failures as pool-level
35448f18d1faSGeorge Wilson 	 * errors.
35458f18d1faSGeorge Wilson 	 */
35468f18d1faSGeorge Wilson 	if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
35478f18d1faSGeorge Wilson 		return;
35488f18d1faSGeorge Wilson 
3549e14bb325SJeff Bonwick 	mutex_enter(&vd->vdev_stat_lock);
3550b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3551e14bb325SJeff Bonwick 		if (zio->io_error == ECKSUM)
3552e14bb325SJeff Bonwick 			vs->vs_checksum_errors++;
3553e14bb325SJeff Bonwick 		else
3554e14bb325SJeff Bonwick 			vs->vs_read_errors++;
3555fa9e4066Sahrens 	}
3556b47119fdSGeorge Wilson 	if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3557e14bb325SJeff Bonwick 		vs->vs_write_errors++;
3558e14bb325SJeff Bonwick 	mutex_exit(&vd->vdev_stat_lock);
3559fa9e4066Sahrens 
35605cabbc6bSPrashanth Sreenivasa 	if (spa->spa_load_state == SPA_LOAD_NONE &&
35615cabbc6bSPrashanth Sreenivasa 	    type == ZIO_TYPE_WRITE && txg != 0 &&
35628ad4d6ddSJeff Bonwick 	    (!(flags & ZIO_FLAG_IO_REPAIR) ||
356344ecc532SGeorge Wilson 	    (flags & ZIO_FLAG_SCAN_THREAD) ||
3564b24ab676SJeff Bonwick 	    spa->spa_claiming)) {
35658ad4d6ddSJeff Bonwick 		/*
3566b24ab676SJeff Bonwick 		 * This is either a normal write (not a repair), or it's
3567b24ab676SJeff Bonwick 		 * a repair induced by the scrub thread, or it's a repair
3568b24ab676SJeff Bonwick 		 * made by zil_claim() during spa_load() in the first txg.
3569b24ab676SJeff Bonwick 		 * In the normal case, we commit the DTL change in the same
3570b24ab676SJeff Bonwick 		 * txg as the block was born.  In the scrub-induced repair
3571b24ab676SJeff Bonwick 		 * case, we know that scrubs run in first-pass syncing context,
3572b24ab676SJeff Bonwick 		 * so we commit the DTL change in spa_syncing_txg(spa).
3573b24ab676SJeff Bonwick 		 * In the zil_claim() case, we commit in spa_first_txg(spa).
35748ad4d6ddSJeff Bonwick 		 *
35758ad4d6ddSJeff Bonwick 		 * We currently do not make DTL entries for failed spontaneous
35768ad4d6ddSJeff Bonwick 		 * self-healing writes triggered by normal (non-scrubbing)
35778ad4d6ddSJeff Bonwick 		 * reads, because we have no transactional context in which to
35788ad4d6ddSJeff Bonwick 		 * do so -- and it's not clear that it'd be desirable anyway.
35798ad4d6ddSJeff Bonwick 		 */
35808ad4d6ddSJeff Bonwick 		if (vd->vdev_ops->vdev_op_leaf) {
35818ad4d6ddSJeff Bonwick 			uint64_t commit_txg = txg;
358244ecc532SGeorge Wilson 			if (flags & ZIO_FLAG_SCAN_THREAD) {
35838ad4d6ddSJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
35848ad4d6ddSJeff Bonwick 				ASSERT(spa_sync_pass(spa) == 1);
35858ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3586b24ab676SJeff Bonwick 				commit_txg = spa_syncing_txg(spa);
3587b24ab676SJeff Bonwick 			} else if (spa->spa_claiming) {
3588b24ab676SJeff Bonwick 				ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3589b24ab676SJeff Bonwick 				commit_txg = spa_first_txg(spa);
35908ad4d6ddSJeff Bonwick 			}
3591b24ab676SJeff Bonwick 			ASSERT(commit_txg >= spa_syncing_txg(spa));
35928ad4d6ddSJeff Bonwick 			if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3593fa9e4066Sahrens 				return;
35948ad4d6ddSJeff Bonwick 			for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
35958ad4d6ddSJeff Bonwick 				vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
35968ad4d6ddSJeff Bonwick 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3597fa9e4066Sahrens 		}
35988ad4d6ddSJeff Bonwick 		if (vd != rvd)
35998ad4d6ddSJeff Bonwick 			vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3600fa9e4066Sahrens 	}
3601fa9e4066Sahrens }
3602fa9e4066Sahrens 
3603fa9e4066Sahrens /*
3604b24ab676SJeff Bonwick  * Update the in-core space usage stats for this vdev, its metaslab class,
3605b24ab676SJeff Bonwick  * and the root vdev.
3606fa9e4066Sahrens  */
3607fa9e4066Sahrens void
3608b24ab676SJeff Bonwick vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3609b24ab676SJeff Bonwick     int64_t space_delta)
3610fa9e4066Sahrens {
361199653d4eSeschrock 	int64_t dspace_delta = space_delta;
36128654d025Sperrin 	spa_t *spa = vd->vdev_spa;
36138654d025Sperrin 	vdev_t *rvd = spa->spa_root_vdev;
3614b24ab676SJeff Bonwick 	metaslab_group_t *mg = vd->vdev_mg;
3615b24ab676SJeff Bonwick 	metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3616fa9e4066Sahrens 
36178654d025Sperrin 	ASSERT(vd == vd->vdev_top);
361899653d4eSeschrock 
36198654d025Sperrin 	/*
36208654d025Sperrin 	 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
36218654d025Sperrin 	 * factor.  We must calculate this here and not at the root vdev
36228654d025Sperrin 	 * because the root vdev's psize-to-asize is simply the max of its
36238654d025Sperrin 	 * childrens', thus not accurate enough for us.
36248654d025Sperrin 	 */
36258654d025Sperrin 	ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3626e6ca193dSGeorge Wilson 	ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
36278654d025Sperrin 	dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
36288654d025Sperrin 	    vd->vdev_deflate_ratio;
36298654d025Sperrin 
36308654d025Sperrin 	mutex_enter(&vd->vdev_stat_lock);
36318654d025Sperrin 	vd->vdev_stat.vs_alloc += alloc_delta;
3632b24ab676SJeff Bonwick 	vd->vdev_stat.vs_space += space_delta;
36338654d025Sperrin 	vd->vdev_stat.vs_dspace += dspace_delta;
36348654d025Sperrin 	mutex_exit(&vd->vdev_stat_lock);
36358654d025Sperrin 
3636b24ab676SJeff Bonwick 	if (mc == spa_normal_class(spa)) {
3637fa94a07fSbrendan 		mutex_enter(&rvd->vdev_stat_lock);
3638fa94a07fSbrendan 		rvd->vdev_stat.vs_alloc += alloc_delta;
3639b24ab676SJeff Bonwick 		rvd->vdev_stat.vs_space += space_delta;
3640fa94a07fSbrendan 		rvd->vdev_stat.vs_dspace += dspace_delta;
3641fa94a07fSbrendan 		mutex_exit(&rvd->vdev_stat_lock);
3642fa94a07fSbrendan 	}
3643b24ab676SJeff Bonwick 
3644b24ab676SJeff Bonwick 	if (mc != NULL) {
3645b24ab676SJeff Bonwick 		ASSERT(rvd == vd->vdev_parent);
3646b24ab676SJeff Bonwick 		ASSERT(vd->vdev_ms_count != 0);
3647b24ab676SJeff Bonwick 
3648b24ab676SJeff Bonwick 		metaslab_class_space_update(mc,
3649b24ab676SJeff Bonwick 		    alloc_delta, defer_delta, space_delta, dspace_delta);
3650b24ab676SJeff Bonwick 	}
3651fa9e4066Sahrens }
3652fa9e4066Sahrens 
3653fa9e4066Sahrens /*
3654fa9e4066Sahrens  * Mark a top-level vdev's config as dirty, placing it on the dirty list
3655fa9e4066Sahrens  * so that it will be written out next time the vdev configuration is synced.
3656fa9e4066Sahrens  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3657fa9e4066Sahrens  */
3658fa9e4066Sahrens void
3659fa9e4066Sahrens vdev_config_dirty(vdev_t *vd)
3660fa9e4066Sahrens {
3661fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
3662fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3663fa9e4066Sahrens 	int c;
3664fa9e4066Sahrens 
3665f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3666f9af39baSGeorge Wilson 
3667c5904d13Seschrock 	/*
36686809eb4eSEric Schrock 	 * If this is an aux vdev (as with l2cache and spare devices), then we
36696809eb4eSEric Schrock 	 * update the vdev config manually and set the sync flag.
3670c5904d13Seschrock 	 */
3671c5904d13Seschrock 	if (vd->vdev_aux != NULL) {
3672c5904d13Seschrock 		spa_aux_vdev_t *sav = vd->vdev_aux;
3673c5904d13Seschrock 		nvlist_t **aux;
3674c5904d13Seschrock 		uint_t naux;
3675c5904d13Seschrock 
3676c5904d13Seschrock 		for (c = 0; c < sav->sav_count; c++) {
3677c5904d13Seschrock 			if (sav->sav_vdevs[c] == vd)
3678c5904d13Seschrock 				break;
3679c5904d13Seschrock 		}
3680c5904d13Seschrock 
3681e14bb325SJeff Bonwick 		if (c == sav->sav_count) {
3682e14bb325SJeff Bonwick 			/*
3683e14bb325SJeff Bonwick 			 * We're being removed.  There's nothing more to do.
3684e14bb325SJeff Bonwick 			 */
3685e14bb325SJeff Bonwick 			ASSERT(sav->sav_sync == B_TRUE);
3686e14bb325SJeff Bonwick 			return;
3687e14bb325SJeff Bonwick 		}
3688e14bb325SJeff Bonwick 
3689c5904d13Seschrock 		sav->sav_sync = B_TRUE;
3690c5904d13Seschrock 
36916809eb4eSEric Schrock 		if (nvlist_lookup_nvlist_array(sav->sav_config,
36926809eb4eSEric Schrock 		    ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
36936809eb4eSEric Schrock 			VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
36946809eb4eSEric Schrock 			    ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
36956809eb4eSEric Schrock 		}
3696c5904d13Seschrock 
3697c5904d13Seschrock 		ASSERT(c < naux);
3698c5904d13Seschrock 
3699c5904d13Seschrock 		/*
3700c5904d13Seschrock 		 * Setting the nvlist in the middle if the array is a little
3701c5904d13Seschrock 		 * sketchy, but it will work.
3702c5904d13Seschrock 		 */
3703c5904d13Seschrock 		nvlist_free(aux[c]);
37043f9d6ad7SLin Ling 		aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3705c5904d13Seschrock 
3706c5904d13Seschrock 		return;
3707c5904d13Seschrock 	}
3708c5904d13Seschrock 
37095dabedeeSbonwick 	/*
3710e14bb325SJeff Bonwick 	 * The dirty list is protected by the SCL_CONFIG lock.  The caller
3711e14bb325SJeff Bonwick 	 * must either hold SCL_CONFIG as writer, or must be the sync thread
3712e14bb325SJeff Bonwick 	 * (which holds SCL_CONFIG as reader).  There's only one sync thread,
37135dabedeeSbonwick 	 * so this is sufficient to ensure mutual exclusion.
37145dabedeeSbonwick 	 */
3715e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3716e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3717e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
37185dabedeeSbonwick 
3719fa9e4066Sahrens 	if (vd == rvd) {
3720fa9e4066Sahrens 		for (c = 0; c < rvd->vdev_children; c++)
3721fa9e4066Sahrens 			vdev_config_dirty(rvd->vdev_child[c]);
3722fa9e4066Sahrens 	} else {
3723fa9e4066Sahrens 		ASSERT(vd == vd->vdev_top);
3724fa9e4066Sahrens 
372588ecc943SGeorge Wilson 		if (!list_link_active(&vd->vdev_config_dirty_node) &&
37265cabbc6bSPrashanth Sreenivasa 		    vdev_is_concrete(vd)) {
3727e14bb325SJeff Bonwick 			list_insert_head(&spa->spa_config_dirty_list, vd);
37285cabbc6bSPrashanth Sreenivasa 		}
3729fa9e4066Sahrens 	}
3730fa9e4066Sahrens }
3731fa9e4066Sahrens 
3732fa9e4066Sahrens void
3733fa9e4066Sahrens vdev_config_clean(vdev_t *vd)
3734fa9e4066Sahrens {
37355dabedeeSbonwick 	spa_t *spa = vd->vdev_spa;
37365dabedeeSbonwick 
3737e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3738e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3739e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_CONFIG, RW_READER)));
37405dabedeeSbonwick 
3741e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3742e14bb325SJeff Bonwick 	list_remove(&spa->spa_config_dirty_list, vd);
3743e14bb325SJeff Bonwick }
3744e14bb325SJeff Bonwick 
3745e14bb325SJeff Bonwick /*
3746e14bb325SJeff Bonwick  * Mark a top-level vdev's state as dirty, so that the next pass of
3747e14bb325SJeff Bonwick  * spa_sync() can convert this into vdev_config_dirty().  We distinguish
3748e14bb325SJeff Bonwick  * the state changes from larger config changes because they require
3749e14bb325SJeff Bonwick  * much less locking, and are often needed for administrative actions.
3750e14bb325SJeff Bonwick  */
3751e14bb325SJeff Bonwick void
3752e14bb325SJeff Bonwick vdev_state_dirty(vdev_t *vd)
3753e14bb325SJeff Bonwick {
3754e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3755e14bb325SJeff Bonwick 
3756f9af39baSGeorge Wilson 	ASSERT(spa_writeable(spa));
3757e14bb325SJeff Bonwick 	ASSERT(vd == vd->vdev_top);
3758e14bb325SJeff Bonwick 
3759e14bb325SJeff Bonwick 	/*
3760e14bb325SJeff Bonwick 	 * The state list is protected by the SCL_STATE lock.  The caller
3761e14bb325SJeff Bonwick 	 * must either hold SCL_STATE as writer, or must be the sync thread
3762e14bb325SJeff Bonwick 	 * (which holds SCL_STATE as reader).  There's only one sync thread,
3763e14bb325SJeff Bonwick 	 * so this is sufficient to ensure mutual exclusion.
3764e14bb325SJeff Bonwick 	 */
3765e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3766e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3767e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3768e14bb325SJeff Bonwick 
37695cabbc6bSPrashanth Sreenivasa 	if (!list_link_active(&vd->vdev_state_dirty_node) &&
37705cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd))
3771e14bb325SJeff Bonwick 		list_insert_head(&spa->spa_state_dirty_list, vd);
3772e14bb325SJeff Bonwick }
3773e14bb325SJeff Bonwick 
3774e14bb325SJeff Bonwick void
3775e14bb325SJeff Bonwick vdev_state_clean(vdev_t *vd)
3776e14bb325SJeff Bonwick {
3777e14bb325SJeff Bonwick 	spa_t *spa = vd->vdev_spa;
3778e14bb325SJeff Bonwick 
3779e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3780e14bb325SJeff Bonwick 	    (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3781e14bb325SJeff Bonwick 	    spa_config_held(spa, SCL_STATE, RW_READER)));
3782e14bb325SJeff Bonwick 
3783e14bb325SJeff Bonwick 	ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3784e14bb325SJeff Bonwick 	list_remove(&spa->spa_state_dirty_list, vd);
3785fa9e4066Sahrens }
3786fa9e4066Sahrens 
378732b87932Sek /*
378832b87932Sek  * Propagate vdev state up from children to parent.
378932b87932Sek  */
379044cd46caSbillm void
379144cd46caSbillm vdev_propagate_state(vdev_t *vd)
379244cd46caSbillm {
37938ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
37948ad4d6ddSJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
379544cd46caSbillm 	int degraded = 0, faulted = 0;
379644cd46caSbillm 	int corrupted = 0;
379744cd46caSbillm 	vdev_t *child;
379844cd46caSbillm 
37993d7072f8Seschrock 	if (vd->vdev_children > 0) {
3800573ca77eSGeorge Wilson 		for (int c = 0; c < vd->vdev_children; c++) {
38013d7072f8Seschrock 			child = vd->vdev_child[c];
380251ece835Seschrock 
380388ecc943SGeorge Wilson 			/*
38045cabbc6bSPrashanth Sreenivasa 			 * Don't factor holes or indirect vdevs into the
38055cabbc6bSPrashanth Sreenivasa 			 * decision.
380688ecc943SGeorge Wilson 			 */
38075cabbc6bSPrashanth Sreenivasa 			if (!vdev_is_concrete(child))
380888ecc943SGeorge Wilson 				continue;
380988ecc943SGeorge Wilson 
3810e14bb325SJeff Bonwick 			if (!vdev_readable(child) ||
38118ad4d6ddSJeff Bonwick 			    (!vdev_writeable(child) && spa_writeable(spa))) {
381251ece835Seschrock 				/*
381351ece835Seschrock 				 * Root special: if there is a top-level log
381451ece835Seschrock 				 * device, treat the root vdev as if it were
381551ece835Seschrock 				 * degraded.
381651ece835Seschrock 				 */
381751ece835Seschrock 				if (child->vdev_islog && vd == rvd)
381851ece835Seschrock 					degraded++;
381951ece835Seschrock 				else
382051ece835Seschrock 					faulted++;
382151ece835Seschrock 			} else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
38223d7072f8Seschrock 				degraded++;
382351ece835Seschrock 			}
382444cd46caSbillm 
38253d7072f8Seschrock 			if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
38263d7072f8Seschrock 				corrupted++;
38273d7072f8Seschrock 		}
382844cd46caSbillm 
38293d7072f8Seschrock 		vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
38303d7072f8Seschrock 
38313d7072f8Seschrock 		/*
3832e14bb325SJeff Bonwick 		 * Root special: if there is a top-level vdev that cannot be
38333d7072f8Seschrock 		 * opened due to corrupted metadata, then propagate the root
38343d7072f8Seschrock 		 * vdev's aux state as 'corrupt' rather than 'insufficient
38353d7072f8Seschrock 		 * replicas'.
38363d7072f8Seschrock 		 */
38373d7072f8Seschrock 		if (corrupted && vd == rvd &&
38383d7072f8Seschrock 		    rvd->vdev_state == VDEV_STATE_CANT_OPEN)
38393d7072f8Seschrock 			vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
38403d7072f8Seschrock 			    VDEV_AUX_CORRUPT_DATA);
38413d7072f8Seschrock 	}
38423d7072f8Seschrock 
384351ece835Seschrock 	if (vd->vdev_parent)
38443d7072f8Seschrock 		vdev_propagate_state(vd->vdev_parent);
384544cd46caSbillm }
384644cd46caSbillm 
3847fa9e4066Sahrens /*
3848ea8dc4b6Seschrock  * Set a vdev's state.  If this is during an open, we don't update the parent
3849ea8dc4b6Seschrock  * state, because we're in the process of opening children depth-first.
3850ea8dc4b6Seschrock  * Otherwise, we propagate the change to the parent.
3851ea8dc4b6Seschrock  *
3852ea8dc4b6Seschrock  * If this routine places a device in a faulted state, an appropriate ereport is
3853ea8dc4b6Seschrock  * generated.
3854fa9e4066Sahrens  */
3855fa9e4066Sahrens void
3856ea8dc4b6Seschrock vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3857fa9e4066Sahrens {
3858560e6e96Seschrock 	uint64_t save_state;
3859c5904d13Seschrock 	spa_t *spa = vd->vdev_spa;
3860ea8dc4b6Seschrock 
3861ea8dc4b6Seschrock 	if (state == vd->vdev_state) {
3862ea8dc4b6Seschrock 		vd->vdev_stat.vs_aux = aux;
3863fa9e4066Sahrens 		return;
3864ea8dc4b6Seschrock 	}
3865ea8dc4b6Seschrock 
3866560e6e96Seschrock 	save_state = vd->vdev_state;
3867fa9e4066Sahrens 
3868fa9e4066Sahrens 	vd->vdev_state = state;
3869fa9e4066Sahrens 	vd->vdev_stat.vs_aux = aux;
3870fa9e4066Sahrens 
38713d7072f8Seschrock 	/*
38723d7072f8Seschrock 	 * If we are setting the vdev state to anything but an open state, then
387398d1cbfeSGeorge Wilson 	 * always close the underlying device unless the device has requested
387498d1cbfeSGeorge Wilson 	 * a delayed close (i.e. we're about to remove or fault the device).
387598d1cbfeSGeorge Wilson 	 * Otherwise, we keep accessible but invalid devices open forever.
387698d1cbfeSGeorge Wilson 	 * We don't call vdev_close() itself, because that implies some extra
387798d1cbfeSGeorge Wilson 	 * checks (offline, etc) that we don't want here.  This is limited to
387898d1cbfeSGeorge Wilson 	 * leaf devices, because otherwise closing the device will affect other
387998d1cbfeSGeorge Wilson 	 * children.
388098d1cbfeSGeorge Wilson 	 */
388198d1cbfeSGeorge Wilson 	if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
388298d1cbfeSGeorge Wilson 	    vd->vdev_ops->vdev_op_leaf)
38833d7072f8Seschrock 		vd->vdev_ops->vdev_op_close(vd);
38843d7072f8Seschrock 
3885069f55e2SEric Schrock 	/*
3886069f55e2SEric Schrock 	 * If we have brought this vdev back into service, we need
3887069f55e2SEric Schrock 	 * to notify fmd so that it can gracefully repair any outstanding
3888069f55e2SEric Schrock 	 * cases due to a missing device.  We do this in all cases, even those
3889069f55e2SEric Schrock 	 * that probably don't correlate to a repaired fault.  This is sure to
3890069f55e2SEric Schrock 	 * catch all cases, and we let the zfs-retire agent sort it out.  If
3891069f55e2SEric Schrock 	 * this is a transient state it's OK, as the retire agent will
3892069f55e2SEric Schrock 	 * double-check the state of the vdev before repairing it.
3893069f55e2SEric Schrock 	 */
3894069f55e2SEric Schrock 	if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3895069f55e2SEric Schrock 	    vd->vdev_prevstate != state)
3896069f55e2SEric Schrock 		zfs_post_state_change(spa, vd);
3897069f55e2SEric Schrock 
38983d7072f8Seschrock 	if (vd->vdev_removed &&
38993d7072f8Seschrock 	    state == VDEV_STATE_CANT_OPEN &&
39003d7072f8Seschrock 	    (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
39013d7072f8Seschrock 		/*
39023d7072f8Seschrock 		 * If the previous state is set to VDEV_STATE_REMOVED, then this
39033d7072f8Seschrock 		 * device was previously marked removed and someone attempted to
39043d7072f8Seschrock 		 * reopen it.  If this failed due to a nonexistent device, then
39053d7072f8Seschrock 		 * keep the device in the REMOVED state.  We also let this be if
39063d7072f8Seschrock 		 * it is one of our special test online cases, which is only
39073d7072f8Seschrock 		 * attempting to online the device and shouldn't generate an FMA
39083d7072f8Seschrock 		 * fault.
39093d7072f8Seschrock 		 */
39103d7072f8Seschrock 		vd->vdev_state = VDEV_STATE_REMOVED;
39113d7072f8Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
39123d7072f8Seschrock 	} else if (state == VDEV_STATE_REMOVED) {
39133d7072f8Seschrock 		vd->vdev_removed = B_TRUE;
39143d7072f8Seschrock 	} else if (state == VDEV_STATE_CANT_OPEN) {
3915ea8dc4b6Seschrock 		/*
3916cb04b873SMark J Musante 		 * If we fail to open a vdev during an import or recovery, we
3917cb04b873SMark J Musante 		 * mark it as "not available", which signifies that it was
3918cb04b873SMark J Musante 		 * never there to begin with.  Failure to open such a device
3919cb04b873SMark J Musante 		 * is not considered an error.
3920ea8dc4b6Seschrock 		 */
3921cb04b873SMark J Musante 		if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3922cb04b873SMark J Musante 		    spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3923560e6e96Seschrock 		    vd->vdev_ops->vdev_op_leaf)
3924560e6e96Seschrock 			vd->vdev_not_present = 1;
3925560e6e96Seschrock 
3926560e6e96Seschrock 		/*
3927560e6e96Seschrock 		 * Post the appropriate ereport.  If the 'prevstate' field is
3928560e6e96Seschrock 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3929560e6e96Seschrock 		 * that this is part of a vdev_reopen().  In this case, we don't
3930560e6e96Seschrock 		 * want to post the ereport if the device was already in the
3931560e6e96Seschrock 		 * CANT_OPEN state beforehand.
39323d7072f8Seschrock 		 *
39333d7072f8Seschrock 		 * If the 'checkremove' flag is set, then this is an attempt to
39343d7072f8Seschrock 		 * online the device in response to an insertion event.  If we
39353d7072f8Seschrock 		 * hit this case, then we have detected an insertion event for a
39363d7072f8Seschrock 		 * faulted or offline device that wasn't in the removed state.
39373d7072f8Seschrock 		 * In this scenario, we don't post an ereport because we are
39383d7072f8Seschrock 		 * about to replace the device, or attempt an online with
39393d7072f8Seschrock 		 * vdev_forcefault, which will generate the fault for us.
3940560e6e96Seschrock 		 */
39413d7072f8Seschrock 		if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
39423d7072f8Seschrock 		    !vd->vdev_not_present && !vd->vdev_checkremove &&
3943c5904d13Seschrock 		    vd != spa->spa_root_vdev) {
3944ea8dc4b6Seschrock 			const char *class;
3945ea8dc4b6Seschrock 
3946ea8dc4b6Seschrock 			switch (aux) {
3947ea8dc4b6Seschrock 			case VDEV_AUX_OPEN_FAILED:
3948ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3949ea8dc4b6Seschrock 				break;
3950ea8dc4b6Seschrock 			case VDEV_AUX_CORRUPT_DATA:
3951ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3952ea8dc4b6Seschrock 				break;
3953ea8dc4b6Seschrock 			case VDEV_AUX_NO_REPLICAS:
3954ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3955ea8dc4b6Seschrock 				break;
3956ea8dc4b6Seschrock 			case VDEV_AUX_BAD_GUID_SUM:
3957ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3958ea8dc4b6Seschrock 				break;
3959ea8dc4b6Seschrock 			case VDEV_AUX_TOO_SMALL:
3960ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3961ea8dc4b6Seschrock 				break;
3962ea8dc4b6Seschrock 			case VDEV_AUX_BAD_LABEL:
3963ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3964ea8dc4b6Seschrock 				break;
3965ea8dc4b6Seschrock 			default:
3966ea8dc4b6Seschrock 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3967ea8dc4b6Seschrock 			}
3968ea8dc4b6Seschrock 
3969c5904d13Seschrock 			zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3970ea8dc4b6Seschrock 		}
3971ea8dc4b6Seschrock 
39723d7072f8Seschrock 		/* Erase any notion of persistent removed state */
39733d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
39743d7072f8Seschrock 	} else {
39753d7072f8Seschrock 		vd->vdev_removed = B_FALSE;
39763d7072f8Seschrock 	}
3977ea8dc4b6Seschrock 
39788b33d774STim Haley 	if (!isopen && vd->vdev_parent)
39798b33d774STim Haley 		vdev_propagate_state(vd->vdev_parent);
3980fa9e4066Sahrens }
398115e6edf1Sgw 
39826f793812SPavel Zakharov boolean_t
39836f793812SPavel Zakharov vdev_children_are_offline(vdev_t *vd)
39846f793812SPavel Zakharov {
39856f793812SPavel Zakharov 	ASSERT(!vd->vdev_ops->vdev_op_leaf);
39866f793812SPavel Zakharov 
39876f793812SPavel Zakharov 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
39886f793812SPavel Zakharov 		if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
39896f793812SPavel Zakharov 			return (B_FALSE);
39906f793812SPavel Zakharov 	}
39916f793812SPavel Zakharov 
39926f793812SPavel Zakharov 	return (B_TRUE);
39936f793812SPavel Zakharov }
39946f793812SPavel Zakharov 
399515e6edf1Sgw /*
399615e6edf1Sgw  * Check the vdev configuration to ensure that it's capable of supporting
3997c8811bd3SToomas Soome  * a root pool. We do not support partial configuration.
3998c8811bd3SToomas Soome  * In addition, only a single top-level vdev is allowed.
399915e6edf1Sgw  */
400015e6edf1Sgw boolean_t
400115e6edf1Sgw vdev_is_bootable(vdev_t *vd)
400215e6edf1Sgw {
400315e6edf1Sgw 	if (!vd->vdev_ops->vdev_op_leaf) {
400415e6edf1Sgw 		char *vdev_type = vd->vdev_ops->vdev_op_type;
400515e6edf1Sgw 
400615e6edf1Sgw 		if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
400715e6edf1Sgw 		    vd->vdev_children > 1) {
400815e6edf1Sgw 			return (B_FALSE);
40095cabbc6bSPrashanth Sreenivasa 		} else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
40105cabbc6bSPrashanth Sreenivasa 		    strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
401115e6edf1Sgw 			return (B_FALSE);
401215e6edf1Sgw 		}
401315e6edf1Sgw 	}
401415e6edf1Sgw 
4015573ca77eSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
401615e6edf1Sgw 		if (!vdev_is_bootable(vd->vdev_child[c]))
401715e6edf1Sgw 			return (B_FALSE);
401815e6edf1Sgw 	}
401915e6edf1Sgw 	return (B_TRUE);
402015e6edf1Sgw }
4021e6ca193dSGeorge Wilson 
40225cabbc6bSPrashanth Sreenivasa boolean_t
40235cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vdev_t *vd)
40245cabbc6bSPrashanth Sreenivasa {
40255cabbc6bSPrashanth Sreenivasa 	vdev_ops_t *ops = vd->vdev_ops;
40265cabbc6bSPrashanth Sreenivasa 	if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
40275cabbc6bSPrashanth Sreenivasa 	    ops == &vdev_missing_ops || ops == &vdev_root_ops) {
40285cabbc6bSPrashanth Sreenivasa 		return (B_FALSE);
40295cabbc6bSPrashanth Sreenivasa 	} else {
40305cabbc6bSPrashanth Sreenivasa 		return (B_TRUE);
40315cabbc6bSPrashanth Sreenivasa 	}
40325cabbc6bSPrashanth Sreenivasa }
40335cabbc6bSPrashanth Sreenivasa 
40344b964adaSGeorge Wilson /*
40354b964adaSGeorge Wilson  * Determine if a log device has valid content.  If the vdev was
40364b964adaSGeorge Wilson  * removed or faulted in the MOS config then we know that
40374b964adaSGeorge Wilson  * the content on the log device has already been written to the pool.
40384b964adaSGeorge Wilson  */
40394b964adaSGeorge Wilson boolean_t
40404b964adaSGeorge Wilson vdev_log_state_valid(vdev_t *vd)
40414b964adaSGeorge Wilson {
40424b964adaSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
40434b964adaSGeorge Wilson 	    !vd->vdev_removed)
40444b964adaSGeorge Wilson 		return (B_TRUE);
40454b964adaSGeorge Wilson 
40464b964adaSGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
40474b964adaSGeorge Wilson 		if (vdev_log_state_valid(vd->vdev_child[c]))
40484b964adaSGeorge Wilson 			return (B_TRUE);
40494b964adaSGeorge Wilson 
40504b964adaSGeorge Wilson 	return (B_FALSE);
40514b964adaSGeorge Wilson }
40524b964adaSGeorge Wilson 
4053573ca77eSGeorge Wilson /*
4054573ca77eSGeorge Wilson  * Expand a vdev if possible.
4055573ca77eSGeorge Wilson  */
4056573ca77eSGeorge Wilson void
4057573ca77eSGeorge Wilson vdev_expand(vdev_t *vd, uint64_t txg)
4058573ca77eSGeorge Wilson {
4059573ca77eSGeorge Wilson 	ASSERT(vd->vdev_top == vd);
4060573ca77eSGeorge Wilson 	ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4061573ca77eSGeorge Wilson 
40625cabbc6bSPrashanth Sreenivasa 	vdev_set_deflate_ratio(vd);
40635cabbc6bSPrashanth Sreenivasa 
40645cabbc6bSPrashanth Sreenivasa 	if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
40655cabbc6bSPrashanth Sreenivasa 	    vdev_is_concrete(vd)) {
4066573ca77eSGeorge Wilson 		VERIFY(vdev_metaslab_init(vd, txg) == 0);
4067573ca77eSGeorge Wilson 		vdev_config_dirty(vd);
4068573ca77eSGeorge Wilson 	}
4069573ca77eSGeorge Wilson }
40701195e687SMark J Musante 
40711195e687SMark J Musante /*
40721195e687SMark J Musante  * Split a vdev.
40731195e687SMark J Musante  */
40741195e687SMark J Musante void
40751195e687SMark J Musante vdev_split(vdev_t *vd)
40761195e687SMark J Musante {
40771195e687SMark J Musante 	vdev_t *cvd, *pvd = vd->vdev_parent;
40781195e687SMark J Musante 
40791195e687SMark J Musante 	vdev_remove_child(pvd, vd);
40801195e687SMark J Musante 	vdev_compact_children(pvd);
40811195e687SMark J Musante 
40821195e687SMark J Musante 	cvd = pvd->vdev_child[0];
40831195e687SMark J Musante 	if (pvd->vdev_children == 1) {
40841195e687SMark J Musante 		vdev_remove_parent(cvd);
40851195e687SMark J Musante 		cvd->vdev_splitting = B_TRUE;
40861195e687SMark J Musante 	}
40871195e687SMark J Musante 	vdev_propagate_state(cvd);
40881195e687SMark J Musante }
4089283b8460SGeorge.Wilson 
4090283b8460SGeorge.Wilson void
4091283b8460SGeorge.Wilson vdev_deadman(vdev_t *vd)
4092283b8460SGeorge.Wilson {
4093283b8460SGeorge.Wilson 	for (int c = 0; c < vd->vdev_children; c++) {
4094283b8460SGeorge.Wilson 		vdev_t *cvd = vd->vdev_child[c];
4095283b8460SGeorge.Wilson 
4096283b8460SGeorge.Wilson 		vdev_deadman(cvd);
4097283b8460SGeorge.Wilson 	}
4098283b8460SGeorge.Wilson 
4099283b8460SGeorge.Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
4100283b8460SGeorge.Wilson 		vdev_queue_t *vq = &vd->vdev_queue;
4101283b8460SGeorge.Wilson 
4102283b8460SGeorge.Wilson 		mutex_enter(&vq->vq_lock);
410369962b56SMatthew Ahrens 		if (avl_numnodes(&vq->vq_active_tree) > 0) {
4104283b8460SGeorge.Wilson 			spa_t *spa = vd->vdev_spa;
4105283b8460SGeorge.Wilson 			zio_t *fio;
4106283b8460SGeorge.Wilson 			uint64_t delta;
4107283b8460SGeorge.Wilson 
4108283b8460SGeorge.Wilson 			/*
4109283b8460SGeorge.Wilson 			 * Look at the head of all the pending queues,
4110283b8460SGeorge.Wilson 			 * if any I/O has been outstanding for longer than
4111283b8460SGeorge.Wilson 			 * the spa_deadman_synctime we panic the system.
4112283b8460SGeorge.Wilson 			 */
411369962b56SMatthew Ahrens 			fio = avl_first(&vq->vq_active_tree);
4114c55e05cbSMatthew Ahrens 			delta = gethrtime() - fio->io_timestamp;
4115c55e05cbSMatthew Ahrens 			if (delta > spa_deadman_synctime(spa)) {
41163ee8c80cSPavel Zakharov 				vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
41173ee8c80cSPavel Zakharov 				    "%lluns, delta %lluns, last io %lluns",
41183ee8c80cSPavel Zakharov 				    fio->io_timestamp, (u_longlong_t)delta,
4119283b8460SGeorge.Wilson 				    vq->vq_io_complete_ts);
4120283b8460SGeorge.Wilson 				fm_panic("I/O to pool '%s' appears to be "
4121283b8460SGeorge.Wilson 				    "hung.", spa_name(spa));
4122283b8460SGeorge.Wilson 			}
4123283b8460SGeorge.Wilson 		}
4124283b8460SGeorge.Wilson 		mutex_exit(&vq->vq_lock);
4125283b8460SGeorge.Wilson 	}
4126283b8460SGeorge.Wilson }
4127