xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa.c (revision 379c004d1f26b343f034bba8a350290691d00d38)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * 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 /*
23*379c004dSEric Schrock  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens /*
28fa9e4066Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
29fa9e4066Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
30fa9e4066Sahrens  * pool.
31fa9e4066Sahrens  */
32fa9e4066Sahrens 
33fa9e4066Sahrens #include <sys/zfs_context.h>
34ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
35fa9e4066Sahrens #include <sys/spa_impl.h>
36fa9e4066Sahrens #include <sys/zio.h>
37fa9e4066Sahrens #include <sys/zio_checksum.h>
38fa9e4066Sahrens #include <sys/zio_compress.h>
39fa9e4066Sahrens #include <sys/dmu.h>
40fa9e4066Sahrens #include <sys/dmu_tx.h>
41fa9e4066Sahrens #include <sys/zap.h>
42fa9e4066Sahrens #include <sys/zil.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab.h>
45fa9e4066Sahrens #include <sys/uberblock_impl.h>
46fa9e4066Sahrens #include <sys/txg.h>
47fa9e4066Sahrens #include <sys/avl.h>
48fa9e4066Sahrens #include <sys/dmu_traverse.h>
49b1b8ab34Slling #include <sys/dmu_objset.h>
50fa9e4066Sahrens #include <sys/unique.h>
51fa9e4066Sahrens #include <sys/dsl_pool.h>
52b1b8ab34Slling #include <sys/dsl_dataset.h>
53fa9e4066Sahrens #include <sys/dsl_dir.h>
54fa9e4066Sahrens #include <sys/dsl_prop.h>
55b1b8ab34Slling #include <sys/dsl_synctask.h>
56fa9e4066Sahrens #include <sys/fs/zfs.h>
57fa94a07fSbrendan #include <sys/arc.h>
58fa9e4066Sahrens #include <sys/callb.h>
5995173954Sek #include <sys/systeminfo.h>
6095173954Sek #include <sys/sunddi.h>
61e7cbe64fSgw #include <sys/spa_boot.h>
62fa9e4066Sahrens 
63990b4856Slling #include "zfs_prop.h"
64b7b97454Sperrin #include "zfs_comutil.h"
65990b4856Slling 
66e14bb325SJeff Bonwick int zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
67e14bb325SJeff Bonwick 	/*	ISSUE	INTR					*/
68e14bb325SJeff Bonwick 	{	1,	1	},	/* ZIO_TYPE_NULL	*/
69e14bb325SJeff Bonwick 	{	1,	8	},	/* ZIO_TYPE_READ	*/
70e14bb325SJeff Bonwick 	{	8,	1	},	/* ZIO_TYPE_WRITE	*/
71e14bb325SJeff Bonwick 	{	1,	1	},	/* ZIO_TYPE_FREE	*/
72e14bb325SJeff Bonwick 	{	1,	1	},	/* ZIO_TYPE_CLAIM	*/
73e14bb325SJeff Bonwick 	{	1,	1	},	/* ZIO_TYPE_IOCTL	*/
74e14bb325SJeff Bonwick };
75416e0cd8Sek 
76990b4856Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
7789a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa);
78990b4856Slling 
79990b4856Slling /*
80990b4856Slling  * ==========================================================================
81990b4856Slling  * SPA properties routines
82990b4856Slling  * ==========================================================================
83990b4856Slling  */
84990b4856Slling 
85990b4856Slling /*
86990b4856Slling  * Add a (source=src, propname=propval) list to an nvlist.
87990b4856Slling  */
889d82f4f6Slling static void
89990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
90990b4856Slling     uint64_t intval, zprop_source_t src)
91990b4856Slling {
92990b4856Slling 	const char *propname = zpool_prop_to_name(prop);
93990b4856Slling 	nvlist_t *propval;
94990b4856Slling 
959d82f4f6Slling 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
969d82f4f6Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
97990b4856Slling 
989d82f4f6Slling 	if (strval != NULL)
999d82f4f6Slling 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1009d82f4f6Slling 	else
1019d82f4f6Slling 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
102990b4856Slling 
1039d82f4f6Slling 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
104990b4856Slling 	nvlist_free(propval);
105990b4856Slling }
106990b4856Slling 
107990b4856Slling /*
108990b4856Slling  * Get property values from the spa configuration.
109990b4856Slling  */
1109d82f4f6Slling static void
111990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
112990b4856Slling {
113*379c004dSEric Schrock 	uint64_t size;
114*379c004dSEric Schrock 	uint64_t used;
115990b4856Slling 	uint64_t cap, version;
116990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
117c5904d13Seschrock 	spa_config_dirent_t *dp;
118990b4856Slling 
119e14bb325SJeff Bonwick 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
120e14bb325SJeff Bonwick 
121*379c004dSEric Schrock 	if (spa->spa_root_vdev != NULL) {
122*379c004dSEric Schrock 		size = spa_get_space(spa);
123*379c004dSEric Schrock 		used = spa_get_alloc(spa);
124*379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
125*379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
126*379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
127*379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
128*379c004dSEric Schrock 		    size - used, src);
129*379c004dSEric Schrock 
130*379c004dSEric Schrock 		cap = (size == 0) ? 0 : (used * 100 / size);
131*379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
132*379c004dSEric Schrock 
133*379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
134*379c004dSEric Schrock 		    spa->spa_root_vdev->vdev_state, src);
135*379c004dSEric Schrock 
136*379c004dSEric Schrock 		version = spa_version(spa);
137*379c004dSEric Schrock 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
138*379c004dSEric Schrock 			src = ZPROP_SRC_DEFAULT;
139*379c004dSEric Schrock 		else
140*379c004dSEric Schrock 			src = ZPROP_SRC_LOCAL;
141*379c004dSEric Schrock 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
142*379c004dSEric Schrock 	}
143990b4856Slling 
1449d82f4f6Slling 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
145990b4856Slling 
1469d82f4f6Slling 	if (spa->spa_root != NULL)
1479d82f4f6Slling 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1489d82f4f6Slling 		    0, ZPROP_SRC_LOCAL);
149990b4856Slling 
150c5904d13Seschrock 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
151c5904d13Seschrock 		if (dp->scd_path == NULL) {
1529d82f4f6Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
153c5904d13Seschrock 			    "none", 0, ZPROP_SRC_LOCAL);
154c5904d13Seschrock 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1559d82f4f6Slling 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
156c5904d13Seschrock 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1572f8aaab3Seschrock 		}
1582f8aaab3Seschrock 	}
159990b4856Slling }
160990b4856Slling 
161990b4856Slling /*
162990b4856Slling  * Get zpool property values.
163990b4856Slling  */
164990b4856Slling int
165990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
166990b4856Slling {
167990b4856Slling 	zap_cursor_t zc;
168990b4856Slling 	zap_attribute_t za;
169990b4856Slling 	objset_t *mos = spa->spa_meta_objset;
170990b4856Slling 	int err;
171990b4856Slling 
1729d82f4f6Slling 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
173990b4856Slling 
174e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);
175e14bb325SJeff Bonwick 
176990b4856Slling 	/*
177990b4856Slling 	 * Get properties from the spa config.
178990b4856Slling 	 */
1799d82f4f6Slling 	spa_prop_get_config(spa, nvp);
180990b4856Slling 
181990b4856Slling 	/* If no pool property object, no more prop to get. */
182990b4856Slling 	if (spa->spa_pool_props_object == 0) {
183990b4856Slling 		mutex_exit(&spa->spa_props_lock);
184990b4856Slling 		return (0);
185990b4856Slling 	}
186990b4856Slling 
187990b4856Slling 	/*
188990b4856Slling 	 * Get properties from the MOS pool property object.
189990b4856Slling 	 */
190990b4856Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
191990b4856Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
192990b4856Slling 	    zap_cursor_advance(&zc)) {
193990b4856Slling 		uint64_t intval = 0;
194990b4856Slling 		char *strval = NULL;
195990b4856Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
196990b4856Slling 		zpool_prop_t prop;
197990b4856Slling 
198990b4856Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
199990b4856Slling 			continue;
200990b4856Slling 
201990b4856Slling 		switch (za.za_integer_length) {
202990b4856Slling 		case 8:
203990b4856Slling 			/* integer property */
204990b4856Slling 			if (za.za_first_integer !=
205990b4856Slling 			    zpool_prop_default_numeric(prop))
206990b4856Slling 				src = ZPROP_SRC_LOCAL;
207990b4856Slling 
208990b4856Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
209990b4856Slling 				dsl_pool_t *dp;
210990b4856Slling 				dsl_dataset_t *ds = NULL;
211990b4856Slling 
212990b4856Slling 				dp = spa_get_dsl(spa);
213990b4856Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
214745cd3c5Smaybee 				if (err = dsl_dataset_hold_obj(dp,
215745cd3c5Smaybee 				    za.za_first_integer, FTAG, &ds)) {
216990b4856Slling 					rw_exit(&dp->dp_config_rwlock);
217990b4856Slling 					break;
218990b4856Slling 				}
219990b4856Slling 
220990b4856Slling 				strval = kmem_alloc(
221990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
222990b4856Slling 				    KM_SLEEP);
223990b4856Slling 				dsl_dataset_name(ds, strval);
224745cd3c5Smaybee 				dsl_dataset_rele(ds, FTAG);
225990b4856Slling 				rw_exit(&dp->dp_config_rwlock);
226990b4856Slling 			} else {
227990b4856Slling 				strval = NULL;
228990b4856Slling 				intval = za.za_first_integer;
229990b4856Slling 			}
230990b4856Slling 
2319d82f4f6Slling 			spa_prop_add_list(*nvp, prop, strval, intval, src);
232990b4856Slling 
233990b4856Slling 			if (strval != NULL)
234990b4856Slling 				kmem_free(strval,
235990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
236990b4856Slling 
237990b4856Slling 			break;
238990b4856Slling 
239990b4856Slling 		case 1:
240990b4856Slling 			/* string property */
241990b4856Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
242990b4856Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
243990b4856Slling 			    za.za_name, 1, za.za_num_integers, strval);
244990b4856Slling 			if (err) {
245990b4856Slling 				kmem_free(strval, za.za_num_integers);
246990b4856Slling 				break;
247990b4856Slling 			}
2489d82f4f6Slling 			spa_prop_add_list(*nvp, prop, strval, 0, src);
249990b4856Slling 			kmem_free(strval, za.za_num_integers);
250990b4856Slling 			break;
251990b4856Slling 
252990b4856Slling 		default:
253990b4856Slling 			break;
254990b4856Slling 		}
255990b4856Slling 	}
256990b4856Slling 	zap_cursor_fini(&zc);
257990b4856Slling 	mutex_exit(&spa->spa_props_lock);
258990b4856Slling out:
259990b4856Slling 	if (err && err != ENOENT) {
260990b4856Slling 		nvlist_free(*nvp);
2619d82f4f6Slling 		*nvp = NULL;
262990b4856Slling 		return (err);
263990b4856Slling 	}
264990b4856Slling 
265990b4856Slling 	return (0);
266990b4856Slling }
267990b4856Slling 
268990b4856Slling /*
269990b4856Slling  * Validate the given pool properties nvlist and modify the list
270990b4856Slling  * for the property values to be set.
271990b4856Slling  */
272990b4856Slling static int
273990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
274990b4856Slling {
275990b4856Slling 	nvpair_t *elem;
276990b4856Slling 	int error = 0, reset_bootfs = 0;
277990b4856Slling 	uint64_t objnum;
278990b4856Slling 
279990b4856Slling 	elem = NULL;
280990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
281990b4856Slling 		zpool_prop_t prop;
282990b4856Slling 		char *propname, *strval;
283990b4856Slling 		uint64_t intval;
284990b4856Slling 		objset_t *os;
2852f8aaab3Seschrock 		char *slash;
286990b4856Slling 
287990b4856Slling 		propname = nvpair_name(elem);
288990b4856Slling 
289990b4856Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
290990b4856Slling 			return (EINVAL);
291990b4856Slling 
292990b4856Slling 		switch (prop) {
293990b4856Slling 		case ZPOOL_PROP_VERSION:
294990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
295990b4856Slling 			if (!error &&
296990b4856Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
297990b4856Slling 				error = EINVAL;
298990b4856Slling 			break;
299990b4856Slling 
300990b4856Slling 		case ZPOOL_PROP_DELEGATION:
301990b4856Slling 		case ZPOOL_PROP_AUTOREPLACE:
302d5b5bb25SRich Morris 		case ZPOOL_PROP_LISTSNAPS:
303990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
304990b4856Slling 			if (!error && intval > 1)
305990b4856Slling 				error = EINVAL;
306990b4856Slling 			break;
307990b4856Slling 
308990b4856Slling 		case ZPOOL_PROP_BOOTFS:
309990b4856Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
310990b4856Slling 				error = ENOTSUP;
311990b4856Slling 				break;
312990b4856Slling 			}
313990b4856Slling 
314990b4856Slling 			/*
31515e6edf1Sgw 			 * Make sure the vdev config is bootable
316990b4856Slling 			 */
31715e6edf1Sgw 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
318990b4856Slling 				error = ENOTSUP;
319990b4856Slling 				break;
320990b4856Slling 			}
321990b4856Slling 
322990b4856Slling 			reset_bootfs = 1;
323990b4856Slling 
324990b4856Slling 			error = nvpair_value_string(elem, &strval);
325990b4856Slling 
326990b4856Slling 			if (!error) {
32715e6edf1Sgw 				uint64_t compress;
32815e6edf1Sgw 
329990b4856Slling 				if (strval == NULL || strval[0] == '\0') {
330990b4856Slling 					objnum = zpool_prop_default_numeric(
331990b4856Slling 					    ZPOOL_PROP_BOOTFS);
332990b4856Slling 					break;
333990b4856Slling 				}
334990b4856Slling 
335990b4856Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
336745cd3c5Smaybee 				    DS_MODE_USER | DS_MODE_READONLY, &os))
337990b4856Slling 					break;
33815e6edf1Sgw 
33915e6edf1Sgw 				/* We don't support gzip bootable datasets */
34015e6edf1Sgw 				if ((error = dsl_prop_get_integer(strval,
34115e6edf1Sgw 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
34215e6edf1Sgw 				    &compress, NULL)) == 0 &&
34315e6edf1Sgw 				    !BOOTFS_COMPRESS_VALID(compress)) {
34415e6edf1Sgw 					error = ENOTSUP;
34515e6edf1Sgw 				} else {
34615e6edf1Sgw 					objnum = dmu_objset_id(os);
34715e6edf1Sgw 				}
348990b4856Slling 				dmu_objset_close(os);
349990b4856Slling 			}
350990b4856Slling 			break;
351e14bb325SJeff Bonwick 
3520a4e9518Sgw 		case ZPOOL_PROP_FAILUREMODE:
3530a4e9518Sgw 			error = nvpair_value_uint64(elem, &intval);
3540a4e9518Sgw 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3550a4e9518Sgw 			    intval > ZIO_FAILURE_MODE_PANIC))
3560a4e9518Sgw 				error = EINVAL;
3570a4e9518Sgw 
3580a4e9518Sgw 			/*
3590a4e9518Sgw 			 * This is a special case which only occurs when
3600a4e9518Sgw 			 * the pool has completely failed. This allows
3610a4e9518Sgw 			 * the user to change the in-core failmode property
3620a4e9518Sgw 			 * without syncing it out to disk (I/Os might
3630a4e9518Sgw 			 * currently be blocked). We do this by returning
3640a4e9518Sgw 			 * EIO to the caller (spa_prop_set) to trick it
3650a4e9518Sgw 			 * into thinking we encountered a property validation
3660a4e9518Sgw 			 * error.
3670a4e9518Sgw 			 */
368e14bb325SJeff Bonwick 			if (!error && spa_suspended(spa)) {
3690a4e9518Sgw 				spa->spa_failmode = intval;
3700a4e9518Sgw 				error = EIO;
3710a4e9518Sgw 			}
3720a4e9518Sgw 			break;
3732f8aaab3Seschrock 
3742f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
3752f8aaab3Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
3762f8aaab3Seschrock 				break;
3772f8aaab3Seschrock 
3782f8aaab3Seschrock 			if (strval[0] == '\0')
3792f8aaab3Seschrock 				break;
3802f8aaab3Seschrock 
3812f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
3822f8aaab3Seschrock 				break;
3832f8aaab3Seschrock 
3842f8aaab3Seschrock 			if (strval[0] != '/') {
3852f8aaab3Seschrock 				error = EINVAL;
3862f8aaab3Seschrock 				break;
3872f8aaab3Seschrock 			}
3882f8aaab3Seschrock 
3892f8aaab3Seschrock 			slash = strrchr(strval, '/');
3902f8aaab3Seschrock 			ASSERT(slash != NULL);
3912f8aaab3Seschrock 
3922f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3932f8aaab3Seschrock 			    strcmp(slash, "/..") == 0)
3942f8aaab3Seschrock 				error = EINVAL;
3952f8aaab3Seschrock 			break;
396990b4856Slling 		}
397990b4856Slling 
398990b4856Slling 		if (error)
399990b4856Slling 			break;
400990b4856Slling 	}
401990b4856Slling 
402990b4856Slling 	if (!error && reset_bootfs) {
403990b4856Slling 		error = nvlist_remove(props,
404990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
405990b4856Slling 
406990b4856Slling 		if (!error) {
407990b4856Slling 			error = nvlist_add_uint64(props,
408990b4856Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
409990b4856Slling 		}
410990b4856Slling 	}
411990b4856Slling 
412990b4856Slling 	return (error);
413990b4856Slling }
414990b4856Slling 
415*379c004dSEric Schrock void
416*379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
417*379c004dSEric Schrock {
418*379c004dSEric Schrock 	char *cachefile;
419*379c004dSEric Schrock 	spa_config_dirent_t *dp;
420*379c004dSEric Schrock 
421*379c004dSEric Schrock 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
422*379c004dSEric Schrock 	    &cachefile) != 0)
423*379c004dSEric Schrock 		return;
424*379c004dSEric Schrock 
425*379c004dSEric Schrock 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
426*379c004dSEric Schrock 	    KM_SLEEP);
427*379c004dSEric Schrock 
428*379c004dSEric Schrock 	if (cachefile[0] == '\0')
429*379c004dSEric Schrock 		dp->scd_path = spa_strdup(spa_config_path);
430*379c004dSEric Schrock 	else if (strcmp(cachefile, "none") == 0)
431*379c004dSEric Schrock 		dp->scd_path = NULL;
432*379c004dSEric Schrock 	else
433*379c004dSEric Schrock 		dp->scd_path = spa_strdup(cachefile);
434*379c004dSEric Schrock 
435*379c004dSEric Schrock 	list_insert_head(&spa->spa_config_list, dp);
436*379c004dSEric Schrock 	if (need_sync)
437*379c004dSEric Schrock 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
438*379c004dSEric Schrock }
439*379c004dSEric Schrock 
440990b4856Slling int
441990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
442990b4856Slling {
443990b4856Slling 	int error;
444*379c004dSEric Schrock 	nvpair_t *elem;
445*379c004dSEric Schrock 	boolean_t need_sync = B_FALSE;
446*379c004dSEric Schrock 	zpool_prop_t prop;
447990b4856Slling 
448990b4856Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
449990b4856Slling 		return (error);
450990b4856Slling 
451*379c004dSEric Schrock 	elem = NULL;
452*379c004dSEric Schrock 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
453*379c004dSEric Schrock 		if ((prop = zpool_name_to_prop(
454*379c004dSEric Schrock 		    nvpair_name(elem))) == ZPROP_INVAL)
455*379c004dSEric Schrock 			return (EINVAL);
456*379c004dSEric Schrock 
457*379c004dSEric Schrock 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
458*379c004dSEric Schrock 			continue;
459*379c004dSEric Schrock 
460*379c004dSEric Schrock 		need_sync = B_TRUE;
461*379c004dSEric Schrock 		break;
462*379c004dSEric Schrock 	}
463*379c004dSEric Schrock 
464*379c004dSEric Schrock 	if (need_sync)
465*379c004dSEric Schrock 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
466*379c004dSEric Schrock 		    spa, nvp, 3));
467*379c004dSEric Schrock 	else
468*379c004dSEric Schrock 		return (0);
469990b4856Slling }
470990b4856Slling 
471990b4856Slling /*
472990b4856Slling  * If the bootfs property value is dsobj, clear it.
473990b4856Slling  */
474990b4856Slling void
475990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
476990b4856Slling {
477990b4856Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
478990b4856Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
479990b4856Slling 		    spa->spa_pool_props_object,
480990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
481990b4856Slling 		spa->spa_bootfs = 0;
482990b4856Slling 	}
483990b4856Slling }
484990b4856Slling 
485fa9e4066Sahrens /*
486fa9e4066Sahrens  * ==========================================================================
487fa9e4066Sahrens  * SPA state manipulation (open/create/destroy/import/export)
488fa9e4066Sahrens  * ==========================================================================
489fa9e4066Sahrens  */
490fa9e4066Sahrens 
491ea8dc4b6Seschrock static int
492ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b)
493ea8dc4b6Seschrock {
494ea8dc4b6Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
495ea8dc4b6Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
496ea8dc4b6Seschrock 	int ret;
497ea8dc4b6Seschrock 
498ea8dc4b6Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
499ea8dc4b6Seschrock 	    sizeof (zbookmark_t));
500ea8dc4b6Seschrock 
501ea8dc4b6Seschrock 	if (ret < 0)
502ea8dc4b6Seschrock 		return (-1);
503ea8dc4b6Seschrock 	else if (ret > 0)
504ea8dc4b6Seschrock 		return (1);
505ea8dc4b6Seschrock 	else
506ea8dc4b6Seschrock 		return (0);
507ea8dc4b6Seschrock }
508ea8dc4b6Seschrock 
509ea8dc4b6Seschrock /*
510ea8dc4b6Seschrock  * Utility function which retrieves copies of the current logs and
511ea8dc4b6Seschrock  * re-initializes them in the process.
512ea8dc4b6Seschrock  */
513ea8dc4b6Seschrock void
514ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
515ea8dc4b6Seschrock {
516ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
517ea8dc4b6Seschrock 
518ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
519ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
520ea8dc4b6Seschrock 
521ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
522ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
523ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
524ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
525ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
526ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
527ea8dc4b6Seschrock }
528ea8dc4b6Seschrock 
529fa9e4066Sahrens /*
530fa9e4066Sahrens  * Activate an uninitialized pool.
531fa9e4066Sahrens  */
532fa9e4066Sahrens static void
5338ad4d6ddSJeff Bonwick spa_activate(spa_t *spa, int mode)
534fa9e4066Sahrens {
535fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
536fa9e4066Sahrens 
537fa9e4066Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
5388ad4d6ddSJeff Bonwick 	spa->spa_mode = mode;
539fa9e4066Sahrens 
540fa9e4066Sahrens 	spa->spa_normal_class = metaslab_class_create();
5418654d025Sperrin 	spa->spa_log_class = metaslab_class_create();
542fa9e4066Sahrens 
543e14bb325SJeff Bonwick 	for (int t = 0; t < ZIO_TYPES; t++) {
544e14bb325SJeff Bonwick 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
545e14bb325SJeff Bonwick 			spa->spa_zio_taskq[t][q] = taskq_create("spa_zio",
546e14bb325SJeff Bonwick 			    zio_taskq_threads[t][q], maxclsyspri, 50,
547e14bb325SJeff Bonwick 			    INT_MAX, TASKQ_PREPOPULATE);
548e14bb325SJeff Bonwick 		}
549fa9e4066Sahrens 	}
550fa9e4066Sahrens 
551e14bb325SJeff Bonwick 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
552e14bb325SJeff Bonwick 	    offsetof(vdev_t, vdev_config_dirty_node));
553e14bb325SJeff Bonwick 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
554e14bb325SJeff Bonwick 	    offsetof(vdev_t, vdev_state_dirty_node));
555fa9e4066Sahrens 
556fa9e4066Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
557fa9e4066Sahrens 	    offsetof(struct vdev, vdev_txg_node));
558ea8dc4b6Seschrock 
559ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
560ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
561ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
562ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
563ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
564ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
565fa9e4066Sahrens }
566fa9e4066Sahrens 
567fa9e4066Sahrens /*
568fa9e4066Sahrens  * Opposite of spa_activate().
569fa9e4066Sahrens  */
570fa9e4066Sahrens static void
571fa9e4066Sahrens spa_deactivate(spa_t *spa)
572fa9e4066Sahrens {
573fa9e4066Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
574fa9e4066Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
575fa9e4066Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
576fa9e4066Sahrens 
577fa9e4066Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
578fa9e4066Sahrens 
579fa9e4066Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
580fa9e4066Sahrens 
581e14bb325SJeff Bonwick 	list_destroy(&spa->spa_config_dirty_list);
582e14bb325SJeff Bonwick 	list_destroy(&spa->spa_state_dirty_list);
583fa9e4066Sahrens 
584e14bb325SJeff Bonwick 	for (int t = 0; t < ZIO_TYPES; t++) {
585e14bb325SJeff Bonwick 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
586e14bb325SJeff Bonwick 			taskq_destroy(spa->spa_zio_taskq[t][q]);
587e14bb325SJeff Bonwick 			spa->spa_zio_taskq[t][q] = NULL;
588e14bb325SJeff Bonwick 		}
589fa9e4066Sahrens 	}
590fa9e4066Sahrens 
591fa9e4066Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
592fa9e4066Sahrens 	spa->spa_normal_class = NULL;
593fa9e4066Sahrens 
5948654d025Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5958654d025Sperrin 	spa->spa_log_class = NULL;
5968654d025Sperrin 
597ea8dc4b6Seschrock 	/*
598ea8dc4b6Seschrock 	 * If this was part of an import or the open otherwise failed, we may
599ea8dc4b6Seschrock 	 * still have errors left in the queues.  Empty them just in case.
600ea8dc4b6Seschrock 	 */
601ea8dc4b6Seschrock 	spa_errlog_drain(spa);
602ea8dc4b6Seschrock 
603ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
604ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_last);
605ea8dc4b6Seschrock 
606fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
607fa9e4066Sahrens }
608fa9e4066Sahrens 
609fa9e4066Sahrens /*
610fa9e4066Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
611fa9e4066Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
612fa9e4066Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
613fa9e4066Sahrens  * All vdev validation is done by the vdev_alloc() routine.
614fa9e4066Sahrens  */
61599653d4eSeschrock static int
61699653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
61799653d4eSeschrock     uint_t id, int atype)
618fa9e4066Sahrens {
619fa9e4066Sahrens 	nvlist_t **child;
620fa9e4066Sahrens 	uint_t c, children;
62199653d4eSeschrock 	int error;
622fa9e4066Sahrens 
62399653d4eSeschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
62499653d4eSeschrock 		return (error);
625fa9e4066Sahrens 
62699653d4eSeschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
62799653d4eSeschrock 		return (0);
628fa9e4066Sahrens 
629e14bb325SJeff Bonwick 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
630e14bb325SJeff Bonwick 	    &child, &children);
631e14bb325SJeff Bonwick 
632e14bb325SJeff Bonwick 	if (error == ENOENT)
633e14bb325SJeff Bonwick 		return (0);
634e14bb325SJeff Bonwick 
635e14bb325SJeff Bonwick 	if (error) {
63699653d4eSeschrock 		vdev_free(*vdp);
63799653d4eSeschrock 		*vdp = NULL;
63899653d4eSeschrock 		return (EINVAL);
639fa9e4066Sahrens 	}
640fa9e4066Sahrens 
641fa9e4066Sahrens 	for (c = 0; c < children; c++) {
64299653d4eSeschrock 		vdev_t *vd;
64399653d4eSeschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
64499653d4eSeschrock 		    atype)) != 0) {
64599653d4eSeschrock 			vdev_free(*vdp);
64699653d4eSeschrock 			*vdp = NULL;
64799653d4eSeschrock 			return (error);
648fa9e4066Sahrens 		}
649fa9e4066Sahrens 	}
650fa9e4066Sahrens 
65199653d4eSeschrock 	ASSERT(*vdp != NULL);
65299653d4eSeschrock 
65399653d4eSeschrock 	return (0);
654fa9e4066Sahrens }
655fa9e4066Sahrens 
656fa9e4066Sahrens /*
657fa9e4066Sahrens  * Opposite of spa_load().
658fa9e4066Sahrens  */
659fa9e4066Sahrens static void
660fa9e4066Sahrens spa_unload(spa_t *spa)
661fa9e4066Sahrens {
66299653d4eSeschrock 	int i;
66399653d4eSeschrock 
664e14bb325SJeff Bonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
665e14bb325SJeff Bonwick 
666ea8dc4b6Seschrock 	/*
667ea8dc4b6Seschrock 	 * Stop async tasks.
668ea8dc4b6Seschrock 	 */
669ea8dc4b6Seschrock 	spa_async_suspend(spa);
670ea8dc4b6Seschrock 
671fa9e4066Sahrens 	/*
672fa9e4066Sahrens 	 * Stop syncing.
673fa9e4066Sahrens 	 */
674fa9e4066Sahrens 	if (spa->spa_sync_on) {
675fa9e4066Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
676fa9e4066Sahrens 		spa->spa_sync_on = B_FALSE;
677fa9e4066Sahrens 	}
678fa9e4066Sahrens 
679fa9e4066Sahrens 	/*
680e14bb325SJeff Bonwick 	 * Wait for any outstanding async I/O to complete.
681fa9e4066Sahrens 	 */
682e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_async_root_lock);
683e14bb325SJeff Bonwick 	while (spa->spa_async_root_count != 0)
684e14bb325SJeff Bonwick 		cv_wait(&spa->spa_async_root_cv, &spa->spa_async_root_lock);
685e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_async_root_lock);
686fa9e4066Sahrens 
687fa9e4066Sahrens 	/*
688fa9e4066Sahrens 	 * Close the dsl pool.
689fa9e4066Sahrens 	 */
690fa9e4066Sahrens 	if (spa->spa_dsl_pool) {
691fa9e4066Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
692fa9e4066Sahrens 		spa->spa_dsl_pool = NULL;
693fa9e4066Sahrens 	}
694fa9e4066Sahrens 
6958ad4d6ddSJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6968ad4d6ddSJeff Bonwick 
6978ad4d6ddSJeff Bonwick 	/*
6988ad4d6ddSJeff Bonwick 	 * Drop and purge level 2 cache
6998ad4d6ddSJeff Bonwick 	 */
7008ad4d6ddSJeff Bonwick 	spa_l2cache_drop(spa);
7018ad4d6ddSJeff Bonwick 
702fa9e4066Sahrens 	/*
703fa9e4066Sahrens 	 * Close all vdevs.
704fa9e4066Sahrens 	 */
7050e34b6a7Sbonwick 	if (spa->spa_root_vdev)
706fa9e4066Sahrens 		vdev_free(spa->spa_root_vdev);
7070e34b6a7Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
708ea8dc4b6Seschrock 
709fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
710fa94a07fSbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
711fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs) {
712fa94a07fSbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
713fa94a07fSbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
714fa94a07fSbrendan 		spa->spa_spares.sav_vdevs = NULL;
71599653d4eSeschrock 	}
716fa94a07fSbrendan 	if (spa->spa_spares.sav_config) {
717fa94a07fSbrendan 		nvlist_free(spa->spa_spares.sav_config);
718fa94a07fSbrendan 		spa->spa_spares.sav_config = NULL;
719fa94a07fSbrendan 	}
7202ce8af81SEric Schrock 	spa->spa_spares.sav_count = 0;
721fa94a07fSbrendan 
722fa94a07fSbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
723fa94a07fSbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
724fa94a07fSbrendan 	if (spa->spa_l2cache.sav_vdevs) {
725fa94a07fSbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
726fa94a07fSbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
727fa94a07fSbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
728fa94a07fSbrendan 	}
729fa94a07fSbrendan 	if (spa->spa_l2cache.sav_config) {
730fa94a07fSbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
731fa94a07fSbrendan 		spa->spa_l2cache.sav_config = NULL;
73299653d4eSeschrock 	}
7332ce8af81SEric Schrock 	spa->spa_l2cache.sav_count = 0;
73499653d4eSeschrock 
735ea8dc4b6Seschrock 	spa->spa_async_suspended = 0;
7368ad4d6ddSJeff Bonwick 
7378ad4d6ddSJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
738fa9e4066Sahrens }
739fa9e4066Sahrens 
74099653d4eSeschrock /*
74199653d4eSeschrock  * Load (or re-load) the current list of vdevs describing the active spares for
74299653d4eSeschrock  * this pool.  When this is called, we have some form of basic information in
743fa94a07fSbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
744fa94a07fSbrendan  * then re-generate a more complete list including status information.
74599653d4eSeschrock  */
74699653d4eSeschrock static void
74799653d4eSeschrock spa_load_spares(spa_t *spa)
74899653d4eSeschrock {
74999653d4eSeschrock 	nvlist_t **spares;
75099653d4eSeschrock 	uint_t nspares;
75199653d4eSeschrock 	int i;
75239c23413Seschrock 	vdev_t *vd, *tvd;
75399653d4eSeschrock 
754e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
755e14bb325SJeff Bonwick 
75699653d4eSeschrock 	/*
75799653d4eSeschrock 	 * First, close and free any existing spare vdevs.
75899653d4eSeschrock 	 */
759fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
760fa94a07fSbrendan 		vd = spa->spa_spares.sav_vdevs[i];
76139c23413Seschrock 
76239c23413Seschrock 		/* Undo the call to spa_activate() below */
763c5904d13Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
764c5904d13Seschrock 		    B_FALSE)) != NULL && tvd->vdev_isspare)
76539c23413Seschrock 			spa_spare_remove(tvd);
76639c23413Seschrock 		vdev_close(vd);
76739c23413Seschrock 		vdev_free(vd);
76899653d4eSeschrock 	}
76939c23413Seschrock 
770fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs)
771fa94a07fSbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
772fa94a07fSbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
77399653d4eSeschrock 
774fa94a07fSbrendan 	if (spa->spa_spares.sav_config == NULL)
77599653d4eSeschrock 		nspares = 0;
77699653d4eSeschrock 	else
777fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
77899653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
77999653d4eSeschrock 
780fa94a07fSbrendan 	spa->spa_spares.sav_count = (int)nspares;
781fa94a07fSbrendan 	spa->spa_spares.sav_vdevs = NULL;
78299653d4eSeschrock 
78399653d4eSeschrock 	if (nspares == 0)
78499653d4eSeschrock 		return;
78599653d4eSeschrock 
78699653d4eSeschrock 	/*
78799653d4eSeschrock 	 * Construct the array of vdevs, opening them to get status in the
78839c23413Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
78939c23413Seschrock 	 * structures associated with it: one in the list of spares (used only
79039c23413Seschrock 	 * for basic validation purposes) and one in the active vdev
79139c23413Seschrock 	 * configuration (if it's spared in).  During this phase we open and
79239c23413Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
79339c23413Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
79499653d4eSeschrock 	 */
795fa94a07fSbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
796fa94a07fSbrendan 	    KM_SLEEP);
797fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
79899653d4eSeschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
79999653d4eSeschrock 		    VDEV_ALLOC_SPARE) == 0);
80099653d4eSeschrock 		ASSERT(vd != NULL);
80199653d4eSeschrock 
802fa94a07fSbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
80399653d4eSeschrock 
804c5904d13Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
805c5904d13Seschrock 		    B_FALSE)) != NULL) {
80639c23413Seschrock 			if (!tvd->vdev_isspare)
80739c23413Seschrock 				spa_spare_add(tvd);
80839c23413Seschrock 
80939c23413Seschrock 			/*
81039c23413Seschrock 			 * We only mark the spare active if we were successfully
81139c23413Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
81239c23413Seschrock 			 * with a bad active spare would result in strange
81339c23413Seschrock 			 * behavior, because multiple pool would think the spare
81439c23413Seschrock 			 * is actively in use.
81539c23413Seschrock 			 *
81639c23413Seschrock 			 * There is a vulnerability here to an equally bizarre
81739c23413Seschrock 			 * circumstance, where a dead active spare is later
81839c23413Seschrock 			 * brought back to life (onlined or otherwise).  Given
81939c23413Seschrock 			 * the rarity of this scenario, and the extra complexity
82039c23413Seschrock 			 * it adds, we ignore the possibility.
82139c23413Seschrock 			 */
82239c23413Seschrock 			if (!vdev_is_dead(tvd))
82339c23413Seschrock 				spa_spare_activate(tvd);
82439c23413Seschrock 		}
82539c23413Seschrock 
826e14bb325SJeff Bonwick 		vd->vdev_top = vd;
827e14bb325SJeff Bonwick 
82899653d4eSeschrock 		if (vdev_open(vd) != 0)
82999653d4eSeschrock 			continue;
83099653d4eSeschrock 
831fa94a07fSbrendan 		if (vdev_validate_aux(vd) == 0)
832fa94a07fSbrendan 			spa_spare_add(vd);
83399653d4eSeschrock 	}
83499653d4eSeschrock 
83599653d4eSeschrock 	/*
83699653d4eSeschrock 	 * Recompute the stashed list of spares, with status information
83799653d4eSeschrock 	 * this time.
83899653d4eSeschrock 	 */
839fa94a07fSbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
84099653d4eSeschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
84199653d4eSeschrock 
842fa94a07fSbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
843fa94a07fSbrendan 	    KM_SLEEP);
844fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
845fa94a07fSbrendan 		spares[i] = vdev_config_generate(spa,
846fa94a07fSbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
847fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
848fa94a07fSbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
849fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
85099653d4eSeschrock 		nvlist_free(spares[i]);
851fa94a07fSbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
852fa94a07fSbrendan }
853fa94a07fSbrendan 
854fa94a07fSbrendan /*
855fa94a07fSbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
856fa94a07fSbrendan  * this pool.  When this is called, we have some form of basic information in
857fa94a07fSbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
858fa94a07fSbrendan  * then re-generate a more complete list including status information.
859fa94a07fSbrendan  * Devices which are already active have their details maintained, and are
860fa94a07fSbrendan  * not re-opened.
861fa94a07fSbrendan  */
862fa94a07fSbrendan static void
863fa94a07fSbrendan spa_load_l2cache(spa_t *spa)
864fa94a07fSbrendan {
865fa94a07fSbrendan 	nvlist_t **l2cache;
866fa94a07fSbrendan 	uint_t nl2cache;
867fa94a07fSbrendan 	int i, j, oldnvdevs;
868c5904d13Seschrock 	uint64_t guid, size;
869fa94a07fSbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
870fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
871fa94a07fSbrendan 
872e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
873e14bb325SJeff Bonwick 
874fa94a07fSbrendan 	if (sav->sav_config != NULL) {
875fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
876fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
877fa94a07fSbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
878fa94a07fSbrendan 	} else {
879fa94a07fSbrendan 		nl2cache = 0;
880fa94a07fSbrendan 	}
881fa94a07fSbrendan 
882fa94a07fSbrendan 	oldvdevs = sav->sav_vdevs;
883fa94a07fSbrendan 	oldnvdevs = sav->sav_count;
884fa94a07fSbrendan 	sav->sav_vdevs = NULL;
885fa94a07fSbrendan 	sav->sav_count = 0;
886fa94a07fSbrendan 
887fa94a07fSbrendan 	/*
888fa94a07fSbrendan 	 * Process new nvlist of vdevs.
889fa94a07fSbrendan 	 */
890fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
891fa94a07fSbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
892fa94a07fSbrendan 		    &guid) == 0);
893fa94a07fSbrendan 
894fa94a07fSbrendan 		newvdevs[i] = NULL;
895fa94a07fSbrendan 		for (j = 0; j < oldnvdevs; j++) {
896fa94a07fSbrendan 			vd = oldvdevs[j];
897fa94a07fSbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
898fa94a07fSbrendan 				/*
899fa94a07fSbrendan 				 * Retain previous vdev for add/remove ops.
900fa94a07fSbrendan 				 */
901fa94a07fSbrendan 				newvdevs[i] = vd;
902fa94a07fSbrendan 				oldvdevs[j] = NULL;
903fa94a07fSbrendan 				break;
904fa94a07fSbrendan 			}
905fa94a07fSbrendan 		}
906fa94a07fSbrendan 
907fa94a07fSbrendan 		if (newvdevs[i] == NULL) {
908fa94a07fSbrendan 			/*
909fa94a07fSbrendan 			 * Create new vdev
910fa94a07fSbrendan 			 */
911fa94a07fSbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
912fa94a07fSbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
913fa94a07fSbrendan 			ASSERT(vd != NULL);
914fa94a07fSbrendan 			newvdevs[i] = vd;
915fa94a07fSbrendan 
916fa94a07fSbrendan 			/*
917fa94a07fSbrendan 			 * Commit this vdev as an l2cache device,
918fa94a07fSbrendan 			 * even if it fails to open.
919fa94a07fSbrendan 			 */
920fa94a07fSbrendan 			spa_l2cache_add(vd);
921fa94a07fSbrendan 
922c5904d13Seschrock 			vd->vdev_top = vd;
923c5904d13Seschrock 			vd->vdev_aux = sav;
924c5904d13Seschrock 
925c5904d13Seschrock 			spa_l2cache_activate(vd);
926c5904d13Seschrock 
927fa94a07fSbrendan 			if (vdev_open(vd) != 0)
928fa94a07fSbrendan 				continue;
929fa94a07fSbrendan 
930fa94a07fSbrendan 			(void) vdev_validate_aux(vd);
931fa94a07fSbrendan 
932fa94a07fSbrendan 			if (!vdev_is_dead(vd)) {
933fa94a07fSbrendan 				size = vdev_get_rsize(vd);
934c5904d13Seschrock 				l2arc_add_vdev(spa, vd,
935c5904d13Seschrock 				    VDEV_LABEL_START_SIZE,
936c5904d13Seschrock 				    size - VDEV_LABEL_START_SIZE);
937fa94a07fSbrendan 			}
938fa94a07fSbrendan 		}
939fa94a07fSbrendan 	}
940fa94a07fSbrendan 
941fa94a07fSbrendan 	/*
942fa94a07fSbrendan 	 * Purge vdevs that were dropped
943fa94a07fSbrendan 	 */
944fa94a07fSbrendan 	for (i = 0; i < oldnvdevs; i++) {
945fa94a07fSbrendan 		uint64_t pool;
946fa94a07fSbrendan 
947fa94a07fSbrendan 		vd = oldvdevs[i];
948fa94a07fSbrendan 		if (vd != NULL) {
9498ad4d6ddSJeff Bonwick 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
9508ad4d6ddSJeff Bonwick 			    pool != 0ULL && l2arc_vdev_present(vd))
951fa94a07fSbrendan 				l2arc_remove_vdev(vd);
952fa94a07fSbrendan 			(void) vdev_close(vd);
953fa94a07fSbrendan 			spa_l2cache_remove(vd);
954fa94a07fSbrendan 		}
955fa94a07fSbrendan 	}
956fa94a07fSbrendan 
957fa94a07fSbrendan 	if (oldvdevs)
958fa94a07fSbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
959fa94a07fSbrendan 
960fa94a07fSbrendan 	if (sav->sav_config == NULL)
961fa94a07fSbrendan 		goto out;
962fa94a07fSbrendan 
963fa94a07fSbrendan 	sav->sav_vdevs = newvdevs;
964fa94a07fSbrendan 	sav->sav_count = (int)nl2cache;
965fa94a07fSbrendan 
966fa94a07fSbrendan 	/*
967fa94a07fSbrendan 	 * Recompute the stashed list of l2cache devices, with status
968fa94a07fSbrendan 	 * information this time.
969fa94a07fSbrendan 	 */
970fa94a07fSbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
971fa94a07fSbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
972fa94a07fSbrendan 
973fa94a07fSbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
974fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
975fa94a07fSbrendan 		l2cache[i] = vdev_config_generate(spa,
976fa94a07fSbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
977fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
978fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
979fa94a07fSbrendan out:
980fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
981fa94a07fSbrendan 		nvlist_free(l2cache[i]);
982fa94a07fSbrendan 	if (sav->sav_count)
983fa94a07fSbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
98499653d4eSeschrock }
98599653d4eSeschrock 
98699653d4eSeschrock static int
98799653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
98899653d4eSeschrock {
98999653d4eSeschrock 	dmu_buf_t *db;
99099653d4eSeschrock 	char *packed = NULL;
99199653d4eSeschrock 	size_t nvsize = 0;
99299653d4eSeschrock 	int error;
99399653d4eSeschrock 	*value = NULL;
99499653d4eSeschrock 
99599653d4eSeschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
99699653d4eSeschrock 	nvsize = *(uint64_t *)db->db_data;
99799653d4eSeschrock 	dmu_buf_rele(db, FTAG);
99899653d4eSeschrock 
99999653d4eSeschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
100099653d4eSeschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
100199653d4eSeschrock 	if (error == 0)
100299653d4eSeschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
100399653d4eSeschrock 	kmem_free(packed, nvsize);
100499653d4eSeschrock 
100599653d4eSeschrock 	return (error);
100699653d4eSeschrock }
100799653d4eSeschrock 
10083d7072f8Seschrock /*
10093d7072f8Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
10103d7072f8Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
10113d7072f8Seschrock  */
10123d7072f8Seschrock static void
10133d7072f8Seschrock spa_check_removed(vdev_t *vd)
10143d7072f8Seschrock {
10153d7072f8Seschrock 	int c;
10163d7072f8Seschrock 
10173d7072f8Seschrock 	for (c = 0; c < vd->vdev_children; c++)
10183d7072f8Seschrock 		spa_check_removed(vd->vdev_child[c]);
10193d7072f8Seschrock 
10203d7072f8Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
10213d7072f8Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
10223d7072f8Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
10233d7072f8Seschrock 	}
10243d7072f8Seschrock }
10253d7072f8Seschrock 
1026b87f3af3Sperrin /*
1027b87f3af3Sperrin  * Check for missing log devices
1028b87f3af3Sperrin  */
1029b87f3af3Sperrin int
1030b87f3af3Sperrin spa_check_logs(spa_t *spa)
1031b87f3af3Sperrin {
1032b87f3af3Sperrin 	switch (spa->spa_log_state) {
1033b87f3af3Sperrin 	case SPA_LOG_MISSING:
1034b87f3af3Sperrin 		/* need to recheck in case slog has been restored */
1035b87f3af3Sperrin 	case SPA_LOG_UNKNOWN:
1036b87f3af3Sperrin 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1037b87f3af3Sperrin 		    DS_FIND_CHILDREN)) {
1038b87f3af3Sperrin 			spa->spa_log_state = SPA_LOG_MISSING;
1039b87f3af3Sperrin 			return (1);
1040b87f3af3Sperrin 		}
1041b87f3af3Sperrin 		break;
1042b87f3af3Sperrin 
1043b87f3af3Sperrin 	case SPA_LOG_CLEAR:
1044b87f3af3Sperrin 		(void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL,
1045b87f3af3Sperrin 		    DS_FIND_CHILDREN);
1046b87f3af3Sperrin 		break;
1047b87f3af3Sperrin 	}
1048b87f3af3Sperrin 	spa->spa_log_state = SPA_LOG_GOOD;
1049b87f3af3Sperrin 	return (0);
1050b87f3af3Sperrin }
1051b87f3af3Sperrin 
1052fa9e4066Sahrens /*
1053fa9e4066Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
1054ea8dc4b6Seschrock  * source of configuration information.
1055fa9e4066Sahrens  */
1056fa9e4066Sahrens static int
1057ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
1058fa9e4066Sahrens {
1059fa9e4066Sahrens 	int error = 0;
1060fa9e4066Sahrens 	nvlist_t *nvroot = NULL;
1061fa9e4066Sahrens 	vdev_t *rvd;
1062fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
10630373e76bSbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
1064fa9e4066Sahrens 	uint64_t pool_guid;
106599653d4eSeschrock 	uint64_t version;
10663d7072f8Seschrock 	uint64_t autoreplace = 0;
10678ad4d6ddSJeff Bonwick 	int orig_mode = spa->spa_mode;
1068b87f3af3Sperrin 	char *ereport = FM_EREPORT_ZFS_POOL;
1069fa9e4066Sahrens 
10708ad4d6ddSJeff Bonwick 	/*
10718ad4d6ddSJeff Bonwick 	 * If this is an untrusted config, access the pool in read-only mode.
10728ad4d6ddSJeff Bonwick 	 * This prevents things like resilvering recently removed devices.
10738ad4d6ddSJeff Bonwick 	 */
10748ad4d6ddSJeff Bonwick 	if (!mosconfig)
10758ad4d6ddSJeff Bonwick 		spa->spa_mode = FREAD;
10768ad4d6ddSJeff Bonwick 
1077e14bb325SJeff Bonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1078e14bb325SJeff Bonwick 
1079ea8dc4b6Seschrock 	spa->spa_load_state = state;
10800373e76bSbonwick 
1081fa9e4066Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1082a9926bf0Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
1083ea8dc4b6Seschrock 		error = EINVAL;
1084ea8dc4b6Seschrock 		goto out;
1085ea8dc4b6Seschrock 	}
1086fa9e4066Sahrens 
108799653d4eSeschrock 	/*
108899653d4eSeschrock 	 * Versioning wasn't explicitly added to the label until later, so if
108999653d4eSeschrock 	 * it's not present treat it as the initial version.
109099653d4eSeschrock 	 */
109199653d4eSeschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
1092e7437265Sahrens 		version = SPA_VERSION_INITIAL;
109399653d4eSeschrock 
1094a9926bf0Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1095a9926bf0Sbonwick 	    &spa->spa_config_txg);
1096a9926bf0Sbonwick 
10970373e76bSbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1098ea8dc4b6Seschrock 	    spa_guid_exists(pool_guid, 0)) {
1099ea8dc4b6Seschrock 		error = EEXIST;
1100ea8dc4b6Seschrock 		goto out;
1101ea8dc4b6Seschrock 	}
1102fa9e4066Sahrens 
1103b5989ec7Seschrock 	spa->spa_load_guid = pool_guid;
1104b5989ec7Seschrock 
1105fa9e4066Sahrens 	/*
110699653d4eSeschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
110799653d4eSeschrock 	 * value that will be returned by spa_version() since parsing the
110899653d4eSeschrock 	 * configuration requires knowing the version number.
1109fa9e4066Sahrens 	 */
1110e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
111199653d4eSeschrock 	spa->spa_ubsync.ub_version = version;
111299653d4eSeschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
1113e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1114fa9e4066Sahrens 
111599653d4eSeschrock 	if (error != 0)
1116ea8dc4b6Seschrock 		goto out;
1117fa9e4066Sahrens 
11180e34b6a7Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1119fa9e4066Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1120fa9e4066Sahrens 
1121fa9e4066Sahrens 	/*
1122fa9e4066Sahrens 	 * Try to open all vdevs, loading each label in the process.
1123fa9e4066Sahrens 	 */
1124e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11250bf246f5Smc 	error = vdev_open(rvd);
1126e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
11270bf246f5Smc 	if (error != 0)
1128ea8dc4b6Seschrock 		goto out;
1129fa9e4066Sahrens 
1130560e6e96Seschrock 	/*
1131560e6e96Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
1132e14bb325SJeff Bonwick 	 * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER.
1133560e6e96Seschrock 	 */
11348ad4d6ddSJeff Bonwick 	if (mosconfig) {
11358ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
11368ad4d6ddSJeff Bonwick 		error = vdev_validate(rvd);
11378ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
11388ad4d6ddSJeff Bonwick 		if (error != 0)
11398ad4d6ddSJeff Bonwick 			goto out;
11408ad4d6ddSJeff Bonwick 	}
1141560e6e96Seschrock 
1142560e6e96Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1143560e6e96Seschrock 		error = ENXIO;
1144560e6e96Seschrock 		goto out;
1145560e6e96Seschrock 	}
1146560e6e96Seschrock 
1147fa9e4066Sahrens 	/*
1148fa9e4066Sahrens 	 * Find the best uberblock.
1149fa9e4066Sahrens 	 */
1150e14bb325SJeff Bonwick 	vdev_uberblock_load(NULL, rvd, ub);
1151fa9e4066Sahrens 
1152fa9e4066Sahrens 	/*
1153fa9e4066Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1154fa9e4066Sahrens 	 */
1155fa9e4066Sahrens 	if (ub->ub_txg == 0) {
1156eaca9bbdSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1157eaca9bbdSeschrock 		    VDEV_AUX_CORRUPT_DATA);
1158ea8dc4b6Seschrock 		error = ENXIO;
1159ea8dc4b6Seschrock 		goto out;
1160ea8dc4b6Seschrock 	}
1161ea8dc4b6Seschrock 
1162ea8dc4b6Seschrock 	/*
1163ea8dc4b6Seschrock 	 * If the pool is newer than the code, we can't open it.
1164ea8dc4b6Seschrock 	 */
1165e7437265Sahrens 	if (ub->ub_version > SPA_VERSION) {
1166eaca9bbdSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1167eaca9bbdSeschrock 		    VDEV_AUX_VERSION_NEWER);
1168ea8dc4b6Seschrock 		error = ENOTSUP;
1169ea8dc4b6Seschrock 		goto out;
1170fa9e4066Sahrens 	}
1171fa9e4066Sahrens 
1172fa9e4066Sahrens 	/*
1173fa9e4066Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1174fa9e4066Sahrens 	 * incomplete configuration.
1175fa9e4066Sahrens 	 */
1176ecc2d604Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
1177ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1178ea8dc4b6Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
1179ea8dc4b6Seschrock 		error = ENXIO;
1180ea8dc4b6Seschrock 		goto out;
1181fa9e4066Sahrens 	}
1182fa9e4066Sahrens 
1183fa9e4066Sahrens 	/*
1184fa9e4066Sahrens 	 * Initialize internal SPA structures.
1185fa9e4066Sahrens 	 */
1186fa9e4066Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1187fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1188fa9e4066Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
1189ea8dc4b6Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1190ea8dc4b6Seschrock 	if (error) {
1191ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1192ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1193ea8dc4b6Seschrock 		goto out;
1194ea8dc4b6Seschrock 	}
1195fa9e4066Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1196fa9e4066Sahrens 
1197ea8dc4b6Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1198fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
1199ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
1200ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1201ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1202ea8dc4b6Seschrock 		error = EIO;
1203ea8dc4b6Seschrock 		goto out;
1204ea8dc4b6Seschrock 	}
1205fa9e4066Sahrens 
1206fa9e4066Sahrens 	if (!mosconfig) {
120799653d4eSeschrock 		nvlist_t *newconfig;
120895173954Sek 		uint64_t hostid;
1209fa9e4066Sahrens 
121099653d4eSeschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
1211ea8dc4b6Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1212ea8dc4b6Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1213ea8dc4b6Seschrock 			error = EIO;
1214ea8dc4b6Seschrock 			goto out;
1215ea8dc4b6Seschrock 		}
1216fa9e4066Sahrens 
121777650510SLin Ling 		if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig,
121877650510SLin Ling 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
121995173954Sek 			char *hostname;
122095173954Sek 			unsigned long myhostid = 0;
122195173954Sek 
122295173954Sek 			VERIFY(nvlist_lookup_string(newconfig,
122395173954Sek 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
122495173954Sek 
122595173954Sek 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
122617194a52Slling 			if (hostid != 0 && myhostid != 0 &&
122717194a52Slling 			    (unsigned long)hostid != myhostid) {
122895173954Sek 				cmn_err(CE_WARN, "pool '%s' could not be "
122995173954Sek 				    "loaded as it was last accessed by "
123077650510SLin Ling 				    "another system (host: %s hostid: 0x%lx). "
123195173954Sek 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
1232e14bb325SJeff Bonwick 				    spa_name(spa), hostname,
123395173954Sek 				    (unsigned long)hostid);
123495173954Sek 				error = EBADF;
123595173954Sek 				goto out;
123695173954Sek 			}
123795173954Sek 		}
123895173954Sek 
1239fa9e4066Sahrens 		spa_config_set(spa, newconfig);
1240fa9e4066Sahrens 		spa_unload(spa);
1241fa9e4066Sahrens 		spa_deactivate(spa);
12428ad4d6ddSJeff Bonwick 		spa_activate(spa, orig_mode);
1243fa9e4066Sahrens 
1244ea8dc4b6Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
1245fa9e4066Sahrens 	}
1246fa9e4066Sahrens 
1247ea8dc4b6Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1248fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
1249ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
1250ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1251ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1252ea8dc4b6Seschrock 		error = EIO;
1253ea8dc4b6Seschrock 		goto out;
1254ea8dc4b6Seschrock 	}
1255fa9e4066Sahrens 
125699653d4eSeschrock 	/*
125799653d4eSeschrock 	 * Load the bit that tells us to use the new accounting function
125899653d4eSeschrock 	 * (raid-z deflation).  If we have an older pool, this will not
125999653d4eSeschrock 	 * be present.
126099653d4eSeschrock 	 */
126199653d4eSeschrock 	error = zap_lookup(spa->spa_meta_objset,
126299653d4eSeschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
126399653d4eSeschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
126499653d4eSeschrock 	if (error != 0 && error != ENOENT) {
126599653d4eSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
126699653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
126799653d4eSeschrock 		error = EIO;
126899653d4eSeschrock 		goto out;
126999653d4eSeschrock 	}
127099653d4eSeschrock 
1271fa9e4066Sahrens 	/*
1272ea8dc4b6Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
1273ea8dc4b6Seschrock 	 * not be present.
1274fa9e4066Sahrens 	 */
1275ea8dc4b6Seschrock 	error = zap_lookup(spa->spa_meta_objset,
1276ea8dc4b6Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
1277ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
1278d80c45e0Sbonwick 	if (error != 0 && error != ENOENT) {
1279ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1280ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1281ea8dc4b6Seschrock 		error = EIO;
1282ea8dc4b6Seschrock 		goto out;
1283ea8dc4b6Seschrock 	}
1284ea8dc4b6Seschrock 
1285ea8dc4b6Seschrock 	error = zap_lookup(spa->spa_meta_objset,
1286ea8dc4b6Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
1287ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
1288ea8dc4b6Seschrock 	if (error != 0 && error != ENOENT) {
1289ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1290ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1291ea8dc4b6Seschrock 		error = EIO;
1292ea8dc4b6Seschrock 		goto out;
1293ea8dc4b6Seschrock 	}
1294ea8dc4b6Seschrock 
129506eeb2adSek 	/*
129606eeb2adSek 	 * Load the history object.  If we have an older pool, this
129706eeb2adSek 	 * will not be present.
129806eeb2adSek 	 */
129906eeb2adSek 	error = zap_lookup(spa->spa_meta_objset,
130006eeb2adSek 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
130106eeb2adSek 	    sizeof (uint64_t), 1, &spa->spa_history);
130206eeb2adSek 	if (error != 0 && error != ENOENT) {
130306eeb2adSek 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
130406eeb2adSek 		    VDEV_AUX_CORRUPT_DATA);
130506eeb2adSek 		error = EIO;
130606eeb2adSek 		goto out;
130706eeb2adSek 	}
130806eeb2adSek 
130999653d4eSeschrock 	/*
131099653d4eSeschrock 	 * Load any hot spares for this pool.
131199653d4eSeschrock 	 */
131299653d4eSeschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1313fa94a07fSbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
131499653d4eSeschrock 	if (error != 0 && error != ENOENT) {
131599653d4eSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
131699653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
131799653d4eSeschrock 		error = EIO;
131899653d4eSeschrock 		goto out;
131999653d4eSeschrock 	}
132099653d4eSeschrock 	if (error == 0) {
1321e7437265Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
1322fa94a07fSbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
1323fa94a07fSbrendan 		    &spa->spa_spares.sav_config) != 0) {
132499653d4eSeschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
132599653d4eSeschrock 			    VDEV_AUX_CORRUPT_DATA);
132699653d4eSeschrock 			error = EIO;
132799653d4eSeschrock 			goto out;
132899653d4eSeschrock 		}
132999653d4eSeschrock 
1330e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
133199653d4eSeschrock 		spa_load_spares(spa);
1332e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
133399653d4eSeschrock 	}
133499653d4eSeschrock 
1335fa94a07fSbrendan 	/*
1336fa94a07fSbrendan 	 * Load any level 2 ARC devices for this pool.
1337fa94a07fSbrendan 	 */
1338fa94a07fSbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1339fa94a07fSbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
1340fa94a07fSbrendan 	    &spa->spa_l2cache.sav_object);
1341fa94a07fSbrendan 	if (error != 0 && error != ENOENT) {
1342fa94a07fSbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1343fa94a07fSbrendan 		    VDEV_AUX_CORRUPT_DATA);
1344fa94a07fSbrendan 		error = EIO;
1345fa94a07fSbrendan 		goto out;
1346fa94a07fSbrendan 	}
1347fa94a07fSbrendan 	if (error == 0) {
1348fa94a07fSbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
1349fa94a07fSbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
1350fa94a07fSbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
1351fa94a07fSbrendan 			vdev_set_state(rvd, B_TRUE,
1352fa94a07fSbrendan 			    VDEV_STATE_CANT_OPEN,
1353fa94a07fSbrendan 			    VDEV_AUX_CORRUPT_DATA);
1354fa94a07fSbrendan 			error = EIO;
1355fa94a07fSbrendan 			goto out;
1356fa94a07fSbrendan 		}
1357fa94a07fSbrendan 
1358e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1359fa94a07fSbrendan 		spa_load_l2cache(spa);
1360e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
1361fa94a07fSbrendan 	}
1362fa94a07fSbrendan 
1363b87f3af3Sperrin 	if (spa_check_logs(spa)) {
1364b87f3af3Sperrin 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1365b87f3af3Sperrin 		    VDEV_AUX_BAD_LOG);
1366b87f3af3Sperrin 		error = ENXIO;
1367b87f3af3Sperrin 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
1368b87f3af3Sperrin 		goto out;
1369b87f3af3Sperrin 	}
1370b87f3af3Sperrin 
1371b87f3af3Sperrin 
1372990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1373ecd6cf80Smarks 
1374b1b8ab34Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1375b1b8ab34Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
1376b1b8ab34Slling 
1377b1b8ab34Slling 	if (error && error != ENOENT) {
1378b1b8ab34Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1379b1b8ab34Slling 		    VDEV_AUX_CORRUPT_DATA);
1380b1b8ab34Slling 		error = EIO;
1381b1b8ab34Slling 		goto out;
1382b1b8ab34Slling 	}
1383b1b8ab34Slling 
1384b1b8ab34Slling 	if (error == 0) {
1385b1b8ab34Slling 		(void) zap_lookup(spa->spa_meta_objset,
1386b1b8ab34Slling 		    spa->spa_pool_props_object,
13873d7072f8Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1388b1b8ab34Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
13893d7072f8Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
13903d7072f8Seschrock 		    spa->spa_pool_props_object,
13913d7072f8Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
13923d7072f8Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
1393ecd6cf80Smarks 		(void) zap_lookup(spa->spa_meta_objset,
1394ecd6cf80Smarks 		    spa->spa_pool_props_object,
1395ecd6cf80Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
1396ecd6cf80Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
13970a4e9518Sgw 		(void) zap_lookup(spa->spa_meta_objset,
13980a4e9518Sgw 		    spa->spa_pool_props_object,
13990a4e9518Sgw 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
14000a4e9518Sgw 		    sizeof (uint64_t), 1, &spa->spa_failmode);
1401b1b8ab34Slling 	}
1402b1b8ab34Slling 
14033d7072f8Seschrock 	/*
14043d7072f8Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
14053d7072f8Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
14063d7072f8Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
14073d7072f8Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
14083d7072f8Seschrock 	 * over.
14093d7072f8Seschrock 	 */
1410b01c3b58Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
14113d7072f8Seschrock 		spa_check_removed(spa->spa_root_vdev);
14123d7072f8Seschrock 
1413ea8dc4b6Seschrock 	/*
1414560e6e96Seschrock 	 * Load the vdev state for all toplevel vdevs.
1415ea8dc4b6Seschrock 	 */
1416560e6e96Seschrock 	vdev_load(rvd);
14170373e76bSbonwick 
1418fa9e4066Sahrens 	/*
1419fa9e4066Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1420fa9e4066Sahrens 	 */
1421e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1422fa9e4066Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
1423e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1424fa9e4066Sahrens 
1425fa9e4066Sahrens 	/*
1426fa9e4066Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1427fa9e4066Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1428fa9e4066Sahrens 	 */
1429ea8dc4b6Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1430ea8dc4b6Seschrock 		error = ENXIO;
1431ea8dc4b6Seschrock 		goto out;
1432ea8dc4b6Seschrock 	}
1433fa9e4066Sahrens 
14348ad4d6ddSJeff Bonwick 	if (spa_writeable(spa)) {
14355dabedeeSbonwick 		dmu_tx_t *tx;
14360373e76bSbonwick 		int need_update = B_FALSE;
14378ad4d6ddSJeff Bonwick 
14388ad4d6ddSJeff Bonwick 		ASSERT(state != SPA_LOAD_TRYIMPORT);
14395dabedeeSbonwick 
14400373e76bSbonwick 		/*
14410373e76bSbonwick 		 * Claim log blocks that haven't been committed yet.
14420373e76bSbonwick 		 * This must all happen in a single txg.
14430373e76bSbonwick 		 */
14445dabedeeSbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1445fa9e4066Sahrens 		    spa_first_txg(spa));
1446e14bb325SJeff Bonwick 		(void) dmu_objset_find(spa_name(spa),
14470b69c2f0Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1448fa9e4066Sahrens 		dmu_tx_commit(tx);
1449fa9e4066Sahrens 
1450fa9e4066Sahrens 		spa->spa_sync_on = B_TRUE;
1451fa9e4066Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1452fa9e4066Sahrens 
1453fa9e4066Sahrens 		/*
1454fa9e4066Sahrens 		 * Wait for all claims to sync.
1455fa9e4066Sahrens 		 */
1456fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
14570e34b6a7Sbonwick 
14580e34b6a7Sbonwick 		/*
14590373e76bSbonwick 		 * If the config cache is stale, or we have uninitialized
14600373e76bSbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
14610e34b6a7Sbonwick 		 */
14620373e76bSbonwick 		if (config_cache_txg != spa->spa_config_txg ||
14630373e76bSbonwick 		    state == SPA_LOAD_IMPORT)
14640373e76bSbonwick 			need_update = B_TRUE;
14650373e76bSbonwick 
14668ad4d6ddSJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++)
14670373e76bSbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
14680373e76bSbonwick 				need_update = B_TRUE;
14690e34b6a7Sbonwick 
14700e34b6a7Sbonwick 		/*
14710373e76bSbonwick 		 * Update the config cache asychronously in case we're the
14720373e76bSbonwick 		 * root pool, in which case the config cache isn't writable yet.
14730e34b6a7Sbonwick 		 */
14740373e76bSbonwick 		if (need_update)
14750373e76bSbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
14768ad4d6ddSJeff Bonwick 
14778ad4d6ddSJeff Bonwick 		/*
14788ad4d6ddSJeff Bonwick 		 * Check all DTLs to see if anything needs resilvering.
14798ad4d6ddSJeff Bonwick 		 */
14808ad4d6ddSJeff Bonwick 		if (vdev_resilver_needed(rvd, NULL, NULL))
14818ad4d6ddSJeff Bonwick 			spa_async_request(spa, SPA_ASYNC_RESILVER);
1482fa9e4066Sahrens 	}
1483fa9e4066Sahrens 
1484ea8dc4b6Seschrock 	error = 0;
1485ea8dc4b6Seschrock out:
1486088f3894Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
148799653d4eSeschrock 	if (error && error != EBADF)
1488b87f3af3Sperrin 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1489ea8dc4b6Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
1490ea8dc4b6Seschrock 	spa->spa_ena = 0;
1491ea8dc4b6Seschrock 
1492ea8dc4b6Seschrock 	return (error);
1493fa9e4066Sahrens }
1494fa9e4066Sahrens 
1495fa9e4066Sahrens /*
1496fa9e4066Sahrens  * Pool Open/Import
1497fa9e4066Sahrens  *
1498fa9e4066Sahrens  * The import case is identical to an open except that the configuration is sent
1499fa9e4066Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1500fa9e4066Sahrens  * case of an open, the pool configuration will exist in the
15013d7072f8Seschrock  * POOL_STATE_UNINITIALIZED state.
1502fa9e4066Sahrens  *
1503fa9e4066Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1504fa9e4066Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1505fa9e4066Sahrens  * ambiguous state.
1506fa9e4066Sahrens  */
1507fa9e4066Sahrens static int
1508fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1509fa9e4066Sahrens {
1510fa9e4066Sahrens 	spa_t *spa;
1511fa9e4066Sahrens 	int error;
1512fa9e4066Sahrens 	int locked = B_FALSE;
1513fa9e4066Sahrens 
1514fa9e4066Sahrens 	*spapp = NULL;
1515fa9e4066Sahrens 
1516fa9e4066Sahrens 	/*
1517fa9e4066Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1518fa9e4066Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1519fa9e4066Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1520fa9e4066Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1521fa9e4066Sahrens 	 */
1522fa9e4066Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1523fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
1524fa9e4066Sahrens 		locked = B_TRUE;
1525fa9e4066Sahrens 	}
1526fa9e4066Sahrens 
1527fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1528fa9e4066Sahrens 		if (locked)
1529fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
1530fa9e4066Sahrens 		return (ENOENT);
1531fa9e4066Sahrens 	}
1532fa9e4066Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1533fa9e4066Sahrens 
15348ad4d6ddSJeff Bonwick 		spa_activate(spa, spa_mode_global);
1535fa9e4066Sahrens 
15360373e76bSbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1537fa9e4066Sahrens 
1538fa9e4066Sahrens 		if (error == EBADF) {
1539fa9e4066Sahrens 			/*
1540560e6e96Seschrock 			 * If vdev_validate() returns failure (indicated by
1541560e6e96Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
1542560e6e96Seschrock 			 * that the pool has been exported or destroyed.  If
1543560e6e96Seschrock 			 * this is the case, the config cache is out of sync and
1544560e6e96Seschrock 			 * we should remove the pool from the namespace.
1545fa9e4066Sahrens 			 */
1546fa9e4066Sahrens 			spa_unload(spa);
1547fa9e4066Sahrens 			spa_deactivate(spa);
1548c5904d13Seschrock 			spa_config_sync(spa, B_TRUE, B_TRUE);
1549fa9e4066Sahrens 			spa_remove(spa);
1550fa9e4066Sahrens 			if (locked)
1551fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
1552fa9e4066Sahrens 			return (ENOENT);
1553ea8dc4b6Seschrock 		}
1554ea8dc4b6Seschrock 
1555ea8dc4b6Seschrock 		if (error) {
1556fa9e4066Sahrens 			/*
1557fa9e4066Sahrens 			 * We can't open the pool, but we still have useful
1558fa9e4066Sahrens 			 * information: the state of each vdev after the
1559fa9e4066Sahrens 			 * attempted vdev_open().  Return this to the user.
1560fa9e4066Sahrens 			 */
1561e14bb325SJeff Bonwick 			if (config != NULL && spa->spa_root_vdev != NULL)
1562fa9e4066Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1563fa9e4066Sahrens 				    B_TRUE);
1564fa9e4066Sahrens 			spa_unload(spa);
1565fa9e4066Sahrens 			spa_deactivate(spa);
1566ea8dc4b6Seschrock 			spa->spa_last_open_failed = B_TRUE;
1567fa9e4066Sahrens 			if (locked)
1568fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
1569fa9e4066Sahrens 			*spapp = NULL;
1570fa9e4066Sahrens 			return (error);
1571ea8dc4b6Seschrock 		} else {
1572ea8dc4b6Seschrock 			spa->spa_last_open_failed = B_FALSE;
1573fa9e4066Sahrens 		}
1574fa9e4066Sahrens 	}
1575fa9e4066Sahrens 
1576fa9e4066Sahrens 	spa_open_ref(spa, tag);
15773d7072f8Seschrock 
1578fa9e4066Sahrens 	if (locked)
1579fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1580fa9e4066Sahrens 
1581fa9e4066Sahrens 	*spapp = spa;
1582fa9e4066Sahrens 
1583e14bb325SJeff Bonwick 	if (config != NULL)
1584fa9e4066Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1585fa9e4066Sahrens 
1586fa9e4066Sahrens 	return (0);
1587fa9e4066Sahrens }
1588fa9e4066Sahrens 
1589fa9e4066Sahrens int
1590fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1591fa9e4066Sahrens {
1592fa9e4066Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1593fa9e4066Sahrens }
1594fa9e4066Sahrens 
1595ea8dc4b6Seschrock /*
1596ea8dc4b6Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
1597ea8dc4b6Seschrock  * preventing it from being exported or destroyed.
1598ea8dc4b6Seschrock  */
1599ea8dc4b6Seschrock spa_t *
1600ea8dc4b6Seschrock spa_inject_addref(char *name)
1601ea8dc4b6Seschrock {
1602ea8dc4b6Seschrock 	spa_t *spa;
1603ea8dc4b6Seschrock 
1604ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
1605ea8dc4b6Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
1606ea8dc4b6Seschrock 		mutex_exit(&spa_namespace_lock);
1607ea8dc4b6Seschrock 		return (NULL);
1608ea8dc4b6Seschrock 	}
1609ea8dc4b6Seschrock 	spa->spa_inject_ref++;
1610ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1611ea8dc4b6Seschrock 
1612ea8dc4b6Seschrock 	return (spa);
1613ea8dc4b6Seschrock }
1614ea8dc4b6Seschrock 
1615ea8dc4b6Seschrock void
1616ea8dc4b6Seschrock spa_inject_delref(spa_t *spa)
1617ea8dc4b6Seschrock {
1618ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
1619ea8dc4b6Seschrock 	spa->spa_inject_ref--;
1620ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1621ea8dc4b6Seschrock }
1622ea8dc4b6Seschrock 
1623fa94a07fSbrendan /*
1624fa94a07fSbrendan  * Add spares device information to the nvlist.
1625fa94a07fSbrendan  */
162699653d4eSeschrock static void
162799653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config)
162899653d4eSeschrock {
162999653d4eSeschrock 	nvlist_t **spares;
163099653d4eSeschrock 	uint_t i, nspares;
163199653d4eSeschrock 	nvlist_t *nvroot;
163299653d4eSeschrock 	uint64_t guid;
163399653d4eSeschrock 	vdev_stat_t *vs;
163499653d4eSeschrock 	uint_t vsc;
163539c23413Seschrock 	uint64_t pool;
163699653d4eSeschrock 
1637fa94a07fSbrendan 	if (spa->spa_spares.sav_count == 0)
163899653d4eSeschrock 		return;
163999653d4eSeschrock 
164099653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist(config,
164199653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1642fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
164399653d4eSeschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
164499653d4eSeschrock 	if (nspares != 0) {
164599653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
164699653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
164799653d4eSeschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
164899653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
164999653d4eSeschrock 
165099653d4eSeschrock 		/*
165199653d4eSeschrock 		 * Go through and find any spares which have since been
165299653d4eSeschrock 		 * repurposed as an active spare.  If this is the case, update
165399653d4eSeschrock 		 * their status appropriately.
165499653d4eSeschrock 		 */
165599653d4eSeschrock 		for (i = 0; i < nspares; i++) {
165699653d4eSeschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
165799653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
165889a89ebfSlling 			if (spa_spare_exists(guid, &pool, NULL) &&
165989a89ebfSlling 			    pool != 0ULL) {
166099653d4eSeschrock 				VERIFY(nvlist_lookup_uint64_array(
166199653d4eSeschrock 				    spares[i], ZPOOL_CONFIG_STATS,
166299653d4eSeschrock 				    (uint64_t **)&vs, &vsc) == 0);
166399653d4eSeschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
166499653d4eSeschrock 				vs->vs_aux = VDEV_AUX_SPARED;
166599653d4eSeschrock 			}
166699653d4eSeschrock 		}
166799653d4eSeschrock 	}
166899653d4eSeschrock }
166999653d4eSeschrock 
1670fa94a07fSbrendan /*
1671fa94a07fSbrendan  * Add l2cache device information to the nvlist, including vdev stats.
1672fa94a07fSbrendan  */
1673fa94a07fSbrendan static void
1674fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
1675fa94a07fSbrendan {
1676fa94a07fSbrendan 	nvlist_t **l2cache;
1677fa94a07fSbrendan 	uint_t i, j, nl2cache;
1678fa94a07fSbrendan 	nvlist_t *nvroot;
1679fa94a07fSbrendan 	uint64_t guid;
1680fa94a07fSbrendan 	vdev_t *vd;
1681fa94a07fSbrendan 	vdev_stat_t *vs;
1682fa94a07fSbrendan 	uint_t vsc;
1683fa94a07fSbrendan 
1684fa94a07fSbrendan 	if (spa->spa_l2cache.sav_count == 0)
1685fa94a07fSbrendan 		return;
1686fa94a07fSbrendan 
1687e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1688fa94a07fSbrendan 
1689fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist(config,
1690fa94a07fSbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1691fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
1692fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1693fa94a07fSbrendan 	if (nl2cache != 0) {
1694fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
1695fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
1696fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
1697fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1698fa94a07fSbrendan 
1699fa94a07fSbrendan 		/*
1700fa94a07fSbrendan 		 * Update level 2 cache device stats.
1701fa94a07fSbrendan 		 */
1702fa94a07fSbrendan 
1703fa94a07fSbrendan 		for (i = 0; i < nl2cache; i++) {
1704fa94a07fSbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
1705fa94a07fSbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
1706fa94a07fSbrendan 
1707fa94a07fSbrendan 			vd = NULL;
1708fa94a07fSbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
1709fa94a07fSbrendan 				if (guid ==
1710fa94a07fSbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
1711fa94a07fSbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
1712fa94a07fSbrendan 					break;
1713fa94a07fSbrendan 				}
1714fa94a07fSbrendan 			}
1715fa94a07fSbrendan 			ASSERT(vd != NULL);
1716fa94a07fSbrendan 
1717fa94a07fSbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
1718fa94a07fSbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
1719fa94a07fSbrendan 			vdev_get_stats(vd, vs);
1720fa94a07fSbrendan 		}
1721fa94a07fSbrendan 	}
1722fa94a07fSbrendan 
1723e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
1724fa94a07fSbrendan }
1725fa94a07fSbrendan 
1726fa9e4066Sahrens int
1727ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1728fa9e4066Sahrens {
1729fa9e4066Sahrens 	int error;
1730fa9e4066Sahrens 	spa_t *spa;
1731fa9e4066Sahrens 
1732fa9e4066Sahrens 	*config = NULL;
1733fa9e4066Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1734fa9e4066Sahrens 
173599653d4eSeschrock 	if (spa && *config != NULL) {
1736ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
1737ea8dc4b6Seschrock 		    spa_get_errlog_size(spa)) == 0);
1738ea8dc4b6Seschrock 
1739e14bb325SJeff Bonwick 		if (spa_suspended(spa))
1740e14bb325SJeff Bonwick 			VERIFY(nvlist_add_uint64(*config,
1741e14bb325SJeff Bonwick 			    ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0);
1742e14bb325SJeff Bonwick 
174399653d4eSeschrock 		spa_add_spares(spa, *config);
1744fa94a07fSbrendan 		spa_add_l2cache(spa, *config);
174599653d4eSeschrock 	}
174699653d4eSeschrock 
1747ea8dc4b6Seschrock 	/*
1748ea8dc4b6Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
1749ea8dc4b6Seschrock 	 * and call spa_lookup() directly.
1750ea8dc4b6Seschrock 	 */
1751ea8dc4b6Seschrock 	if (altroot) {
1752ea8dc4b6Seschrock 		if (spa == NULL) {
1753ea8dc4b6Seschrock 			mutex_enter(&spa_namespace_lock);
1754ea8dc4b6Seschrock 			spa = spa_lookup(name);
1755ea8dc4b6Seschrock 			if (spa)
1756ea8dc4b6Seschrock 				spa_altroot(spa, altroot, buflen);
1757ea8dc4b6Seschrock 			else
1758ea8dc4b6Seschrock 				altroot[0] = '\0';
1759ea8dc4b6Seschrock 			spa = NULL;
1760ea8dc4b6Seschrock 			mutex_exit(&spa_namespace_lock);
1761ea8dc4b6Seschrock 		} else {
1762ea8dc4b6Seschrock 			spa_altroot(spa, altroot, buflen);
1763ea8dc4b6Seschrock 		}
1764ea8dc4b6Seschrock 	}
1765ea8dc4b6Seschrock 
1766fa9e4066Sahrens 	if (spa != NULL)
1767fa9e4066Sahrens 		spa_close(spa, FTAG);
1768fa9e4066Sahrens 
1769fa9e4066Sahrens 	return (error);
1770fa9e4066Sahrens }
1771fa9e4066Sahrens 
177299653d4eSeschrock /*
1773fa94a07fSbrendan  * Validate that the auxiliary device array is well formed.  We must have an
1774fa94a07fSbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
1775fa94a07fSbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
1776fa94a07fSbrendan  * specified, as long as they are well-formed.
177799653d4eSeschrock  */
177899653d4eSeschrock static int
1779fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
1780fa94a07fSbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
1781fa94a07fSbrendan     vdev_labeltype_t label)
178299653d4eSeschrock {
1783fa94a07fSbrendan 	nvlist_t **dev;
1784fa94a07fSbrendan 	uint_t i, ndev;
178599653d4eSeschrock 	vdev_t *vd;
178699653d4eSeschrock 	int error;
178799653d4eSeschrock 
1788e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1789e14bb325SJeff Bonwick 
179099653d4eSeschrock 	/*
1791fa94a07fSbrendan 	 * It's acceptable to have no devs specified.
179299653d4eSeschrock 	 */
1793fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
179499653d4eSeschrock 		return (0);
179599653d4eSeschrock 
1796fa94a07fSbrendan 	if (ndev == 0)
179799653d4eSeschrock 		return (EINVAL);
179899653d4eSeschrock 
179999653d4eSeschrock 	/*
1800fa94a07fSbrendan 	 * Make sure the pool is formatted with a version that supports this
1801fa94a07fSbrendan 	 * device type.
180299653d4eSeschrock 	 */
1803fa94a07fSbrendan 	if (spa_version(spa) < version)
180499653d4eSeschrock 		return (ENOTSUP);
180599653d4eSeschrock 
180639c23413Seschrock 	/*
1807fa94a07fSbrendan 	 * Set the pending device list so we correctly handle device in-use
180839c23413Seschrock 	 * checking.
180939c23413Seschrock 	 */
1810fa94a07fSbrendan 	sav->sav_pending = dev;
1811fa94a07fSbrendan 	sav->sav_npending = ndev;
181239c23413Seschrock 
1813fa94a07fSbrendan 	for (i = 0; i < ndev; i++) {
1814fa94a07fSbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
181599653d4eSeschrock 		    mode)) != 0)
181639c23413Seschrock 			goto out;
181799653d4eSeschrock 
181899653d4eSeschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
181999653d4eSeschrock 			vdev_free(vd);
182039c23413Seschrock 			error = EINVAL;
182139c23413Seschrock 			goto out;
182299653d4eSeschrock 		}
182399653d4eSeschrock 
1824fa94a07fSbrendan 		/*
1825e14bb325SJeff Bonwick 		 * The L2ARC currently only supports disk devices in
1826e14bb325SJeff Bonwick 		 * kernel context.  For user-level testing, we allow it.
1827fa94a07fSbrendan 		 */
1828e14bb325SJeff Bonwick #ifdef _KERNEL
1829fa94a07fSbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
1830fa94a07fSbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
1831fa94a07fSbrendan 			error = ENOTBLK;
1832fa94a07fSbrendan 			goto out;
1833fa94a07fSbrendan 		}
1834e14bb325SJeff Bonwick #endif
183599653d4eSeschrock 		vd->vdev_top = vd;
183699653d4eSeschrock 
183739c23413Seschrock 		if ((error = vdev_open(vd)) == 0 &&
1838fa94a07fSbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
1839fa94a07fSbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
184039c23413Seschrock 			    vd->vdev_guid) == 0);
184139c23413Seschrock 		}
184299653d4eSeschrock 
184399653d4eSeschrock 		vdev_free(vd);
184439c23413Seschrock 
1845fa94a07fSbrendan 		if (error &&
1846fa94a07fSbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
184739c23413Seschrock 			goto out;
184839c23413Seschrock 		else
184939c23413Seschrock 			error = 0;
185099653d4eSeschrock 	}
185199653d4eSeschrock 
185239c23413Seschrock out:
1853fa94a07fSbrendan 	sav->sav_pending = NULL;
1854fa94a07fSbrendan 	sav->sav_npending = 0;
185539c23413Seschrock 	return (error);
185699653d4eSeschrock }
185799653d4eSeschrock 
1858fa94a07fSbrendan static int
1859fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
1860fa94a07fSbrendan {
1861fa94a07fSbrendan 	int error;
1862fa94a07fSbrendan 
1863e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1864e14bb325SJeff Bonwick 
1865fa94a07fSbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1866fa94a07fSbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
1867fa94a07fSbrendan 	    VDEV_LABEL_SPARE)) != 0) {
1868fa94a07fSbrendan 		return (error);
1869fa94a07fSbrendan 	}
1870fa94a07fSbrendan 
1871fa94a07fSbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1872fa94a07fSbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
1873fa94a07fSbrendan 	    VDEV_LABEL_L2CACHE));
1874fa94a07fSbrendan }
1875fa94a07fSbrendan 
1876fa94a07fSbrendan static void
1877fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
1878fa94a07fSbrendan     const char *config)
1879fa94a07fSbrendan {
1880fa94a07fSbrendan 	int i;
1881fa94a07fSbrendan 
1882fa94a07fSbrendan 	if (sav->sav_config != NULL) {
1883fa94a07fSbrendan 		nvlist_t **olddevs;
1884fa94a07fSbrendan 		uint_t oldndevs;
1885fa94a07fSbrendan 		nvlist_t **newdevs;
1886fa94a07fSbrendan 
1887fa94a07fSbrendan 		/*
1888fa94a07fSbrendan 		 * Generate new dev list by concatentating with the
1889fa94a07fSbrendan 		 * current dev list.
1890fa94a07fSbrendan 		 */
1891fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
1892fa94a07fSbrendan 		    &olddevs, &oldndevs) == 0);
1893fa94a07fSbrendan 
1894fa94a07fSbrendan 		newdevs = kmem_alloc(sizeof (void *) *
1895fa94a07fSbrendan 		    (ndevs + oldndevs), KM_SLEEP);
1896fa94a07fSbrendan 		for (i = 0; i < oldndevs; i++)
1897fa94a07fSbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
1898fa94a07fSbrendan 			    KM_SLEEP) == 0);
1899fa94a07fSbrendan 		for (i = 0; i < ndevs; i++)
1900fa94a07fSbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
1901fa94a07fSbrendan 			    KM_SLEEP) == 0);
1902fa94a07fSbrendan 
1903fa94a07fSbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
1904fa94a07fSbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
1905fa94a07fSbrendan 
1906fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1907fa94a07fSbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
1908fa94a07fSbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
1909fa94a07fSbrendan 			nvlist_free(newdevs[i]);
1910fa94a07fSbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
1911fa94a07fSbrendan 	} else {
1912fa94a07fSbrendan 		/*
1913fa94a07fSbrendan 		 * Generate a new dev list.
1914fa94a07fSbrendan 		 */
1915fa94a07fSbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
1916fa94a07fSbrendan 		    KM_SLEEP) == 0);
1917fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
1918fa94a07fSbrendan 		    devs, ndevs) == 0);
1919fa94a07fSbrendan 	}
1920fa94a07fSbrendan }
1921fa94a07fSbrendan 
1922fa94a07fSbrendan /*
1923fa94a07fSbrendan  * Stop and drop level 2 ARC devices
1924fa94a07fSbrendan  */
1925fa94a07fSbrendan void
1926fa94a07fSbrendan spa_l2cache_drop(spa_t *spa)
1927fa94a07fSbrendan {
1928fa94a07fSbrendan 	vdev_t *vd;
1929fa94a07fSbrendan 	int i;
1930fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
1931fa94a07fSbrendan 
1932fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++) {
1933fa94a07fSbrendan 		uint64_t pool;
1934fa94a07fSbrendan 
1935fa94a07fSbrendan 		vd = sav->sav_vdevs[i];
1936fa94a07fSbrendan 		ASSERT(vd != NULL);
1937fa94a07fSbrendan 
19388ad4d6ddSJeff Bonwick 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
19398ad4d6ddSJeff Bonwick 		    pool != 0ULL && l2arc_vdev_present(vd))
1940fa94a07fSbrendan 			l2arc_remove_vdev(vd);
1941fa94a07fSbrendan 		if (vd->vdev_isl2cache)
1942fa94a07fSbrendan 			spa_l2cache_remove(vd);
1943fa94a07fSbrendan 		vdev_clear_stats(vd);
1944fa94a07fSbrendan 		(void) vdev_close(vd);
1945fa94a07fSbrendan 	}
1946fa94a07fSbrendan }
1947fa94a07fSbrendan 
1948fa9e4066Sahrens /*
1949fa9e4066Sahrens  * Pool Creation
1950fa9e4066Sahrens  */
1951fa9e4066Sahrens int
1952990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
19530a48a24eStimh     const char *history_str, nvlist_t *zplprops)
1954fa9e4066Sahrens {
1955fa9e4066Sahrens 	spa_t *spa;
1956990b4856Slling 	char *altroot = NULL;
19570373e76bSbonwick 	vdev_t *rvd;
1958fa9e4066Sahrens 	dsl_pool_t *dp;
1959fa9e4066Sahrens 	dmu_tx_t *tx;
196099653d4eSeschrock 	int c, error = 0;
1961fa9e4066Sahrens 	uint64_t txg = TXG_INITIAL;
1962fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1963fa94a07fSbrendan 	uint_t nspares, nl2cache;
1964990b4856Slling 	uint64_t version;
1965fa9e4066Sahrens 
1966fa9e4066Sahrens 	/*
1967fa9e4066Sahrens 	 * If this pool already exists, return failure.
1968fa9e4066Sahrens 	 */
1969fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1970fa9e4066Sahrens 	if (spa_lookup(pool) != NULL) {
1971fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1972fa9e4066Sahrens 		return (EEXIST);
1973fa9e4066Sahrens 	}
1974fa9e4066Sahrens 
1975fa9e4066Sahrens 	/*
1976fa9e4066Sahrens 	 * Allocate a new spa_t structure.
1977fa9e4066Sahrens 	 */
1978990b4856Slling 	(void) nvlist_lookup_string(props,
1979990b4856Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
19800373e76bSbonwick 	spa = spa_add(pool, altroot);
19818ad4d6ddSJeff Bonwick 	spa_activate(spa, spa_mode_global);
1982fa9e4066Sahrens 
1983fa9e4066Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
1984990b4856Slling 
1985990b4856Slling 	if (props && (error = spa_prop_validate(spa, props))) {
1986990b4856Slling 		spa_unload(spa);
1987990b4856Slling 		spa_deactivate(spa);
1988990b4856Slling 		spa_remove(spa);
1989c5904d13Seschrock 		mutex_exit(&spa_namespace_lock);
1990990b4856Slling 		return (error);
1991990b4856Slling 	}
1992990b4856Slling 
1993990b4856Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
1994990b4856Slling 	    &version) != 0)
1995990b4856Slling 		version = SPA_VERSION;
1996990b4856Slling 	ASSERT(version <= SPA_VERSION);
1997990b4856Slling 	spa->spa_uberblock.ub_version = version;
1998fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1999fa9e4066Sahrens 
20000373e76bSbonwick 	/*
20010373e76bSbonwick 	 * Create the root vdev.
20020373e76bSbonwick 	 */
2003e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
20040373e76bSbonwick 
200599653d4eSeschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
20060373e76bSbonwick 
200799653d4eSeschrock 	ASSERT(error != 0 || rvd != NULL);
200899653d4eSeschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
20090373e76bSbonwick 
2010b7b97454Sperrin 	if (error == 0 && !zfs_allocatable_devs(nvroot))
20110373e76bSbonwick 		error = EINVAL;
201299653d4eSeschrock 
201399653d4eSeschrock 	if (error == 0 &&
201499653d4eSeschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
2015fa94a07fSbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
201699653d4eSeschrock 	    VDEV_ALLOC_ADD)) == 0) {
201799653d4eSeschrock 		for (c = 0; c < rvd->vdev_children; c++)
201899653d4eSeschrock 			vdev_init(rvd->vdev_child[c], txg);
201999653d4eSeschrock 		vdev_config_dirty(rvd);
20200373e76bSbonwick 	}
20210373e76bSbonwick 
2022e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
2023fa9e4066Sahrens 
202499653d4eSeschrock 	if (error != 0) {
2025fa9e4066Sahrens 		spa_unload(spa);
2026fa9e4066Sahrens 		spa_deactivate(spa);
2027fa9e4066Sahrens 		spa_remove(spa);
2028fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
2029fa9e4066Sahrens 		return (error);
2030fa9e4066Sahrens 	}
2031fa9e4066Sahrens 
203299653d4eSeschrock 	/*
203399653d4eSeschrock 	 * Get the list of spares, if specified.
203499653d4eSeschrock 	 */
203599653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
203699653d4eSeschrock 	    &spares, &nspares) == 0) {
2037fa94a07fSbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
203899653d4eSeschrock 		    KM_SLEEP) == 0);
2039fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
204099653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2041e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
204299653d4eSeschrock 		spa_load_spares(spa);
2043e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
2044fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
2045fa94a07fSbrendan 	}
2046fa94a07fSbrendan 
2047fa94a07fSbrendan 	/*
2048fa94a07fSbrendan 	 * Get the list of level 2 cache devices, if specified.
2049fa94a07fSbrendan 	 */
2050fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2051fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
2052fa94a07fSbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2053fa94a07fSbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2054fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2055fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2056e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2057fa94a07fSbrendan 		spa_load_l2cache(spa);
2058e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
2059fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
206099653d4eSeschrock 	}
206199653d4eSeschrock 
20620a48a24eStimh 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2063fa9e4066Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
2064fa9e4066Sahrens 
2065fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
2066fa9e4066Sahrens 
2067fa9e4066Sahrens 	/*
2068fa9e4066Sahrens 	 * Create the pool config object.
2069fa9e4066Sahrens 	 */
2070fa9e4066Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
2071f7991ba4STim Haley 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2072fa9e4066Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2073fa9e4066Sahrens 
2074ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
2075fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
2076ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
2077ea8dc4b6Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
2078ea8dc4b6Seschrock 	}
2079fa9e4066Sahrens 
2080990b4856Slling 	/* Newly created pools with the right version are always deflated. */
2081990b4856Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
2082990b4856Slling 		spa->spa_deflate = TRUE;
2083990b4856Slling 		if (zap_add(spa->spa_meta_objset,
2084990b4856Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
2085990b4856Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
2086990b4856Slling 			cmn_err(CE_PANIC, "failed to add deflate");
2087990b4856Slling 		}
208899653d4eSeschrock 	}
208999653d4eSeschrock 
2090fa9e4066Sahrens 	/*
2091fa9e4066Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2092fa9e4066Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2093fa9e4066Sahrens 	 * keeps changing.
2094fa9e4066Sahrens 	 */
2095fa9e4066Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2096fa9e4066Sahrens 	    1 << 14, tx);
2097fa9e4066Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2098fa9e4066Sahrens 	    ZIO_COMPRESS_OFF, tx);
2099fa9e4066Sahrens 
2100ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
2101fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
2102ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
2103ea8dc4b6Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
2104ea8dc4b6Seschrock 	}
2105fa9e4066Sahrens 
210606eeb2adSek 	/*
210706eeb2adSek 	 * Create the pool's history object.
210806eeb2adSek 	 */
2109990b4856Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
2110990b4856Slling 		spa_history_create_obj(spa, tx);
2111990b4856Slling 
2112990b4856Slling 	/*
2113990b4856Slling 	 * Set pool properties.
2114990b4856Slling 	 */
2115990b4856Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
2116990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
21170a4e9518Sgw 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
2118*379c004dSEric Schrock 	if (props != NULL) {
2119*379c004dSEric Schrock 		spa_configfile_set(spa, props, B_FALSE);
2120990b4856Slling 		spa_sync_props(spa, props, CRED(), tx);
2121*379c004dSEric Schrock 	}
212206eeb2adSek 
2123fa9e4066Sahrens 	dmu_tx_commit(tx);
2124fa9e4066Sahrens 
2125fa9e4066Sahrens 	spa->spa_sync_on = B_TRUE;
2126fa9e4066Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2127fa9e4066Sahrens 
2128fa9e4066Sahrens 	/*
2129fa9e4066Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2130fa9e4066Sahrens 	 * bean counters are appropriately updated.
2131fa9e4066Sahrens 	 */
2132fa9e4066Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2133fa9e4066Sahrens 
2134c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
2135fa9e4066Sahrens 
2136990b4856Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
2137228975ccSek 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
2138228975ccSek 
2139fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
2140fa9e4066Sahrens 
2141088f3894Sahrens 	spa->spa_minref = refcount_count(&spa->spa_refcount);
2142088f3894Sahrens 
2143fa9e4066Sahrens 	return (0);
2144fa9e4066Sahrens }
2145fa9e4066Sahrens 
2146fa9e4066Sahrens /*
2147fa9e4066Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2148fa9e4066Sahrens  * then call spa_load() to do the dirty work.
2149fa9e4066Sahrens  */
2150e7cbe64fSgw static int
2151e7cbe64fSgw spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
2152c5904d13Seschrock     boolean_t isroot, boolean_t allowfaulted)
2153fa9e4066Sahrens {
2154fa9e4066Sahrens 	spa_t *spa;
2155990b4856Slling 	char *altroot = NULL;
2156c5904d13Seschrock 	int error, loaderr;
215799653d4eSeschrock 	nvlist_t *nvroot;
2158fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
2159fa94a07fSbrendan 	uint_t nspares, nl2cache;
2160fa9e4066Sahrens 
2161fa9e4066Sahrens 	/*
2162fa9e4066Sahrens 	 * If a pool with this name exists, return failure.
2163fa9e4066Sahrens 	 */
2164fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
216500504c01SLin Ling 	if ((spa = spa_lookup(pool)) != NULL) {
216600504c01SLin Ling 		if (isroot) {
216700504c01SLin Ling 			/*
216800504c01SLin Ling 			 * Remove the existing root pool from the
216900504c01SLin Ling 			 * namespace so that we can replace it with
217000504c01SLin Ling 			 * the correct config we just read in.
217100504c01SLin Ling 			 */
217200504c01SLin Ling 			ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
217300504c01SLin Ling 			spa_remove(spa);
217400504c01SLin Ling 		} else {
217500504c01SLin Ling 			mutex_exit(&spa_namespace_lock);
217600504c01SLin Ling 			return (EEXIST);
217700504c01SLin Ling 		}
2178fa9e4066Sahrens 	}
2179fa9e4066Sahrens 
2180fa9e4066Sahrens 	/*
21810373e76bSbonwick 	 * Create and initialize the spa structure.
2182fa9e4066Sahrens 	 */
2183990b4856Slling 	(void) nvlist_lookup_string(props,
2184990b4856Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
21850373e76bSbonwick 	spa = spa_add(pool, altroot);
21868ad4d6ddSJeff Bonwick 	spa_activate(spa, spa_mode_global);
2187fa9e4066Sahrens 
2188c5904d13Seschrock 	if (allowfaulted)
2189c5904d13Seschrock 		spa->spa_import_faulted = B_TRUE;
2190bf82a41bSeschrock 	spa->spa_is_root = isroot;
2191c5904d13Seschrock 
21925dabedeeSbonwick 	/*
21930373e76bSbonwick 	 * Pass off the heavy lifting to spa_load().
2194088f3894Sahrens 	 * Pass TRUE for mosconfig (unless this is a root pool) because
2195088f3894Sahrens 	 * the user-supplied config is actually the one to trust when
2196088f3894Sahrens 	 * doing an import.
21975dabedeeSbonwick 	 */
2198088f3894Sahrens 	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2199fa9e4066Sahrens 
2200e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
220199653d4eSeschrock 	/*
220299653d4eSeschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
220399653d4eSeschrock 	 * and conflicts with spa_has_spare().
220499653d4eSeschrock 	 */
2205e7cbe64fSgw 	if (!isroot && spa->spa_spares.sav_config) {
2206fa94a07fSbrendan 		nvlist_free(spa->spa_spares.sav_config);
2207fa94a07fSbrendan 		spa->spa_spares.sav_config = NULL;
220899653d4eSeschrock 		spa_load_spares(spa);
220999653d4eSeschrock 	}
2210e7cbe64fSgw 	if (!isroot && spa->spa_l2cache.sav_config) {
2211fa94a07fSbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
2212fa94a07fSbrendan 		spa->spa_l2cache.sav_config = NULL;
2213fa94a07fSbrendan 		spa_load_l2cache(spa);
2214fa94a07fSbrendan 	}
221599653d4eSeschrock 
221699653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
221799653d4eSeschrock 	    &nvroot) == 0);
2218fa94a07fSbrendan 	if (error == 0)
2219fa94a07fSbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
2220fa94a07fSbrendan 	if (error == 0)
2221fa94a07fSbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
2222fa94a07fSbrendan 		    VDEV_ALLOC_L2CACHE);
2223e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
222499653d4eSeschrock 
2225*379c004dSEric Schrock 	if (props != NULL)
2226*379c004dSEric Schrock 		spa_configfile_set(spa, props, B_FALSE);
2227*379c004dSEric Schrock 
22288ad4d6ddSJeff Bonwick 	if (error != 0 || (props && spa_writeable(spa) &&
22298ad4d6ddSJeff Bonwick 	    (error = spa_prop_set(spa, props)))) {
2230c5904d13Seschrock 		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
2231c5904d13Seschrock 			/*
2232c5904d13Seschrock 			 * If we failed to load the pool, but 'allowfaulted' is
2233c5904d13Seschrock 			 * set, then manually set the config as if the config
2234c5904d13Seschrock 			 * passed in was specified in the cache file.
2235c5904d13Seschrock 			 */
2236c5904d13Seschrock 			error = 0;
2237c5904d13Seschrock 			spa->spa_import_faulted = B_FALSE;
2238e14bb325SJeff Bonwick 			if (spa->spa_config == NULL)
2239c5904d13Seschrock 				spa->spa_config = spa_config_generate(spa,
2240c5904d13Seschrock 				    NULL, -1ULL, B_TRUE);
2241c5904d13Seschrock 			spa_unload(spa);
2242c5904d13Seschrock 			spa_deactivate(spa);
2243c5904d13Seschrock 			spa_config_sync(spa, B_FALSE, B_TRUE);
2244c5904d13Seschrock 		} else {
2245c5904d13Seschrock 			spa_unload(spa);
2246c5904d13Seschrock 			spa_deactivate(spa);
2247c5904d13Seschrock 			spa_remove(spa);
2248c5904d13Seschrock 		}
2249fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
2250fa9e4066Sahrens 		return (error);
2251fa9e4066Sahrens 	}
2252fa9e4066Sahrens 
225399653d4eSeschrock 	/*
2254fa94a07fSbrendan 	 * Override any spares and level 2 cache devices as specified by
2255fa94a07fSbrendan 	 * the user, as these may have correct device names/devids, etc.
225699653d4eSeschrock 	 */
225799653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
225899653d4eSeschrock 	    &spares, &nspares) == 0) {
2259fa94a07fSbrendan 		if (spa->spa_spares.sav_config)
2260fa94a07fSbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
226199653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
226299653d4eSeschrock 		else
2263fa94a07fSbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
226499653d4eSeschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2265fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
226699653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2267e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
226899653d4eSeschrock 		spa_load_spares(spa);
2269e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
2270fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
2271fa94a07fSbrendan 	}
2272fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2273fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
2274fa94a07fSbrendan 		if (spa->spa_l2cache.sav_config)
2275fa94a07fSbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
2276fa94a07fSbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
2277fa94a07fSbrendan 		else
2278fa94a07fSbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2279fa94a07fSbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2280fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2281fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2282e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2283fa94a07fSbrendan 		spa_load_l2cache(spa);
2284e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
2285fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
228699653d4eSeschrock 	}
228799653d4eSeschrock 
22888ad4d6ddSJeff Bonwick 	if (spa_writeable(spa)) {
2289c5904d13Seschrock 		/*
2290c5904d13Seschrock 		 * Update the config cache to include the newly-imported pool.
2291c5904d13Seschrock 		 */
2292e7cbe64fSgw 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
2293c5904d13Seschrock 	}
2294fa9e4066Sahrens 
2295c5904d13Seschrock 	spa->spa_import_faulted = B_FALSE;
22963d7072f8Seschrock 	mutex_exit(&spa_namespace_lock);
22973d7072f8Seschrock 
2298fa9e4066Sahrens 	return (0);
2299fa9e4066Sahrens }
2300fa9e4066Sahrens 
2301e7cbe64fSgw #ifdef _KERNEL
2302e7cbe64fSgw /*
2303e7cbe64fSgw  * Build a "root" vdev for a top level vdev read in from a rootpool
2304e7cbe64fSgw  * device label.
2305e7cbe64fSgw  */
2306e7cbe64fSgw static void
2307e7cbe64fSgw spa_build_rootpool_config(nvlist_t *config)
2308e7cbe64fSgw {
2309e7cbe64fSgw 	nvlist_t *nvtop, *nvroot;
2310e7cbe64fSgw 	uint64_t pgid;
2311e7cbe64fSgw 
2312e7cbe64fSgw 	/*
2313e7cbe64fSgw 	 * Add this top-level vdev to the child array.
2314e7cbe64fSgw 	 */
2315e7cbe64fSgw 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
2316e7cbe64fSgw 	    == 0);
2317e7cbe64fSgw 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
2318e7cbe64fSgw 	    == 0);
2319e7cbe64fSgw 
2320e7cbe64fSgw 	/*
2321e7cbe64fSgw 	 * Put this pool's top-level vdevs into a root vdev.
2322e7cbe64fSgw 	 */
2323e7cbe64fSgw 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2324e7cbe64fSgw 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
2325e7cbe64fSgw 	    == 0);
2326e7cbe64fSgw 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
2327e7cbe64fSgw 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
2328e7cbe64fSgw 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2329e7cbe64fSgw 	    &nvtop, 1) == 0);
2330e7cbe64fSgw 
2331e7cbe64fSgw 	/*
2332e7cbe64fSgw 	 * Replace the existing vdev_tree with the new root vdev in
2333e7cbe64fSgw 	 * this pool's configuration (remove the old, add the new).
2334e7cbe64fSgw 	 */
2335e7cbe64fSgw 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
2336e7cbe64fSgw 	nvlist_free(nvroot);
2337e7cbe64fSgw }
2338e7cbe64fSgw 
2339e7cbe64fSgw /*
2340e7cbe64fSgw  * Get the root pool information from the root disk, then import the root pool
2341e7cbe64fSgw  * during the system boot up time.
2342e7cbe64fSgw  */
2343f940fbb1SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
2344e7cbe64fSgw 
2345051aabe6Staylor int
2346051aabe6Staylor spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
2347e7cbe64fSgw     uint64_t *besttxg)
2348e7cbe64fSgw {
2349e7cbe64fSgw 	nvlist_t *config;
2350e7cbe64fSgw 	uint64_t txg;
2351f940fbb1SLin Ling 	int error;
2352e7cbe64fSgw 
2353f940fbb1SLin Ling 	if (error = vdev_disk_read_rootlabel(devpath, devid, &config))
2354f940fbb1SLin Ling 		return (error);
2355e7cbe64fSgw 
2356e7cbe64fSgw 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
2357e7cbe64fSgw 
2358051aabe6Staylor 	if (bestconf != NULL)
2359e7cbe64fSgw 		*bestconf = config;
2360f940fbb1SLin Ling 	else
2361f940fbb1SLin Ling 		nvlist_free(config);
2362051aabe6Staylor 	*besttxg = txg;
2363051aabe6Staylor 	return (0);
2364e7cbe64fSgw }
2365e7cbe64fSgw 
2366e7cbe64fSgw boolean_t
2367e7cbe64fSgw spa_rootdev_validate(nvlist_t *nv)
2368e7cbe64fSgw {
2369e7cbe64fSgw 	uint64_t ival;
2370e7cbe64fSgw 
2371e7cbe64fSgw 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2372e7cbe64fSgw 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2373e7cbe64fSgw 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2374e7cbe64fSgw 		return (B_FALSE);
2375e7cbe64fSgw 
2376e7cbe64fSgw 	return (B_TRUE);
2377e7cbe64fSgw }
2378e7cbe64fSgw 
2379051aabe6Staylor 
2380051aabe6Staylor /*
2381051aabe6Staylor  * Given the boot device's physical path or devid, check if the device
2382051aabe6Staylor  * is in a valid state.  If so, return the configuration from the vdev
2383051aabe6Staylor  * label.
2384051aabe6Staylor  */
2385051aabe6Staylor int
2386051aabe6Staylor spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
2387051aabe6Staylor {
2388051aabe6Staylor 	nvlist_t *conf = NULL;
2389051aabe6Staylor 	uint64_t txg = 0;
2390051aabe6Staylor 	nvlist_t *nvtop, **child;
2391051aabe6Staylor 	char *type;
2392051aabe6Staylor 	char *bootpath = NULL;
2393051aabe6Staylor 	uint_t children, c;
2394051aabe6Staylor 	char *tmp;
2395f940fbb1SLin Ling 	int error;
2396051aabe6Staylor 
2397051aabe6Staylor 	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
2398051aabe6Staylor 		*tmp = '\0';
2399f940fbb1SLin Ling 	if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) {
2400051aabe6Staylor 		cmn_err(CE_NOTE, "error reading device label");
2401f940fbb1SLin Ling 		return (error);
2402051aabe6Staylor 	}
2403051aabe6Staylor 	if (txg == 0) {
2404051aabe6Staylor 		cmn_err(CE_NOTE, "this device is detached");
2405051aabe6Staylor 		nvlist_free(conf);
2406051aabe6Staylor 		return (EINVAL);
2407051aabe6Staylor 	}
2408051aabe6Staylor 
2409051aabe6Staylor 	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
2410051aabe6Staylor 	    &nvtop) == 0);
2411051aabe6Staylor 	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
2412051aabe6Staylor 
2413051aabe6Staylor 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2414051aabe6Staylor 		if (spa_rootdev_validate(nvtop)) {
2415051aabe6Staylor 			goto out;
2416051aabe6Staylor 		} else {
2417051aabe6Staylor 			nvlist_free(conf);
2418051aabe6Staylor 			return (EINVAL);
2419051aabe6Staylor 		}
2420051aabe6Staylor 	}
2421051aabe6Staylor 
2422051aabe6Staylor 	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
2423051aabe6Staylor 
2424051aabe6Staylor 	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
2425051aabe6Staylor 	    &child, &children) == 0);
2426051aabe6Staylor 
2427051aabe6Staylor 	/*
2428051aabe6Staylor 	 * Go thru vdevs in the mirror to see if the given device
2429051aabe6Staylor 	 * has the most recent txg. Only the device with the most
2430051aabe6Staylor 	 * recent txg has valid information and should be booted.
2431051aabe6Staylor 	 */
2432051aabe6Staylor 	for (c = 0; c < children; c++) {
2433051aabe6Staylor 		char *cdevid, *cpath;
2434051aabe6Staylor 		uint64_t tmptxg;
2435051aabe6Staylor 
2436ffb5616eSLin Ling 		cpath = NULL;
2437ffb5616eSLin Ling 		cdevid = NULL;
2438051aabe6Staylor 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
2439ffb5616eSLin Ling 		    &cpath) != 0 && nvlist_lookup_string(child[c],
2440ffb5616eSLin Ling 		    ZPOOL_CONFIG_DEVID, &cdevid) != 0)
2441051aabe6Staylor 			return (EINVAL);
2442f4565e39SLin Ling 		if ((spa_check_rootconf(cpath, cdevid, NULL,
2443f4565e39SLin Ling 		    &tmptxg) == 0) && (tmptxg > txg)) {
2444051aabe6Staylor 			txg = tmptxg;
2445051aabe6Staylor 			VERIFY(nvlist_lookup_string(child[c],
2446051aabe6Staylor 			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
2447051aabe6Staylor 		}
2448051aabe6Staylor 	}
2449051aabe6Staylor 
2450051aabe6Staylor 	/* Does the best device match the one we've booted from? */
2451051aabe6Staylor 	if (bootpath) {
2452051aabe6Staylor 		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
2453051aabe6Staylor 		return (EINVAL);
2454051aabe6Staylor 	}
2455051aabe6Staylor out:
2456051aabe6Staylor 	*bestconf = conf;
2457051aabe6Staylor 	return (0);
2458051aabe6Staylor }
2459051aabe6Staylor 
2460e7cbe64fSgw /*
2461e7cbe64fSgw  * Import a root pool.
2462e7cbe64fSgw  *
2463051aabe6Staylor  * For x86. devpath_list will consist of devid and/or physpath name of
2464051aabe6Staylor  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
2465051aabe6Staylor  * The GRUB "findroot" command will return the vdev we should boot.
2466e7cbe64fSgw  *
2467e7cbe64fSgw  * For Sparc, devpath_list consists the physpath name of the booting device
2468e7cbe64fSgw  * no matter the rootpool is a single device pool or a mirrored pool.
2469e7cbe64fSgw  * e.g.
2470e7cbe64fSgw  *	"/pci@1f,0/ide@d/disk@0,0:a"
2471e7cbe64fSgw  */
2472e7cbe64fSgw int
2473051aabe6Staylor spa_import_rootpool(char *devpath, char *devid)
2474e7cbe64fSgw {
2475e7cbe64fSgw 	nvlist_t *conf = NULL;
2476e7cbe64fSgw 	char *pname;
2477e7cbe64fSgw 	int error;
2478e7cbe64fSgw 
2479e7cbe64fSgw 	/*
2480e7cbe64fSgw 	 * Get the vdev pathname and configuation from the most
2481e7cbe64fSgw 	 * recently updated vdev (highest txg).
2482e7cbe64fSgw 	 */
2483051aabe6Staylor 	if (error = spa_get_rootconf(devpath, devid, &conf))
2484e7cbe64fSgw 		goto msg_out;
2485e7cbe64fSgw 
2486e7cbe64fSgw 	/*
2487e7cbe64fSgw 	 * Add type "root" vdev to the config.
2488e7cbe64fSgw 	 */
2489e7cbe64fSgw 	spa_build_rootpool_config(conf);
2490e7cbe64fSgw 
2491e7cbe64fSgw 	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
2492e7cbe64fSgw 
2493bf82a41bSeschrock 	/*
2494bf82a41bSeschrock 	 * We specify 'allowfaulted' for this to be treated like spa_open()
2495bf82a41bSeschrock 	 * instead of spa_import().  This prevents us from marking vdevs as
2496bf82a41bSeschrock 	 * persistently unavailable, and generates FMA ereports as if it were a
2497bf82a41bSeschrock 	 * pool open, not import.
2498bf82a41bSeschrock 	 */
2499bf82a41bSeschrock 	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
250000504c01SLin Ling 	ASSERT(error != EEXIST);
2501e7cbe64fSgw 
2502e7cbe64fSgw 	nvlist_free(conf);
2503e7cbe64fSgw 	return (error);
2504e7cbe64fSgw 
2505e7cbe64fSgw msg_out:
2506051aabe6Staylor 	cmn_err(CE_NOTE, "\n"
2507e7cbe64fSgw 	    "  ***************************************************  \n"
2508e7cbe64fSgw 	    "  *  This device is not bootable!                   *  \n"
2509e7cbe64fSgw 	    "  *  It is either offlined or detached or faulted.  *  \n"
2510e7cbe64fSgw 	    "  *  Please try to boot from a different device.    *  \n"
2511051aabe6Staylor 	    "  ***************************************************  ");
2512e7cbe64fSgw 
2513e7cbe64fSgw 	return (error);
2514e7cbe64fSgw }
2515e7cbe64fSgw #endif
2516e7cbe64fSgw 
2517e7cbe64fSgw /*
2518e7cbe64fSgw  * Import a non-root pool into the system.
2519e7cbe64fSgw  */
2520e7cbe64fSgw int
2521e7cbe64fSgw spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
2522e7cbe64fSgw {
2523c5904d13Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
2524e7cbe64fSgw }
2525e7cbe64fSgw 
2526c5904d13Seschrock int
2527c5904d13Seschrock spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
2528c5904d13Seschrock {
2529c5904d13Seschrock 	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
2530c5904d13Seschrock }
2531c5904d13Seschrock 
2532c5904d13Seschrock 
2533fa9e4066Sahrens /*
2534fa9e4066Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2535fa9e4066Sahrens  * to get the vdev stats associated with the imported devices.
2536fa9e4066Sahrens  */
2537fa9e4066Sahrens #define	TRYIMPORT_NAME	"$import"
2538fa9e4066Sahrens 
2539fa9e4066Sahrens nvlist_t *
2540fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig)
2541fa9e4066Sahrens {
2542fa9e4066Sahrens 	nvlist_t *config = NULL;
2543fa9e4066Sahrens 	char *poolname;
2544fa9e4066Sahrens 	spa_t *spa;
2545fa9e4066Sahrens 	uint64_t state;
2546fa9e4066Sahrens 
2547fa9e4066Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2548fa9e4066Sahrens 		return (NULL);
2549fa9e4066Sahrens 
2550fa9e4066Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2551fa9e4066Sahrens 		return (NULL);
2552fa9e4066Sahrens 
2553fa9e4066Sahrens 	/*
25540373e76bSbonwick 	 * Create and initialize the spa structure.
2555fa9e4066Sahrens 	 */
25560373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
25570373e76bSbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
25588ad4d6ddSJeff Bonwick 	spa_activate(spa, FREAD);
2559fa9e4066Sahrens 
2560fa9e4066Sahrens 	/*
25610373e76bSbonwick 	 * Pass off the heavy lifting to spa_load().
2562ecc2d604Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
2563ecc2d604Sbonwick 	 * is actually the one to trust when doing an import.
2564fa9e4066Sahrens 	 */
2565ecc2d604Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2566fa9e4066Sahrens 
2567fa9e4066Sahrens 	/*
2568fa9e4066Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2569fa9e4066Sahrens 	 */
2570fa9e4066Sahrens 	if (spa->spa_root_vdev != NULL) {
2571fa9e4066Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2572fa9e4066Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2573fa9e4066Sahrens 		    poolname) == 0);
2574fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2575fa9e4066Sahrens 		    state) == 0);
257695173954Sek 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
257795173954Sek 		    spa->spa_uberblock.ub_timestamp) == 0);
257899653d4eSeschrock 
2579e7cbe64fSgw 		/*
2580e7cbe64fSgw 		 * If the bootfs property exists on this pool then we
2581e7cbe64fSgw 		 * copy it out so that external consumers can tell which
2582e7cbe64fSgw 		 * pools are bootable.
2583e7cbe64fSgw 		 */
2584e7cbe64fSgw 		if (spa->spa_bootfs) {
2585e7cbe64fSgw 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2586e7cbe64fSgw 
2587e7cbe64fSgw 			/*
2588e7cbe64fSgw 			 * We have to play games with the name since the
2589e7cbe64fSgw 			 * pool was opened as TRYIMPORT_NAME.
2590e7cbe64fSgw 			 */
2591e14bb325SJeff Bonwick 			if (dsl_dsobj_to_dsname(spa_name(spa),
2592e7cbe64fSgw 			    spa->spa_bootfs, tmpname) == 0) {
2593e7cbe64fSgw 				char *cp;
2594e7cbe64fSgw 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2595e7cbe64fSgw 
2596e7cbe64fSgw 				cp = strchr(tmpname, '/');
2597e7cbe64fSgw 				if (cp == NULL) {
2598e7cbe64fSgw 					(void) strlcpy(dsname, tmpname,
2599e7cbe64fSgw 					    MAXPATHLEN);
2600e7cbe64fSgw 				} else {
2601e7cbe64fSgw 					(void) snprintf(dsname, MAXPATHLEN,
2602e7cbe64fSgw 					    "%s/%s", poolname, ++cp);
2603e7cbe64fSgw 				}
2604e7cbe64fSgw 				VERIFY(nvlist_add_string(config,
2605e7cbe64fSgw 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
2606e7cbe64fSgw 				kmem_free(dsname, MAXPATHLEN);
2607e7cbe64fSgw 			}
2608e7cbe64fSgw 			kmem_free(tmpname, MAXPATHLEN);
2609e7cbe64fSgw 		}
2610e7cbe64fSgw 
261199653d4eSeschrock 		/*
2612fa94a07fSbrendan 		 * Add the list of hot spares and level 2 cache devices.
261399653d4eSeschrock 		 */
261499653d4eSeschrock 		spa_add_spares(spa, config);
2615fa94a07fSbrendan 		spa_add_l2cache(spa, config);
2616fa9e4066Sahrens 	}
2617fa9e4066Sahrens 
2618fa9e4066Sahrens 	spa_unload(spa);
2619fa9e4066Sahrens 	spa_deactivate(spa);
2620fa9e4066Sahrens 	spa_remove(spa);
2621fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
2622fa9e4066Sahrens 
2623fa9e4066Sahrens 	return (config);
2624fa9e4066Sahrens }
2625fa9e4066Sahrens 
2626fa9e4066Sahrens /*
2627fa9e4066Sahrens  * Pool export/destroy
2628fa9e4066Sahrens  *
2629fa9e4066Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2630fa9e4066Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2631fa9e4066Sahrens  * update the pool state and sync all the labels to disk, removing the
2632394ab0cbSGeorge Wilson  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
2633394ab0cbSGeorge Wilson  * we don't sync the labels or remove the configuration cache.
2634fa9e4066Sahrens  */
2635fa9e4066Sahrens static int
263689a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
2637394ab0cbSGeorge Wilson     boolean_t force, boolean_t hardforce)
2638fa9e4066Sahrens {
2639fa9e4066Sahrens 	spa_t *spa;
2640fa9e4066Sahrens 
264144cd46caSbillm 	if (oldconfig)
264244cd46caSbillm 		*oldconfig = NULL;
264344cd46caSbillm 
26448ad4d6ddSJeff Bonwick 	if (!(spa_mode_global & FWRITE))
2645fa9e4066Sahrens 		return (EROFS);
2646fa9e4066Sahrens 
2647fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
2648fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2649fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
2650fa9e4066Sahrens 		return (ENOENT);
2651fa9e4066Sahrens 	}
2652fa9e4066Sahrens 
2653ea8dc4b6Seschrock 	/*
2654ea8dc4b6Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
2655ea8dc4b6Seschrock 	 * reacquire the namespace lock, and see if we can export.
2656ea8dc4b6Seschrock 	 */
2657ea8dc4b6Seschrock 	spa_open_ref(spa, FTAG);
2658ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
2659ea8dc4b6Seschrock 	spa_async_suspend(spa);
2660ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
2661ea8dc4b6Seschrock 	spa_close(spa, FTAG);
2662ea8dc4b6Seschrock 
2663fa9e4066Sahrens 	/*
2664fa9e4066Sahrens 	 * The pool will be in core if it's openable,
2665fa9e4066Sahrens 	 * in which case we can modify its state.
2666fa9e4066Sahrens 	 */
2667fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2668fa9e4066Sahrens 		/*
2669fa9e4066Sahrens 		 * Objsets may be open only because they're dirty, so we
2670fa9e4066Sahrens 		 * have to force it to sync before checking spa_refcnt.
2671fa9e4066Sahrens 		 */
2672fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2673fa9e4066Sahrens 
2674ea8dc4b6Seschrock 		/*
2675ea8dc4b6Seschrock 		 * A pool cannot be exported or destroyed if there are active
2676ea8dc4b6Seschrock 		 * references.  If we are resetting a pool, allow references by
2677ea8dc4b6Seschrock 		 * fault injection handlers.
2678ea8dc4b6Seschrock 		 */
2679ea8dc4b6Seschrock 		if (!spa_refcount_zero(spa) ||
2680ea8dc4b6Seschrock 		    (spa->spa_inject_ref != 0 &&
2681ea8dc4b6Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
2682ea8dc4b6Seschrock 			spa_async_resume(spa);
2683fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
2684fa9e4066Sahrens 			return (EBUSY);
2685fa9e4066Sahrens 		}
2686fa9e4066Sahrens 
268789a89ebfSlling 		/*
268889a89ebfSlling 		 * A pool cannot be exported if it has an active shared spare.
268989a89ebfSlling 		 * This is to prevent other pools stealing the active spare
269089a89ebfSlling 		 * from an exported pool. At user's own will, such pool can
269189a89ebfSlling 		 * be forcedly exported.
269289a89ebfSlling 		 */
269389a89ebfSlling 		if (!force && new_state == POOL_STATE_EXPORTED &&
269489a89ebfSlling 		    spa_has_active_shared_spare(spa)) {
269589a89ebfSlling 			spa_async_resume(spa);
269689a89ebfSlling 			mutex_exit(&spa_namespace_lock);
269789a89ebfSlling 			return (EXDEV);
269889a89ebfSlling 		}
269989a89ebfSlling 
2700fa9e4066Sahrens 		/*
2701fa9e4066Sahrens 		 * We want this to be reflected on every label,
2702fa9e4066Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2703fa9e4066Sahrens 		 * final sync that pushes these changes out.
2704fa9e4066Sahrens 		 */
2705394ab0cbSGeorge Wilson 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
2706e14bb325SJeff Bonwick 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2707ea8dc4b6Seschrock 			spa->spa_state = new_state;
27080373e76bSbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
2709ea8dc4b6Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
2710e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_ALL, FTAG);
2711ea8dc4b6Seschrock 		}
2712fa9e4066Sahrens 	}
2713fa9e4066Sahrens 
27143d7072f8Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
27153d7072f8Seschrock 
2716fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2717fa9e4066Sahrens 		spa_unload(spa);
2718fa9e4066Sahrens 		spa_deactivate(spa);
2719fa9e4066Sahrens 	}
2720fa9e4066Sahrens 
272144cd46caSbillm 	if (oldconfig && spa->spa_config)
272244cd46caSbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
272344cd46caSbillm 
2724ea8dc4b6Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
2725394ab0cbSGeorge Wilson 		if (!hardforce)
2726394ab0cbSGeorge Wilson 			spa_config_sync(spa, B_TRUE, B_TRUE);
2727ea8dc4b6Seschrock 		spa_remove(spa);
2728ea8dc4b6Seschrock 	}
2729fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
2730fa9e4066Sahrens 
2731fa9e4066Sahrens 	return (0);
2732fa9e4066Sahrens }
2733fa9e4066Sahrens 
2734fa9e4066Sahrens /*
2735fa9e4066Sahrens  * Destroy a storage pool.
2736fa9e4066Sahrens  */
2737fa9e4066Sahrens int
2738fa9e4066Sahrens spa_destroy(char *pool)
2739fa9e4066Sahrens {
2740394ab0cbSGeorge Wilson 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
2741394ab0cbSGeorge Wilson 	    B_FALSE, B_FALSE));
2742fa9e4066Sahrens }
2743fa9e4066Sahrens 
2744fa9e4066Sahrens /*
2745fa9e4066Sahrens  * Export a storage pool.
2746fa9e4066Sahrens  */
2747fa9e4066Sahrens int
2748394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
2749394ab0cbSGeorge Wilson     boolean_t hardforce)
2750fa9e4066Sahrens {
2751394ab0cbSGeorge Wilson 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
2752394ab0cbSGeorge Wilson 	    force, hardforce));
2753fa9e4066Sahrens }
2754fa9e4066Sahrens 
2755ea8dc4b6Seschrock /*
2756ea8dc4b6Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
2757ea8dc4b6Seschrock  * from the namespace in any way.
2758ea8dc4b6Seschrock  */
2759ea8dc4b6Seschrock int
2760ea8dc4b6Seschrock spa_reset(char *pool)
2761ea8dc4b6Seschrock {
276289a89ebfSlling 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
2763394ab0cbSGeorge Wilson 	    B_FALSE, B_FALSE));
2764ea8dc4b6Seschrock }
2765ea8dc4b6Seschrock 
2766fa9e4066Sahrens /*
2767fa9e4066Sahrens  * ==========================================================================
2768fa9e4066Sahrens  * Device manipulation
2769fa9e4066Sahrens  * ==========================================================================
2770fa9e4066Sahrens  */
2771fa9e4066Sahrens 
2772fa9e4066Sahrens /*
27738654d025Sperrin  * Add a device to a storage pool.
2774fa9e4066Sahrens  */
2775fa9e4066Sahrens int
2776fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2777fa9e4066Sahrens {
2778fa9e4066Sahrens 	uint64_t txg;
27798ad4d6ddSJeff Bonwick 	int error;
2780fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
27810e34b6a7Sbonwick 	vdev_t *vd, *tvd;
2782fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
2783fa94a07fSbrendan 	uint_t nspares, nl2cache;
2784fa9e4066Sahrens 
2785fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
2786fa9e4066Sahrens 
278799653d4eSeschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
278899653d4eSeschrock 	    VDEV_ALLOC_ADD)) != 0)
278999653d4eSeschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
2790fa9e4066Sahrens 
2791e14bb325SJeff Bonwick 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
279299653d4eSeschrock 
2793fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
2794fa94a07fSbrendan 	    &nspares) != 0)
279599653d4eSeschrock 		nspares = 0;
279699653d4eSeschrock 
2797fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
2798fa94a07fSbrendan 	    &nl2cache) != 0)
2799fa94a07fSbrendan 		nl2cache = 0;
2800fa94a07fSbrendan 
2801e14bb325SJeff Bonwick 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
2802fa9e4066Sahrens 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
2803fa9e4066Sahrens 
2804e14bb325SJeff Bonwick 	if (vd->vdev_children != 0 &&
2805e14bb325SJeff Bonwick 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
2806e14bb325SJeff Bonwick 		return (spa_vdev_exit(spa, vd, txg, error));
280799653d4eSeschrock 
280839c23413Seschrock 	/*
2809fa94a07fSbrendan 	 * We must validate the spares and l2cache devices after checking the
2810fa94a07fSbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
281139c23413Seschrock 	 */
2812e14bb325SJeff Bonwick 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
281339c23413Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
281439c23413Seschrock 
281539c23413Seschrock 	/*
281639c23413Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
281739c23413Seschrock 	 */
28188ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++) {
281939c23413Seschrock 		tvd = vd->vdev_child[c];
282039c23413Seschrock 		vdev_remove_child(vd, tvd);
282139c23413Seschrock 		tvd->vdev_id = rvd->vdev_children;
282239c23413Seschrock 		vdev_add_child(rvd, tvd);
282339c23413Seschrock 		vdev_config_dirty(tvd);
282439c23413Seschrock 	}
282539c23413Seschrock 
282699653d4eSeschrock 	if (nspares != 0) {
2827fa94a07fSbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
2828fa94a07fSbrendan 		    ZPOOL_CONFIG_SPARES);
282999653d4eSeschrock 		spa_load_spares(spa);
2830fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
2831fa94a07fSbrendan 	}
2832fa94a07fSbrendan 
2833fa94a07fSbrendan 	if (nl2cache != 0) {
2834fa94a07fSbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
2835fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE);
2836fa94a07fSbrendan 		spa_load_l2cache(spa);
2837fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2838fa9e4066Sahrens 	}
2839fa9e4066Sahrens 
2840fa9e4066Sahrens 	/*
28410e34b6a7Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
28420e34b6a7Sbonwick 	 * If other threads start allocating from these vdevs before we
28430e34b6a7Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
28440e34b6a7Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
28450e34b6a7Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
28460e34b6a7Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
28470373e76bSbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
28480e34b6a7Sbonwick 	 *
28490e34b6a7Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
28500e34b6a7Sbonwick 	 * if we lose power at any point in this sequence, the remaining
28510e34b6a7Sbonwick 	 * steps will be completed the next time we load the pool.
28520e34b6a7Sbonwick 	 */
28530373e76bSbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
28540e34b6a7Sbonwick 
28550373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
28560373e76bSbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
28570373e76bSbonwick 	mutex_exit(&spa_namespace_lock);
2858fa9e4066Sahrens 
28590373e76bSbonwick 	return (0);
2860fa9e4066Sahrens }
2861fa9e4066Sahrens 
2862fa9e4066Sahrens /*
2863fa9e4066Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2864fa9e4066Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2865fa9e4066Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2866fa9e4066Sahrens  *
2867fa9e4066Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2868fa9e4066Sahrens  * existing device; in this case the two devices are made into their own
28693d7072f8Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2870fa9e4066Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2871fa9e4066Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2872fa9e4066Sahrens  * completion of resilvering, the first disk (the one being replaced)
2873fa9e4066Sahrens  * is automatically detached.
2874fa9e4066Sahrens  */
2875fa9e4066Sahrens int
2876ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2877fa9e4066Sahrens {
2878fa9e4066Sahrens 	uint64_t txg, open_txg;
2879fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2880fa9e4066Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
288199653d4eSeschrock 	vdev_ops_t *pvops;
28829b3f6b42SEric Kustarz 	dmu_tx_t *tx;
28839b3f6b42SEric Kustarz 	char *oldvdpath, *newvdpath;
28849b3f6b42SEric Kustarz 	int newvd_isspare;
28859b3f6b42SEric Kustarz 	int error;
2886fa9e4066Sahrens 
2887fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
2888fa9e4066Sahrens 
2889c5904d13Seschrock 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2890fa9e4066Sahrens 
2891fa9e4066Sahrens 	if (oldvd == NULL)
2892fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2893fa9e4066Sahrens 
28940e34b6a7Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
28950e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
28960e34b6a7Sbonwick 
2897fa9e4066Sahrens 	pvd = oldvd->vdev_parent;
2898fa9e4066Sahrens 
289999653d4eSeschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
29003d7072f8Seschrock 	    VDEV_ALLOC_ADD)) != 0)
29013d7072f8Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
29023d7072f8Seschrock 
29033d7072f8Seschrock 	if (newrootvd->vdev_children != 1)
2904fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2905fa9e4066Sahrens 
2906fa9e4066Sahrens 	newvd = newrootvd->vdev_child[0];
2907fa9e4066Sahrens 
2908fa9e4066Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2909fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2910fa9e4066Sahrens 
291199653d4eSeschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2912fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2913fa9e4066Sahrens 
29148654d025Sperrin 	/*
29158654d025Sperrin 	 * Spares can't replace logs
29168654d025Sperrin 	 */
2917ee0eb9f2SEric Schrock 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
29188654d025Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
29198654d025Sperrin 
292099653d4eSeschrock 	if (!replacing) {
292199653d4eSeschrock 		/*
292299653d4eSeschrock 		 * For attach, the only allowable parent is a mirror or the root
292399653d4eSeschrock 		 * vdev.
292499653d4eSeschrock 		 */
292599653d4eSeschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
292699653d4eSeschrock 		    pvd->vdev_ops != &vdev_root_ops)
292799653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
292899653d4eSeschrock 
292999653d4eSeschrock 		pvops = &vdev_mirror_ops;
293099653d4eSeschrock 	} else {
293199653d4eSeschrock 		/*
293299653d4eSeschrock 		 * Active hot spares can only be replaced by inactive hot
293399653d4eSeschrock 		 * spares.
293499653d4eSeschrock 		 */
293599653d4eSeschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
293699653d4eSeschrock 		    pvd->vdev_child[1] == oldvd &&
293799653d4eSeschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
293899653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
293999653d4eSeschrock 
294099653d4eSeschrock 		/*
294199653d4eSeschrock 		 * If the source is a hot spare, and the parent isn't already a
294299653d4eSeschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
294339c23413Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
294439c23413Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
294539c23413Seschrock 		 * the same (spare replaces spare, non-spare replaces
294639c23413Seschrock 		 * non-spare).
294799653d4eSeschrock 		 */
294899653d4eSeschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
294999653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
295039c23413Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
295139c23413Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
295239c23413Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
295399653d4eSeschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
295499653d4eSeschrock 		    newvd->vdev_isspare)
295599653d4eSeschrock 			pvops = &vdev_spare_ops;
295699653d4eSeschrock 		else
295799653d4eSeschrock 			pvops = &vdev_replacing_ops;
295899653d4eSeschrock 	}
295999653d4eSeschrock 
29602a79c5feSlling 	/*
29612a79c5feSlling 	 * Compare the new device size with the replaceable/attachable
29622a79c5feSlling 	 * device size.
29632a79c5feSlling 	 */
29642a79c5feSlling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2965fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2966fa9e4066Sahrens 
2967ecc2d604Sbonwick 	/*
2968ecc2d604Sbonwick 	 * The new device cannot have a higher alignment requirement
2969ecc2d604Sbonwick 	 * than the top-level vdev.
2970ecc2d604Sbonwick 	 */
2971ecc2d604Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2972fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2973fa9e4066Sahrens 
2974fa9e4066Sahrens 	/*
2975fa9e4066Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2976fa9e4066Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2977fa9e4066Sahrens 	 */
2978fa9e4066Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2979fa9e4066Sahrens 		spa_strfree(oldvd->vdev_path);
2980fa9e4066Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2981fa9e4066Sahrens 		    KM_SLEEP);
2982fa9e4066Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2983fa9e4066Sahrens 		    newvd->vdev_path, "old");
2984fa9e4066Sahrens 		if (oldvd->vdev_devid != NULL) {
2985fa9e4066Sahrens 			spa_strfree(oldvd->vdev_devid);
2986fa9e4066Sahrens 			oldvd->vdev_devid = NULL;
2987fa9e4066Sahrens 		}
2988fa9e4066Sahrens 	}
2989fa9e4066Sahrens 
2990fa9e4066Sahrens 	/*
299199653d4eSeschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
299299653d4eSeschrock 	 * mirror/replacing/spare vdev above oldvd.
2993fa9e4066Sahrens 	 */
2994fa9e4066Sahrens 	if (pvd->vdev_ops != pvops)
2995fa9e4066Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2996fa9e4066Sahrens 
2997fa9e4066Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2998fa9e4066Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2999fa9e4066Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
3000fa9e4066Sahrens 
3001fa9e4066Sahrens 	/*
3002fa9e4066Sahrens 	 * Extract the new device from its root and add it to pvd.
3003fa9e4066Sahrens 	 */
3004fa9e4066Sahrens 	vdev_remove_child(newrootvd, newvd);
3005fa9e4066Sahrens 	newvd->vdev_id = pvd->vdev_children;
3006fa9e4066Sahrens 	vdev_add_child(pvd, newvd);
3007fa9e4066Sahrens 
3008ea8dc4b6Seschrock 	/*
3009ea8dc4b6Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
3010ea8dc4b6Seschrock 	 * the addition of newvd may have decreased our parent's asize.
3011ea8dc4b6Seschrock 	 */
3012ea8dc4b6Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
3013ea8dc4b6Seschrock 
3014fa9e4066Sahrens 	tvd = newvd->vdev_top;
3015fa9e4066Sahrens 	ASSERT(pvd->vdev_top == tvd);
3016fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3017fa9e4066Sahrens 
3018fa9e4066Sahrens 	vdev_config_dirty(tvd);
3019fa9e4066Sahrens 
3020fa9e4066Sahrens 	/*
3021fa9e4066Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3022fa9e4066Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3023fa9e4066Sahrens 	 */
3024fa9e4066Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3025fa9e4066Sahrens 
30268ad4d6ddSJeff Bonwick 	vdev_dtl_dirty(newvd, DTL_MISSING,
30278ad4d6ddSJeff Bonwick 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3028fa9e4066Sahrens 
302939c23413Seschrock 	if (newvd->vdev_isspare)
303039c23413Seschrock 		spa_spare_activate(newvd);
3031e14bb325SJeff Bonwick 	oldvdpath = spa_strdup(oldvd->vdev_path);
3032e14bb325SJeff Bonwick 	newvdpath = spa_strdup(newvd->vdev_path);
30339b3f6b42SEric Kustarz 	newvd_isspare = newvd->vdev_isspare;
3034ea8dc4b6Seschrock 
3035fa9e4066Sahrens 	/*
3036fa9e4066Sahrens 	 * Mark newvd's DTL dirty in this txg.
3037fa9e4066Sahrens 	 */
3038ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3039fa9e4066Sahrens 
3040fa9e4066Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3041fa9e4066Sahrens 
30429b3f6b42SEric Kustarz 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
30439b3f6b42SEric Kustarz 	if (dmu_tx_assign(tx, TXG_WAIT) == 0) {
30449b3f6b42SEric Kustarz 		spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx,
30459b3f6b42SEric Kustarz 		    CRED(),  "%s vdev=%s %s vdev=%s",
30469b3f6b42SEric Kustarz 		    replacing && newvd_isspare ? "spare in" :
30479b3f6b42SEric Kustarz 		    replacing ? "replace" : "attach", newvdpath,
30489b3f6b42SEric Kustarz 		    replacing ? "for" : "to", oldvdpath);
30499b3f6b42SEric Kustarz 		dmu_tx_commit(tx);
30509b3f6b42SEric Kustarz 	} else {
30519b3f6b42SEric Kustarz 		dmu_tx_abort(tx);
30529b3f6b42SEric Kustarz 	}
30539b3f6b42SEric Kustarz 
30549b3f6b42SEric Kustarz 	spa_strfree(oldvdpath);
30559b3f6b42SEric Kustarz 	spa_strfree(newvdpath);
30569b3f6b42SEric Kustarz 
3057fa9e4066Sahrens 	/*
3058088f3894Sahrens 	 * Kick off a resilver to update newvd.
3059fa9e4066Sahrens 	 */
3060088f3894Sahrens 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3061fa9e4066Sahrens 
3062fa9e4066Sahrens 	return (0);
3063fa9e4066Sahrens }
3064fa9e4066Sahrens 
3065fa9e4066Sahrens /*
3066fa9e4066Sahrens  * Detach a device from a mirror or replacing vdev.
3067fa9e4066Sahrens  * If 'replace_done' is specified, only detach if the parent
3068fa9e4066Sahrens  * is a replacing vdev.
3069fa9e4066Sahrens  */
3070fa9e4066Sahrens int
30718ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3072fa9e4066Sahrens {
3073fa9e4066Sahrens 	uint64_t txg;
30748ad4d6ddSJeff Bonwick 	int error;
3075fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3076fa9e4066Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
307799653d4eSeschrock 	boolean_t unspare = B_FALSE;
307899653d4eSeschrock 	uint64_t unspare_guid;
3079bf82a41bSeschrock 	size_t len;
3080fa9e4066Sahrens 
3081fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
3082fa9e4066Sahrens 
3083c5904d13Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3084fa9e4066Sahrens 
3085fa9e4066Sahrens 	if (vd == NULL)
3086fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3087fa9e4066Sahrens 
30880e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
30890e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
30900e34b6a7Sbonwick 
3091fa9e4066Sahrens 	pvd = vd->vdev_parent;
3092fa9e4066Sahrens 
30938ad4d6ddSJeff Bonwick 	/*
30948ad4d6ddSJeff Bonwick 	 * If the parent/child relationship is not as expected, don't do it.
30958ad4d6ddSJeff Bonwick 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
30968ad4d6ddSJeff Bonwick 	 * vdev that's replacing B with C.  The user's intent in replacing
30978ad4d6ddSJeff Bonwick 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
30988ad4d6ddSJeff Bonwick 	 * the replace by detaching C, the expected behavior is to end up
30998ad4d6ddSJeff Bonwick 	 * M(A,B).  But suppose that right after deciding to detach C,
31008ad4d6ddSJeff Bonwick 	 * the replacement of B completes.  We would have M(A,C), and then
31018ad4d6ddSJeff Bonwick 	 * ask to detach C, which would leave us with just A -- not what
31028ad4d6ddSJeff Bonwick 	 * the user wanted.  To prevent this, we make sure that the
31038ad4d6ddSJeff Bonwick 	 * parent/child relationship hasn't changed -- in this example,
31048ad4d6ddSJeff Bonwick 	 * that C's parent is still the replacing vdev R.
31058ad4d6ddSJeff Bonwick 	 */
31068ad4d6ddSJeff Bonwick 	if (pvd->vdev_guid != pguid && pguid != 0)
31078ad4d6ddSJeff Bonwick 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
31088ad4d6ddSJeff Bonwick 
3109fa9e4066Sahrens 	/*
3110fa9e4066Sahrens 	 * If replace_done is specified, only remove this device if it's
311199653d4eSeschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
311299653d4eSeschrock 	 * disk can be removed.
311399653d4eSeschrock 	 */
311499653d4eSeschrock 	if (replace_done) {
311599653d4eSeschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
311699653d4eSeschrock 			if (vd->vdev_id != 0)
311799653d4eSeschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
311899653d4eSeschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
311999653d4eSeschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
312099653d4eSeschrock 		}
312199653d4eSeschrock 	}
312299653d4eSeschrock 
312399653d4eSeschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
3124e7437265Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
3125fa9e4066Sahrens 
3126fa9e4066Sahrens 	/*
312799653d4eSeschrock 	 * Only mirror, replacing, and spare vdevs support detach.
3128fa9e4066Sahrens 	 */
3129fa9e4066Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
313099653d4eSeschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
313199653d4eSeschrock 	    pvd->vdev_ops != &vdev_spare_ops)
3132fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3133fa9e4066Sahrens 
3134fa9e4066Sahrens 	/*
31358ad4d6ddSJeff Bonwick 	 * If this device has the only valid copy of some data,
31368ad4d6ddSJeff Bonwick 	 * we cannot safely detach it.
3137fa9e4066Sahrens 	 */
31388ad4d6ddSJeff Bonwick 	if (vdev_dtl_required(vd))
3139fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3140fa9e4066Sahrens 
31418ad4d6ddSJeff Bonwick 	ASSERT(pvd->vdev_children >= 2);
3142fa9e4066Sahrens 
3143bf82a41bSeschrock 	/*
3144bf82a41bSeschrock 	 * If we are detaching the second disk from a replacing vdev, then
3145bf82a41bSeschrock 	 * check to see if we changed the original vdev's path to have "/old"
3146bf82a41bSeschrock 	 * at the end in spa_vdev_attach().  If so, undo that change now.
3147bf82a41bSeschrock 	 */
3148bf82a41bSeschrock 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
3149bf82a41bSeschrock 	    pvd->vdev_child[0]->vdev_path != NULL &&
3150bf82a41bSeschrock 	    pvd->vdev_child[1]->vdev_path != NULL) {
3151bf82a41bSeschrock 		ASSERT(pvd->vdev_child[1] == vd);
3152bf82a41bSeschrock 		cvd = pvd->vdev_child[0];
3153bf82a41bSeschrock 		len = strlen(vd->vdev_path);
3154bf82a41bSeschrock 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
3155bf82a41bSeschrock 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
3156bf82a41bSeschrock 			spa_strfree(cvd->vdev_path);
3157bf82a41bSeschrock 			cvd->vdev_path = spa_strdup(vd->vdev_path);
3158bf82a41bSeschrock 		}
3159bf82a41bSeschrock 	}
3160bf82a41bSeschrock 
316199653d4eSeschrock 	/*
316299653d4eSeschrock 	 * If we are detaching the original disk from a spare, then it implies
316399653d4eSeschrock 	 * that the spare should become a real disk, and be removed from the
316499653d4eSeschrock 	 * active spare list for the pool.
316599653d4eSeschrock 	 */
316699653d4eSeschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
31678ad4d6ddSJeff Bonwick 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
316899653d4eSeschrock 		unspare = B_TRUE;
316999653d4eSeschrock 
3170fa9e4066Sahrens 	/*
3171fa9e4066Sahrens 	 * Erase the disk labels so the disk can be used for other things.
3172fa9e4066Sahrens 	 * This must be done after all other error cases are handled,
3173fa9e4066Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
3174fa9e4066Sahrens 	 * But if we can't do it, don't treat the error as fatal --
3175fa9e4066Sahrens 	 * it may be that the unwritability of the disk is the reason
3176fa9e4066Sahrens 	 * it's being detached!
3177fa9e4066Sahrens 	 */
317839c23413Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3179fa9e4066Sahrens 
3180fa9e4066Sahrens 	/*
3181fa9e4066Sahrens 	 * Remove vd from its parent and compact the parent's children.
3182fa9e4066Sahrens 	 */
3183fa9e4066Sahrens 	vdev_remove_child(pvd, vd);
3184fa9e4066Sahrens 	vdev_compact_children(pvd);
3185fa9e4066Sahrens 
3186fa9e4066Sahrens 	/*
3187fa9e4066Sahrens 	 * Remember one of the remaining children so we can get tvd below.
3188fa9e4066Sahrens 	 */
3189fa9e4066Sahrens 	cvd = pvd->vdev_child[0];
3190fa9e4066Sahrens 
319199653d4eSeschrock 	/*
319299653d4eSeschrock 	 * If we need to remove the remaining child from the list of hot spares,
31938ad4d6ddSJeff Bonwick 	 * do it now, marking the vdev as no longer a spare in the process.
31948ad4d6ddSJeff Bonwick 	 * We must do this before vdev_remove_parent(), because that can
31958ad4d6ddSJeff Bonwick 	 * change the GUID if it creates a new toplevel GUID.  For a similar
31968ad4d6ddSJeff Bonwick 	 * reason, we must remove the spare now, in the same txg as the detach;
31978ad4d6ddSJeff Bonwick 	 * otherwise someone could attach a new sibling, change the GUID, and
31988ad4d6ddSJeff Bonwick 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
319999653d4eSeschrock 	 */
320099653d4eSeschrock 	if (unspare) {
320199653d4eSeschrock 		ASSERT(cvd->vdev_isspare);
320239c23413Seschrock 		spa_spare_remove(cvd);
320399653d4eSeschrock 		unspare_guid = cvd->vdev_guid;
32048ad4d6ddSJeff Bonwick 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
320599653d4eSeschrock 	}
320699653d4eSeschrock 
3207fa9e4066Sahrens 	/*
3208fa9e4066Sahrens 	 * If the parent mirror/replacing vdev only has one child,
3209fa9e4066Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
3210fa9e4066Sahrens 	 */
3211fa9e4066Sahrens 	if (pvd->vdev_children == 1)
3212fa9e4066Sahrens 		vdev_remove_parent(cvd);
3213fa9e4066Sahrens 
3214fa9e4066Sahrens 	/*
3215fa9e4066Sahrens 	 * We don't set tvd until now because the parent we just removed
3216fa9e4066Sahrens 	 * may have been the previous top-level vdev.
3217fa9e4066Sahrens 	 */
3218fa9e4066Sahrens 	tvd = cvd->vdev_top;
3219fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
3220fa9e4066Sahrens 
3221fa9e4066Sahrens 	/*
322239c23413Seschrock 	 * Reevaluate the parent vdev state.
3223fa9e4066Sahrens 	 */
32243d7072f8Seschrock 	vdev_propagate_state(cvd);
3225fa9e4066Sahrens 
3226fa9e4066Sahrens 	/*
322739c23413Seschrock 	 * If the device we just detached was smaller than the others, it may be
322839c23413Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
322939c23413Seschrock 	 * can't fail because the existing metaslabs are already in core, so
323039c23413Seschrock 	 * there's nothing to read from disk.
3231fa9e4066Sahrens 	 */
3232ecc2d604Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3233fa9e4066Sahrens 
3234fa9e4066Sahrens 	vdev_config_dirty(tvd);
3235fa9e4066Sahrens 
3236fa9e4066Sahrens 	/*
323739c23413Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
323839c23413Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
323939c23413Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
324039c23413Seschrock 	 * prevent vd from being accessed after it's freed.
3241fa9e4066Sahrens 	 */
32428ad4d6ddSJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
3243fa9e4066Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
3244ecc2d604Sbonwick 	vd->vdev_detached = B_TRUE;
3245ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3246fa9e4066Sahrens 
32473d7072f8Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
32483d7072f8Seschrock 
324999653d4eSeschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
325099653d4eSeschrock 
325199653d4eSeschrock 	/*
325239c23413Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
325339c23413Seschrock 	 * then we want to go through and remove the device from the hot spare
325439c23413Seschrock 	 * list of every other pool.
325599653d4eSeschrock 	 */
325699653d4eSeschrock 	if (unspare) {
32578ad4d6ddSJeff Bonwick 		spa_t *myspa = spa;
325899653d4eSeschrock 		spa = NULL;
325999653d4eSeschrock 		mutex_enter(&spa_namespace_lock);
326099653d4eSeschrock 		while ((spa = spa_next(spa)) != NULL) {
326199653d4eSeschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
326299653d4eSeschrock 				continue;
32638ad4d6ddSJeff Bonwick 			if (spa == myspa)
32648ad4d6ddSJeff Bonwick 				continue;
32659af0a4dfSJeff Bonwick 			spa_open_ref(spa, FTAG);
32669af0a4dfSJeff Bonwick 			mutex_exit(&spa_namespace_lock);
326799653d4eSeschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
32689af0a4dfSJeff Bonwick 			mutex_enter(&spa_namespace_lock);
32699af0a4dfSJeff Bonwick 			spa_close(spa, FTAG);
327099653d4eSeschrock 		}
327199653d4eSeschrock 		mutex_exit(&spa_namespace_lock);
327299653d4eSeschrock 	}
327399653d4eSeschrock 
327499653d4eSeschrock 	return (error);
327599653d4eSeschrock }
327699653d4eSeschrock 
3277e14bb325SJeff Bonwick static nvlist_t *
3278e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
327999653d4eSeschrock {
3280e14bb325SJeff Bonwick 	for (int i = 0; i < count; i++) {
3281e14bb325SJeff Bonwick 		uint64_t guid;
328299653d4eSeschrock 
3283e14bb325SJeff Bonwick 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
3284e14bb325SJeff Bonwick 		    &guid) == 0);
328599653d4eSeschrock 
3286e14bb325SJeff Bonwick 		if (guid == target_guid)
3287e14bb325SJeff Bonwick 			return (nvpp[i]);
328899653d4eSeschrock 	}
328999653d4eSeschrock 
3290e14bb325SJeff Bonwick 	return (NULL);
3291fa94a07fSbrendan }
3292fa94a07fSbrendan 
3293e14bb325SJeff Bonwick static void
3294e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
3295e14bb325SJeff Bonwick 	nvlist_t *dev_to_remove)
3296fa94a07fSbrendan {
3297e14bb325SJeff Bonwick 	nvlist_t **newdev = NULL;
3298fa94a07fSbrendan 
3299e14bb325SJeff Bonwick 	if (count > 1)
3300e14bb325SJeff Bonwick 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
3301fa94a07fSbrendan 
3302e14bb325SJeff Bonwick 	for (int i = 0, j = 0; i < count; i++) {
3303e14bb325SJeff Bonwick 		if (dev[i] == dev_to_remove)
3304e14bb325SJeff Bonwick 			continue;
3305e14bb325SJeff Bonwick 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
3306fa94a07fSbrendan 	}
3307fa94a07fSbrendan 
3308e14bb325SJeff Bonwick 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
3309e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
3310fa94a07fSbrendan 
3311e14bb325SJeff Bonwick 	for (int i = 0; i < count - 1; i++)
3312e14bb325SJeff Bonwick 		nvlist_free(newdev[i]);
3313fa94a07fSbrendan 
3314e14bb325SJeff Bonwick 	if (count > 1)
3315e14bb325SJeff Bonwick 		kmem_free(newdev, (count - 1) * sizeof (void *));
3316fa94a07fSbrendan }
3317fa94a07fSbrendan 
3318fa94a07fSbrendan /*
3319fa94a07fSbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
3320fa94a07fSbrendan  * spares and level 2 ARC devices.
3321fa94a07fSbrendan  */
3322fa94a07fSbrendan int
3323fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
3324fa94a07fSbrendan {
3325fa94a07fSbrendan 	vdev_t *vd;
3326e14bb325SJeff Bonwick 	nvlist_t **spares, **l2cache, *nv;
3327fa94a07fSbrendan 	uint_t nspares, nl2cache;
33288ad4d6ddSJeff Bonwick 	uint64_t txg = 0;
3329fa94a07fSbrendan 	int error = 0;
33308ad4d6ddSJeff Bonwick 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
3331fa94a07fSbrendan 
33328ad4d6ddSJeff Bonwick 	if (!locked)
33338ad4d6ddSJeff Bonwick 		txg = spa_vdev_enter(spa);
3334fa94a07fSbrendan 
3335c5904d13Seschrock 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3336fa94a07fSbrendan 
3337fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
3338fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3339e14bb325SJeff Bonwick 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
3340e14bb325SJeff Bonwick 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
3341e14bb325SJeff Bonwick 		/*
3342e14bb325SJeff Bonwick 		 * Only remove the hot spare if it's not currently in use
3343e14bb325SJeff Bonwick 		 * in this pool.
3344e14bb325SJeff Bonwick 		 */
3345e14bb325SJeff Bonwick 		if (vd == NULL || unspare) {
3346e14bb325SJeff Bonwick 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
3347e14bb325SJeff Bonwick 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
3348e14bb325SJeff Bonwick 			spa_load_spares(spa);
3349e14bb325SJeff Bonwick 			spa->spa_spares.sav_sync = B_TRUE;
3350e14bb325SJeff Bonwick 		} else {
3351e14bb325SJeff Bonwick 			error = EBUSY;
3352e14bb325SJeff Bonwick 		}
3353e14bb325SJeff Bonwick 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
3354fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3355e14bb325SJeff Bonwick 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
3356e14bb325SJeff Bonwick 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
3357e14bb325SJeff Bonwick 		/*
3358e14bb325SJeff Bonwick 		 * Cache devices can always be removed.
3359e14bb325SJeff Bonwick 		 */
3360e14bb325SJeff Bonwick 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
3361e14bb325SJeff Bonwick 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
3362fa94a07fSbrendan 		spa_load_l2cache(spa);
3363fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3364e14bb325SJeff Bonwick 	} else if (vd != NULL) {
3365e14bb325SJeff Bonwick 		/*
3366e14bb325SJeff Bonwick 		 * Normal vdevs cannot be removed (yet).
3367e14bb325SJeff Bonwick 		 */
3368e14bb325SJeff Bonwick 		error = ENOTSUP;
3369e14bb325SJeff Bonwick 	} else {
3370e14bb325SJeff Bonwick 		/*
3371e14bb325SJeff Bonwick 		 * There is no vdev of any kind with the specified guid.
3372e14bb325SJeff Bonwick 		 */
3373e14bb325SJeff Bonwick 		error = ENOENT;
3374fa94a07fSbrendan 	}
337599653d4eSeschrock 
33768ad4d6ddSJeff Bonwick 	if (!locked)
33778ad4d6ddSJeff Bonwick 		return (spa_vdev_exit(spa, NULL, txg, error));
33788ad4d6ddSJeff Bonwick 
33798ad4d6ddSJeff Bonwick 	return (error);
3380fa9e4066Sahrens }
3381fa9e4066Sahrens 
3382fa9e4066Sahrens /*
33833d7072f8Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
33843d7072f8Seschrock  * current spared, so we can detach it.
3385fa9e4066Sahrens  */
3386ea8dc4b6Seschrock static vdev_t *
33873d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3388fa9e4066Sahrens {
3389ea8dc4b6Seschrock 	vdev_t *newvd, *oldvd;
3390fa9e4066Sahrens 	int c;
3391fa9e4066Sahrens 
3392ea8dc4b6Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
33933d7072f8Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
3394ea8dc4b6Seschrock 		if (oldvd != NULL)
3395ea8dc4b6Seschrock 			return (oldvd);
3396ea8dc4b6Seschrock 	}
3397fa9e4066Sahrens 
33983d7072f8Seschrock 	/*
33993d7072f8Seschrock 	 * Check for a completed replacement.
34003d7072f8Seschrock 	 */
3401fa9e4066Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
3402ea8dc4b6Seschrock 		oldvd = vd->vdev_child[0];
3403ea8dc4b6Seschrock 		newvd = vd->vdev_child[1];
3404ea8dc4b6Seschrock 
34058ad4d6ddSJeff Bonwick 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
34068ad4d6ddSJeff Bonwick 		    !vdev_dtl_required(oldvd))
3407ea8dc4b6Seschrock 			return (oldvd);
3408fa9e4066Sahrens 	}
3409ea8dc4b6Seschrock 
34103d7072f8Seschrock 	/*
34113d7072f8Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
34123d7072f8Seschrock 	 */
34133d7072f8Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
34143d7072f8Seschrock 		newvd = vd->vdev_child[0];
34153d7072f8Seschrock 		oldvd = vd->vdev_child[1];
34163d7072f8Seschrock 
34173d7072f8Seschrock 		if (newvd->vdev_unspare &&
34188ad4d6ddSJeff Bonwick 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
34198ad4d6ddSJeff Bonwick 		    !vdev_dtl_required(oldvd)) {
34203d7072f8Seschrock 			newvd->vdev_unspare = 0;
34213d7072f8Seschrock 			return (oldvd);
34223d7072f8Seschrock 		}
34233d7072f8Seschrock 	}
34243d7072f8Seschrock 
3425ea8dc4b6Seschrock 	return (NULL);
3426fa9e4066Sahrens }
3427fa9e4066Sahrens 
3428ea8dc4b6Seschrock static void
34293d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa)
3430fa9e4066Sahrens {
34318ad4d6ddSJeff Bonwick 	vdev_t *vd, *pvd, *ppvd;
34328ad4d6ddSJeff Bonwick 	uint64_t guid, sguid, pguid, ppguid;
3433ea8dc4b6Seschrock 
34348ad4d6ddSJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3435ea8dc4b6Seschrock 
34363d7072f8Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
34378ad4d6ddSJeff Bonwick 		pvd = vd->vdev_parent;
34388ad4d6ddSJeff Bonwick 		ppvd = pvd->vdev_parent;
3439ea8dc4b6Seschrock 		guid = vd->vdev_guid;
34408ad4d6ddSJeff Bonwick 		pguid = pvd->vdev_guid;
34418ad4d6ddSJeff Bonwick 		ppguid = ppvd->vdev_guid;
34428ad4d6ddSJeff Bonwick 		sguid = 0;
344399653d4eSeschrock 		/*
344499653d4eSeschrock 		 * If we have just finished replacing a hot spared device, then
344599653d4eSeschrock 		 * we need to detach the parent's first child (the original hot
344699653d4eSeschrock 		 * spare) as well.
344799653d4eSeschrock 		 */
34488ad4d6ddSJeff Bonwick 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
344999653d4eSeschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
34508ad4d6ddSJeff Bonwick 			ASSERT(ppvd->vdev_children == 2);
34518ad4d6ddSJeff Bonwick 			sguid = ppvd->vdev_child[1]->vdev_guid;
345299653d4eSeschrock 		}
34538ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, FTAG);
34548ad4d6ddSJeff Bonwick 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
3455ea8dc4b6Seschrock 			return;
34568ad4d6ddSJeff Bonwick 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
345799653d4eSeschrock 			return;
34588ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3459fa9e4066Sahrens 	}
3460fa9e4066Sahrens 
34618ad4d6ddSJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
3462fa9e4066Sahrens }
3463fa9e4066Sahrens 
3464c67d9675Seschrock /*
3465c67d9675Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
3466c67d9675Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
3467c67d9675Seschrock  */
3468c67d9675Seschrock int
3469c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
3470c67d9675Seschrock {
3471c5904d13Seschrock 	vdev_t *vd;
3472c67d9675Seschrock 	uint64_t txg;
3473c67d9675Seschrock 
3474c67d9675Seschrock 	txg = spa_vdev_enter(spa);
3475c67d9675Seschrock 
3476c5904d13Seschrock 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
347799653d4eSeschrock 		/*
3478c5904d13Seschrock 		 * Determine if this is a reference to a hot spare device.  If
3479c5904d13Seschrock 		 * it is, update the path manually as there is no associated
3480c5904d13Seschrock 		 * vdev_t that can be synced to disk.
348199653d4eSeschrock 		 */
3482c5904d13Seschrock 		nvlist_t **spares;
3483c5904d13Seschrock 		uint_t i, nspares;
3484fa94a07fSbrendan 
3485fa94a07fSbrendan 		if (spa->spa_spares.sav_config != NULL) {
3486fa94a07fSbrendan 			VERIFY(nvlist_lookup_nvlist_array(
3487fa94a07fSbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
3488fa94a07fSbrendan 			    &spares, &nspares) == 0);
348999653d4eSeschrock 			for (i = 0; i < nspares; i++) {
349099653d4eSeschrock 				uint64_t theguid;
349199653d4eSeschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
349299653d4eSeschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
3493fa94a07fSbrendan 				if (theguid == guid) {
3494fa94a07fSbrendan 					VERIFY(nvlist_add_string(spares[i],
3495fa94a07fSbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
3496fa94a07fSbrendan 					spa_load_spares(spa);
3497fa94a07fSbrendan 					spa->spa_spares.sav_sync = B_TRUE;
3498fa94a07fSbrendan 					return (spa_vdev_exit(spa, NULL, txg,
3499fa94a07fSbrendan 					    0));
3500fa94a07fSbrendan 				}
350199653d4eSeschrock 			}
3502fa94a07fSbrendan 		}
350399653d4eSeschrock 
3504fa94a07fSbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
350599653d4eSeschrock 	}
3506c67d9675Seschrock 
35070e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
35080e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
35090e34b6a7Sbonwick 
3510c67d9675Seschrock 	spa_strfree(vd->vdev_path);
3511c67d9675Seschrock 	vd->vdev_path = spa_strdup(newpath);
3512c67d9675Seschrock 
3513c67d9675Seschrock 	vdev_config_dirty(vd->vdev_top);
3514c67d9675Seschrock 
3515c67d9675Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
3516c67d9675Seschrock }
3517c67d9675Seschrock 
3518fa9e4066Sahrens /*
3519fa9e4066Sahrens  * ==========================================================================
3520fa9e4066Sahrens  * SPA Scrubbing
3521fa9e4066Sahrens  * ==========================================================================
3522fa9e4066Sahrens  */
3523fa9e4066Sahrens 
3524ea8dc4b6Seschrock int
3525088f3894Sahrens spa_scrub(spa_t *spa, pool_scrub_type_t type)
3526fa9e4066Sahrens {
3527e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
3528bb8b5132Sek 
3529fa9e4066Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3530fa9e4066Sahrens 		return (ENOTSUP);
3531fa9e4066Sahrens 
3532fa9e4066Sahrens 	/*
3533088f3894Sahrens 	 * If a resilver was requested, but there is no DTL on a
3534088f3894Sahrens 	 * writeable leaf device, we have nothing to do.
3535fa9e4066Sahrens 	 */
3536088f3894Sahrens 	if (type == POOL_SCRUB_RESILVER &&
3537088f3894Sahrens 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
3538088f3894Sahrens 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3539ea8dc4b6Seschrock 		return (0);
3540ea8dc4b6Seschrock 	}
3541fa9e4066Sahrens 
3542088f3894Sahrens 	if (type == POOL_SCRUB_EVERYTHING &&
3543088f3894Sahrens 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
3544088f3894Sahrens 	    spa->spa_dsl_pool->dp_scrub_isresilver)
3545088f3894Sahrens 		return (EBUSY);
3546fa9e4066Sahrens 
3547088f3894Sahrens 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
3548088f3894Sahrens 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
3549088f3894Sahrens 	} else if (type == POOL_SCRUB_NONE) {
3550088f3894Sahrens 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
3551ea8dc4b6Seschrock 	} else {
3552088f3894Sahrens 		return (EINVAL);
3553fa9e4066Sahrens 	}
3554fa9e4066Sahrens }
3555fa9e4066Sahrens 
3556ea8dc4b6Seschrock /*
3557ea8dc4b6Seschrock  * ==========================================================================
3558ea8dc4b6Seschrock  * SPA async task processing
3559ea8dc4b6Seschrock  * ==========================================================================
3560ea8dc4b6Seschrock  */
3561ea8dc4b6Seschrock 
3562ea8dc4b6Seschrock static void
35633d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3564fa9e4066Sahrens {
356549cf58c0SBrendan Gregg - Sun Microsystems 	if (vd->vdev_remove_wanted) {
356649cf58c0SBrendan Gregg - Sun Microsystems 		vd->vdev_remove_wanted = 0;
356749cf58c0SBrendan Gregg - Sun Microsystems 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
3568e14bb325SJeff Bonwick 		vdev_clear(spa, vd);
3569e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
3570ea8dc4b6Seschrock 	}
357149cf58c0SBrendan Gregg - Sun Microsystems 
3572e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
357349cf58c0SBrendan Gregg - Sun Microsystems 		spa_async_remove(spa, vd->vdev_child[c]);
3574ea8dc4b6Seschrock }
3575fa9e4066Sahrens 
3576e14bb325SJeff Bonwick static void
3577e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd)
3578e14bb325SJeff Bonwick {
3579e14bb325SJeff Bonwick 	if (vd->vdev_probe_wanted) {
3580e14bb325SJeff Bonwick 		vd->vdev_probe_wanted = 0;
3581e14bb325SJeff Bonwick 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
3582e14bb325SJeff Bonwick 	}
3583e14bb325SJeff Bonwick 
3584e14bb325SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
3585e14bb325SJeff Bonwick 		spa_async_probe(spa, vd->vdev_child[c]);
3586e14bb325SJeff Bonwick }
3587e14bb325SJeff Bonwick 
3588ea8dc4b6Seschrock static void
3589ea8dc4b6Seschrock spa_async_thread(spa_t *spa)
3590ea8dc4b6Seschrock {
3591e14bb325SJeff Bonwick 	int tasks;
3592ea8dc4b6Seschrock 
3593ea8dc4b6Seschrock 	ASSERT(spa->spa_sync_on);
3594ea8dc4b6Seschrock 
3595ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3596ea8dc4b6Seschrock 	tasks = spa->spa_async_tasks;
3597ea8dc4b6Seschrock 	spa->spa_async_tasks = 0;
3598ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3599ea8dc4b6Seschrock 
36000373e76bSbonwick 	/*
36010373e76bSbonwick 	 * See if the config needs to be updated.
36020373e76bSbonwick 	 */
36030373e76bSbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
36040373e76bSbonwick 		mutex_enter(&spa_namespace_lock);
36050373e76bSbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36060373e76bSbonwick 		mutex_exit(&spa_namespace_lock);
36070373e76bSbonwick 	}
36080373e76bSbonwick 
3609ea8dc4b6Seschrock 	/*
36103d7072f8Seschrock 	 * See if any devices need to be marked REMOVED.
3611ea8dc4b6Seschrock 	 */
3612e14bb325SJeff Bonwick 	if (tasks & SPA_ASYNC_REMOVE) {
3613e14bb325SJeff Bonwick 		spa_vdev_state_enter(spa);
36143d7072f8Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
3615e14bb325SJeff Bonwick 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
361649cf58c0SBrendan Gregg - Sun Microsystems 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
3617e14bb325SJeff Bonwick 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
361849cf58c0SBrendan Gregg - Sun Microsystems 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
3619e14bb325SJeff Bonwick 		(void) spa_vdev_state_exit(spa, NULL, 0);
3620e14bb325SJeff Bonwick 	}
3621e14bb325SJeff Bonwick 
3622e14bb325SJeff Bonwick 	/*
3623e14bb325SJeff Bonwick 	 * See if any devices need to be probed.
3624e14bb325SJeff Bonwick 	 */
3625e14bb325SJeff Bonwick 	if (tasks & SPA_ASYNC_PROBE) {
3626e14bb325SJeff Bonwick 		spa_vdev_state_enter(spa);
3627e14bb325SJeff Bonwick 		spa_async_probe(spa, spa->spa_root_vdev);
3628e14bb325SJeff Bonwick 		(void) spa_vdev_state_exit(spa, NULL, 0);
36293d7072f8Seschrock 	}
3630ea8dc4b6Seschrock 
3631ea8dc4b6Seschrock 	/*
3632ea8dc4b6Seschrock 	 * If any devices are done replacing, detach them.
3633ea8dc4b6Seschrock 	 */
36343d7072f8Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
36353d7072f8Seschrock 		spa_vdev_resilver_done(spa);
3636fa9e4066Sahrens 
3637ea8dc4b6Seschrock 	/*
3638ea8dc4b6Seschrock 	 * Kick off a resilver.
3639ea8dc4b6Seschrock 	 */
3640088f3894Sahrens 	if (tasks & SPA_ASYNC_RESILVER)
3641088f3894Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
3642ea8dc4b6Seschrock 
3643ea8dc4b6Seschrock 	/*
3644ea8dc4b6Seschrock 	 * Let the world know that we're done.
3645ea8dc4b6Seschrock 	 */
3646ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3647ea8dc4b6Seschrock 	spa->spa_async_thread = NULL;
3648ea8dc4b6Seschrock 	cv_broadcast(&spa->spa_async_cv);
3649ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3650ea8dc4b6Seschrock 	thread_exit();
3651ea8dc4b6Seschrock }
3652ea8dc4b6Seschrock 
3653ea8dc4b6Seschrock void
3654ea8dc4b6Seschrock spa_async_suspend(spa_t *spa)
3655ea8dc4b6Seschrock {
3656ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3657ea8dc4b6Seschrock 	spa->spa_async_suspended++;
3658ea8dc4b6Seschrock 	while (spa->spa_async_thread != NULL)
3659ea8dc4b6Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
3660ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3661ea8dc4b6Seschrock }
3662ea8dc4b6Seschrock 
3663ea8dc4b6Seschrock void
3664ea8dc4b6Seschrock spa_async_resume(spa_t *spa)
3665ea8dc4b6Seschrock {
3666ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3667ea8dc4b6Seschrock 	ASSERT(spa->spa_async_suspended != 0);
3668ea8dc4b6Seschrock 	spa->spa_async_suspended--;
3669ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3670ea8dc4b6Seschrock }
3671ea8dc4b6Seschrock 
3672ea8dc4b6Seschrock static void
3673ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa)
3674ea8dc4b6Seschrock {
3675ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3676ea8dc4b6Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
36770373e76bSbonwick 	    spa->spa_async_thread == NULL &&
36780373e76bSbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
3679ea8dc4b6Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
3680ea8dc4b6Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
3681ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3682ea8dc4b6Seschrock }
3683ea8dc4b6Seschrock 
3684ea8dc4b6Seschrock void
3685ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task)
3686ea8dc4b6Seschrock {
3687ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3688ea8dc4b6Seschrock 	spa->spa_async_tasks |= task;
3689ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3690fa9e4066Sahrens }
3691fa9e4066Sahrens 
3692fa9e4066Sahrens /*
3693fa9e4066Sahrens  * ==========================================================================
3694fa9e4066Sahrens  * SPA syncing routines
3695fa9e4066Sahrens  * ==========================================================================
3696fa9e4066Sahrens  */
3697fa9e4066Sahrens 
3698fa9e4066Sahrens static void
3699fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3700fa9e4066Sahrens {
3701fa9e4066Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3702fa9e4066Sahrens 	dmu_tx_t *tx;
3703fa9e4066Sahrens 	blkptr_t blk;
3704fa9e4066Sahrens 	uint64_t itor = 0;
3705fa9e4066Sahrens 	zio_t *zio;
3706fa9e4066Sahrens 	int error;
3707fa9e4066Sahrens 	uint8_t c = 1;
3708fa9e4066Sahrens 
3709e14bb325SJeff Bonwick 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
3710fa9e4066Sahrens 
3711e14bb325SJeff Bonwick 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
3712e14bb325SJeff Bonwick 		ASSERT(blk.blk_birth < txg);
3713e14bb325SJeff Bonwick 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
3714e14bb325SJeff Bonwick 		    ZIO_FLAG_MUSTSUCCEED));
3715e14bb325SJeff Bonwick 	}
3716fa9e4066Sahrens 
3717fa9e4066Sahrens 	error = zio_wait(zio);
3718fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
3719fa9e4066Sahrens 
3720fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3721fa9e4066Sahrens 	bplist_vacate(bpl, tx);
3722fa9e4066Sahrens 
3723fa9e4066Sahrens 	/*
3724fa9e4066Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3725fa9e4066Sahrens 	 * (Usually only the first block is needed.)
3726fa9e4066Sahrens 	 */
3727fa9e4066Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3728fa9e4066Sahrens 	dmu_tx_commit(tx);
3729fa9e4066Sahrens }
3730fa9e4066Sahrens 
3731fa9e4066Sahrens static void
373299653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
3733fa9e4066Sahrens {
3734fa9e4066Sahrens 	char *packed = NULL;
3735f7991ba4STim Haley 	size_t bufsize;
3736fa9e4066Sahrens 	size_t nvsize = 0;
3737fa9e4066Sahrens 	dmu_buf_t *db;
3738fa9e4066Sahrens 
373999653d4eSeschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
3740fa9e4066Sahrens 
3741f7991ba4STim Haley 	/*
3742f7991ba4STim Haley 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
3743f7991ba4STim Haley 	 * information.  This avoids the dbuf_will_dirty() path and
3744f7991ba4STim Haley 	 * saves us a pre-read to get data we don't actually care about.
3745f7991ba4STim Haley 	 */
3746f7991ba4STim Haley 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
3747f7991ba4STim Haley 	packed = kmem_alloc(bufsize, KM_SLEEP);
3748fa9e4066Sahrens 
374999653d4eSeschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
3750ea8dc4b6Seschrock 	    KM_SLEEP) == 0);
3751f7991ba4STim Haley 	bzero(packed + nvsize, bufsize - nvsize);
3752fa9e4066Sahrens 
3753f7991ba4STim Haley 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
3754fa9e4066Sahrens 
3755f7991ba4STim Haley 	kmem_free(packed, bufsize);
3756fa9e4066Sahrens 
375799653d4eSeschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
3758fa9e4066Sahrens 	dmu_buf_will_dirty(db, tx);
3759fa9e4066Sahrens 	*(uint64_t *)db->db_data = nvsize;
3760ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
3761fa9e4066Sahrens }
3762fa9e4066Sahrens 
376399653d4eSeschrock static void
3764fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
3765fa94a07fSbrendan     const char *config, const char *entry)
376699653d4eSeschrock {
376799653d4eSeschrock 	nvlist_t *nvroot;
3768fa94a07fSbrendan 	nvlist_t **list;
376999653d4eSeschrock 	int i;
377099653d4eSeschrock 
3771fa94a07fSbrendan 	if (!sav->sav_sync)
377299653d4eSeschrock 		return;
377399653d4eSeschrock 
377499653d4eSeschrock 	/*
3775fa94a07fSbrendan 	 * Update the MOS nvlist describing the list of available devices.
3776fa94a07fSbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
37773d7072f8Seschrock 	 * valid and the vdevs are labeled appropriately.
377899653d4eSeschrock 	 */
3779fa94a07fSbrendan 	if (sav->sav_object == 0) {
3780fa94a07fSbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
3781fa94a07fSbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
3782fa94a07fSbrendan 		    sizeof (uint64_t), tx);
378399653d4eSeschrock 		VERIFY(zap_update(spa->spa_meta_objset,
3784fa94a07fSbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
3785fa94a07fSbrendan 		    &sav->sav_object, tx) == 0);
378699653d4eSeschrock 	}
378799653d4eSeschrock 
378899653d4eSeschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3789fa94a07fSbrendan 	if (sav->sav_count == 0) {
3790fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
379199653d4eSeschrock 	} else {
3792fa94a07fSbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
3793fa94a07fSbrendan 		for (i = 0; i < sav->sav_count; i++)
3794fa94a07fSbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
3795fa94a07fSbrendan 			    B_FALSE, B_FALSE, B_TRUE);
3796fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
3797fa94a07fSbrendan 		    sav->sav_count) == 0);
3798fa94a07fSbrendan 		for (i = 0; i < sav->sav_count; i++)
3799fa94a07fSbrendan 			nvlist_free(list[i]);
3800fa94a07fSbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
380199653d4eSeschrock 	}
380299653d4eSeschrock 
3803fa94a07fSbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
380406eeb2adSek 	nvlist_free(nvroot);
380599653d4eSeschrock 
3806fa94a07fSbrendan 	sav->sav_sync = B_FALSE;
380799653d4eSeschrock }
380899653d4eSeschrock 
380999653d4eSeschrock static void
381099653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
381199653d4eSeschrock {
381299653d4eSeschrock 	nvlist_t *config;
381399653d4eSeschrock 
3814e14bb325SJeff Bonwick 	if (list_is_empty(&spa->spa_config_dirty_list))
381599653d4eSeschrock 		return;
381699653d4eSeschrock 
3817e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3818e14bb325SJeff Bonwick 
3819e14bb325SJeff Bonwick 	config = spa_config_generate(spa, spa->spa_root_vdev,
3820e14bb325SJeff Bonwick 	    dmu_tx_get_txg(tx), B_FALSE);
3821e14bb325SJeff Bonwick 
3822e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
382399653d4eSeschrock 
382499653d4eSeschrock 	if (spa->spa_config_syncing)
382599653d4eSeschrock 		nvlist_free(spa->spa_config_syncing);
382699653d4eSeschrock 	spa->spa_config_syncing = config;
382799653d4eSeschrock 
382899653d4eSeschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
382999653d4eSeschrock }
383099653d4eSeschrock 
3831990b4856Slling /*
3832990b4856Slling  * Set zpool properties.
3833990b4856Slling  */
3834b1b8ab34Slling static void
3835ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
3836b1b8ab34Slling {
3837b1b8ab34Slling 	spa_t *spa = arg1;
3838b1b8ab34Slling 	objset_t *mos = spa->spa_meta_objset;
3839990b4856Slling 	nvlist_t *nvp = arg2;
3840990b4856Slling 	nvpair_t *elem;
38413d7072f8Seschrock 	uint64_t intval;
3842c5904d13Seschrock 	char *strval;
3843990b4856Slling 	zpool_prop_t prop;
3844990b4856Slling 	const char *propname;
3845990b4856Slling 	zprop_type_t proptype;
3846b1b8ab34Slling 
3847e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);
3848e14bb325SJeff Bonwick 
3849990b4856Slling 	elem = NULL;
3850990b4856Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
3851990b4856Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
3852990b4856Slling 		case ZPOOL_PROP_VERSION:
3853990b4856Slling 			/*
3854990b4856Slling 			 * Only set version for non-zpool-creation cases
3855990b4856Slling 			 * (set/import). spa_create() needs special care
3856990b4856Slling 			 * for version setting.
3857990b4856Slling 			 */
3858990b4856Slling 			if (tx->tx_txg != TXG_INITIAL) {
3859990b4856Slling 				VERIFY(nvpair_value_uint64(elem,
3860990b4856Slling 				    &intval) == 0);
3861990b4856Slling 				ASSERT(intval <= SPA_VERSION);
3862990b4856Slling 				ASSERT(intval >= spa_version(spa));
3863990b4856Slling 				spa->spa_uberblock.ub_version = intval;
3864990b4856Slling 				vdev_config_dirty(spa->spa_root_vdev);
3865990b4856Slling 			}
3866ecd6cf80Smarks 			break;
3867990b4856Slling 
3868990b4856Slling 		case ZPOOL_PROP_ALTROOT:
3869990b4856Slling 			/*
3870990b4856Slling 			 * 'altroot' is a non-persistent property. It should
3871990b4856Slling 			 * have been set temporarily at creation or import time.
3872990b4856Slling 			 */
3873990b4856Slling 			ASSERT(spa->spa_root != NULL);
3874b1b8ab34Slling 			break;
38753d7072f8Seschrock 
38762f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
3877990b4856Slling 			/*
3878*379c004dSEric Schrock 			 * 'cachefile' is also a non-persisitent property.
3879990b4856Slling 			 */
38803d7072f8Seschrock 			break;
3881990b4856Slling 		default:
3882990b4856Slling 			/*
3883990b4856Slling 			 * Set pool property values in the poolprops mos object.
3884990b4856Slling 			 */
3885990b4856Slling 			if (spa->spa_pool_props_object == 0) {
3886990b4856Slling 				objset_t *mos = spa->spa_meta_objset;
3887990b4856Slling 
3888990b4856Slling 				VERIFY((spa->spa_pool_props_object =
3889990b4856Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
3890990b4856Slling 				    DMU_OT_NONE, 0, tx)) > 0);
3891990b4856Slling 
3892990b4856Slling 				VERIFY(zap_update(mos,
3893990b4856Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
3894990b4856Slling 				    8, 1, &spa->spa_pool_props_object, tx)
3895990b4856Slling 				    == 0);
3896990b4856Slling 			}
3897990b4856Slling 
3898990b4856Slling 			/* normalize the property name */
3899990b4856Slling 			propname = zpool_prop_to_name(prop);
3900990b4856Slling 			proptype = zpool_prop_get_type(prop);
3901990b4856Slling 
3902990b4856Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
3903990b4856Slling 				ASSERT(proptype == PROP_TYPE_STRING);
3904990b4856Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
3905990b4856Slling 				VERIFY(zap_update(mos,
3906990b4856Slling 				    spa->spa_pool_props_object, propname,
3907990b4856Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
3908990b4856Slling 
3909990b4856Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
3910990b4856Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3911990b4856Slling 
3912990b4856Slling 				if (proptype == PROP_TYPE_INDEX) {
3913990b4856Slling 					const char *unused;
3914990b4856Slling 					VERIFY(zpool_prop_index_to_string(
3915990b4856Slling 					    prop, intval, &unused) == 0);
3916990b4856Slling 				}
3917990b4856Slling 				VERIFY(zap_update(mos,
3918990b4856Slling 				    spa->spa_pool_props_object, propname,
3919990b4856Slling 				    8, 1, &intval, tx) == 0);
3920990b4856Slling 			} else {
3921990b4856Slling 				ASSERT(0); /* not allowed */
3922990b4856Slling 			}
3923990b4856Slling 
39240a4e9518Sgw 			switch (prop) {
39250a4e9518Sgw 			case ZPOOL_PROP_DELEGATION:
3926990b4856Slling 				spa->spa_delegation = intval;
39270a4e9518Sgw 				break;
39280a4e9518Sgw 			case ZPOOL_PROP_BOOTFS:
3929990b4856Slling 				spa->spa_bootfs = intval;
39300a4e9518Sgw 				break;
39310a4e9518Sgw 			case ZPOOL_PROP_FAILUREMODE:
39320a4e9518Sgw 				spa->spa_failmode = intval;
39330a4e9518Sgw 				break;
39340a4e9518Sgw 			default:
39350a4e9518Sgw 				break;
39360a4e9518Sgw 			}
3937990b4856Slling 		}
3938990b4856Slling 
3939990b4856Slling 		/* log internal history if this is not a zpool create */
3940990b4856Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
3941990b4856Slling 		    tx->tx_txg != TXG_INITIAL) {
3942990b4856Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
3943990b4856Slling 			    spa, tx, cr, "%s %lld %s",
3944e14bb325SJeff Bonwick 			    nvpair_name(elem), intval, spa_name(spa));
3945b1b8ab34Slling 		}
3946b1b8ab34Slling 	}
3947e14bb325SJeff Bonwick 
3948e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_props_lock);
3949b1b8ab34Slling }
3950b1b8ab34Slling 
3951fa9e4066Sahrens /*
3952fa9e4066Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3953fa9e4066Sahrens  * part of the process, so we iterate until it converges.
3954fa9e4066Sahrens  */
3955fa9e4066Sahrens void
3956fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg)
3957fa9e4066Sahrens {
3958fa9e4066Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3959fa9e4066Sahrens 	objset_t *mos = spa->spa_meta_objset;
3960fa9e4066Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
39610373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3962fa9e4066Sahrens 	vdev_t *vd;
3963fa9e4066Sahrens 	dmu_tx_t *tx;
3964fa9e4066Sahrens 	int dirty_vdevs;
3965e14bb325SJeff Bonwick 	int error;
3966fa9e4066Sahrens 
3967fa9e4066Sahrens 	/*
3968fa9e4066Sahrens 	 * Lock out configuration changes.
3969fa9e4066Sahrens 	 */
3970e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3971fa9e4066Sahrens 
3972fa9e4066Sahrens 	spa->spa_syncing_txg = txg;
3973fa9e4066Sahrens 	spa->spa_sync_pass = 0;
3974fa9e4066Sahrens 
3975e14bb325SJeff Bonwick 	/*
3976e14bb325SJeff Bonwick 	 * If there are any pending vdev state changes, convert them
3977e14bb325SJeff Bonwick 	 * into config changes that go out with this transaction group.
3978e14bb325SJeff Bonwick 	 */
3979e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
39808ad4d6ddSJeff Bonwick 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
39818ad4d6ddSJeff Bonwick 		/*
39828ad4d6ddSJeff Bonwick 		 * We need the write lock here because, for aux vdevs,
39838ad4d6ddSJeff Bonwick 		 * calling vdev_config_dirty() modifies sav_config.
39848ad4d6ddSJeff Bonwick 		 * This is ugly and will become unnecessary when we
39858ad4d6ddSJeff Bonwick 		 * eliminate the aux vdev wart by integrating all vdevs
39868ad4d6ddSJeff Bonwick 		 * into the root vdev tree.
39878ad4d6ddSJeff Bonwick 		 */
39888ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
39898ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
39908ad4d6ddSJeff Bonwick 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
39918ad4d6ddSJeff Bonwick 			vdev_state_clean(vd);
39928ad4d6ddSJeff Bonwick 			vdev_config_dirty(vd);
39938ad4d6ddSJeff Bonwick 		}
39948ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
39958ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
3996e14bb325SJeff Bonwick 	}
3997e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
3998e14bb325SJeff Bonwick 
3999ea8dc4b6Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4000fa9e4066Sahrens 
400199653d4eSeschrock 	tx = dmu_tx_create_assigned(dp, txg);
400299653d4eSeschrock 
400399653d4eSeschrock 	/*
4004e7437265Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
400599653d4eSeschrock 	 * set spa_deflate if we have no raid-z vdevs.
400699653d4eSeschrock 	 */
4007e7437265Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
4008e7437265Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
400999653d4eSeschrock 		int i;
401099653d4eSeschrock 
401199653d4eSeschrock 		for (i = 0; i < rvd->vdev_children; i++) {
401299653d4eSeschrock 			vd = rvd->vdev_child[i];
401399653d4eSeschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
401499653d4eSeschrock 				break;
401599653d4eSeschrock 		}
401699653d4eSeschrock 		if (i == rvd->vdev_children) {
401799653d4eSeschrock 			spa->spa_deflate = TRUE;
401899653d4eSeschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
401999653d4eSeschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
402099653d4eSeschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
402199653d4eSeschrock 		}
402299653d4eSeschrock 	}
402399653d4eSeschrock 
4024088f3894Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
4025088f3894Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
4026088f3894Sahrens 		dsl_pool_create_origin(dp, tx);
4027088f3894Sahrens 
4028088f3894Sahrens 		/* Keeping the origin open increases spa_minref */
4029088f3894Sahrens 		spa->spa_minref += 3;
4030088f3894Sahrens 	}
4031088f3894Sahrens 
4032088f3894Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
4033088f3894Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
4034088f3894Sahrens 		dsl_pool_upgrade_clones(dp, tx);
4035088f3894Sahrens 	}
4036088f3894Sahrens 
4037fa9e4066Sahrens 	/*
4038fa9e4066Sahrens 	 * If anything has changed in this txg, push the deferred frees
4039fa9e4066Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4040fa9e4066Sahrens 	 * don't generate work on an otherwise idle system.
4041fa9e4066Sahrens 	 */
4042fa9e4066Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
40431615a317Sek 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
40441615a317Sek 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4045fa9e4066Sahrens 		spa_sync_deferred_frees(spa, txg);
4046fa9e4066Sahrens 
4047fa9e4066Sahrens 	/*
4048fa9e4066Sahrens 	 * Iterate to convergence.
4049fa9e4066Sahrens 	 */
4050fa9e4066Sahrens 	do {
4051fa9e4066Sahrens 		spa->spa_sync_pass++;
4052fa9e4066Sahrens 
4053fa9e4066Sahrens 		spa_sync_config_object(spa, tx);
4054fa94a07fSbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
4055fa94a07fSbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
4056fa94a07fSbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
4057fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
4058ea8dc4b6Seschrock 		spa_errlog_sync(spa, txg);
4059fa9e4066Sahrens 		dsl_pool_sync(dp, txg);
4060fa9e4066Sahrens 
4061fa9e4066Sahrens 		dirty_vdevs = 0;
4062fa9e4066Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4063fa9e4066Sahrens 			vdev_sync(vd, txg);
4064fa9e4066Sahrens 			dirty_vdevs++;
4065fa9e4066Sahrens 		}
4066fa9e4066Sahrens 
4067fa9e4066Sahrens 		bplist_sync(bpl, tx);
4068fa9e4066Sahrens 	} while (dirty_vdevs);
4069fa9e4066Sahrens 
4070fa9e4066Sahrens 	bplist_close(bpl);
4071fa9e4066Sahrens 
4072fa9e4066Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4073fa9e4066Sahrens 
4074fa9e4066Sahrens 	/*
4075fa9e4066Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4076fa9e4066Sahrens 	 * to commit the transaction group.
40770373e76bSbonwick 	 *
407817f17c2dSbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
407917f17c2dSbonwick 	 * random top-level vdevs that are known to be visible in the
4080e14bb325SJeff Bonwick 	 * config cache (see spa_vdev_add() for a complete description).
4081e14bb325SJeff Bonwick 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
40820373e76bSbonwick 	 */
4083e14bb325SJeff Bonwick 	for (;;) {
4084e14bb325SJeff Bonwick 		/*
4085e14bb325SJeff Bonwick 		 * We hold SCL_STATE to prevent vdev open/close/etc.
4086e14bb325SJeff Bonwick 		 * while we're attempting to write the vdev labels.
4087e14bb325SJeff Bonwick 		 */
4088e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4089e14bb325SJeff Bonwick 
4090e14bb325SJeff Bonwick 		if (list_is_empty(&spa->spa_config_dirty_list)) {
4091e14bb325SJeff Bonwick 			vdev_t *svd[SPA_DVAS_PER_BP];
4092e14bb325SJeff Bonwick 			int svdcount = 0;
4093e14bb325SJeff Bonwick 			int children = rvd->vdev_children;
4094e14bb325SJeff Bonwick 			int c0 = spa_get_random(children);
4095e14bb325SJeff Bonwick 			int c;
4096e14bb325SJeff Bonwick 
4097e14bb325SJeff Bonwick 			for (c = 0; c < children; c++) {
4098e14bb325SJeff Bonwick 				vd = rvd->vdev_child[(c0 + c) % children];
4099e14bb325SJeff Bonwick 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
4100e14bb325SJeff Bonwick 					continue;
4101e14bb325SJeff Bonwick 				svd[svdcount++] = vd;
4102e14bb325SJeff Bonwick 				if (svdcount == SPA_DVAS_PER_BP)
4103e14bb325SJeff Bonwick 					break;
4104e14bb325SJeff Bonwick 			}
4105e14bb325SJeff Bonwick 			error = vdev_config_sync(svd, svdcount, txg);
4106e14bb325SJeff Bonwick 		} else {
4107e14bb325SJeff Bonwick 			error = vdev_config_sync(rvd->vdev_child,
4108e14bb325SJeff Bonwick 			    rvd->vdev_children, txg);
41090373e76bSbonwick 		}
4110e14bb325SJeff Bonwick 
4111e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, FTAG);
4112e14bb325SJeff Bonwick 
4113e14bb325SJeff Bonwick 		if (error == 0)
4114e14bb325SJeff Bonwick 			break;
4115e14bb325SJeff Bonwick 		zio_suspend(spa, NULL);
4116e14bb325SJeff Bonwick 		zio_resume_wait(spa);
41170373e76bSbonwick 	}
411899653d4eSeschrock 	dmu_tx_commit(tx);
411999653d4eSeschrock 
41200373e76bSbonwick 	/*
41210373e76bSbonwick 	 * Clear the dirty config list.
4122fa9e4066Sahrens 	 */
4123e14bb325SJeff Bonwick 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
41240373e76bSbonwick 		vdev_config_clean(vd);
41250373e76bSbonwick 
41260373e76bSbonwick 	/*
41270373e76bSbonwick 	 * Now that the new config has synced transactionally,
41280373e76bSbonwick 	 * let it become visible to the config cache.
41290373e76bSbonwick 	 */
41300373e76bSbonwick 	if (spa->spa_config_syncing != NULL) {
41310373e76bSbonwick 		spa_config_set(spa, spa->spa_config_syncing);
41320373e76bSbonwick 		spa->spa_config_txg = txg;
41330373e76bSbonwick 		spa->spa_config_syncing = NULL;
41340373e76bSbonwick 	}
4135fa9e4066Sahrens 
4136fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4137fa9e4066Sahrens 
4138fa9e4066Sahrens 	/*
4139fa9e4066Sahrens 	 * Clean up the ZIL records for the synced txg.
4140fa9e4066Sahrens 	 */
4141fa9e4066Sahrens 	dsl_pool_zil_clean(dp);
4142fa9e4066Sahrens 
4143fa9e4066Sahrens 	/*
4144fa9e4066Sahrens 	 * Update usable space statistics.
4145fa9e4066Sahrens 	 */
4146fa9e4066Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4147fa9e4066Sahrens 		vdev_sync_done(vd, txg);
4148fa9e4066Sahrens 
4149fa9e4066Sahrens 	/*
4150fa9e4066Sahrens 	 * It had better be the case that we didn't dirty anything
415199653d4eSeschrock 	 * since vdev_config_sync().
4152fa9e4066Sahrens 	 */
4153fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4154fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4155fa9e4066Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4156fa9e4066Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4157fa9e4066Sahrens 
4158e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
4159ea8dc4b6Seschrock 
4160ea8dc4b6Seschrock 	/*
4161ea8dc4b6Seschrock 	 * If any async tasks have been requested, kick them off.
4162ea8dc4b6Seschrock 	 */
4163ea8dc4b6Seschrock 	spa_async_dispatch(spa);
4164fa9e4066Sahrens }
4165fa9e4066Sahrens 
4166fa9e4066Sahrens /*
4167fa9e4066Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4168fa9e4066Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4169fa9e4066Sahrens  * sync.
4170fa9e4066Sahrens  */
4171fa9e4066Sahrens void
4172fa9e4066Sahrens spa_sync_allpools(void)
4173fa9e4066Sahrens {
4174fa9e4066Sahrens 	spa_t *spa = NULL;
4175fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
4176fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
4177e14bb325SJeff Bonwick 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4178fa9e4066Sahrens 			continue;
4179fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
4180fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
4181fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4182fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
4183fa9e4066Sahrens 		spa_close(spa, FTAG);
4184fa9e4066Sahrens 	}
4185fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
4186fa9e4066Sahrens }
4187fa9e4066Sahrens 
4188fa9e4066Sahrens /*
4189fa9e4066Sahrens  * ==========================================================================
4190fa9e4066Sahrens  * Miscellaneous routines
4191fa9e4066Sahrens  * ==========================================================================
4192fa9e4066Sahrens  */
4193fa9e4066Sahrens 
4194fa9e4066Sahrens /*
4195fa9e4066Sahrens  * Remove all pools in the system.
4196fa9e4066Sahrens  */
4197fa9e4066Sahrens void
4198fa9e4066Sahrens spa_evict_all(void)
4199fa9e4066Sahrens {
4200fa9e4066Sahrens 	spa_t *spa;
4201fa9e4066Sahrens 
4202fa9e4066Sahrens 	/*
4203fa9e4066Sahrens 	 * Remove all cached state.  All pools should be closed now,
4204fa9e4066Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4205fa9e4066Sahrens 	 */
4206fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
4207fa9e4066Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4208fa9e4066Sahrens 		/*
4209ea8dc4b6Seschrock 		 * Stop async tasks.  The async thread may need to detach
4210ea8dc4b6Seschrock 		 * a device that's been replaced, which requires grabbing
4211ea8dc4b6Seschrock 		 * spa_namespace_lock, so we must drop it here.
4212fa9e4066Sahrens 		 */
4213fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
4214fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
4215ea8dc4b6Seschrock 		spa_async_suspend(spa);
4216fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
4217fa9e4066Sahrens 		spa_close(spa, FTAG);
4218fa9e4066Sahrens 
4219fa9e4066Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4220fa9e4066Sahrens 			spa_unload(spa);
4221fa9e4066Sahrens 			spa_deactivate(spa);
4222fa9e4066Sahrens 		}
4223fa9e4066Sahrens 		spa_remove(spa);
4224fa9e4066Sahrens 	}
4225fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
4226fa9e4066Sahrens }
4227ea8dc4b6Seschrock 
4228ea8dc4b6Seschrock vdev_t *
4229c5904d13Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
4230ea8dc4b6Seschrock {
4231c5904d13Seschrock 	vdev_t *vd;
4232c5904d13Seschrock 	int i;
4233c5904d13Seschrock 
4234c5904d13Seschrock 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
4235c5904d13Seschrock 		return (vd);
4236c5904d13Seschrock 
4237c5904d13Seschrock 	if (l2cache) {
4238c5904d13Seschrock 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
4239c5904d13Seschrock 			vd = spa->spa_l2cache.sav_vdevs[i];
4240c5904d13Seschrock 			if (vd->vdev_guid == guid)
4241c5904d13Seschrock 				return (vd);
4242c5904d13Seschrock 		}
4243c5904d13Seschrock 	}
4244c5904d13Seschrock 
4245c5904d13Seschrock 	return (NULL);
4246ea8dc4b6Seschrock }
4247eaca9bbdSeschrock 
4248eaca9bbdSeschrock void
4249990b4856Slling spa_upgrade(spa_t *spa, uint64_t version)
4250eaca9bbdSeschrock {
4251e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4252eaca9bbdSeschrock 
4253eaca9bbdSeschrock 	/*
4254eaca9bbdSeschrock 	 * This should only be called for a non-faulted pool, and since a
4255eaca9bbdSeschrock 	 * future version would result in an unopenable pool, this shouldn't be
4256eaca9bbdSeschrock 	 * possible.
4257eaca9bbdSeschrock 	 */
4258e7437265Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
4259990b4856Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
4260eaca9bbdSeschrock 
4261990b4856Slling 	spa->spa_uberblock.ub_version = version;
4262eaca9bbdSeschrock 	vdev_config_dirty(spa->spa_root_vdev);
4263eaca9bbdSeschrock 
4264e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
426599653d4eSeschrock 
426699653d4eSeschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
426799653d4eSeschrock }
426899653d4eSeschrock 
426999653d4eSeschrock boolean_t
427099653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid)
427199653d4eSeschrock {
427299653d4eSeschrock 	int i;
427339c23413Seschrock 	uint64_t spareguid;
4274fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
427599653d4eSeschrock 
4276fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
4277fa94a07fSbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
427899653d4eSeschrock 			return (B_TRUE);
427999653d4eSeschrock 
4280fa94a07fSbrendan 	for (i = 0; i < sav->sav_npending; i++) {
4281fa94a07fSbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
4282fa94a07fSbrendan 		    &spareguid) == 0 && spareguid == guid)
428339c23413Seschrock 			return (B_TRUE);
428439c23413Seschrock 	}
428539c23413Seschrock 
428699653d4eSeschrock 	return (B_FALSE);
4287eaca9bbdSeschrock }
4288b1b8ab34Slling 
428989a89ebfSlling /*
429089a89ebfSlling  * Check if a pool has an active shared spare device.
429189a89ebfSlling  * Note: reference count of an active spare is 2, as a spare and as a replace
429289a89ebfSlling  */
429389a89ebfSlling static boolean_t
429489a89ebfSlling spa_has_active_shared_spare(spa_t *spa)
429589a89ebfSlling {
429689a89ebfSlling 	int i, refcnt;
429789a89ebfSlling 	uint64_t pool;
429889a89ebfSlling 	spa_aux_vdev_t *sav = &spa->spa_spares;
429989a89ebfSlling 
430089a89ebfSlling 	for (i = 0; i < sav->sav_count; i++) {
430189a89ebfSlling 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
430289a89ebfSlling 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
430389a89ebfSlling 		    refcnt > 2)
430489a89ebfSlling 			return (B_TRUE);
430589a89ebfSlling 	}
430689a89ebfSlling 
430789a89ebfSlling 	return (B_FALSE);
430889a89ebfSlling }
430989a89ebfSlling 
43103d7072f8Seschrock /*
43113d7072f8Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
43123d7072f8Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
43133d7072f8Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
43143d7072f8Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
43153d7072f8Seschrock  * or zdb as real changes.
43163d7072f8Seschrock  */
43173d7072f8Seschrock void
43183d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
43193d7072f8Seschrock {
43203d7072f8Seschrock #ifdef _KERNEL
43213d7072f8Seschrock 	sysevent_t		*ev;
43223d7072f8Seschrock 	sysevent_attr_list_t	*attr = NULL;
43233d7072f8Seschrock 	sysevent_value_t	value;
43243d7072f8Seschrock 	sysevent_id_t		eid;
43253d7072f8Seschrock 
43263d7072f8Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
43273d7072f8Seschrock 	    SE_SLEEP);
43283d7072f8Seschrock 
43293d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
43303d7072f8Seschrock 	value.value.sv_string = spa_name(spa);
43313d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
43323d7072f8Seschrock 		goto done;
43333d7072f8Seschrock 
43343d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
43353d7072f8Seschrock 	value.value.sv_uint64 = spa_guid(spa);
43363d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
43373d7072f8Seschrock 		goto done;
43383d7072f8Seschrock 
43393d7072f8Seschrock 	if (vd) {
43403d7072f8Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
43413d7072f8Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
43423d7072f8Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
43433d7072f8Seschrock 		    SE_SLEEP) != 0)
43443d7072f8Seschrock 			goto done;
43453d7072f8Seschrock 
43463d7072f8Seschrock 		if (vd->vdev_path) {
43473d7072f8Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
43483d7072f8Seschrock 			value.value.sv_string = vd->vdev_path;
43493d7072f8Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
43503d7072f8Seschrock 			    &value, SE_SLEEP) != 0)
43513d7072f8Seschrock 				goto done;
43523d7072f8Seschrock 		}
43533d7072f8Seschrock 	}
43543d7072f8Seschrock 
4355b01c3b58Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
4356b01c3b58Seschrock 		goto done;
4357b01c3b58Seschrock 	attr = NULL;
4358b01c3b58Seschrock 
43593d7072f8Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
43603d7072f8Seschrock 
43613d7072f8Seschrock done:
43623d7072f8Seschrock 	if (attr)
43633d7072f8Seschrock 		sysevent_free_attr(attr);
43643d7072f8Seschrock 	sysevent_free(ev);
43653d7072f8Seschrock #endif
43663d7072f8Seschrock }
4367