xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa.c (revision b01c3b58f7eb7fb570f606f96f130fb9b2018b49)
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*b01c3b58Seschrock  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
31fa9e4066Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
32fa9e4066Sahrens  * pool.
33fa9e4066Sahrens  */
34fa9e4066Sahrens 
35fa9e4066Sahrens #include <sys/zfs_context.h>
36ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
37fa9e4066Sahrens #include <sys/spa_impl.h>
38fa9e4066Sahrens #include <sys/zio.h>
39fa9e4066Sahrens #include <sys/zio_checksum.h>
40fa9e4066Sahrens #include <sys/zio_compress.h>
41fa9e4066Sahrens #include <sys/dmu.h>
42fa9e4066Sahrens #include <sys/dmu_tx.h>
43fa9e4066Sahrens #include <sys/zap.h>
44fa9e4066Sahrens #include <sys/zil.h>
45fa9e4066Sahrens #include <sys/vdev_impl.h>
46fa9e4066Sahrens #include <sys/metaslab.h>
47fa9e4066Sahrens #include <sys/uberblock_impl.h>
48fa9e4066Sahrens #include <sys/txg.h>
49fa9e4066Sahrens #include <sys/avl.h>
50fa9e4066Sahrens #include <sys/dmu_traverse.h>
51b1b8ab34Slling #include <sys/dmu_objset.h>
52fa9e4066Sahrens #include <sys/unique.h>
53fa9e4066Sahrens #include <sys/dsl_pool.h>
54b1b8ab34Slling #include <sys/dsl_dataset.h>
55fa9e4066Sahrens #include <sys/dsl_dir.h>
56fa9e4066Sahrens #include <sys/dsl_prop.h>
57b1b8ab34Slling #include <sys/dsl_synctask.h>
58fa9e4066Sahrens #include <sys/fs/zfs.h>
59fa94a07fSbrendan #include <sys/arc.h>
60fa9e4066Sahrens #include <sys/callb.h>
6195173954Sek #include <sys/systeminfo.h>
6295173954Sek #include <sys/sunddi.h>
63fa9e4066Sahrens 
64990b4856Slling #include "zfs_prop.h"
65990b4856Slling 
66416e0cd8Sek int zio_taskq_threads = 8;
67416e0cd8Sek 
68990b4856Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
69990b4856Slling 
70990b4856Slling /*
71990b4856Slling  * ==========================================================================
72990b4856Slling  * SPA properties routines
73990b4856Slling  * ==========================================================================
74990b4856Slling  */
75990b4856Slling 
76990b4856Slling /*
77990b4856Slling  * Add a (source=src, propname=propval) list to an nvlist.
78990b4856Slling  */
79990b4856Slling static int
80990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
81990b4856Slling     uint64_t intval, zprop_source_t src)
82990b4856Slling {
83990b4856Slling 	const char *propname = zpool_prop_to_name(prop);
84990b4856Slling 	nvlist_t *propval;
85990b4856Slling 	int err = 0;
86990b4856Slling 
87990b4856Slling 	if (err = nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP))
88990b4856Slling 		return (err);
89990b4856Slling 
90990b4856Slling 	if (err = nvlist_add_uint64(propval, ZPROP_SOURCE, src))
91990b4856Slling 		goto out;
92990b4856Slling 
93990b4856Slling 	if (strval != NULL) {
94990b4856Slling 		if (err = nvlist_add_string(propval, ZPROP_VALUE, strval))
95990b4856Slling 			goto out;
96990b4856Slling 	} else {
97990b4856Slling 		if (err = nvlist_add_uint64(propval, ZPROP_VALUE, intval))
98990b4856Slling 			goto out;
99990b4856Slling 	}
100990b4856Slling 
101990b4856Slling 	err = nvlist_add_nvlist(nvl, propname, propval);
102990b4856Slling out:
103990b4856Slling 	nvlist_free(propval);
104990b4856Slling 	return (err);
105990b4856Slling }
106990b4856Slling 
107990b4856Slling /*
108990b4856Slling  * Get property values from the spa configuration.
109990b4856Slling  */
110990b4856Slling static int
111990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
112990b4856Slling {
113990b4856Slling 	uint64_t size = spa_get_space(spa);
114990b4856Slling 	uint64_t used = spa_get_alloc(spa);
115990b4856Slling 	uint64_t cap, version;
116990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
117990b4856Slling 	int err;
1182f8aaab3Seschrock 	char *cachefile;
1192f8aaab3Seschrock 	size_t len;
120990b4856Slling 
121990b4856Slling 	/*
122990b4856Slling 	 * readonly properties
123990b4856Slling 	 */
124990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name,
125990b4856Slling 	    0, src))
126990b4856Slling 		return (err);
127990b4856Slling 
128990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src))
129990b4856Slling 		return (err);
130990b4856Slling 
131990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src))
132990b4856Slling 		return (err);
133990b4856Slling 
134990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
135990b4856Slling 	    size - used, src))
136990b4856Slling 		return (err);
137990b4856Slling 
138990b4856Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
139990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src))
140990b4856Slling 		return (err);
141990b4856Slling 
142990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL,
143990b4856Slling 	    spa_guid(spa), src))
144990b4856Slling 		return (err);
145990b4856Slling 
146990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
147990b4856Slling 	    spa->spa_root_vdev->vdev_state, src))
148990b4856Slling 		return (err);
149990b4856Slling 
150990b4856Slling 	/*
151990b4856Slling 	 * settable properties that are not stored in the pool property object.
152990b4856Slling 	 */
153990b4856Slling 	version = spa_version(spa);
154990b4856Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
155990b4856Slling 		src = ZPROP_SRC_DEFAULT;
156990b4856Slling 	else
157990b4856Slling 		src = ZPROP_SRC_LOCAL;
158990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
159990b4856Slling 	    version, src))
160990b4856Slling 		return (err);
161990b4856Slling 
162990b4856Slling 	if (spa->spa_root != NULL) {
163990b4856Slling 		src = ZPROP_SRC_LOCAL;
164990b4856Slling 		if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT,
165990b4856Slling 		    spa->spa_root, 0, src))
166990b4856Slling 			return (err);
167990b4856Slling 	}
168990b4856Slling 
1692f8aaab3Seschrock 	if (spa->spa_config_dir != NULL) {
1702f8aaab3Seschrock 		if (strcmp(spa->spa_config_dir, "none") == 0) {
1712f8aaab3Seschrock 			err = spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1722f8aaab3Seschrock 			    spa->spa_config_dir, 0, ZPROP_SRC_LOCAL);
1732f8aaab3Seschrock 		} else {
1742f8aaab3Seschrock 			len = strlen(spa->spa_config_dir) +
1752f8aaab3Seschrock 			    strlen(spa->spa_config_file) + 2;
1762f8aaab3Seschrock 			cachefile = kmem_alloc(len, KM_SLEEP);
1772f8aaab3Seschrock 			(void) snprintf(cachefile, len, "%s/%s",
1782f8aaab3Seschrock 			    spa->spa_config_dir, spa->spa_config_file);
1792f8aaab3Seschrock 			err = spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
1802f8aaab3Seschrock 			    cachefile, 0, ZPROP_SRC_LOCAL);
1812f8aaab3Seschrock 			kmem_free(cachefile, len);
1822f8aaab3Seschrock 		}
1832f8aaab3Seschrock 
1842f8aaab3Seschrock 		if (err)
1852f8aaab3Seschrock 			return (err);
1862f8aaab3Seschrock 	}
187990b4856Slling 
188990b4856Slling 	return (0);
189990b4856Slling }
190990b4856Slling 
191990b4856Slling /*
192990b4856Slling  * Get zpool property values.
193990b4856Slling  */
194990b4856Slling int
195990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
196990b4856Slling {
197990b4856Slling 	zap_cursor_t zc;
198990b4856Slling 	zap_attribute_t za;
199990b4856Slling 	objset_t *mos = spa->spa_meta_objset;
200990b4856Slling 	int err;
201990b4856Slling 
202990b4856Slling 	if (err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP))
203990b4856Slling 		return (err);
204990b4856Slling 
205990b4856Slling 	/*
206990b4856Slling 	 * Get properties from the spa config.
207990b4856Slling 	 */
208990b4856Slling 	if (err = spa_prop_get_config(spa, nvp))
209990b4856Slling 		goto out;
210990b4856Slling 
211990b4856Slling 	mutex_enter(&spa->spa_props_lock);
212990b4856Slling 	/* If no pool property object, no more prop to get. */
213990b4856Slling 	if (spa->spa_pool_props_object == 0) {
214990b4856Slling 		mutex_exit(&spa->spa_props_lock);
215990b4856Slling 		return (0);
216990b4856Slling 	}
217990b4856Slling 
218990b4856Slling 	/*
219990b4856Slling 	 * Get properties from the MOS pool property object.
220990b4856Slling 	 */
221990b4856Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
222990b4856Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
223990b4856Slling 	    zap_cursor_advance(&zc)) {
224990b4856Slling 		uint64_t intval = 0;
225990b4856Slling 		char *strval = NULL;
226990b4856Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
227990b4856Slling 		zpool_prop_t prop;
228990b4856Slling 
229990b4856Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
230990b4856Slling 			continue;
231990b4856Slling 
232990b4856Slling 		switch (za.za_integer_length) {
233990b4856Slling 		case 8:
234990b4856Slling 			/* integer property */
235990b4856Slling 			if (za.za_first_integer !=
236990b4856Slling 			    zpool_prop_default_numeric(prop))
237990b4856Slling 				src = ZPROP_SRC_LOCAL;
238990b4856Slling 
239990b4856Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
240990b4856Slling 				dsl_pool_t *dp;
241990b4856Slling 				dsl_dataset_t *ds = NULL;
242990b4856Slling 
243990b4856Slling 				dp = spa_get_dsl(spa);
244990b4856Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
245990b4856Slling 				if (err = dsl_dataset_open_obj(dp,
246990b4856Slling 				    za.za_first_integer, NULL, DS_MODE_NONE,
247990b4856Slling 				    FTAG, &ds)) {
248990b4856Slling 					rw_exit(&dp->dp_config_rwlock);
249990b4856Slling 					break;
250990b4856Slling 				}
251990b4856Slling 
252990b4856Slling 				strval = kmem_alloc(
253990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
254990b4856Slling 				    KM_SLEEP);
255990b4856Slling 				dsl_dataset_name(ds, strval);
256990b4856Slling 				dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
257990b4856Slling 				rw_exit(&dp->dp_config_rwlock);
258990b4856Slling 			} else {
259990b4856Slling 				strval = NULL;
260990b4856Slling 				intval = za.za_first_integer;
261990b4856Slling 			}
262990b4856Slling 
263990b4856Slling 			err = spa_prop_add_list(*nvp, prop, strval,
264990b4856Slling 			    intval, src);
265990b4856Slling 
266990b4856Slling 			if (strval != NULL)
267990b4856Slling 				kmem_free(strval,
268990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
269990b4856Slling 
270990b4856Slling 			break;
271990b4856Slling 
272990b4856Slling 		case 1:
273990b4856Slling 			/* string property */
274990b4856Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
275990b4856Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
276990b4856Slling 			    za.za_name, 1, za.za_num_integers, strval);
277990b4856Slling 			if (err) {
278990b4856Slling 				kmem_free(strval, za.za_num_integers);
279990b4856Slling 				break;
280990b4856Slling 			}
281990b4856Slling 			err = spa_prop_add_list(*nvp, prop, strval, 0, src);
282990b4856Slling 			kmem_free(strval, za.za_num_integers);
283990b4856Slling 			break;
284990b4856Slling 
285990b4856Slling 		default:
286990b4856Slling 			break;
287990b4856Slling 		}
288990b4856Slling 	}
289990b4856Slling 	zap_cursor_fini(&zc);
290990b4856Slling 	mutex_exit(&spa->spa_props_lock);
291990b4856Slling out:
292990b4856Slling 	if (err && err != ENOENT) {
293990b4856Slling 		nvlist_free(*nvp);
294990b4856Slling 		return (err);
295990b4856Slling 	}
296990b4856Slling 
297990b4856Slling 	return (0);
298990b4856Slling }
299990b4856Slling 
300990b4856Slling /*
301990b4856Slling  * Validate the given pool properties nvlist and modify the list
302990b4856Slling  * for the property values to be set.
303990b4856Slling  */
304990b4856Slling static int
305990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
306990b4856Slling {
307990b4856Slling 	nvpair_t *elem;
308990b4856Slling 	int error = 0, reset_bootfs = 0;
309990b4856Slling 	uint64_t objnum;
310990b4856Slling 
311990b4856Slling 	elem = NULL;
312990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
313990b4856Slling 		zpool_prop_t prop;
314990b4856Slling 		char *propname, *strval;
315990b4856Slling 		uint64_t intval;
316990b4856Slling 		vdev_t *rvdev;
317990b4856Slling 		char *vdev_type;
318990b4856Slling 		objset_t *os;
3192f8aaab3Seschrock 		char *slash;
320990b4856Slling 
321990b4856Slling 		propname = nvpair_name(elem);
322990b4856Slling 
323990b4856Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
324990b4856Slling 			return (EINVAL);
325990b4856Slling 
326990b4856Slling 		switch (prop) {
327990b4856Slling 		case ZPOOL_PROP_VERSION:
328990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
329990b4856Slling 			if (!error &&
330990b4856Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
331990b4856Slling 				error = EINVAL;
332990b4856Slling 			break;
333990b4856Slling 
334990b4856Slling 		case ZPOOL_PROP_DELEGATION:
335990b4856Slling 		case ZPOOL_PROP_AUTOREPLACE:
336990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
337990b4856Slling 			if (!error && intval > 1)
338990b4856Slling 				error = EINVAL;
339990b4856Slling 			break;
340990b4856Slling 
341990b4856Slling 		case ZPOOL_PROP_BOOTFS:
342990b4856Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
343990b4856Slling 				error = ENOTSUP;
344990b4856Slling 				break;
345990b4856Slling 			}
346990b4856Slling 
347990b4856Slling 			/*
348990b4856Slling 			 * A bootable filesystem can not be on a RAIDZ pool
349990b4856Slling 			 * nor a striped pool with more than 1 device.
350990b4856Slling 			 */
351990b4856Slling 			rvdev = spa->spa_root_vdev;
352990b4856Slling 			vdev_type =
353990b4856Slling 			    rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
354990b4856Slling 			if (rvdev->vdev_children > 1 ||
355990b4856Slling 			    strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
356990b4856Slling 			    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
357990b4856Slling 				error = ENOTSUP;
358990b4856Slling 				break;
359990b4856Slling 			}
360990b4856Slling 
361990b4856Slling 			reset_bootfs = 1;
362990b4856Slling 
363990b4856Slling 			error = nvpair_value_string(elem, &strval);
364990b4856Slling 
365990b4856Slling 			if (!error) {
366990b4856Slling 				if (strval == NULL || strval[0] == '\0') {
367990b4856Slling 					objnum = zpool_prop_default_numeric(
368990b4856Slling 					    ZPOOL_PROP_BOOTFS);
369990b4856Slling 					break;
370990b4856Slling 				}
371990b4856Slling 
372990b4856Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
373990b4856Slling 				    DS_MODE_STANDARD | DS_MODE_READONLY, &os))
374990b4856Slling 					break;
375990b4856Slling 				objnum = dmu_objset_id(os);
376990b4856Slling 				dmu_objset_close(os);
377990b4856Slling 			}
378990b4856Slling 			break;
3790a4e9518Sgw 		case ZPOOL_PROP_FAILUREMODE:
3800a4e9518Sgw 			error = nvpair_value_uint64(elem, &intval);
3810a4e9518Sgw 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3820a4e9518Sgw 			    intval > ZIO_FAILURE_MODE_PANIC))
3830a4e9518Sgw 				error = EINVAL;
3840a4e9518Sgw 
3850a4e9518Sgw 			/*
3860a4e9518Sgw 			 * This is a special case which only occurs when
3870a4e9518Sgw 			 * the pool has completely failed. This allows
3880a4e9518Sgw 			 * the user to change the in-core failmode property
3890a4e9518Sgw 			 * without syncing it out to disk (I/Os might
3900a4e9518Sgw 			 * currently be blocked). We do this by returning
3910a4e9518Sgw 			 * EIO to the caller (spa_prop_set) to trick it
3920a4e9518Sgw 			 * into thinking we encountered a property validation
3930a4e9518Sgw 			 * error.
3940a4e9518Sgw 			 */
3950a4e9518Sgw 			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
3960a4e9518Sgw 				spa->spa_failmode = intval;
3970a4e9518Sgw 				error = EIO;
3980a4e9518Sgw 			}
3990a4e9518Sgw 			break;
4002f8aaab3Seschrock 
4012f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
4022f8aaab3Seschrock 			if ((error = nvpair_value_string(elem, &strval)) != 0)
4032f8aaab3Seschrock 				break;
4042f8aaab3Seschrock 
4052f8aaab3Seschrock 			if (strval[0] == '\0')
4062f8aaab3Seschrock 				break;
4072f8aaab3Seschrock 
4082f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
4092f8aaab3Seschrock 				break;
4102f8aaab3Seschrock 
4112f8aaab3Seschrock 			if (strval[0] != '/') {
4122f8aaab3Seschrock 				error = EINVAL;
4132f8aaab3Seschrock 				break;
4142f8aaab3Seschrock 			}
4152f8aaab3Seschrock 
4162f8aaab3Seschrock 			slash = strrchr(strval, '/');
4172f8aaab3Seschrock 			ASSERT(slash != NULL);
4182f8aaab3Seschrock 
4192f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4202f8aaab3Seschrock 			    strcmp(slash, "/..") == 0)
4212f8aaab3Seschrock 				error = EINVAL;
4222f8aaab3Seschrock 			break;
423990b4856Slling 		}
424990b4856Slling 
425990b4856Slling 		if (error)
426990b4856Slling 			break;
427990b4856Slling 	}
428990b4856Slling 
429990b4856Slling 	if (!error && reset_bootfs) {
430990b4856Slling 		error = nvlist_remove(props,
431990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
432990b4856Slling 
433990b4856Slling 		if (!error) {
434990b4856Slling 			error = nvlist_add_uint64(props,
435990b4856Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
436990b4856Slling 		}
437990b4856Slling 	}
438990b4856Slling 
439990b4856Slling 	return (error);
440990b4856Slling }
441990b4856Slling 
442990b4856Slling int
443990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
444990b4856Slling {
445990b4856Slling 	int error;
446990b4856Slling 
447990b4856Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
448990b4856Slling 		return (error);
449990b4856Slling 
450990b4856Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
451990b4856Slling 	    spa, nvp, 3));
452990b4856Slling }
453990b4856Slling 
454990b4856Slling /*
455990b4856Slling  * If the bootfs property value is dsobj, clear it.
456990b4856Slling  */
457990b4856Slling void
458990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
459990b4856Slling {
460990b4856Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
461990b4856Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
462990b4856Slling 		    spa->spa_pool_props_object,
463990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
464990b4856Slling 		spa->spa_bootfs = 0;
465990b4856Slling 	}
466990b4856Slling }
467990b4856Slling 
468fa9e4066Sahrens /*
469fa9e4066Sahrens  * ==========================================================================
470fa9e4066Sahrens  * SPA state manipulation (open/create/destroy/import/export)
471fa9e4066Sahrens  * ==========================================================================
472fa9e4066Sahrens  */
473fa9e4066Sahrens 
474ea8dc4b6Seschrock static int
475ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b)
476ea8dc4b6Seschrock {
477ea8dc4b6Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
478ea8dc4b6Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
479ea8dc4b6Seschrock 	int ret;
480ea8dc4b6Seschrock 
481ea8dc4b6Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
482ea8dc4b6Seschrock 	    sizeof (zbookmark_t));
483ea8dc4b6Seschrock 
484ea8dc4b6Seschrock 	if (ret < 0)
485ea8dc4b6Seschrock 		return (-1);
486ea8dc4b6Seschrock 	else if (ret > 0)
487ea8dc4b6Seschrock 		return (1);
488ea8dc4b6Seschrock 	else
489ea8dc4b6Seschrock 		return (0);
490ea8dc4b6Seschrock }
491ea8dc4b6Seschrock 
492ea8dc4b6Seschrock /*
493ea8dc4b6Seschrock  * Utility function which retrieves copies of the current logs and
494ea8dc4b6Seschrock  * re-initializes them in the process.
495ea8dc4b6Seschrock  */
496ea8dc4b6Seschrock void
497ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
498ea8dc4b6Seschrock {
499ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
500ea8dc4b6Seschrock 
501ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
502ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
503ea8dc4b6Seschrock 
504ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
505ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
506ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
507ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
508ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
509ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
510ea8dc4b6Seschrock }
511ea8dc4b6Seschrock 
512fa9e4066Sahrens /*
513fa9e4066Sahrens  * Activate an uninitialized pool.
514fa9e4066Sahrens  */
515fa9e4066Sahrens static void
516fa9e4066Sahrens spa_activate(spa_t *spa)
517fa9e4066Sahrens {
518fa9e4066Sahrens 	int t;
519fa9e4066Sahrens 
520fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
521fa9e4066Sahrens 
522fa9e4066Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
523fa9e4066Sahrens 
524fa9e4066Sahrens 	spa->spa_normal_class = metaslab_class_create();
5258654d025Sperrin 	spa->spa_log_class = metaslab_class_create();
526fa9e4066Sahrens 
527fa9e4066Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
528fa9e4066Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
529416e0cd8Sek 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
530fa9e4066Sahrens 		    TASKQ_PREPOPULATE);
531fa9e4066Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
532416e0cd8Sek 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
533fa9e4066Sahrens 		    TASKQ_PREPOPULATE);
534fa9e4066Sahrens 	}
535fa9e4066Sahrens 
536fa9e4066Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
537fa9e4066Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
5380a4e9518Sgw 	list_create(&spa->spa_zio_list, sizeof (zio_t),
5390a4e9518Sgw 	    offsetof(zio_t, zio_link_node));
540fa9e4066Sahrens 
541fa9e4066Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
542fa9e4066Sahrens 	    offsetof(struct vdev, vdev_txg_node));
543ea8dc4b6Seschrock 
544ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
545ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
546ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
547ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
548ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
549ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
550fa9e4066Sahrens }
551fa9e4066Sahrens 
552fa9e4066Sahrens /*
553fa9e4066Sahrens  * Opposite of spa_activate().
554fa9e4066Sahrens  */
555fa9e4066Sahrens static void
556fa9e4066Sahrens spa_deactivate(spa_t *spa)
557fa9e4066Sahrens {
558fa9e4066Sahrens 	int t;
559fa9e4066Sahrens 
560fa9e4066Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
561fa9e4066Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
562fa9e4066Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
563fa9e4066Sahrens 
564fa9e4066Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
565fa9e4066Sahrens 
566fa9e4066Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
567fa9e4066Sahrens 
568fa9e4066Sahrens 	list_destroy(&spa->spa_dirty_list);
5690a4e9518Sgw 	list_destroy(&spa->spa_zio_list);
570fa9e4066Sahrens 
571fa9e4066Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
572fa9e4066Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
573fa9e4066Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
574fa9e4066Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
575fa9e4066Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
576fa9e4066Sahrens 	}
577fa9e4066Sahrens 
578fa9e4066Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
579fa9e4066Sahrens 	spa->spa_normal_class = NULL;
580fa9e4066Sahrens 
5818654d025Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5828654d025Sperrin 	spa->spa_log_class = NULL;
5838654d025Sperrin 
584ea8dc4b6Seschrock 	/*
585ea8dc4b6Seschrock 	 * If this was part of an import or the open otherwise failed, we may
586ea8dc4b6Seschrock 	 * still have errors left in the queues.  Empty them just in case.
587ea8dc4b6Seschrock 	 */
588ea8dc4b6Seschrock 	spa_errlog_drain(spa);
589ea8dc4b6Seschrock 
590ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
591ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_last);
592ea8dc4b6Seschrock 
593fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
594fa9e4066Sahrens }
595fa9e4066Sahrens 
596fa9e4066Sahrens /*
597fa9e4066Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
598fa9e4066Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
599fa9e4066Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
600fa9e4066Sahrens  * All vdev validation is done by the vdev_alloc() routine.
601fa9e4066Sahrens  */
60299653d4eSeschrock static int
60399653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
60499653d4eSeschrock     uint_t id, int atype)
605fa9e4066Sahrens {
606fa9e4066Sahrens 	nvlist_t **child;
607fa9e4066Sahrens 	uint_t c, children;
60899653d4eSeschrock 	int error;
609fa9e4066Sahrens 
61099653d4eSeschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
61199653d4eSeschrock 		return (error);
612fa9e4066Sahrens 
61399653d4eSeschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
61499653d4eSeschrock 		return (0);
615fa9e4066Sahrens 
616fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
617fa9e4066Sahrens 	    &child, &children) != 0) {
61899653d4eSeschrock 		vdev_free(*vdp);
61999653d4eSeschrock 		*vdp = NULL;
62099653d4eSeschrock 		return (EINVAL);
621fa9e4066Sahrens 	}
622fa9e4066Sahrens 
623fa9e4066Sahrens 	for (c = 0; c < children; c++) {
62499653d4eSeschrock 		vdev_t *vd;
62599653d4eSeschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
62699653d4eSeschrock 		    atype)) != 0) {
62799653d4eSeschrock 			vdev_free(*vdp);
62899653d4eSeschrock 			*vdp = NULL;
62999653d4eSeschrock 			return (error);
630fa9e4066Sahrens 		}
631fa9e4066Sahrens 	}
632fa9e4066Sahrens 
63399653d4eSeschrock 	ASSERT(*vdp != NULL);
63499653d4eSeschrock 
63599653d4eSeschrock 	return (0);
636fa9e4066Sahrens }
637fa9e4066Sahrens 
638fa9e4066Sahrens /*
639fa9e4066Sahrens  * Opposite of spa_load().
640fa9e4066Sahrens  */
641fa9e4066Sahrens static void
642fa9e4066Sahrens spa_unload(spa_t *spa)
643fa9e4066Sahrens {
64499653d4eSeschrock 	int i;
64599653d4eSeschrock 
646ea8dc4b6Seschrock 	/*
647ea8dc4b6Seschrock 	 * Stop async tasks.
648ea8dc4b6Seschrock 	 */
649ea8dc4b6Seschrock 	spa_async_suspend(spa);
650ea8dc4b6Seschrock 
651fa9e4066Sahrens 	/*
652fa9e4066Sahrens 	 * Stop syncing.
653fa9e4066Sahrens 	 */
654fa9e4066Sahrens 	if (spa->spa_sync_on) {
655fa9e4066Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
656fa9e4066Sahrens 		spa->spa_sync_on = B_FALSE;
657fa9e4066Sahrens 	}
658fa9e4066Sahrens 
659fa9e4066Sahrens 	/*
660fa9e4066Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
661fa9e4066Sahrens 	 */
662ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
663ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
664fa9e4066Sahrens 
665fa94a07fSbrendan 	/*
666fa94a07fSbrendan 	 * Drop and purge level 2 cache
667fa94a07fSbrendan 	 */
668fa94a07fSbrendan 	spa_l2cache_drop(spa);
669fa94a07fSbrendan 
670fa9e4066Sahrens 	/*
671fa9e4066Sahrens 	 * Close the dsl pool.
672fa9e4066Sahrens 	 */
673fa9e4066Sahrens 	if (spa->spa_dsl_pool) {
674fa9e4066Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
675fa9e4066Sahrens 		spa->spa_dsl_pool = NULL;
676fa9e4066Sahrens 	}
677fa9e4066Sahrens 
678fa9e4066Sahrens 	/*
679fa9e4066Sahrens 	 * Close all vdevs.
680fa9e4066Sahrens 	 */
6810e34b6a7Sbonwick 	if (spa->spa_root_vdev)
682fa9e4066Sahrens 		vdev_free(spa->spa_root_vdev);
6830e34b6a7Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
684ea8dc4b6Seschrock 
685fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
686fa94a07fSbrendan 		vdev_free(spa->spa_spares.sav_vdevs[i]);
687fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs) {
688fa94a07fSbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
689fa94a07fSbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
690fa94a07fSbrendan 		spa->spa_spares.sav_vdevs = NULL;
69199653d4eSeschrock 	}
692fa94a07fSbrendan 	if (spa->spa_spares.sav_config) {
693fa94a07fSbrendan 		nvlist_free(spa->spa_spares.sav_config);
694fa94a07fSbrendan 		spa->spa_spares.sav_config = NULL;
695fa94a07fSbrendan 	}
696fa94a07fSbrendan 
697fa94a07fSbrendan 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
698fa94a07fSbrendan 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
699fa94a07fSbrendan 	if (spa->spa_l2cache.sav_vdevs) {
700fa94a07fSbrendan 		kmem_free(spa->spa_l2cache.sav_vdevs,
701fa94a07fSbrendan 		    spa->spa_l2cache.sav_count * sizeof (void *));
702fa94a07fSbrendan 		spa->spa_l2cache.sav_vdevs = NULL;
703fa94a07fSbrendan 	}
704fa94a07fSbrendan 	if (spa->spa_l2cache.sav_config) {
705fa94a07fSbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
706fa94a07fSbrendan 		spa->spa_l2cache.sav_config = NULL;
70799653d4eSeschrock 	}
70899653d4eSeschrock 
709ea8dc4b6Seschrock 	spa->spa_async_suspended = 0;
710fa9e4066Sahrens }
711fa9e4066Sahrens 
71299653d4eSeschrock /*
71399653d4eSeschrock  * Load (or re-load) the current list of vdevs describing the active spares for
71499653d4eSeschrock  * this pool.  When this is called, we have some form of basic information in
715fa94a07fSbrendan  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
716fa94a07fSbrendan  * then re-generate a more complete list including status information.
71799653d4eSeschrock  */
71899653d4eSeschrock static void
71999653d4eSeschrock spa_load_spares(spa_t *spa)
72099653d4eSeschrock {
72199653d4eSeschrock 	nvlist_t **spares;
72299653d4eSeschrock 	uint_t nspares;
72399653d4eSeschrock 	int i;
72439c23413Seschrock 	vdev_t *vd, *tvd;
72599653d4eSeschrock 
72699653d4eSeschrock 	/*
72799653d4eSeschrock 	 * First, close and free any existing spare vdevs.
72899653d4eSeschrock 	 */
729fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
730fa94a07fSbrendan 		vd = spa->spa_spares.sav_vdevs[i];
73139c23413Seschrock 
73239c23413Seschrock 		/* Undo the call to spa_activate() below */
73339c23413Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL &&
73439c23413Seschrock 		    tvd->vdev_isspare)
73539c23413Seschrock 			spa_spare_remove(tvd);
73639c23413Seschrock 		vdev_close(vd);
73739c23413Seschrock 		vdev_free(vd);
73899653d4eSeschrock 	}
73939c23413Seschrock 
740fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs)
741fa94a07fSbrendan 		kmem_free(spa->spa_spares.sav_vdevs,
742fa94a07fSbrendan 		    spa->spa_spares.sav_count * sizeof (void *));
74399653d4eSeschrock 
744fa94a07fSbrendan 	if (spa->spa_spares.sav_config == NULL)
74599653d4eSeschrock 		nspares = 0;
74699653d4eSeschrock 	else
747fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
74899653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
74999653d4eSeschrock 
750fa94a07fSbrendan 	spa->spa_spares.sav_count = (int)nspares;
751fa94a07fSbrendan 	spa->spa_spares.sav_vdevs = NULL;
75299653d4eSeschrock 
75399653d4eSeschrock 	if (nspares == 0)
75499653d4eSeschrock 		return;
75599653d4eSeschrock 
75699653d4eSeschrock 	/*
75799653d4eSeschrock 	 * Construct the array of vdevs, opening them to get status in the
75839c23413Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
75939c23413Seschrock 	 * structures associated with it: one in the list of spares (used only
76039c23413Seschrock 	 * for basic validation purposes) and one in the active vdev
76139c23413Seschrock 	 * configuration (if it's spared in).  During this phase we open and
76239c23413Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
76339c23413Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
76499653d4eSeschrock 	 */
765fa94a07fSbrendan 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
766fa94a07fSbrendan 	    KM_SLEEP);
767fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
76899653d4eSeschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
76999653d4eSeschrock 		    VDEV_ALLOC_SPARE) == 0);
77099653d4eSeschrock 		ASSERT(vd != NULL);
77199653d4eSeschrock 
772fa94a07fSbrendan 		spa->spa_spares.sav_vdevs[i] = vd;
77399653d4eSeschrock 
77439c23413Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) {
77539c23413Seschrock 			if (!tvd->vdev_isspare)
77639c23413Seschrock 				spa_spare_add(tvd);
77739c23413Seschrock 
77839c23413Seschrock 			/*
77939c23413Seschrock 			 * We only mark the spare active if we were successfully
78039c23413Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
78139c23413Seschrock 			 * with a bad active spare would result in strange
78239c23413Seschrock 			 * behavior, because multiple pool would think the spare
78339c23413Seschrock 			 * is actively in use.
78439c23413Seschrock 			 *
78539c23413Seschrock 			 * There is a vulnerability here to an equally bizarre
78639c23413Seschrock 			 * circumstance, where a dead active spare is later
78739c23413Seschrock 			 * brought back to life (onlined or otherwise).  Given
78839c23413Seschrock 			 * the rarity of this scenario, and the extra complexity
78939c23413Seschrock 			 * it adds, we ignore the possibility.
79039c23413Seschrock 			 */
79139c23413Seschrock 			if (!vdev_is_dead(tvd))
79239c23413Seschrock 				spa_spare_activate(tvd);
79339c23413Seschrock 		}
79439c23413Seschrock 
79599653d4eSeschrock 		if (vdev_open(vd) != 0)
79699653d4eSeschrock 			continue;
79799653d4eSeschrock 
79899653d4eSeschrock 		vd->vdev_top = vd;
799fa94a07fSbrendan 		if (vdev_validate_aux(vd) == 0)
800fa94a07fSbrendan 			spa_spare_add(vd);
80199653d4eSeschrock 	}
80299653d4eSeschrock 
80399653d4eSeschrock 	/*
80499653d4eSeschrock 	 * Recompute the stashed list of spares, with status information
80599653d4eSeschrock 	 * this time.
80699653d4eSeschrock 	 */
807fa94a07fSbrendan 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
80899653d4eSeschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
80999653d4eSeschrock 
810fa94a07fSbrendan 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
811fa94a07fSbrendan 	    KM_SLEEP);
812fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
813fa94a07fSbrendan 		spares[i] = vdev_config_generate(spa,
814fa94a07fSbrendan 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
815fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
816fa94a07fSbrendan 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
817fa94a07fSbrendan 	for (i = 0; i < spa->spa_spares.sav_count; i++)
81899653d4eSeschrock 		nvlist_free(spares[i]);
819fa94a07fSbrendan 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
820fa94a07fSbrendan }
821fa94a07fSbrendan 
822fa94a07fSbrendan /*
823fa94a07fSbrendan  * Load (or re-load) the current list of vdevs describing the active l2cache for
824fa94a07fSbrendan  * this pool.  When this is called, we have some form of basic information in
825fa94a07fSbrendan  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
826fa94a07fSbrendan  * then re-generate a more complete list including status information.
827fa94a07fSbrendan  * Devices which are already active have their details maintained, and are
828fa94a07fSbrendan  * not re-opened.
829fa94a07fSbrendan  */
830fa94a07fSbrendan static void
831fa94a07fSbrendan spa_load_l2cache(spa_t *spa)
832fa94a07fSbrendan {
833fa94a07fSbrendan 	nvlist_t **l2cache;
834fa94a07fSbrendan 	uint_t nl2cache;
835fa94a07fSbrendan 	int i, j, oldnvdevs;
836fa94a07fSbrendan 	uint64_t guid;
837fa94a07fSbrendan 	vdev_t *vd, **oldvdevs, **newvdevs;
838fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
839fa94a07fSbrendan 
840fa94a07fSbrendan 	if (sav->sav_config != NULL) {
841fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
842fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
843fa94a07fSbrendan 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
844fa94a07fSbrendan 	} else {
845fa94a07fSbrendan 		nl2cache = 0;
846fa94a07fSbrendan 	}
847fa94a07fSbrendan 
848fa94a07fSbrendan 	oldvdevs = sav->sav_vdevs;
849fa94a07fSbrendan 	oldnvdevs = sav->sav_count;
850fa94a07fSbrendan 	sav->sav_vdevs = NULL;
851fa94a07fSbrendan 	sav->sav_count = 0;
852fa94a07fSbrendan 
853fa94a07fSbrendan 	/*
854fa94a07fSbrendan 	 * Process new nvlist of vdevs.
855fa94a07fSbrendan 	 */
856fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
857fa94a07fSbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
858fa94a07fSbrendan 		    &guid) == 0);
859fa94a07fSbrendan 
860fa94a07fSbrendan 		newvdevs[i] = NULL;
861fa94a07fSbrendan 		for (j = 0; j < oldnvdevs; j++) {
862fa94a07fSbrendan 			vd = oldvdevs[j];
863fa94a07fSbrendan 			if (vd != NULL && guid == vd->vdev_guid) {
864fa94a07fSbrendan 				/*
865fa94a07fSbrendan 				 * Retain previous vdev for add/remove ops.
866fa94a07fSbrendan 				 */
867fa94a07fSbrendan 				newvdevs[i] = vd;
868fa94a07fSbrendan 				oldvdevs[j] = NULL;
869fa94a07fSbrendan 				break;
870fa94a07fSbrendan 			}
871fa94a07fSbrendan 		}
872fa94a07fSbrendan 
873fa94a07fSbrendan 		if (newvdevs[i] == NULL) {
874fa94a07fSbrendan 			/*
875fa94a07fSbrendan 			 * Create new vdev
876fa94a07fSbrendan 			 */
877fa94a07fSbrendan 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
878fa94a07fSbrendan 			    VDEV_ALLOC_L2CACHE) == 0);
879fa94a07fSbrendan 			ASSERT(vd != NULL);
880fa94a07fSbrendan 			newvdevs[i] = vd;
881fa94a07fSbrendan 
882fa94a07fSbrendan 			/*
883fa94a07fSbrendan 			 * Commit this vdev as an l2cache device,
884fa94a07fSbrendan 			 * even if it fails to open.
885fa94a07fSbrendan 			 */
886fa94a07fSbrendan 			spa_l2cache_add(vd);
887fa94a07fSbrendan 
888fa94a07fSbrendan 			if (vdev_open(vd) != 0)
889fa94a07fSbrendan 				continue;
890fa94a07fSbrendan 
891fa94a07fSbrendan 			vd->vdev_top = vd;
892fa94a07fSbrendan 			(void) vdev_validate_aux(vd);
893fa94a07fSbrendan 
894fa94a07fSbrendan 			if (!vdev_is_dead(vd)) {
895fa94a07fSbrendan 				uint64_t size;
896fa94a07fSbrendan 				size = vdev_get_rsize(vd);
897fa94a07fSbrendan 				ASSERT3U(size, >, 0);
898fa94a07fSbrendan 				if (spa_mode & FWRITE) {
899fa94a07fSbrendan 					l2arc_add_vdev(spa, vd,
900fa94a07fSbrendan 					    VDEV_LABEL_START_SIZE,
901fa94a07fSbrendan 					    size - VDEV_LABEL_START_SIZE);
902fa94a07fSbrendan 				}
903fa94a07fSbrendan 				spa_l2cache_activate(vd);
904fa94a07fSbrendan 			}
905fa94a07fSbrendan 		}
906fa94a07fSbrendan 	}
907fa94a07fSbrendan 
908fa94a07fSbrendan 	/*
909fa94a07fSbrendan 	 * Purge vdevs that were dropped
910fa94a07fSbrendan 	 */
911fa94a07fSbrendan 	for (i = 0; i < oldnvdevs; i++) {
912fa94a07fSbrendan 		uint64_t pool;
913fa94a07fSbrendan 
914fa94a07fSbrendan 		vd = oldvdevs[i];
915fa94a07fSbrendan 		if (vd != NULL) {
916fa94a07fSbrendan 			if (spa_mode & FWRITE &&
917fa94a07fSbrendan 			    spa_l2cache_exists(vd->vdev_guid, &pool) &&
918fa94a07fSbrendan 			    pool != 0ULL) {
919fa94a07fSbrendan 				l2arc_remove_vdev(vd);
920fa94a07fSbrendan 			}
921fa94a07fSbrendan 			(void) vdev_close(vd);
922fa94a07fSbrendan 			spa_l2cache_remove(vd);
923fa94a07fSbrendan 		}
924fa94a07fSbrendan 	}
925fa94a07fSbrendan 
926fa94a07fSbrendan 	if (oldvdevs)
927fa94a07fSbrendan 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
928fa94a07fSbrendan 
929fa94a07fSbrendan 	if (sav->sav_config == NULL)
930fa94a07fSbrendan 		goto out;
931fa94a07fSbrendan 
932fa94a07fSbrendan 	sav->sav_vdevs = newvdevs;
933fa94a07fSbrendan 	sav->sav_count = (int)nl2cache;
934fa94a07fSbrendan 
935fa94a07fSbrendan 	/*
936fa94a07fSbrendan 	 * Recompute the stashed list of l2cache devices, with status
937fa94a07fSbrendan 	 * information this time.
938fa94a07fSbrendan 	 */
939fa94a07fSbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
940fa94a07fSbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
941fa94a07fSbrendan 
942fa94a07fSbrendan 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
943fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
944fa94a07fSbrendan 		l2cache[i] = vdev_config_generate(spa,
945fa94a07fSbrendan 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
946fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
947fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
948fa94a07fSbrendan out:
949fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
950fa94a07fSbrendan 		nvlist_free(l2cache[i]);
951fa94a07fSbrendan 	if (sav->sav_count)
952fa94a07fSbrendan 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
95399653d4eSeschrock }
95499653d4eSeschrock 
95599653d4eSeschrock static int
95699653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
95799653d4eSeschrock {
95899653d4eSeschrock 	dmu_buf_t *db;
95999653d4eSeschrock 	char *packed = NULL;
96099653d4eSeschrock 	size_t nvsize = 0;
96199653d4eSeschrock 	int error;
96299653d4eSeschrock 	*value = NULL;
96399653d4eSeschrock 
96499653d4eSeschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
96599653d4eSeschrock 	nvsize = *(uint64_t *)db->db_data;
96699653d4eSeschrock 	dmu_buf_rele(db, FTAG);
96799653d4eSeschrock 
96899653d4eSeschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
96999653d4eSeschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
97099653d4eSeschrock 	if (error == 0)
97199653d4eSeschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
97299653d4eSeschrock 	kmem_free(packed, nvsize);
97399653d4eSeschrock 
97499653d4eSeschrock 	return (error);
97599653d4eSeschrock }
97699653d4eSeschrock 
9773d7072f8Seschrock /*
9783d7072f8Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
9793d7072f8Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
9803d7072f8Seschrock  */
9813d7072f8Seschrock static void
9823d7072f8Seschrock spa_check_removed(vdev_t *vd)
9833d7072f8Seschrock {
9843d7072f8Seschrock 	int c;
9853d7072f8Seschrock 
9863d7072f8Seschrock 	for (c = 0; c < vd->vdev_children; c++)
9873d7072f8Seschrock 		spa_check_removed(vd->vdev_child[c]);
9883d7072f8Seschrock 
9893d7072f8Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
9903d7072f8Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
9913d7072f8Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
9923d7072f8Seschrock 	}
9933d7072f8Seschrock }
9943d7072f8Seschrock 
995fa9e4066Sahrens /*
996fa9e4066Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
997ea8dc4b6Seschrock  * source of configuration information.
998fa9e4066Sahrens  */
999fa9e4066Sahrens static int
1000ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
1001fa9e4066Sahrens {
1002fa9e4066Sahrens 	int error = 0;
1003fa9e4066Sahrens 	nvlist_t *nvroot = NULL;
1004fa9e4066Sahrens 	vdev_t *rvd;
1005fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
10060373e76bSbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
1007fa9e4066Sahrens 	uint64_t pool_guid;
100899653d4eSeschrock 	uint64_t version;
1009fa9e4066Sahrens 	zio_t *zio;
10103d7072f8Seschrock 	uint64_t autoreplace = 0;
1011fa9e4066Sahrens 
1012ea8dc4b6Seschrock 	spa->spa_load_state = state;
10130373e76bSbonwick 
1014fa9e4066Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1015a9926bf0Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
1016ea8dc4b6Seschrock 		error = EINVAL;
1017ea8dc4b6Seschrock 		goto out;
1018ea8dc4b6Seschrock 	}
1019fa9e4066Sahrens 
102099653d4eSeschrock 	/*
102199653d4eSeschrock 	 * Versioning wasn't explicitly added to the label until later, so if
102299653d4eSeschrock 	 * it's not present treat it as the initial version.
102399653d4eSeschrock 	 */
102499653d4eSeschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
1025e7437265Sahrens 		version = SPA_VERSION_INITIAL;
102699653d4eSeschrock 
1027a9926bf0Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1028a9926bf0Sbonwick 	    &spa->spa_config_txg);
1029a9926bf0Sbonwick 
10300373e76bSbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1031ea8dc4b6Seschrock 	    spa_guid_exists(pool_guid, 0)) {
1032ea8dc4b6Seschrock 		error = EEXIST;
1033ea8dc4b6Seschrock 		goto out;
1034ea8dc4b6Seschrock 	}
1035fa9e4066Sahrens 
1036b5989ec7Seschrock 	spa->spa_load_guid = pool_guid;
1037b5989ec7Seschrock 
1038fa9e4066Sahrens 	/*
103999653d4eSeschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
104099653d4eSeschrock 	 * value that will be returned by spa_version() since parsing the
104199653d4eSeschrock 	 * configuration requires knowing the version number.
1042fa9e4066Sahrens 	 */
1043ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
104499653d4eSeschrock 	spa->spa_ubsync.ub_version = version;
104599653d4eSeschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
1046ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
1047fa9e4066Sahrens 
104899653d4eSeschrock 	if (error != 0)
1049ea8dc4b6Seschrock 		goto out;
1050fa9e4066Sahrens 
10510e34b6a7Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
1052fa9e4066Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 	/*
1055fa9e4066Sahrens 	 * Try to open all vdevs, loading each label in the process.
1056fa9e4066Sahrens 	 */
10570bf246f5Smc 	error = vdev_open(rvd);
10580bf246f5Smc 	if (error != 0)
1059ea8dc4b6Seschrock 		goto out;
1060fa9e4066Sahrens 
1061560e6e96Seschrock 	/*
1062560e6e96Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
1063560e6e96Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
1064560e6e96Seschrock 	 * flag.
1065560e6e96Seschrock 	 */
1066560e6e96Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
1067560e6e96Seschrock 	error = vdev_validate(rvd);
1068560e6e96Seschrock 	spa_config_exit(spa, FTAG);
1069560e6e96Seschrock 
10700bf246f5Smc 	if (error != 0)
1071560e6e96Seschrock 		goto out;
1072560e6e96Seschrock 
1073560e6e96Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1074560e6e96Seschrock 		error = ENXIO;
1075560e6e96Seschrock 		goto out;
1076560e6e96Seschrock 	}
1077560e6e96Seschrock 
1078fa9e4066Sahrens 	/*
1079fa9e4066Sahrens 	 * Find the best uberblock.
1080fa9e4066Sahrens 	 */
1081fa9e4066Sahrens 	bzero(ub, sizeof (uberblock_t));
1082fa9e4066Sahrens 
1083fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
1084fa9e4066Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1085fa9e4066Sahrens 	vdev_uberblock_load(zio, rvd, ub);
1086fa9e4066Sahrens 	error = zio_wait(zio);
1087fa9e4066Sahrens 
1088fa9e4066Sahrens 	/*
1089fa9e4066Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
1090fa9e4066Sahrens 	 */
1091fa9e4066Sahrens 	if (ub->ub_txg == 0) {
1092eaca9bbdSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1093eaca9bbdSeschrock 		    VDEV_AUX_CORRUPT_DATA);
1094ea8dc4b6Seschrock 		error = ENXIO;
1095ea8dc4b6Seschrock 		goto out;
1096ea8dc4b6Seschrock 	}
1097ea8dc4b6Seschrock 
1098ea8dc4b6Seschrock 	/*
1099ea8dc4b6Seschrock 	 * If the pool is newer than the code, we can't open it.
1100ea8dc4b6Seschrock 	 */
1101e7437265Sahrens 	if (ub->ub_version > SPA_VERSION) {
1102eaca9bbdSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1103eaca9bbdSeschrock 		    VDEV_AUX_VERSION_NEWER);
1104ea8dc4b6Seschrock 		error = ENOTSUP;
1105ea8dc4b6Seschrock 		goto out;
1106fa9e4066Sahrens 	}
1107fa9e4066Sahrens 
1108fa9e4066Sahrens 	/*
1109fa9e4066Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
1110fa9e4066Sahrens 	 * incomplete configuration.
1111fa9e4066Sahrens 	 */
1112ecc2d604Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
1113ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1114ea8dc4b6Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
1115ea8dc4b6Seschrock 		error = ENXIO;
1116ea8dc4b6Seschrock 		goto out;
1117fa9e4066Sahrens 	}
1118fa9e4066Sahrens 
1119fa9e4066Sahrens 	/*
1120fa9e4066Sahrens 	 * Initialize internal SPA structures.
1121fa9e4066Sahrens 	 */
1122fa9e4066Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
1123fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1124fa9e4066Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
1125ea8dc4b6Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1126ea8dc4b6Seschrock 	if (error) {
1127ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1128ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1129ea8dc4b6Seschrock 		goto out;
1130ea8dc4b6Seschrock 	}
1131fa9e4066Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1132fa9e4066Sahrens 
1133ea8dc4b6Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1134fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
1135ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
1136ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1137ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1138ea8dc4b6Seschrock 		error = EIO;
1139ea8dc4b6Seschrock 		goto out;
1140ea8dc4b6Seschrock 	}
1141fa9e4066Sahrens 
1142fa9e4066Sahrens 	if (!mosconfig) {
114399653d4eSeschrock 		nvlist_t *newconfig;
114495173954Sek 		uint64_t hostid;
1145fa9e4066Sahrens 
114699653d4eSeschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
1147ea8dc4b6Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1148ea8dc4b6Seschrock 			    VDEV_AUX_CORRUPT_DATA);
1149ea8dc4b6Seschrock 			error = EIO;
1150ea8dc4b6Seschrock 			goto out;
1151ea8dc4b6Seschrock 		}
1152fa9e4066Sahrens 
115395173954Sek 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
115495173954Sek 		    &hostid) == 0) {
115595173954Sek 			char *hostname;
115695173954Sek 			unsigned long myhostid = 0;
115795173954Sek 
115895173954Sek 			VERIFY(nvlist_lookup_string(newconfig,
115995173954Sek 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
116095173954Sek 
116195173954Sek 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
116217194a52Slling 			if (hostid != 0 && myhostid != 0 &&
116317194a52Slling 			    (unsigned long)hostid != myhostid) {
116495173954Sek 				cmn_err(CE_WARN, "pool '%s' could not be "
116595173954Sek 				    "loaded as it was last accessed by "
116695173954Sek 				    "another system (host: %s hostid: 0x%lx).  "
116795173954Sek 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
116895173954Sek 				    spa->spa_name, hostname,
116995173954Sek 				    (unsigned long)hostid);
117095173954Sek 				error = EBADF;
117195173954Sek 				goto out;
117295173954Sek 			}
117395173954Sek 		}
117495173954Sek 
1175fa9e4066Sahrens 		spa_config_set(spa, newconfig);
1176fa9e4066Sahrens 		spa_unload(spa);
1177fa9e4066Sahrens 		spa_deactivate(spa);
1178fa9e4066Sahrens 		spa_activate(spa);
1179fa9e4066Sahrens 
1180ea8dc4b6Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
1181fa9e4066Sahrens 	}
1182fa9e4066Sahrens 
1183ea8dc4b6Seschrock 	if (zap_lookup(spa->spa_meta_objset,
1184fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
1185ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
1186ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1187ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1188ea8dc4b6Seschrock 		error = EIO;
1189ea8dc4b6Seschrock 		goto out;
1190ea8dc4b6Seschrock 	}
1191fa9e4066Sahrens 
119299653d4eSeschrock 	/*
119399653d4eSeschrock 	 * Load the bit that tells us to use the new accounting function
119499653d4eSeschrock 	 * (raid-z deflation).  If we have an older pool, this will not
119599653d4eSeschrock 	 * be present.
119699653d4eSeschrock 	 */
119799653d4eSeschrock 	error = zap_lookup(spa->spa_meta_objset,
119899653d4eSeschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
119999653d4eSeschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
120099653d4eSeschrock 	if (error != 0 && error != ENOENT) {
120199653d4eSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
120299653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
120399653d4eSeschrock 		error = EIO;
120499653d4eSeschrock 		goto out;
120599653d4eSeschrock 	}
120699653d4eSeschrock 
1207fa9e4066Sahrens 	/*
1208ea8dc4b6Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
1209ea8dc4b6Seschrock 	 * not be present.
1210fa9e4066Sahrens 	 */
1211ea8dc4b6Seschrock 	error = zap_lookup(spa->spa_meta_objset,
1212ea8dc4b6Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
1213ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
1214d80c45e0Sbonwick 	if (error != 0 && error != ENOENT) {
1215ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1216ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1217ea8dc4b6Seschrock 		error = EIO;
1218ea8dc4b6Seschrock 		goto out;
1219ea8dc4b6Seschrock 	}
1220ea8dc4b6Seschrock 
1221ea8dc4b6Seschrock 	error = zap_lookup(spa->spa_meta_objset,
1222ea8dc4b6Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
1223ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
1224ea8dc4b6Seschrock 	if (error != 0 && error != ENOENT) {
1225ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1226ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1227ea8dc4b6Seschrock 		error = EIO;
1228ea8dc4b6Seschrock 		goto out;
1229ea8dc4b6Seschrock 	}
1230ea8dc4b6Seschrock 
123106eeb2adSek 	/*
123206eeb2adSek 	 * Load the history object.  If we have an older pool, this
123306eeb2adSek 	 * will not be present.
123406eeb2adSek 	 */
123506eeb2adSek 	error = zap_lookup(spa->spa_meta_objset,
123606eeb2adSek 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
123706eeb2adSek 	    sizeof (uint64_t), 1, &spa->spa_history);
123806eeb2adSek 	if (error != 0 && error != ENOENT) {
123906eeb2adSek 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
124006eeb2adSek 		    VDEV_AUX_CORRUPT_DATA);
124106eeb2adSek 		error = EIO;
124206eeb2adSek 		goto out;
124306eeb2adSek 	}
124406eeb2adSek 
124599653d4eSeschrock 	/*
124699653d4eSeschrock 	 * Load any hot spares for this pool.
124799653d4eSeschrock 	 */
124899653d4eSeschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1249fa94a07fSbrendan 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
125099653d4eSeschrock 	if (error != 0 && error != ENOENT) {
125199653d4eSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
125299653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
125399653d4eSeschrock 		error = EIO;
125499653d4eSeschrock 		goto out;
125599653d4eSeschrock 	}
125699653d4eSeschrock 	if (error == 0) {
1257e7437265Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
1258fa94a07fSbrendan 		if (load_nvlist(spa, spa->spa_spares.sav_object,
1259fa94a07fSbrendan 		    &spa->spa_spares.sav_config) != 0) {
126099653d4eSeschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
126199653d4eSeschrock 			    VDEV_AUX_CORRUPT_DATA);
126299653d4eSeschrock 			error = EIO;
126399653d4eSeschrock 			goto out;
126499653d4eSeschrock 		}
126599653d4eSeschrock 
126699653d4eSeschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
126799653d4eSeschrock 		spa_load_spares(spa);
126899653d4eSeschrock 		spa_config_exit(spa, FTAG);
126999653d4eSeschrock 	}
127099653d4eSeschrock 
1271fa94a07fSbrendan 	/*
1272fa94a07fSbrendan 	 * Load any level 2 ARC devices for this pool.
1273fa94a07fSbrendan 	 */
1274fa94a07fSbrendan 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1275fa94a07fSbrendan 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
1276fa94a07fSbrendan 	    &spa->spa_l2cache.sav_object);
1277fa94a07fSbrendan 	if (error != 0 && error != ENOENT) {
1278fa94a07fSbrendan 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1279fa94a07fSbrendan 		    VDEV_AUX_CORRUPT_DATA);
1280fa94a07fSbrendan 		error = EIO;
1281fa94a07fSbrendan 		goto out;
1282fa94a07fSbrendan 	}
1283fa94a07fSbrendan 	if (error == 0) {
1284fa94a07fSbrendan 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
1285fa94a07fSbrendan 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
1286fa94a07fSbrendan 		    &spa->spa_l2cache.sav_config) != 0) {
1287fa94a07fSbrendan 			vdev_set_state(rvd, B_TRUE,
1288fa94a07fSbrendan 			    VDEV_STATE_CANT_OPEN,
1289fa94a07fSbrendan 			    VDEV_AUX_CORRUPT_DATA);
1290fa94a07fSbrendan 			error = EIO;
1291fa94a07fSbrendan 			goto out;
1292fa94a07fSbrendan 		}
1293fa94a07fSbrendan 
1294fa94a07fSbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
1295fa94a07fSbrendan 		spa_load_l2cache(spa);
1296fa94a07fSbrendan 		spa_config_exit(spa, FTAG);
1297fa94a07fSbrendan 	}
1298fa94a07fSbrendan 
1299990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1300ecd6cf80Smarks 
1301b1b8ab34Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1302b1b8ab34Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
1303b1b8ab34Slling 
1304b1b8ab34Slling 	if (error && error != ENOENT) {
1305b1b8ab34Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1306b1b8ab34Slling 		    VDEV_AUX_CORRUPT_DATA);
1307b1b8ab34Slling 		error = EIO;
1308b1b8ab34Slling 		goto out;
1309b1b8ab34Slling 	}
1310b1b8ab34Slling 
1311b1b8ab34Slling 	if (error == 0) {
1312b1b8ab34Slling 		(void) zap_lookup(spa->spa_meta_objset,
1313b1b8ab34Slling 		    spa->spa_pool_props_object,
13143d7072f8Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1315b1b8ab34Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
13163d7072f8Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
13173d7072f8Seschrock 		    spa->spa_pool_props_object,
13183d7072f8Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
13193d7072f8Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
1320ecd6cf80Smarks 		(void) zap_lookup(spa->spa_meta_objset,
1321ecd6cf80Smarks 		    spa->spa_pool_props_object,
1322ecd6cf80Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
1323ecd6cf80Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
13240a4e9518Sgw 		(void) zap_lookup(spa->spa_meta_objset,
13250a4e9518Sgw 		    spa->spa_pool_props_object,
13260a4e9518Sgw 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
13270a4e9518Sgw 		    sizeof (uint64_t), 1, &spa->spa_failmode);
1328b1b8ab34Slling 	}
1329b1b8ab34Slling 
13303d7072f8Seschrock 	/*
13313d7072f8Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
13323d7072f8Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
13333d7072f8Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
13343d7072f8Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
13353d7072f8Seschrock 	 * over.
13363d7072f8Seschrock 	 */
1337*b01c3b58Seschrock 	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
13383d7072f8Seschrock 		spa_check_removed(spa->spa_root_vdev);
13393d7072f8Seschrock 
1340ea8dc4b6Seschrock 	/*
1341560e6e96Seschrock 	 * Load the vdev state for all toplevel vdevs.
1342ea8dc4b6Seschrock 	 */
1343560e6e96Seschrock 	vdev_load(rvd);
13440373e76bSbonwick 
1345fa9e4066Sahrens 	/*
1346fa9e4066Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1347fa9e4066Sahrens 	 */
1348ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1349fa9e4066Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
1350ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	/*
1353fa9e4066Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1354fa9e4066Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1355fa9e4066Sahrens 	 */
1356ea8dc4b6Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1357ea8dc4b6Seschrock 		error = ENXIO;
1358ea8dc4b6Seschrock 		goto out;
1359ea8dc4b6Seschrock 	}
1360fa9e4066Sahrens 
1361ea8dc4b6Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
13625dabedeeSbonwick 		dmu_tx_t *tx;
13630373e76bSbonwick 		int need_update = B_FALSE;
13640373e76bSbonwick 		int c;
13655dabedeeSbonwick 
13660373e76bSbonwick 		/*
13670373e76bSbonwick 		 * Claim log blocks that haven't been committed yet.
13680373e76bSbonwick 		 * This must all happen in a single txg.
13690373e76bSbonwick 		 */
13705dabedeeSbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1371fa9e4066Sahrens 		    spa_first_txg(spa));
13720b69c2f0Sahrens 		(void) dmu_objset_find(spa->spa_name,
13730b69c2f0Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1374fa9e4066Sahrens 		dmu_tx_commit(tx);
1375fa9e4066Sahrens 
1376fa9e4066Sahrens 		spa->spa_sync_on = B_TRUE;
1377fa9e4066Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1378fa9e4066Sahrens 
1379fa9e4066Sahrens 		/*
1380fa9e4066Sahrens 		 * Wait for all claims to sync.
1381fa9e4066Sahrens 		 */
1382fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
13830e34b6a7Sbonwick 
13840e34b6a7Sbonwick 		/*
13850373e76bSbonwick 		 * If the config cache is stale, or we have uninitialized
13860373e76bSbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
13870e34b6a7Sbonwick 		 */
13880373e76bSbonwick 		if (config_cache_txg != spa->spa_config_txg ||
13890373e76bSbonwick 		    state == SPA_LOAD_IMPORT)
13900373e76bSbonwick 			need_update = B_TRUE;
13910373e76bSbonwick 
13920373e76bSbonwick 		for (c = 0; c < rvd->vdev_children; c++)
13930373e76bSbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
13940373e76bSbonwick 				need_update = B_TRUE;
13950e34b6a7Sbonwick 
13960e34b6a7Sbonwick 		/*
13970373e76bSbonwick 		 * Update the config cache asychronously in case we're the
13980373e76bSbonwick 		 * root pool, in which case the config cache isn't writable yet.
13990e34b6a7Sbonwick 		 */
14000373e76bSbonwick 		if (need_update)
14010373e76bSbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1402fa9e4066Sahrens 	}
1403fa9e4066Sahrens 
1404ea8dc4b6Seschrock 	error = 0;
1405ea8dc4b6Seschrock out:
140699653d4eSeschrock 	if (error && error != EBADF)
1407ea8dc4b6Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
1408ea8dc4b6Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
1409ea8dc4b6Seschrock 	spa->spa_ena = 0;
1410ea8dc4b6Seschrock 
1411ea8dc4b6Seschrock 	return (error);
1412fa9e4066Sahrens }
1413fa9e4066Sahrens 
1414fa9e4066Sahrens /*
1415fa9e4066Sahrens  * Pool Open/Import
1416fa9e4066Sahrens  *
1417fa9e4066Sahrens  * The import case is identical to an open except that the configuration is sent
1418fa9e4066Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1419fa9e4066Sahrens  * case of an open, the pool configuration will exist in the
14203d7072f8Seschrock  * POOL_STATE_UNINITIALIZED state.
1421fa9e4066Sahrens  *
1422fa9e4066Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1423fa9e4066Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1424fa9e4066Sahrens  * ambiguous state.
1425fa9e4066Sahrens  */
1426fa9e4066Sahrens static int
1427fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1428fa9e4066Sahrens {
1429fa9e4066Sahrens 	spa_t *spa;
1430fa9e4066Sahrens 	int error;
1431fa9e4066Sahrens 	int loaded = B_FALSE;
1432fa9e4066Sahrens 	int locked = B_FALSE;
1433fa9e4066Sahrens 
1434fa9e4066Sahrens 	*spapp = NULL;
1435fa9e4066Sahrens 
1436fa9e4066Sahrens 	/*
1437fa9e4066Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1438fa9e4066Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1439fa9e4066Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1440fa9e4066Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1441fa9e4066Sahrens 	 */
1442fa9e4066Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1443fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
1444fa9e4066Sahrens 		locked = B_TRUE;
1445fa9e4066Sahrens 	}
1446fa9e4066Sahrens 
1447fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1448fa9e4066Sahrens 		if (locked)
1449fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
1450fa9e4066Sahrens 		return (ENOENT);
1451fa9e4066Sahrens 	}
1452fa9e4066Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1453fa9e4066Sahrens 
1454fa9e4066Sahrens 		spa_activate(spa);
1455fa9e4066Sahrens 
14560373e76bSbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1457fa9e4066Sahrens 
1458fa9e4066Sahrens 		if (error == EBADF) {
1459fa9e4066Sahrens 			/*
1460560e6e96Seschrock 			 * If vdev_validate() returns failure (indicated by
1461560e6e96Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
1462560e6e96Seschrock 			 * that the pool has been exported or destroyed.  If
1463560e6e96Seschrock 			 * this is the case, the config cache is out of sync and
1464560e6e96Seschrock 			 * we should remove the pool from the namespace.
1465fa9e4066Sahrens 			 */
146699653d4eSeschrock 			zfs_post_ok(spa, NULL);
1467fa9e4066Sahrens 			spa_unload(spa);
1468fa9e4066Sahrens 			spa_deactivate(spa);
1469fa9e4066Sahrens 			spa_remove(spa);
1470fa9e4066Sahrens 			spa_config_sync();
1471fa9e4066Sahrens 			if (locked)
1472fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
1473fa9e4066Sahrens 			return (ENOENT);
1474ea8dc4b6Seschrock 		}
1475ea8dc4b6Seschrock 
1476ea8dc4b6Seschrock 		if (error) {
1477fa9e4066Sahrens 			/*
1478fa9e4066Sahrens 			 * We can't open the pool, but we still have useful
1479fa9e4066Sahrens 			 * information: the state of each vdev after the
1480fa9e4066Sahrens 			 * attempted vdev_open().  Return this to the user.
1481fa9e4066Sahrens 			 */
14820373e76bSbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
14830373e76bSbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1484fa9e4066Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1485fa9e4066Sahrens 				    B_TRUE);
14860373e76bSbonwick 				spa_config_exit(spa, FTAG);
14870373e76bSbonwick 			}
1488fa9e4066Sahrens 			spa_unload(spa);
1489fa9e4066Sahrens 			spa_deactivate(spa);
1490ea8dc4b6Seschrock 			spa->spa_last_open_failed = B_TRUE;
1491fa9e4066Sahrens 			if (locked)
1492fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
1493fa9e4066Sahrens 			*spapp = NULL;
1494fa9e4066Sahrens 			return (error);
1495ea8dc4b6Seschrock 		} else {
1496ea8dc4b6Seschrock 			zfs_post_ok(spa, NULL);
1497ea8dc4b6Seschrock 			spa->spa_last_open_failed = B_FALSE;
1498fa9e4066Sahrens 		}
1499fa9e4066Sahrens 
1500fa9e4066Sahrens 		loaded = B_TRUE;
1501fa9e4066Sahrens 	}
1502fa9e4066Sahrens 
1503fa9e4066Sahrens 	spa_open_ref(spa, tag);
15043d7072f8Seschrock 
15053d7072f8Seschrock 	/*
15063d7072f8Seschrock 	 * If we just loaded the pool, resilver anything that's out of date.
15073d7072f8Seschrock 	 */
15083d7072f8Seschrock 	if (loaded && (spa_mode & FWRITE))
15093d7072f8Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
15103d7072f8Seschrock 
1511fa9e4066Sahrens 	if (locked)
1512fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1513fa9e4066Sahrens 
1514fa9e4066Sahrens 	*spapp = spa;
1515fa9e4066Sahrens 
1516fa9e4066Sahrens 	if (config != NULL) {
1517ea8dc4b6Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1518fa9e4066Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1519ea8dc4b6Seschrock 		spa_config_exit(spa, FTAG);
1520fa9e4066Sahrens 	}
1521fa9e4066Sahrens 
1522fa9e4066Sahrens 	return (0);
1523fa9e4066Sahrens }
1524fa9e4066Sahrens 
1525fa9e4066Sahrens int
1526fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1527fa9e4066Sahrens {
1528fa9e4066Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1529fa9e4066Sahrens }
1530fa9e4066Sahrens 
1531ea8dc4b6Seschrock /*
1532ea8dc4b6Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
1533ea8dc4b6Seschrock  * preventing it from being exported or destroyed.
1534ea8dc4b6Seschrock  */
1535ea8dc4b6Seschrock spa_t *
1536ea8dc4b6Seschrock spa_inject_addref(char *name)
1537ea8dc4b6Seschrock {
1538ea8dc4b6Seschrock 	spa_t *spa;
1539ea8dc4b6Seschrock 
1540ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
1541ea8dc4b6Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
1542ea8dc4b6Seschrock 		mutex_exit(&spa_namespace_lock);
1543ea8dc4b6Seschrock 		return (NULL);
1544ea8dc4b6Seschrock 	}
1545ea8dc4b6Seschrock 	spa->spa_inject_ref++;
1546ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1547ea8dc4b6Seschrock 
1548ea8dc4b6Seschrock 	return (spa);
1549ea8dc4b6Seschrock }
1550ea8dc4b6Seschrock 
1551ea8dc4b6Seschrock void
1552ea8dc4b6Seschrock spa_inject_delref(spa_t *spa)
1553ea8dc4b6Seschrock {
1554ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
1555ea8dc4b6Seschrock 	spa->spa_inject_ref--;
1556ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1557ea8dc4b6Seschrock }
1558ea8dc4b6Seschrock 
1559fa94a07fSbrendan /*
1560fa94a07fSbrendan  * Add spares device information to the nvlist.
1561fa94a07fSbrendan  */
156299653d4eSeschrock static void
156399653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config)
156499653d4eSeschrock {
156599653d4eSeschrock 	nvlist_t **spares;
156699653d4eSeschrock 	uint_t i, nspares;
156799653d4eSeschrock 	nvlist_t *nvroot;
156899653d4eSeschrock 	uint64_t guid;
156999653d4eSeschrock 	vdev_stat_t *vs;
157099653d4eSeschrock 	uint_t vsc;
157139c23413Seschrock 	uint64_t pool;
157299653d4eSeschrock 
1573fa94a07fSbrendan 	if (spa->spa_spares.sav_count == 0)
157499653d4eSeschrock 		return;
157599653d4eSeschrock 
157699653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist(config,
157799653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1578fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
157999653d4eSeschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
158099653d4eSeschrock 	if (nspares != 0) {
158199653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
158299653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
158399653d4eSeschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
158499653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
158599653d4eSeschrock 
158699653d4eSeschrock 		/*
158799653d4eSeschrock 		 * Go through and find any spares which have since been
158899653d4eSeschrock 		 * repurposed as an active spare.  If this is the case, update
158999653d4eSeschrock 		 * their status appropriately.
159099653d4eSeschrock 		 */
159199653d4eSeschrock 		for (i = 0; i < nspares; i++) {
159299653d4eSeschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
159399653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
159439c23413Seschrock 			if (spa_spare_exists(guid, &pool) && pool != 0ULL) {
159599653d4eSeschrock 				VERIFY(nvlist_lookup_uint64_array(
159699653d4eSeschrock 				    spares[i], ZPOOL_CONFIG_STATS,
159799653d4eSeschrock 				    (uint64_t **)&vs, &vsc) == 0);
159899653d4eSeschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
159999653d4eSeschrock 				vs->vs_aux = VDEV_AUX_SPARED;
160099653d4eSeschrock 			}
160199653d4eSeschrock 		}
160299653d4eSeschrock 	}
160399653d4eSeschrock }
160499653d4eSeschrock 
1605fa94a07fSbrendan /*
1606fa94a07fSbrendan  * Add l2cache device information to the nvlist, including vdev stats.
1607fa94a07fSbrendan  */
1608fa94a07fSbrendan static void
1609fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
1610fa94a07fSbrendan {
1611fa94a07fSbrendan 	nvlist_t **l2cache;
1612fa94a07fSbrendan 	uint_t i, j, nl2cache;
1613fa94a07fSbrendan 	nvlist_t *nvroot;
1614fa94a07fSbrendan 	uint64_t guid;
1615fa94a07fSbrendan 	vdev_t *vd;
1616fa94a07fSbrendan 	vdev_stat_t *vs;
1617fa94a07fSbrendan 	uint_t vsc;
1618fa94a07fSbrendan 
1619fa94a07fSbrendan 	if (spa->spa_l2cache.sav_count == 0)
1620fa94a07fSbrendan 		return;
1621fa94a07fSbrendan 
1622fa94a07fSbrendan 	spa_config_enter(spa, RW_READER, FTAG);
1623fa94a07fSbrendan 
1624fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist(config,
1625fa94a07fSbrendan 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1626fa94a07fSbrendan 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
1627fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1628fa94a07fSbrendan 	if (nl2cache != 0) {
1629fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot,
1630fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
1631fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
1632fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1633fa94a07fSbrendan 
1634fa94a07fSbrendan 		/*
1635fa94a07fSbrendan 		 * Update level 2 cache device stats.
1636fa94a07fSbrendan 		 */
1637fa94a07fSbrendan 
1638fa94a07fSbrendan 		for (i = 0; i < nl2cache; i++) {
1639fa94a07fSbrendan 			VERIFY(nvlist_lookup_uint64(l2cache[i],
1640fa94a07fSbrendan 			    ZPOOL_CONFIG_GUID, &guid) == 0);
1641fa94a07fSbrendan 
1642fa94a07fSbrendan 			vd = NULL;
1643fa94a07fSbrendan 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
1644fa94a07fSbrendan 				if (guid ==
1645fa94a07fSbrendan 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
1646fa94a07fSbrendan 					vd = spa->spa_l2cache.sav_vdevs[j];
1647fa94a07fSbrendan 					break;
1648fa94a07fSbrendan 				}
1649fa94a07fSbrendan 			}
1650fa94a07fSbrendan 			ASSERT(vd != NULL);
1651fa94a07fSbrendan 
1652fa94a07fSbrendan 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
1653fa94a07fSbrendan 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
1654fa94a07fSbrendan 			vdev_get_stats(vd, vs);
1655fa94a07fSbrendan 		}
1656fa94a07fSbrendan 	}
1657fa94a07fSbrendan 
1658fa94a07fSbrendan 	spa_config_exit(spa, FTAG);
1659fa94a07fSbrendan }
1660fa94a07fSbrendan 
1661fa9e4066Sahrens int
1662ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1663fa9e4066Sahrens {
1664fa9e4066Sahrens 	int error;
1665fa9e4066Sahrens 	spa_t *spa;
1666fa9e4066Sahrens 
1667fa9e4066Sahrens 	*config = NULL;
1668fa9e4066Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1669fa9e4066Sahrens 
167099653d4eSeschrock 	if (spa && *config != NULL) {
1671ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
1672ea8dc4b6Seschrock 		    spa_get_errlog_size(spa)) == 0);
1673ea8dc4b6Seschrock 
167499653d4eSeschrock 		spa_add_spares(spa, *config);
1675fa94a07fSbrendan 		spa_add_l2cache(spa, *config);
167699653d4eSeschrock 	}
167799653d4eSeschrock 
1678ea8dc4b6Seschrock 	/*
1679ea8dc4b6Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
1680ea8dc4b6Seschrock 	 * and call spa_lookup() directly.
1681ea8dc4b6Seschrock 	 */
1682ea8dc4b6Seschrock 	if (altroot) {
1683ea8dc4b6Seschrock 		if (spa == NULL) {
1684ea8dc4b6Seschrock 			mutex_enter(&spa_namespace_lock);
1685ea8dc4b6Seschrock 			spa = spa_lookup(name);
1686ea8dc4b6Seschrock 			if (spa)
1687ea8dc4b6Seschrock 				spa_altroot(spa, altroot, buflen);
1688ea8dc4b6Seschrock 			else
1689ea8dc4b6Seschrock 				altroot[0] = '\0';
1690ea8dc4b6Seschrock 			spa = NULL;
1691ea8dc4b6Seschrock 			mutex_exit(&spa_namespace_lock);
1692ea8dc4b6Seschrock 		} else {
1693ea8dc4b6Seschrock 			spa_altroot(spa, altroot, buflen);
1694ea8dc4b6Seschrock 		}
1695ea8dc4b6Seschrock 	}
1696ea8dc4b6Seschrock 
1697fa9e4066Sahrens 	if (spa != NULL)
1698fa9e4066Sahrens 		spa_close(spa, FTAG);
1699fa9e4066Sahrens 
1700fa9e4066Sahrens 	return (error);
1701fa9e4066Sahrens }
1702fa9e4066Sahrens 
170399653d4eSeschrock /*
1704fa94a07fSbrendan  * Validate that the auxiliary device array is well formed.  We must have an
1705fa94a07fSbrendan  * array of nvlists, each which describes a valid leaf vdev.  If this is an
1706fa94a07fSbrendan  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
1707fa94a07fSbrendan  * specified, as long as they are well-formed.
170899653d4eSeschrock  */
170999653d4eSeschrock static int
1710fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
1711fa94a07fSbrendan     spa_aux_vdev_t *sav, const char *config, uint64_t version,
1712fa94a07fSbrendan     vdev_labeltype_t label)
171399653d4eSeschrock {
1714fa94a07fSbrendan 	nvlist_t **dev;
1715fa94a07fSbrendan 	uint_t i, ndev;
171699653d4eSeschrock 	vdev_t *vd;
171799653d4eSeschrock 	int error;
171899653d4eSeschrock 
171999653d4eSeschrock 	/*
1720fa94a07fSbrendan 	 * It's acceptable to have no devs specified.
172199653d4eSeschrock 	 */
1722fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
172399653d4eSeschrock 		return (0);
172499653d4eSeschrock 
1725fa94a07fSbrendan 	if (ndev == 0)
172699653d4eSeschrock 		return (EINVAL);
172799653d4eSeschrock 
172899653d4eSeschrock 	/*
1729fa94a07fSbrendan 	 * Make sure the pool is formatted with a version that supports this
1730fa94a07fSbrendan 	 * device type.
173199653d4eSeschrock 	 */
1732fa94a07fSbrendan 	if (spa_version(spa) < version)
173399653d4eSeschrock 		return (ENOTSUP);
173499653d4eSeschrock 
173539c23413Seschrock 	/*
1736fa94a07fSbrendan 	 * Set the pending device list so we correctly handle device in-use
173739c23413Seschrock 	 * checking.
173839c23413Seschrock 	 */
1739fa94a07fSbrendan 	sav->sav_pending = dev;
1740fa94a07fSbrendan 	sav->sav_npending = ndev;
174139c23413Seschrock 
1742fa94a07fSbrendan 	for (i = 0; i < ndev; i++) {
1743fa94a07fSbrendan 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
174499653d4eSeschrock 		    mode)) != 0)
174539c23413Seschrock 			goto out;
174699653d4eSeschrock 
174799653d4eSeschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
174899653d4eSeschrock 			vdev_free(vd);
174939c23413Seschrock 			error = EINVAL;
175039c23413Seschrock 			goto out;
175199653d4eSeschrock 		}
175299653d4eSeschrock 
1753fa94a07fSbrendan 		/*
1754fa94a07fSbrendan 		 * The L2ARC currently only supports disk devices.
1755fa94a07fSbrendan 		 */
1756fa94a07fSbrendan 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
1757fa94a07fSbrendan 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
1758fa94a07fSbrendan 			error = ENOTBLK;
1759fa94a07fSbrendan 			goto out;
1760fa94a07fSbrendan 		}
1761fa94a07fSbrendan 
176299653d4eSeschrock 		vd->vdev_top = vd;
176399653d4eSeschrock 
176439c23413Seschrock 		if ((error = vdev_open(vd)) == 0 &&
1765fa94a07fSbrendan 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
1766fa94a07fSbrendan 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
176739c23413Seschrock 			    vd->vdev_guid) == 0);
176839c23413Seschrock 		}
176999653d4eSeschrock 
177099653d4eSeschrock 		vdev_free(vd);
177139c23413Seschrock 
1772fa94a07fSbrendan 		if (error &&
1773fa94a07fSbrendan 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
177439c23413Seschrock 			goto out;
177539c23413Seschrock 		else
177639c23413Seschrock 			error = 0;
177799653d4eSeschrock 	}
177899653d4eSeschrock 
177939c23413Seschrock out:
1780fa94a07fSbrendan 	sav->sav_pending = NULL;
1781fa94a07fSbrendan 	sav->sav_npending = 0;
178239c23413Seschrock 	return (error);
178399653d4eSeschrock }
178499653d4eSeschrock 
1785fa94a07fSbrendan static int
1786fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
1787fa94a07fSbrendan {
1788fa94a07fSbrendan 	int error;
1789fa94a07fSbrendan 
1790fa94a07fSbrendan 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1791fa94a07fSbrendan 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
1792fa94a07fSbrendan 	    VDEV_LABEL_SPARE)) != 0) {
1793fa94a07fSbrendan 		return (error);
1794fa94a07fSbrendan 	}
1795fa94a07fSbrendan 
1796fa94a07fSbrendan 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1797fa94a07fSbrendan 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
1798fa94a07fSbrendan 	    VDEV_LABEL_L2CACHE));
1799fa94a07fSbrendan }
1800fa94a07fSbrendan 
1801fa94a07fSbrendan static void
1802fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
1803fa94a07fSbrendan     const char *config)
1804fa94a07fSbrendan {
1805fa94a07fSbrendan 	int i;
1806fa94a07fSbrendan 
1807fa94a07fSbrendan 	if (sav->sav_config != NULL) {
1808fa94a07fSbrendan 		nvlist_t **olddevs;
1809fa94a07fSbrendan 		uint_t oldndevs;
1810fa94a07fSbrendan 		nvlist_t **newdevs;
1811fa94a07fSbrendan 
1812fa94a07fSbrendan 		/*
1813fa94a07fSbrendan 		 * Generate new dev list by concatentating with the
1814fa94a07fSbrendan 		 * current dev list.
1815fa94a07fSbrendan 		 */
1816fa94a07fSbrendan 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
1817fa94a07fSbrendan 		    &olddevs, &oldndevs) == 0);
1818fa94a07fSbrendan 
1819fa94a07fSbrendan 		newdevs = kmem_alloc(sizeof (void *) *
1820fa94a07fSbrendan 		    (ndevs + oldndevs), KM_SLEEP);
1821fa94a07fSbrendan 		for (i = 0; i < oldndevs; i++)
1822fa94a07fSbrendan 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
1823fa94a07fSbrendan 			    KM_SLEEP) == 0);
1824fa94a07fSbrendan 		for (i = 0; i < ndevs; i++)
1825fa94a07fSbrendan 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
1826fa94a07fSbrendan 			    KM_SLEEP) == 0);
1827fa94a07fSbrendan 
1828fa94a07fSbrendan 		VERIFY(nvlist_remove(sav->sav_config, config,
1829fa94a07fSbrendan 		    DATA_TYPE_NVLIST_ARRAY) == 0);
1830fa94a07fSbrendan 
1831fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1832fa94a07fSbrendan 		    config, newdevs, ndevs + oldndevs) == 0);
1833fa94a07fSbrendan 		for (i = 0; i < oldndevs + ndevs; i++)
1834fa94a07fSbrendan 			nvlist_free(newdevs[i]);
1835fa94a07fSbrendan 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
1836fa94a07fSbrendan 	} else {
1837fa94a07fSbrendan 		/*
1838fa94a07fSbrendan 		 * Generate a new dev list.
1839fa94a07fSbrendan 		 */
1840fa94a07fSbrendan 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
1841fa94a07fSbrendan 		    KM_SLEEP) == 0);
1842fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
1843fa94a07fSbrendan 		    devs, ndevs) == 0);
1844fa94a07fSbrendan 	}
1845fa94a07fSbrendan }
1846fa94a07fSbrendan 
1847fa94a07fSbrendan /*
1848fa94a07fSbrendan  * Stop and drop level 2 ARC devices
1849fa94a07fSbrendan  */
1850fa94a07fSbrendan void
1851fa94a07fSbrendan spa_l2cache_drop(spa_t *spa)
1852fa94a07fSbrendan {
1853fa94a07fSbrendan 	vdev_t *vd;
1854fa94a07fSbrendan 	int i;
1855fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
1856fa94a07fSbrendan 
1857fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++) {
1858fa94a07fSbrendan 		uint64_t pool;
1859fa94a07fSbrendan 
1860fa94a07fSbrendan 		vd = sav->sav_vdevs[i];
1861fa94a07fSbrendan 		ASSERT(vd != NULL);
1862fa94a07fSbrendan 
1863fa94a07fSbrendan 		if (spa_mode & FWRITE &&
1864fa94a07fSbrendan 		    spa_l2cache_exists(vd->vdev_guid, &pool) && pool != 0ULL) {
1865fa94a07fSbrendan 			l2arc_remove_vdev(vd);
1866fa94a07fSbrendan 		}
1867fa94a07fSbrendan 		if (vd->vdev_isl2cache)
1868fa94a07fSbrendan 			spa_l2cache_remove(vd);
1869fa94a07fSbrendan 		vdev_clear_stats(vd);
1870fa94a07fSbrendan 		(void) vdev_close(vd);
1871fa94a07fSbrendan 	}
1872fa94a07fSbrendan }
1873fa94a07fSbrendan 
1874fa9e4066Sahrens /*
1875fa9e4066Sahrens  * Pool Creation
1876fa9e4066Sahrens  */
1877fa9e4066Sahrens int
1878990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
1879228975ccSek     const char *history_str)
1880fa9e4066Sahrens {
1881fa9e4066Sahrens 	spa_t *spa;
1882990b4856Slling 	char *altroot = NULL;
18830373e76bSbonwick 	vdev_t *rvd;
1884fa9e4066Sahrens 	dsl_pool_t *dp;
1885fa9e4066Sahrens 	dmu_tx_t *tx;
188699653d4eSeschrock 	int c, error = 0;
1887fa9e4066Sahrens 	uint64_t txg = TXG_INITIAL;
1888fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1889fa94a07fSbrendan 	uint_t nspares, nl2cache;
1890990b4856Slling 	uint64_t version;
1891fa9e4066Sahrens 
1892fa9e4066Sahrens 	/*
1893fa9e4066Sahrens 	 * If this pool already exists, return failure.
1894fa9e4066Sahrens 	 */
1895fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1896fa9e4066Sahrens 	if (spa_lookup(pool) != NULL) {
1897fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1898fa9e4066Sahrens 		return (EEXIST);
1899fa9e4066Sahrens 	}
1900fa9e4066Sahrens 
1901fa9e4066Sahrens 	/*
1902fa9e4066Sahrens 	 * Allocate a new spa_t structure.
1903fa9e4066Sahrens 	 */
1904990b4856Slling 	(void) nvlist_lookup_string(props,
1905990b4856Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
19060373e76bSbonwick 	spa = spa_add(pool, altroot);
1907fa9e4066Sahrens 	spa_activate(spa);
1908fa9e4066Sahrens 
1909fa9e4066Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
1910990b4856Slling 
1911990b4856Slling 	if (props && (error = spa_prop_validate(spa, props))) {
1912990b4856Slling 		spa_unload(spa);
1913990b4856Slling 		spa_deactivate(spa);
1914990b4856Slling 		spa_remove(spa);
1915990b4856Slling 		return (error);
1916990b4856Slling 	}
1917990b4856Slling 
1918990b4856Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
1919990b4856Slling 	    &version) != 0)
1920990b4856Slling 		version = SPA_VERSION;
1921990b4856Slling 	ASSERT(version <= SPA_VERSION);
1922990b4856Slling 	spa->spa_uberblock.ub_version = version;
1923fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1924fa9e4066Sahrens 
19250373e76bSbonwick 	/*
19260373e76bSbonwick 	 * Create the root vdev.
19270373e76bSbonwick 	 */
19280373e76bSbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
19290373e76bSbonwick 
193099653d4eSeschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
19310373e76bSbonwick 
193299653d4eSeschrock 	ASSERT(error != 0 || rvd != NULL);
193399653d4eSeschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
19340373e76bSbonwick 
193599653d4eSeschrock 	if (error == 0 && rvd->vdev_children == 0)
19360373e76bSbonwick 		error = EINVAL;
193799653d4eSeschrock 
193899653d4eSeschrock 	if (error == 0 &&
193999653d4eSeschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
1940fa94a07fSbrendan 	    (error = spa_validate_aux(spa, nvroot, txg,
194199653d4eSeschrock 	    VDEV_ALLOC_ADD)) == 0) {
194299653d4eSeschrock 		for (c = 0; c < rvd->vdev_children; c++)
194399653d4eSeschrock 			vdev_init(rvd->vdev_child[c], txg);
194499653d4eSeschrock 		vdev_config_dirty(rvd);
19450373e76bSbonwick 	}
19460373e76bSbonwick 
19470373e76bSbonwick 	spa_config_exit(spa, FTAG);
1948fa9e4066Sahrens 
194999653d4eSeschrock 	if (error != 0) {
1950fa9e4066Sahrens 		spa_unload(spa);
1951fa9e4066Sahrens 		spa_deactivate(spa);
1952fa9e4066Sahrens 		spa_remove(spa);
1953fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1954fa9e4066Sahrens 		return (error);
1955fa9e4066Sahrens 	}
1956fa9e4066Sahrens 
195799653d4eSeschrock 	/*
195899653d4eSeschrock 	 * Get the list of spares, if specified.
195999653d4eSeschrock 	 */
196099653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
196199653d4eSeschrock 	    &spares, &nspares) == 0) {
1962fa94a07fSbrendan 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
196399653d4eSeschrock 		    KM_SLEEP) == 0);
1964fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
196599653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
196699653d4eSeschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
196799653d4eSeschrock 		spa_load_spares(spa);
196899653d4eSeschrock 		spa_config_exit(spa, FTAG);
1969fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
1970fa94a07fSbrendan 	}
1971fa94a07fSbrendan 
1972fa94a07fSbrendan 	/*
1973fa94a07fSbrendan 	 * Get the list of level 2 cache devices, if specified.
1974fa94a07fSbrendan 	 */
1975fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1976fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1977fa94a07fSbrendan 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
1978fa94a07fSbrendan 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
1979fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
1980fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
1981fa94a07fSbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
1982fa94a07fSbrendan 		spa_load_l2cache(spa);
1983fa94a07fSbrendan 		spa_config_exit(spa, FTAG);
1984fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
198599653d4eSeschrock 	}
198699653d4eSeschrock 
1987fa9e4066Sahrens 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg);
1988fa9e4066Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1989fa9e4066Sahrens 
1990fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1991fa9e4066Sahrens 
1992fa9e4066Sahrens 	/*
1993fa9e4066Sahrens 	 * Create the pool config object.
1994fa9e4066Sahrens 	 */
1995fa9e4066Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1996fa9e4066Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1997fa9e4066Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1998fa9e4066Sahrens 
1999ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
2000fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
2001ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
2002ea8dc4b6Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
2003ea8dc4b6Seschrock 	}
2004fa9e4066Sahrens 
2005990b4856Slling 	/* Newly created pools with the right version are always deflated. */
2006990b4856Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
2007990b4856Slling 		spa->spa_deflate = TRUE;
2008990b4856Slling 		if (zap_add(spa->spa_meta_objset,
2009990b4856Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
2010990b4856Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
2011990b4856Slling 			cmn_err(CE_PANIC, "failed to add deflate");
2012990b4856Slling 		}
201399653d4eSeschrock 	}
201499653d4eSeschrock 
2015fa9e4066Sahrens 	/*
2016fa9e4066Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
2017fa9e4066Sahrens 	 * because sync-to-convergence takes longer if the blocksize
2018fa9e4066Sahrens 	 * keeps changing.
2019fa9e4066Sahrens 	 */
2020fa9e4066Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2021fa9e4066Sahrens 	    1 << 14, tx);
2022fa9e4066Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2023fa9e4066Sahrens 	    ZIO_COMPRESS_OFF, tx);
2024fa9e4066Sahrens 
2025ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
2026fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
2027ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
2028ea8dc4b6Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
2029ea8dc4b6Seschrock 	}
2030fa9e4066Sahrens 
203106eeb2adSek 	/*
203206eeb2adSek 	 * Create the pool's history object.
203306eeb2adSek 	 */
2034990b4856Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
2035990b4856Slling 		spa_history_create_obj(spa, tx);
2036990b4856Slling 
2037990b4856Slling 	/*
2038990b4856Slling 	 * Set pool properties.
2039990b4856Slling 	 */
2040990b4856Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
2041990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
20420a4e9518Sgw 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
2043990b4856Slling 	if (props)
2044990b4856Slling 		spa_sync_props(spa, props, CRED(), tx);
204506eeb2adSek 
2046fa9e4066Sahrens 	dmu_tx_commit(tx);
2047fa9e4066Sahrens 
2048fa9e4066Sahrens 	spa->spa_sync_on = B_TRUE;
2049fa9e4066Sahrens 	txg_sync_start(spa->spa_dsl_pool);
2050fa9e4066Sahrens 
2051fa9e4066Sahrens 	/*
2052fa9e4066Sahrens 	 * We explicitly wait for the first transaction to complete so that our
2053fa9e4066Sahrens 	 * bean counters are appropriately updated.
2054fa9e4066Sahrens 	 */
2055fa9e4066Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
2056fa9e4066Sahrens 
2057fa9e4066Sahrens 	spa_config_sync();
2058fa9e4066Sahrens 
2059990b4856Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
2060228975ccSek 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
2061228975ccSek 
2062fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
2063fa9e4066Sahrens 
2064fa9e4066Sahrens 	return (0);
2065fa9e4066Sahrens }
2066fa9e4066Sahrens 
2067fa9e4066Sahrens /*
2068fa9e4066Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
2069fa9e4066Sahrens  * then call spa_load() to do the dirty work.
2070fa9e4066Sahrens  */
2071fa9e4066Sahrens int
2072990b4856Slling spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
2073fa9e4066Sahrens {
2074fa9e4066Sahrens 	spa_t *spa;
2075990b4856Slling 	char *altroot = NULL;
2076fa9e4066Sahrens 	int error;
207799653d4eSeschrock 	nvlist_t *nvroot;
2078fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
2079fa94a07fSbrendan 	uint_t nspares, nl2cache;
2080fa9e4066Sahrens 
2081fa9e4066Sahrens 	/*
2082fa9e4066Sahrens 	 * If a pool with this name exists, return failure.
2083fa9e4066Sahrens 	 */
2084fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
2085fa9e4066Sahrens 	if (spa_lookup(pool) != NULL) {
2086fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
2087fa9e4066Sahrens 		return (EEXIST);
2088fa9e4066Sahrens 	}
2089fa9e4066Sahrens 
2090fa9e4066Sahrens 	/*
20910373e76bSbonwick 	 * Create and initialize the spa structure.
2092fa9e4066Sahrens 	 */
2093990b4856Slling 	(void) nvlist_lookup_string(props,
2094990b4856Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
20950373e76bSbonwick 	spa = spa_add(pool, altroot);
2096fa9e4066Sahrens 	spa_activate(spa);
2097fa9e4066Sahrens 
20985dabedeeSbonwick 	/*
20990373e76bSbonwick 	 * Pass off the heavy lifting to spa_load().
2100ecc2d604Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
2101ecc2d604Sbonwick 	 * is actually the one to trust when doing an import.
21025dabedeeSbonwick 	 */
2103ecc2d604Sbonwick 	error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE);
2104fa9e4066Sahrens 
210599653d4eSeschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
210699653d4eSeschrock 	/*
210799653d4eSeschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
210899653d4eSeschrock 	 * and conflicts with spa_has_spare().
210999653d4eSeschrock 	 */
2110fa94a07fSbrendan 	if (spa->spa_spares.sav_config) {
2111fa94a07fSbrendan 		nvlist_free(spa->spa_spares.sav_config);
2112fa94a07fSbrendan 		spa->spa_spares.sav_config = NULL;
211399653d4eSeschrock 		spa_load_spares(spa);
211499653d4eSeschrock 	}
2115fa94a07fSbrendan 	if (spa->spa_l2cache.sav_config) {
2116fa94a07fSbrendan 		nvlist_free(spa->spa_l2cache.sav_config);
2117fa94a07fSbrendan 		spa->spa_l2cache.sav_config = NULL;
2118fa94a07fSbrendan 		spa_load_l2cache(spa);
2119fa94a07fSbrendan 	}
212099653d4eSeschrock 
212199653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
212299653d4eSeschrock 	    &nvroot) == 0);
2123fa94a07fSbrendan 	if (error == 0)
2124fa94a07fSbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
2125fa94a07fSbrendan 	if (error == 0)
2126fa94a07fSbrendan 		error = spa_validate_aux(spa, nvroot, -1ULL,
2127fa94a07fSbrendan 		    VDEV_ALLOC_L2CACHE);
212899653d4eSeschrock 	spa_config_exit(spa, FTAG);
212999653d4eSeschrock 
2130990b4856Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
2131fa9e4066Sahrens 		spa_unload(spa);
2132fa9e4066Sahrens 		spa_deactivate(spa);
2133fa9e4066Sahrens 		spa_remove(spa);
2134fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
2135fa9e4066Sahrens 		return (error);
2136fa9e4066Sahrens 	}
2137fa9e4066Sahrens 
213899653d4eSeschrock 	/*
2139fa94a07fSbrendan 	 * Override any spares and level 2 cache devices as specified by
2140fa94a07fSbrendan 	 * the user, as these may have correct device names/devids, etc.
214199653d4eSeschrock 	 */
214299653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
214399653d4eSeschrock 	    &spares, &nspares) == 0) {
2144fa94a07fSbrendan 		if (spa->spa_spares.sav_config)
2145fa94a07fSbrendan 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
214699653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
214799653d4eSeschrock 		else
2148fa94a07fSbrendan 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
214999653d4eSeschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2150fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
215199653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
215299653d4eSeschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
215399653d4eSeschrock 		spa_load_spares(spa);
215499653d4eSeschrock 		spa_config_exit(spa, FTAG);
2155fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
2156fa94a07fSbrendan 	}
2157fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2158fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
2159fa94a07fSbrendan 		if (spa->spa_l2cache.sav_config)
2160fa94a07fSbrendan 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
2161fa94a07fSbrendan 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
2162fa94a07fSbrendan 		else
2163fa94a07fSbrendan 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2164fa94a07fSbrendan 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2165fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2166fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2167fa94a07fSbrendan 		spa_config_enter(spa, RW_WRITER, FTAG);
2168fa94a07fSbrendan 		spa_load_l2cache(spa);
2169fa94a07fSbrendan 		spa_config_exit(spa, FTAG);
2170fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
217199653d4eSeschrock 	}
217299653d4eSeschrock 
21730373e76bSbonwick 	/*
21740373e76bSbonwick 	 * Update the config cache to include the newly-imported pool.
21750373e76bSbonwick 	 */
2176de6628f0Sck 	if (spa_mode & FWRITE)
2177de6628f0Sck 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
21780373e76bSbonwick 
2179fa9e4066Sahrens 	/*
2180fa9e4066Sahrens 	 * Resilver anything that's out of date.
2181fa9e4066Sahrens 	 */
2182fa9e4066Sahrens 	if (spa_mode & FWRITE)
2183fa9e4066Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
2184fa9e4066Sahrens 
21853d7072f8Seschrock 	mutex_exit(&spa_namespace_lock);
21863d7072f8Seschrock 
2187fa9e4066Sahrens 	return (0);
2188fa9e4066Sahrens }
2189fa9e4066Sahrens 
2190fa9e4066Sahrens /*
2191fa9e4066Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
2192fa9e4066Sahrens  * to get the vdev stats associated with the imported devices.
2193fa9e4066Sahrens  */
2194fa9e4066Sahrens #define	TRYIMPORT_NAME	"$import"
2195fa9e4066Sahrens 
2196fa9e4066Sahrens nvlist_t *
2197fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig)
2198fa9e4066Sahrens {
2199fa9e4066Sahrens 	nvlist_t *config = NULL;
2200fa9e4066Sahrens 	char *poolname;
2201fa9e4066Sahrens 	spa_t *spa;
2202fa9e4066Sahrens 	uint64_t state;
2203fa9e4066Sahrens 
2204fa9e4066Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2205fa9e4066Sahrens 		return (NULL);
2206fa9e4066Sahrens 
2207fa9e4066Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2208fa9e4066Sahrens 		return (NULL);
2209fa9e4066Sahrens 
2210fa9e4066Sahrens 	/*
22110373e76bSbonwick 	 * Create and initialize the spa structure.
2212fa9e4066Sahrens 	 */
22130373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
22140373e76bSbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
2215fa9e4066Sahrens 	spa_activate(spa);
2216fa9e4066Sahrens 
2217fa9e4066Sahrens 	/*
22180373e76bSbonwick 	 * Pass off the heavy lifting to spa_load().
2219ecc2d604Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
2220ecc2d604Sbonwick 	 * is actually the one to trust when doing an import.
2221fa9e4066Sahrens 	 */
2222ecc2d604Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2223fa9e4066Sahrens 
2224fa9e4066Sahrens 	/*
2225fa9e4066Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
2226fa9e4066Sahrens 	 */
2227fa9e4066Sahrens 	if (spa->spa_root_vdev != NULL) {
22280373e76bSbonwick 		spa_config_enter(spa, RW_READER, FTAG);
2229fa9e4066Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
22300373e76bSbonwick 		spa_config_exit(spa, FTAG);
2231fa9e4066Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2232fa9e4066Sahrens 		    poolname) == 0);
2233fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2234fa9e4066Sahrens 		    state) == 0);
223595173954Sek 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
223695173954Sek 		    spa->spa_uberblock.ub_timestamp) == 0);
223799653d4eSeschrock 
223899653d4eSeschrock 		/*
2239fa94a07fSbrendan 		 * Add the list of hot spares and level 2 cache devices.
224099653d4eSeschrock 		 */
224199653d4eSeschrock 		spa_add_spares(spa, config);
2242fa94a07fSbrendan 		spa_add_l2cache(spa, config);
2243fa9e4066Sahrens 	}
2244fa9e4066Sahrens 
2245fa9e4066Sahrens 	spa_unload(spa);
2246fa9e4066Sahrens 	spa_deactivate(spa);
2247fa9e4066Sahrens 	spa_remove(spa);
2248fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
2249fa9e4066Sahrens 
2250fa9e4066Sahrens 	return (config);
2251fa9e4066Sahrens }
2252fa9e4066Sahrens 
2253fa9e4066Sahrens /*
2254fa9e4066Sahrens  * Pool export/destroy
2255fa9e4066Sahrens  *
2256fa9e4066Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
2257fa9e4066Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
2258fa9e4066Sahrens  * update the pool state and sync all the labels to disk, removing the
2259fa9e4066Sahrens  * configuration from the cache afterwards.
2260fa9e4066Sahrens  */
2261fa9e4066Sahrens static int
226244cd46caSbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig)
2263fa9e4066Sahrens {
2264fa9e4066Sahrens 	spa_t *spa;
2265fa9e4066Sahrens 
226644cd46caSbillm 	if (oldconfig)
226744cd46caSbillm 		*oldconfig = NULL;
226844cd46caSbillm 
2269fa9e4066Sahrens 	if (!(spa_mode & FWRITE))
2270fa9e4066Sahrens 		return (EROFS);
2271fa9e4066Sahrens 
2272fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
2273fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
2274fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
2275fa9e4066Sahrens 		return (ENOENT);
2276fa9e4066Sahrens 	}
2277fa9e4066Sahrens 
2278ea8dc4b6Seschrock 	/*
2279ea8dc4b6Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
2280ea8dc4b6Seschrock 	 * reacquire the namespace lock, and see if we can export.
2281ea8dc4b6Seschrock 	 */
2282ea8dc4b6Seschrock 	spa_open_ref(spa, FTAG);
2283ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
2284ea8dc4b6Seschrock 	spa_async_suspend(spa);
2285ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
2286ea8dc4b6Seschrock 	spa_close(spa, FTAG);
2287ea8dc4b6Seschrock 
2288fa9e4066Sahrens 	/*
2289fa9e4066Sahrens 	 * The pool will be in core if it's openable,
2290fa9e4066Sahrens 	 * in which case we can modify its state.
2291fa9e4066Sahrens 	 */
2292fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2293fa9e4066Sahrens 		/*
2294fa9e4066Sahrens 		 * Objsets may be open only because they're dirty, so we
2295fa9e4066Sahrens 		 * have to force it to sync before checking spa_refcnt.
2296fa9e4066Sahrens 		 */
2297fa9e4066Sahrens 		spa_scrub_suspend(spa);
2298fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
2299fa9e4066Sahrens 
2300ea8dc4b6Seschrock 		/*
2301ea8dc4b6Seschrock 		 * A pool cannot be exported or destroyed if there are active
2302ea8dc4b6Seschrock 		 * references.  If we are resetting a pool, allow references by
2303ea8dc4b6Seschrock 		 * fault injection handlers.
2304ea8dc4b6Seschrock 		 */
2305ea8dc4b6Seschrock 		if (!spa_refcount_zero(spa) ||
2306ea8dc4b6Seschrock 		    (spa->spa_inject_ref != 0 &&
2307ea8dc4b6Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
2308fa9e4066Sahrens 			spa_scrub_resume(spa);
2309ea8dc4b6Seschrock 			spa_async_resume(spa);
2310fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
2311fa9e4066Sahrens 			return (EBUSY);
2312fa9e4066Sahrens 		}
2313fa9e4066Sahrens 
2314fa9e4066Sahrens 		spa_scrub_resume(spa);
2315fa9e4066Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
2316fa9e4066Sahrens 
2317fa9e4066Sahrens 		/*
2318fa9e4066Sahrens 		 * We want this to be reflected on every label,
2319fa9e4066Sahrens 		 * so mark them all dirty.  spa_unload() will do the
2320fa9e4066Sahrens 		 * final sync that pushes these changes out.
2321fa9e4066Sahrens 		 */
2322ea8dc4b6Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
23235dabedeeSbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
2324ea8dc4b6Seschrock 			spa->spa_state = new_state;
23250373e76bSbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
2326ea8dc4b6Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
23275dabedeeSbonwick 			spa_config_exit(spa, FTAG);
2328ea8dc4b6Seschrock 		}
2329fa9e4066Sahrens 	}
2330fa9e4066Sahrens 
23313d7072f8Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
23323d7072f8Seschrock 
2333fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2334fa9e4066Sahrens 		spa_unload(spa);
2335fa9e4066Sahrens 		spa_deactivate(spa);
2336fa9e4066Sahrens 	}
2337fa9e4066Sahrens 
233844cd46caSbillm 	if (oldconfig && spa->spa_config)
233944cd46caSbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
234044cd46caSbillm 
2341ea8dc4b6Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
23422f8aaab3Seschrock 		spa_config_check(spa->spa_config_dir,
23432f8aaab3Seschrock 		    spa->spa_config_file);
2344ea8dc4b6Seschrock 		spa_remove(spa);
2345ea8dc4b6Seschrock 		spa_config_sync();
2346ea8dc4b6Seschrock 	}
2347fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
2348fa9e4066Sahrens 
2349fa9e4066Sahrens 	return (0);
2350fa9e4066Sahrens }
2351fa9e4066Sahrens 
2352fa9e4066Sahrens /*
2353fa9e4066Sahrens  * Destroy a storage pool.
2354fa9e4066Sahrens  */
2355fa9e4066Sahrens int
2356fa9e4066Sahrens spa_destroy(char *pool)
2357fa9e4066Sahrens {
235844cd46caSbillm 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL));
2359fa9e4066Sahrens }
2360fa9e4066Sahrens 
2361fa9e4066Sahrens /*
2362fa9e4066Sahrens  * Export a storage pool.
2363fa9e4066Sahrens  */
2364fa9e4066Sahrens int
236544cd46caSbillm spa_export(char *pool, nvlist_t **oldconfig)
2366fa9e4066Sahrens {
236744cd46caSbillm 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig));
2368fa9e4066Sahrens }
2369fa9e4066Sahrens 
2370ea8dc4b6Seschrock /*
2371ea8dc4b6Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
2372ea8dc4b6Seschrock  * from the namespace in any way.
2373ea8dc4b6Seschrock  */
2374ea8dc4b6Seschrock int
2375ea8dc4b6Seschrock spa_reset(char *pool)
2376ea8dc4b6Seschrock {
237744cd46caSbillm 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL));
2378ea8dc4b6Seschrock }
2379ea8dc4b6Seschrock 
2380ea8dc4b6Seschrock 
2381fa9e4066Sahrens /*
2382fa9e4066Sahrens  * ==========================================================================
2383fa9e4066Sahrens  * Device manipulation
2384fa9e4066Sahrens  * ==========================================================================
2385fa9e4066Sahrens  */
2386fa9e4066Sahrens 
2387fa9e4066Sahrens /*
23888654d025Sperrin  * Add a device to a storage pool.
2389fa9e4066Sahrens  */
2390fa9e4066Sahrens int
2391fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2392fa9e4066Sahrens {
2393fa9e4066Sahrens 	uint64_t txg;
23940373e76bSbonwick 	int c, error;
2395fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
23960e34b6a7Sbonwick 	vdev_t *vd, *tvd;
2397fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
2398fa94a07fSbrendan 	uint_t nspares, nl2cache;
2399fa9e4066Sahrens 
2400fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
2401fa9e4066Sahrens 
240299653d4eSeschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
240399653d4eSeschrock 	    VDEV_ALLOC_ADD)) != 0)
240499653d4eSeschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
2405fa9e4066Sahrens 
240639c23413Seschrock 	spa->spa_pending_vdev = vd;
240799653d4eSeschrock 
2408fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
2409fa94a07fSbrendan 	    &nspares) != 0)
241099653d4eSeschrock 		nspares = 0;
241199653d4eSeschrock 
2412fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
2413fa94a07fSbrendan 	    &nl2cache) != 0)
2414fa94a07fSbrendan 		nl2cache = 0;
2415fa94a07fSbrendan 
2416fa94a07fSbrendan 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) {
241739c23413Seschrock 		spa->spa_pending_vdev = NULL;
2418fa9e4066Sahrens 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
241939c23413Seschrock 	}
2420fa9e4066Sahrens 
242199653d4eSeschrock 	if (vd->vdev_children != 0) {
242239c23413Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
242339c23413Seschrock 			spa->spa_pending_vdev = NULL;
242499653d4eSeschrock 			return (spa_vdev_exit(spa, vd, txg, error));
242599653d4eSeschrock 		}
242699653d4eSeschrock 	}
242799653d4eSeschrock 
242839c23413Seschrock 	/*
2429fa94a07fSbrendan 	 * We must validate the spares and l2cache devices after checking the
2430fa94a07fSbrendan 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
243139c23413Seschrock 	 */
2432fa94a07fSbrendan 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) {
243339c23413Seschrock 		spa->spa_pending_vdev = NULL;
243439c23413Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
243539c23413Seschrock 	}
243639c23413Seschrock 
243739c23413Seschrock 	spa->spa_pending_vdev = NULL;
243839c23413Seschrock 
243939c23413Seschrock 	/*
244039c23413Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
244139c23413Seschrock 	 */
244239c23413Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
244339c23413Seschrock 		tvd = vd->vdev_child[c];
244439c23413Seschrock 		vdev_remove_child(vd, tvd);
244539c23413Seschrock 		tvd->vdev_id = rvd->vdev_children;
244639c23413Seschrock 		vdev_add_child(rvd, tvd);
244739c23413Seschrock 		vdev_config_dirty(tvd);
244839c23413Seschrock 	}
244939c23413Seschrock 
245099653d4eSeschrock 	if (nspares != 0) {
2451fa94a07fSbrendan 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
2452fa94a07fSbrendan 		    ZPOOL_CONFIG_SPARES);
245399653d4eSeschrock 		spa_load_spares(spa);
2454fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
2455fa94a07fSbrendan 	}
2456fa94a07fSbrendan 
2457fa94a07fSbrendan 	if (nl2cache != 0) {
2458fa94a07fSbrendan 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
2459fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE);
2460fa94a07fSbrendan 		spa_load_l2cache(spa);
2461fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
2462fa9e4066Sahrens 	}
2463fa9e4066Sahrens 
2464fa9e4066Sahrens 	/*
24650e34b6a7Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
24660e34b6a7Sbonwick 	 * If other threads start allocating from these vdevs before we
24670e34b6a7Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
24680e34b6a7Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
24690e34b6a7Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
24700e34b6a7Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
24710373e76bSbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
24720e34b6a7Sbonwick 	 *
24730e34b6a7Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
24740e34b6a7Sbonwick 	 * if we lose power at any point in this sequence, the remaining
24750e34b6a7Sbonwick 	 * steps will be completed the next time we load the pool.
24760e34b6a7Sbonwick 	 */
24770373e76bSbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
24780e34b6a7Sbonwick 
24790373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
24800373e76bSbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
24810373e76bSbonwick 	mutex_exit(&spa_namespace_lock);
2482fa9e4066Sahrens 
24830373e76bSbonwick 	return (0);
2484fa9e4066Sahrens }
2485fa9e4066Sahrens 
2486fa9e4066Sahrens /*
2487fa9e4066Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2488fa9e4066Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2489fa9e4066Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2490fa9e4066Sahrens  *
2491fa9e4066Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2492fa9e4066Sahrens  * existing device; in this case the two devices are made into their own
24933d7072f8Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2494fa9e4066Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2495fa9e4066Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2496fa9e4066Sahrens  * completion of resilvering, the first disk (the one being replaced)
2497fa9e4066Sahrens  * is automatically detached.
2498fa9e4066Sahrens  */
2499fa9e4066Sahrens int
2500ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2501fa9e4066Sahrens {
2502fa9e4066Sahrens 	uint64_t txg, open_txg;
2503fa9e4066Sahrens 	int error;
2504fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2505fa9e4066Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
250699653d4eSeschrock 	vdev_ops_t *pvops;
25078654d025Sperrin 	int is_log;
2508fa9e4066Sahrens 
2509fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
2510fa9e4066Sahrens 
2511ea8dc4b6Seschrock 	oldvd = vdev_lookup_by_guid(rvd, guid);
2512fa9e4066Sahrens 
2513fa9e4066Sahrens 	if (oldvd == NULL)
2514fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2515fa9e4066Sahrens 
25160e34b6a7Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
25170e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
25180e34b6a7Sbonwick 
2519fa9e4066Sahrens 	pvd = oldvd->vdev_parent;
2520fa9e4066Sahrens 
252199653d4eSeschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
25223d7072f8Seschrock 	    VDEV_ALLOC_ADD)) != 0)
25233d7072f8Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
25243d7072f8Seschrock 
25253d7072f8Seschrock 	if (newrootvd->vdev_children != 1)
2526fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2527fa9e4066Sahrens 
2528fa9e4066Sahrens 	newvd = newrootvd->vdev_child[0];
2529fa9e4066Sahrens 
2530fa9e4066Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2531fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2532fa9e4066Sahrens 
253399653d4eSeschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2534fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2535fa9e4066Sahrens 
25368654d025Sperrin 	/*
25378654d025Sperrin 	 * Spares can't replace logs
25388654d025Sperrin 	 */
25398654d025Sperrin 	is_log = oldvd->vdev_islog;
25408654d025Sperrin 	if (is_log && newvd->vdev_isspare)
25418654d025Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
25428654d025Sperrin 
254399653d4eSeschrock 	if (!replacing) {
254499653d4eSeschrock 		/*
254599653d4eSeschrock 		 * For attach, the only allowable parent is a mirror or the root
254699653d4eSeschrock 		 * vdev.
254799653d4eSeschrock 		 */
254899653d4eSeschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
254999653d4eSeschrock 		    pvd->vdev_ops != &vdev_root_ops)
255099653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
255199653d4eSeschrock 
255299653d4eSeschrock 		pvops = &vdev_mirror_ops;
255399653d4eSeschrock 	} else {
255499653d4eSeschrock 		/*
255599653d4eSeschrock 		 * Active hot spares can only be replaced by inactive hot
255699653d4eSeschrock 		 * spares.
255799653d4eSeschrock 		 */
255899653d4eSeschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
255999653d4eSeschrock 		    pvd->vdev_child[1] == oldvd &&
256099653d4eSeschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
256199653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
256299653d4eSeschrock 
256399653d4eSeschrock 		/*
256499653d4eSeschrock 		 * If the source is a hot spare, and the parent isn't already a
256599653d4eSeschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
256639c23413Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
256739c23413Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
256839c23413Seschrock 		 * the same (spare replaces spare, non-spare replaces
256939c23413Seschrock 		 * non-spare).
257099653d4eSeschrock 		 */
257199653d4eSeschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
257299653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
257339c23413Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
257439c23413Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
257539c23413Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
257699653d4eSeschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
257799653d4eSeschrock 		    newvd->vdev_isspare)
257899653d4eSeschrock 			pvops = &vdev_spare_ops;
257999653d4eSeschrock 		else
258099653d4eSeschrock 			pvops = &vdev_replacing_ops;
258199653d4eSeschrock 	}
258299653d4eSeschrock 
25832a79c5feSlling 	/*
25842a79c5feSlling 	 * Compare the new device size with the replaceable/attachable
25852a79c5feSlling 	 * device size.
25862a79c5feSlling 	 */
25872a79c5feSlling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2588fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2589fa9e4066Sahrens 
2590ecc2d604Sbonwick 	/*
2591ecc2d604Sbonwick 	 * The new device cannot have a higher alignment requirement
2592ecc2d604Sbonwick 	 * than the top-level vdev.
2593ecc2d604Sbonwick 	 */
2594ecc2d604Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2595fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2596fa9e4066Sahrens 
2597fa9e4066Sahrens 	/*
2598fa9e4066Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2599fa9e4066Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2600fa9e4066Sahrens 	 */
2601fa9e4066Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2602fa9e4066Sahrens 		spa_strfree(oldvd->vdev_path);
2603fa9e4066Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2604fa9e4066Sahrens 		    KM_SLEEP);
2605fa9e4066Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2606fa9e4066Sahrens 		    newvd->vdev_path, "old");
2607fa9e4066Sahrens 		if (oldvd->vdev_devid != NULL) {
2608fa9e4066Sahrens 			spa_strfree(oldvd->vdev_devid);
2609fa9e4066Sahrens 			oldvd->vdev_devid = NULL;
2610fa9e4066Sahrens 		}
2611fa9e4066Sahrens 	}
2612fa9e4066Sahrens 
2613fa9e4066Sahrens 	/*
261499653d4eSeschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
261599653d4eSeschrock 	 * mirror/replacing/spare vdev above oldvd.
2616fa9e4066Sahrens 	 */
2617fa9e4066Sahrens 	if (pvd->vdev_ops != pvops)
2618fa9e4066Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2619fa9e4066Sahrens 
2620fa9e4066Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2621fa9e4066Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2622fa9e4066Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2623fa9e4066Sahrens 
2624fa9e4066Sahrens 	/*
2625fa9e4066Sahrens 	 * Extract the new device from its root and add it to pvd.
2626fa9e4066Sahrens 	 */
2627fa9e4066Sahrens 	vdev_remove_child(newrootvd, newvd);
2628fa9e4066Sahrens 	newvd->vdev_id = pvd->vdev_children;
2629fa9e4066Sahrens 	vdev_add_child(pvd, newvd);
2630fa9e4066Sahrens 
2631ea8dc4b6Seschrock 	/*
2632ea8dc4b6Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
2633ea8dc4b6Seschrock 	 * the addition of newvd may have decreased our parent's asize.
2634ea8dc4b6Seschrock 	 */
2635ea8dc4b6Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
2636ea8dc4b6Seschrock 
2637fa9e4066Sahrens 	tvd = newvd->vdev_top;
2638fa9e4066Sahrens 	ASSERT(pvd->vdev_top == tvd);
2639fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2640fa9e4066Sahrens 
2641fa9e4066Sahrens 	vdev_config_dirty(tvd);
2642fa9e4066Sahrens 
2643fa9e4066Sahrens 	/*
2644fa9e4066Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2645fa9e4066Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2646fa9e4066Sahrens 	 */
2647fa9e4066Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2648fa9e4066Sahrens 
2649fa9e4066Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2650fa9e4066Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2651fa9e4066Sahrens 	    open_txg - TXG_INITIAL + 1);
2652fa9e4066Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2653fa9e4066Sahrens 
265439c23413Seschrock 	if (newvd->vdev_isspare)
265539c23413Seschrock 		spa_spare_activate(newvd);
2656ea8dc4b6Seschrock 
2657fa9e4066Sahrens 	/*
2658fa9e4066Sahrens 	 * Mark newvd's DTL dirty in this txg.
2659fa9e4066Sahrens 	 */
2660ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2661fa9e4066Sahrens 
2662fa9e4066Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2663fa9e4066Sahrens 
2664fa9e4066Sahrens 	/*
26653d7072f8Seschrock 	 * Kick off a resilver to update newvd.  We need to grab the namespace
26663d7072f8Seschrock 	 * lock because spa_scrub() needs to post a sysevent with the pool name.
2667fa9e4066Sahrens 	 */
26683d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
2669fa9e4066Sahrens 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
26703d7072f8Seschrock 	mutex_exit(&spa_namespace_lock);
2671fa9e4066Sahrens 
2672fa9e4066Sahrens 	return (0);
2673fa9e4066Sahrens }
2674fa9e4066Sahrens 
2675fa9e4066Sahrens /*
2676fa9e4066Sahrens  * Detach a device from a mirror or replacing vdev.
2677fa9e4066Sahrens  * If 'replace_done' is specified, only detach if the parent
2678fa9e4066Sahrens  * is a replacing vdev.
2679fa9e4066Sahrens  */
2680fa9e4066Sahrens int
2681ea8dc4b6Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2682fa9e4066Sahrens {
2683fa9e4066Sahrens 	uint64_t txg;
2684fa9e4066Sahrens 	int c, t, error;
2685fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2686fa9e4066Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
268799653d4eSeschrock 	boolean_t unspare = B_FALSE;
268899653d4eSeschrock 	uint64_t unspare_guid;
2689fa9e4066Sahrens 
2690fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
2691fa9e4066Sahrens 
2692ea8dc4b6Seschrock 	vd = vdev_lookup_by_guid(rvd, guid);
2693fa9e4066Sahrens 
2694fa9e4066Sahrens 	if (vd == NULL)
2695fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2696fa9e4066Sahrens 
26970e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
26980e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
26990e34b6a7Sbonwick 
2700fa9e4066Sahrens 	pvd = vd->vdev_parent;
2701fa9e4066Sahrens 
2702fa9e4066Sahrens 	/*
2703fa9e4066Sahrens 	 * If replace_done is specified, only remove this device if it's
270499653d4eSeschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
270599653d4eSeschrock 	 * disk can be removed.
270699653d4eSeschrock 	 */
270799653d4eSeschrock 	if (replace_done) {
270899653d4eSeschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
270999653d4eSeschrock 			if (vd->vdev_id != 0)
271099653d4eSeschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
271199653d4eSeschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
271299653d4eSeschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
271399653d4eSeschrock 		}
271499653d4eSeschrock 	}
271599653d4eSeschrock 
271699653d4eSeschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
2717e7437265Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
2718fa9e4066Sahrens 
2719fa9e4066Sahrens 	/*
272099653d4eSeschrock 	 * Only mirror, replacing, and spare vdevs support detach.
2721fa9e4066Sahrens 	 */
2722fa9e4066Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
272399653d4eSeschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
272499653d4eSeschrock 	    pvd->vdev_ops != &vdev_spare_ops)
2725fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2726fa9e4066Sahrens 
2727fa9e4066Sahrens 	/*
2728fa9e4066Sahrens 	 * If there's only one replica, you can't detach it.
2729fa9e4066Sahrens 	 */
2730fa9e4066Sahrens 	if (pvd->vdev_children <= 1)
2731fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2732fa9e4066Sahrens 
2733fa9e4066Sahrens 	/*
2734fa9e4066Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
2735fa9e4066Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
2736fa9e4066Sahrens 	 *
2737fa9e4066Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
2738fa9e4066Sahrens 	 * precise DTL check.
2739fa9e4066Sahrens 	 */
2740fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
2741fa9e4066Sahrens 		uint64_t dirty;
2742fa9e4066Sahrens 
2743fa9e4066Sahrens 		cvd = pvd->vdev_child[c];
2744fa9e4066Sahrens 		if (cvd == vd)
2745fa9e4066Sahrens 			continue;
2746fa9e4066Sahrens 		if (vdev_is_dead(cvd))
2747fa9e4066Sahrens 			continue;
2748fa9e4066Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
2749fa9e4066Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
2750fa9e4066Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
2751fa9e4066Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
2752fa9e4066Sahrens 		if (!dirty)
2753fa9e4066Sahrens 			break;
2754fa9e4066Sahrens 	}
275599653d4eSeschrock 
275699653d4eSeschrock 	/*
275799653d4eSeschrock 	 * If we are a replacing or spare vdev, then we can always detach the
275899653d4eSeschrock 	 * latter child, as that is how one cancels the operation.
275999653d4eSeschrock 	 */
276099653d4eSeschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
276199653d4eSeschrock 	    c == pvd->vdev_children)
2762fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2763fa9e4066Sahrens 
276499653d4eSeschrock 	/*
276599653d4eSeschrock 	 * If we are detaching the original disk from a spare, then it implies
276699653d4eSeschrock 	 * that the spare should become a real disk, and be removed from the
276799653d4eSeschrock 	 * active spare list for the pool.
276899653d4eSeschrock 	 */
276999653d4eSeschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
277099653d4eSeschrock 	    vd->vdev_id == 0)
277199653d4eSeschrock 		unspare = B_TRUE;
277299653d4eSeschrock 
2773fa9e4066Sahrens 	/*
2774fa9e4066Sahrens 	 * Erase the disk labels so the disk can be used for other things.
2775fa9e4066Sahrens 	 * This must be done after all other error cases are handled,
2776fa9e4066Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
2777fa9e4066Sahrens 	 * But if we can't do it, don't treat the error as fatal --
2778fa9e4066Sahrens 	 * it may be that the unwritability of the disk is the reason
2779fa9e4066Sahrens 	 * it's being detached!
2780fa9e4066Sahrens 	 */
278139c23413Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
2782fa9e4066Sahrens 
2783fa9e4066Sahrens 	/*
2784fa9e4066Sahrens 	 * Remove vd from its parent and compact the parent's children.
2785fa9e4066Sahrens 	 */
2786fa9e4066Sahrens 	vdev_remove_child(pvd, vd);
2787fa9e4066Sahrens 	vdev_compact_children(pvd);
2788fa9e4066Sahrens 
2789fa9e4066Sahrens 	/*
2790fa9e4066Sahrens 	 * Remember one of the remaining children so we can get tvd below.
2791fa9e4066Sahrens 	 */
2792fa9e4066Sahrens 	cvd = pvd->vdev_child[0];
2793fa9e4066Sahrens 
279499653d4eSeschrock 	/*
279599653d4eSeschrock 	 * If we need to remove the remaining child from the list of hot spares,
279699653d4eSeschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
279799653d4eSeschrock 	 * must do this before vdev_remove_parent(), because that can change the
279899653d4eSeschrock 	 * GUID if it creates a new toplevel GUID.
279999653d4eSeschrock 	 */
280099653d4eSeschrock 	if (unspare) {
280199653d4eSeschrock 		ASSERT(cvd->vdev_isspare);
280239c23413Seschrock 		spa_spare_remove(cvd);
280399653d4eSeschrock 		unspare_guid = cvd->vdev_guid;
280499653d4eSeschrock 	}
280599653d4eSeschrock 
2806fa9e4066Sahrens 	/*
2807fa9e4066Sahrens 	 * If the parent mirror/replacing vdev only has one child,
2808fa9e4066Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
2809fa9e4066Sahrens 	 */
2810fa9e4066Sahrens 	if (pvd->vdev_children == 1)
2811fa9e4066Sahrens 		vdev_remove_parent(cvd);
2812fa9e4066Sahrens 
2813fa9e4066Sahrens 	/*
2814fa9e4066Sahrens 	 * We don't set tvd until now because the parent we just removed
2815fa9e4066Sahrens 	 * may have been the previous top-level vdev.
2816fa9e4066Sahrens 	 */
2817fa9e4066Sahrens 	tvd = cvd->vdev_top;
2818fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2819fa9e4066Sahrens 
2820fa9e4066Sahrens 	/*
282139c23413Seschrock 	 * Reevaluate the parent vdev state.
2822fa9e4066Sahrens 	 */
28233d7072f8Seschrock 	vdev_propagate_state(cvd);
2824fa9e4066Sahrens 
2825fa9e4066Sahrens 	/*
282639c23413Seschrock 	 * If the device we just detached was smaller than the others, it may be
282739c23413Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
282839c23413Seschrock 	 * can't fail because the existing metaslabs are already in core, so
282939c23413Seschrock 	 * there's nothing to read from disk.
2830fa9e4066Sahrens 	 */
2831ecc2d604Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
2832fa9e4066Sahrens 
2833fa9e4066Sahrens 	vdev_config_dirty(tvd);
2834fa9e4066Sahrens 
2835fa9e4066Sahrens 	/*
283639c23413Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
283739c23413Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
283839c23413Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
283939c23413Seschrock 	 * prevent vd from being accessed after it's freed.
2840fa9e4066Sahrens 	 */
2841fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++)
2842fa9e4066Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
2843ecc2d604Sbonwick 	vd->vdev_detached = B_TRUE;
2844ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
2845fa9e4066Sahrens 
28463d7072f8Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
28473d7072f8Seschrock 
284899653d4eSeschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
284999653d4eSeschrock 
285099653d4eSeschrock 	/*
285139c23413Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
285239c23413Seschrock 	 * then we want to go through and remove the device from the hot spare
285339c23413Seschrock 	 * list of every other pool.
285499653d4eSeschrock 	 */
285599653d4eSeschrock 	if (unspare) {
285699653d4eSeschrock 		spa = NULL;
285799653d4eSeschrock 		mutex_enter(&spa_namespace_lock);
285899653d4eSeschrock 		while ((spa = spa_next(spa)) != NULL) {
285999653d4eSeschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
286099653d4eSeschrock 				continue;
286199653d4eSeschrock 
286299653d4eSeschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
286399653d4eSeschrock 		}
286499653d4eSeschrock 		mutex_exit(&spa_namespace_lock);
286599653d4eSeschrock 	}
286699653d4eSeschrock 
286799653d4eSeschrock 	return (error);
286899653d4eSeschrock }
286999653d4eSeschrock 
287099653d4eSeschrock /*
2871fa94a07fSbrendan  * Remove a spares vdev from the nvlist config.
287299653d4eSeschrock  */
2873fa94a07fSbrendan static int
2874fa94a07fSbrendan spa_remove_spares(spa_aux_vdev_t *sav, uint64_t guid, boolean_t unspare,
2875fa94a07fSbrendan     nvlist_t **spares, int nspares, vdev_t *vd)
287699653d4eSeschrock {
2877fa94a07fSbrendan 	nvlist_t *nv, **newspares;
2878fa94a07fSbrendan 	int i, j;
287999653d4eSeschrock 
288099653d4eSeschrock 	nv = NULL;
2881fa94a07fSbrendan 	for (i = 0; i < nspares; i++) {
2882fa94a07fSbrendan 		uint64_t theguid;
288399653d4eSeschrock 
2884fa94a07fSbrendan 		VERIFY(nvlist_lookup_uint64(spares[i],
2885fa94a07fSbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
2886fa94a07fSbrendan 		if (theguid == guid) {
2887fa94a07fSbrendan 			nv = spares[i];
2888fa94a07fSbrendan 			break;
288999653d4eSeschrock 		}
289099653d4eSeschrock 	}
289199653d4eSeschrock 
289299653d4eSeschrock 	/*
2893fa94a07fSbrendan 	 * Only remove the hot spare if it's not currently in use in this pool.
289499653d4eSeschrock 	 */
2895fa94a07fSbrendan 	if (nv == NULL && vd == NULL)
2896fa94a07fSbrendan 		return (ENOENT);
289799653d4eSeschrock 
2898fa94a07fSbrendan 	if (nv == NULL && vd != NULL)
2899fa94a07fSbrendan 		return (ENOTSUP);
290099653d4eSeschrock 
2901fa94a07fSbrendan 	if (!unspare && nv != NULL && vd != NULL)
2902fa94a07fSbrendan 		return (EBUSY);
290399653d4eSeschrock 
290499653d4eSeschrock 	if (nspares == 1) {
290599653d4eSeschrock 		newspares = NULL;
290699653d4eSeschrock 	} else {
290799653d4eSeschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
290899653d4eSeschrock 		    KM_SLEEP);
290999653d4eSeschrock 		for (i = 0, j = 0; i < nspares; i++) {
291099653d4eSeschrock 			if (spares[i] != nv)
291199653d4eSeschrock 				VERIFY(nvlist_dup(spares[i],
291299653d4eSeschrock 				    &newspares[j++], KM_SLEEP) == 0);
291399653d4eSeschrock 		}
291499653d4eSeschrock 	}
291599653d4eSeschrock 
2916fa94a07fSbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_SPARES,
291799653d4eSeschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
2918fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2919fa94a07fSbrendan 	    ZPOOL_CONFIG_SPARES, newspares, nspares - 1) == 0);
292099653d4eSeschrock 	for (i = 0; i < nspares - 1; i++)
292199653d4eSeschrock 		nvlist_free(newspares[i]);
292299653d4eSeschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
2923fa94a07fSbrendan 
2924fa94a07fSbrendan 	return (0);
2925fa94a07fSbrendan }
2926fa94a07fSbrendan 
2927fa94a07fSbrendan /*
2928fa94a07fSbrendan  * Remove an l2cache vdev from the nvlist config.
2929fa94a07fSbrendan  */
2930fa94a07fSbrendan static int
2931fa94a07fSbrendan spa_remove_l2cache(spa_aux_vdev_t *sav, uint64_t guid, nvlist_t **l2cache,
2932fa94a07fSbrendan     int nl2cache, vdev_t *vd)
2933fa94a07fSbrendan {
2934fa94a07fSbrendan 	nvlist_t *nv, **newl2cache;
2935fa94a07fSbrendan 	int i, j;
2936fa94a07fSbrendan 
2937fa94a07fSbrendan 	nv = NULL;
2938fa94a07fSbrendan 	for (i = 0; i < nl2cache; i++) {
2939fa94a07fSbrendan 		uint64_t theguid;
2940fa94a07fSbrendan 
2941fa94a07fSbrendan 		VERIFY(nvlist_lookup_uint64(l2cache[i],
2942fa94a07fSbrendan 		    ZPOOL_CONFIG_GUID, &theguid) == 0);
2943fa94a07fSbrendan 		if (theguid == guid) {
2944fa94a07fSbrendan 			nv = l2cache[i];
2945fa94a07fSbrendan 			break;
2946fa94a07fSbrendan 		}
2947fa94a07fSbrendan 	}
2948fa94a07fSbrendan 
2949fa94a07fSbrendan 	if (vd == NULL) {
2950fa94a07fSbrendan 		for (i = 0; i < nl2cache; i++) {
2951fa94a07fSbrendan 			if (sav->sav_vdevs[i]->vdev_guid == guid) {
2952fa94a07fSbrendan 				vd = sav->sav_vdevs[i];
2953fa94a07fSbrendan 				break;
2954fa94a07fSbrendan 			}
2955fa94a07fSbrendan 		}
2956fa94a07fSbrendan 	}
2957fa94a07fSbrendan 
2958fa94a07fSbrendan 	if (nv == NULL && vd == NULL)
2959fa94a07fSbrendan 		return (ENOENT);
2960fa94a07fSbrendan 
2961fa94a07fSbrendan 	if (nv == NULL && vd != NULL)
2962fa94a07fSbrendan 		return (ENOTSUP);
2963fa94a07fSbrendan 
2964fa94a07fSbrendan 	if (nl2cache == 1) {
2965fa94a07fSbrendan 		newl2cache = NULL;
2966fa94a07fSbrendan 	} else {
2967fa94a07fSbrendan 		newl2cache = kmem_alloc((nl2cache - 1) * sizeof (void *),
2968fa94a07fSbrendan 		    KM_SLEEP);
2969fa94a07fSbrendan 		for (i = 0, j = 0; i < nl2cache; i++) {
2970fa94a07fSbrendan 			if (l2cache[i] != nv)
2971fa94a07fSbrendan 				VERIFY(nvlist_dup(l2cache[i],
2972fa94a07fSbrendan 				    &newl2cache[j++], KM_SLEEP) == 0);
2973fa94a07fSbrendan 		}
2974fa94a07fSbrendan 	}
2975fa94a07fSbrendan 
2976fa94a07fSbrendan 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
2977fa94a07fSbrendan 	    DATA_TYPE_NVLIST_ARRAY) == 0);
2978fa94a07fSbrendan 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2979fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, newl2cache, nl2cache - 1) == 0);
2980fa94a07fSbrendan 	for (i = 0; i < nl2cache - 1; i++)
2981fa94a07fSbrendan 		nvlist_free(newl2cache[i]);
2982fa94a07fSbrendan 	kmem_free(newl2cache, (nl2cache - 1) * sizeof (void *));
2983fa94a07fSbrendan 
2984fa94a07fSbrendan 	return (0);
2985fa94a07fSbrendan }
2986fa94a07fSbrendan 
2987fa94a07fSbrendan /*
2988fa94a07fSbrendan  * Remove a device from the pool.  Currently, this supports removing only hot
2989fa94a07fSbrendan  * spares and level 2 ARC devices.
2990fa94a07fSbrendan  */
2991fa94a07fSbrendan int
2992fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
2993fa94a07fSbrendan {
2994fa94a07fSbrendan 	vdev_t *vd;
2995fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
2996fa94a07fSbrendan 	uint_t nspares, nl2cache;
2997fa94a07fSbrendan 	int error = 0;
2998fa94a07fSbrendan 
2999fa94a07fSbrendan 	spa_config_enter(spa, RW_WRITER, FTAG);
3000fa94a07fSbrendan 
3001fa94a07fSbrendan 	vd = spa_lookup_by_guid(spa, guid);
3002fa94a07fSbrendan 
3003fa94a07fSbrendan 	if (spa->spa_spares.sav_vdevs != NULL &&
3004fa94a07fSbrendan 	    spa_spare_exists(guid, NULL) &&
3005fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3006fa94a07fSbrendan 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
3007fa94a07fSbrendan 		if ((error = spa_remove_spares(&spa->spa_spares, guid, unspare,
3008fa94a07fSbrendan 		    spares, nspares, vd)) != 0)
3009fa94a07fSbrendan 			goto out;
3010fa94a07fSbrendan 		spa_load_spares(spa);
3011fa94a07fSbrendan 		spa->spa_spares.sav_sync = B_TRUE;
3012fa94a07fSbrendan 		goto out;
3013fa94a07fSbrendan 	}
3014fa94a07fSbrendan 
3015fa94a07fSbrendan 	if (spa->spa_l2cache.sav_vdevs != NULL &&
3016fa94a07fSbrendan 	    spa_l2cache_exists(guid, NULL) &&
3017fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3018fa94a07fSbrendan 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0) {
3019fa94a07fSbrendan 		if ((error = spa_remove_l2cache(&spa->spa_l2cache, guid,
3020fa94a07fSbrendan 		    l2cache, nl2cache, vd)) != 0)
3021fa94a07fSbrendan 			goto out;
3022fa94a07fSbrendan 		spa_load_l2cache(spa);
3023fa94a07fSbrendan 		spa->spa_l2cache.sav_sync = B_TRUE;
3024fa94a07fSbrendan 	}
302599653d4eSeschrock 
302699653d4eSeschrock out:
302799653d4eSeschrock 	spa_config_exit(spa, FTAG);
3028fa94a07fSbrendan 	return (error);
3029fa9e4066Sahrens }
3030fa9e4066Sahrens 
3031fa9e4066Sahrens /*
30323d7072f8Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
30333d7072f8Seschrock  * current spared, so we can detach it.
3034fa9e4066Sahrens  */
3035ea8dc4b6Seschrock static vdev_t *
30363d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
3037fa9e4066Sahrens {
3038ea8dc4b6Seschrock 	vdev_t *newvd, *oldvd;
3039fa9e4066Sahrens 	int c;
3040fa9e4066Sahrens 
3041ea8dc4b6Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
30423d7072f8Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
3043ea8dc4b6Seschrock 		if (oldvd != NULL)
3044ea8dc4b6Seschrock 			return (oldvd);
3045ea8dc4b6Seschrock 	}
3046fa9e4066Sahrens 
30473d7072f8Seschrock 	/*
30483d7072f8Seschrock 	 * Check for a completed replacement.
30493d7072f8Seschrock 	 */
3050fa9e4066Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
3051ea8dc4b6Seschrock 		oldvd = vd->vdev_child[0];
3052ea8dc4b6Seschrock 		newvd = vd->vdev_child[1];
3053ea8dc4b6Seschrock 
3054ea8dc4b6Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
3055ea8dc4b6Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
3056ea8dc4b6Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
3057ea8dc4b6Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
3058ea8dc4b6Seschrock 			return (oldvd);
3059fa9e4066Sahrens 		}
3060ea8dc4b6Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
3061fa9e4066Sahrens 	}
3062ea8dc4b6Seschrock 
30633d7072f8Seschrock 	/*
30643d7072f8Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
30653d7072f8Seschrock 	 */
30663d7072f8Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
30673d7072f8Seschrock 		newvd = vd->vdev_child[0];
30683d7072f8Seschrock 		oldvd = vd->vdev_child[1];
30693d7072f8Seschrock 
30703d7072f8Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
30713d7072f8Seschrock 		if (newvd->vdev_unspare &&
30723d7072f8Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
30733d7072f8Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
30743d7072f8Seschrock 			newvd->vdev_unspare = 0;
30753d7072f8Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
30763d7072f8Seschrock 			return (oldvd);
30773d7072f8Seschrock 		}
30783d7072f8Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
30793d7072f8Seschrock 	}
30803d7072f8Seschrock 
3081ea8dc4b6Seschrock 	return (NULL);
3082fa9e4066Sahrens }
3083fa9e4066Sahrens 
3084ea8dc4b6Seschrock static void
30853d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa)
3086fa9e4066Sahrens {
3087ea8dc4b6Seschrock 	vdev_t *vd;
308899653d4eSeschrock 	vdev_t *pvd;
3089ea8dc4b6Seschrock 	uint64_t guid;
309099653d4eSeschrock 	uint64_t pguid = 0;
3091ea8dc4b6Seschrock 
3092ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3093ea8dc4b6Seschrock 
30943d7072f8Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
3095ea8dc4b6Seschrock 		guid = vd->vdev_guid;
309699653d4eSeschrock 		/*
309799653d4eSeschrock 		 * If we have just finished replacing a hot spared device, then
309899653d4eSeschrock 		 * we need to detach the parent's first child (the original hot
309999653d4eSeschrock 		 * spare) as well.
310099653d4eSeschrock 		 */
310199653d4eSeschrock 		pvd = vd->vdev_parent;
310299653d4eSeschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
310399653d4eSeschrock 		    pvd->vdev_id == 0) {
310499653d4eSeschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
310599653d4eSeschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
310699653d4eSeschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
310799653d4eSeschrock 		}
3108ea8dc4b6Seschrock 		spa_config_exit(spa, FTAG);
3109ea8dc4b6Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
3110ea8dc4b6Seschrock 			return;
311199653d4eSeschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
311299653d4eSeschrock 			return;
3113ea8dc4b6Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
3114fa9e4066Sahrens 	}
3115fa9e4066Sahrens 
3116ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
3117fa9e4066Sahrens }
3118fa9e4066Sahrens 
3119c67d9675Seschrock /*
3120c67d9675Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
3121c67d9675Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
3122c67d9675Seschrock  */
3123c67d9675Seschrock int
3124c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
3125c67d9675Seschrock {
3126c67d9675Seschrock 	vdev_t *rvd, *vd;
3127c67d9675Seschrock 	uint64_t txg;
3128c67d9675Seschrock 
3129c67d9675Seschrock 	rvd = spa->spa_root_vdev;
3130c67d9675Seschrock 
3131c67d9675Seschrock 	txg = spa_vdev_enter(spa);
3132c67d9675Seschrock 
313399653d4eSeschrock 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
313499653d4eSeschrock 		/*
3135fa94a07fSbrendan 		 * Determine if this is a reference to a hot spare or l2cache
3136fa94a07fSbrendan 		 * device.  If it is, update the path as stored in their
3137fa94a07fSbrendan 		 * device list.
313899653d4eSeschrock 		 */
3139fa94a07fSbrendan 		nvlist_t **spares, **l2cache;
3140fa94a07fSbrendan 		uint_t i, nspares, nl2cache;
3141fa94a07fSbrendan 
3142fa94a07fSbrendan 		if (spa->spa_spares.sav_config != NULL) {
3143fa94a07fSbrendan 			VERIFY(nvlist_lookup_nvlist_array(
3144fa94a07fSbrendan 			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
3145fa94a07fSbrendan 			    &spares, &nspares) == 0);
314699653d4eSeschrock 			for (i = 0; i < nspares; i++) {
314799653d4eSeschrock 				uint64_t theguid;
314899653d4eSeschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
314999653d4eSeschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
3150fa94a07fSbrendan 				if (theguid == guid) {
3151fa94a07fSbrendan 					VERIFY(nvlist_add_string(spares[i],
3152fa94a07fSbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
3153fa94a07fSbrendan 					spa_load_spares(spa);
3154fa94a07fSbrendan 					spa->spa_spares.sav_sync = B_TRUE;
3155fa94a07fSbrendan 					return (spa_vdev_exit(spa, NULL, txg,
3156fa94a07fSbrendan 					    0));
3157fa94a07fSbrendan 				}
315899653d4eSeschrock 			}
3159fa94a07fSbrendan 		}
316099653d4eSeschrock 
3161fa94a07fSbrendan 		if (spa->spa_l2cache.sav_config != NULL) {
3162fa94a07fSbrendan 			VERIFY(nvlist_lookup_nvlist_array(
3163fa94a07fSbrendan 			    spa->spa_l2cache.sav_config, ZPOOL_CONFIG_L2CACHE,
3164fa94a07fSbrendan 			    &l2cache, &nl2cache) == 0);
3165fa94a07fSbrendan 			for (i = 0; i < nl2cache; i++) {
3166fa94a07fSbrendan 				uint64_t theguid;
3167fa94a07fSbrendan 				VERIFY(nvlist_lookup_uint64(l2cache[i],
3168fa94a07fSbrendan 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
3169fa94a07fSbrendan 				if (theguid == guid) {
3170fa94a07fSbrendan 					VERIFY(nvlist_add_string(l2cache[i],
3171fa94a07fSbrendan 					    ZPOOL_CONFIG_PATH, newpath) == 0);
3172fa94a07fSbrendan 					spa_load_l2cache(spa);
3173fa94a07fSbrendan 					spa->spa_l2cache.sav_sync = B_TRUE;
3174fa94a07fSbrendan 					return (spa_vdev_exit(spa, NULL, txg,
3175fa94a07fSbrendan 					    0));
3176fa94a07fSbrendan 				}
3177fa94a07fSbrendan 			}
317899653d4eSeschrock 		}
3179fa94a07fSbrendan 
3180fa94a07fSbrendan 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
318199653d4eSeschrock 	}
3182c67d9675Seschrock 
31830e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
31840e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
31850e34b6a7Sbonwick 
3186c67d9675Seschrock 	spa_strfree(vd->vdev_path);
3187c67d9675Seschrock 	vd->vdev_path = spa_strdup(newpath);
3188c67d9675Seschrock 
3189c67d9675Seschrock 	vdev_config_dirty(vd->vdev_top);
3190c67d9675Seschrock 
3191c67d9675Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
3192c67d9675Seschrock }
3193c67d9675Seschrock 
3194fa9e4066Sahrens /*
3195fa9e4066Sahrens  * ==========================================================================
3196fa9e4066Sahrens  * SPA Scrubbing
3197fa9e4066Sahrens  * ==========================================================================
3198fa9e4066Sahrens  */
3199fa9e4066Sahrens 
3200fa9e4066Sahrens static void
3201fa9e4066Sahrens spa_scrub_io_done(zio_t *zio)
3202fa9e4066Sahrens {
3203fa9e4066Sahrens 	spa_t *spa = zio->io_spa;
3204fa9e4066Sahrens 
32050e8c6158Smaybee 	arc_data_buf_free(zio->io_data, zio->io_size);
3206fa9e4066Sahrens 
3207fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3208ea8dc4b6Seschrock 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
320944cd46caSbillm 		vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev;
3210ea8dc4b6Seschrock 		spa->spa_scrub_errors++;
3211fa9e4066Sahrens 		mutex_enter(&vd->vdev_stat_lock);
3212fa9e4066Sahrens 		vd->vdev_stat.vs_scrub_errors++;
3213fa9e4066Sahrens 		mutex_exit(&vd->vdev_stat_lock);
3214fa9e4066Sahrens 	}
321505b2b3b8Smishra 
321605b2b3b8Smishra 	if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight)
3217ea8dc4b6Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
321805b2b3b8Smishra 
321905b2b3b8Smishra 	ASSERT(spa->spa_scrub_inflight >= 0);
322005b2b3b8Smishra 
3221ea8dc4b6Seschrock 	mutex_exit(&spa->spa_scrub_lock);
3222fa9e4066Sahrens }
3223fa9e4066Sahrens 
3224fa9e4066Sahrens static void
3225ea8dc4b6Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags,
3226ea8dc4b6Seschrock     zbookmark_t *zb)
3227fa9e4066Sahrens {
3228fa9e4066Sahrens 	size_t size = BP_GET_LSIZE(bp);
322905b2b3b8Smishra 	void *data;
3230fa9e4066Sahrens 
3231fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
323205b2b3b8Smishra 	/*
323305b2b3b8Smishra 	 * Do not give too much work to vdev(s).
323405b2b3b8Smishra 	 */
323505b2b3b8Smishra 	while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) {
323605b2b3b8Smishra 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
323705b2b3b8Smishra 	}
3238fa9e4066Sahrens 	spa->spa_scrub_inflight++;
3239fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3240fa9e4066Sahrens 
32410e8c6158Smaybee 	data = arc_data_buf_alloc(size);
324205b2b3b8Smishra 
3243ea8dc4b6Seschrock 	if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
3244ea8dc4b6Seschrock 		flags |= ZIO_FLAG_SPECULATIVE;	/* intent log block */
3245ea8dc4b6Seschrock 
3246d80c45e0Sbonwick 	flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL;
3247ea8dc4b6Seschrock 
3248fa9e4066Sahrens 	zio_nowait(zio_read(NULL, spa, bp, data, size,
3249ea8dc4b6Seschrock 	    spa_scrub_io_done, NULL, priority, flags, zb));
3250fa9e4066Sahrens }
3251fa9e4066Sahrens 
3252fa9e4066Sahrens /* ARGSUSED */
3253fa9e4066Sahrens static int
3254fa9e4066Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
3255fa9e4066Sahrens {
3256fa9e4066Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
325744cd46caSbillm 	vdev_t *vd = spa->spa_root_vdev;
325844cd46caSbillm 	dva_t *dva = bp->blk_dva;
325944cd46caSbillm 	int needs_resilver = B_FALSE;
326044cd46caSbillm 	int d;
3261fa9e4066Sahrens 
326244cd46caSbillm 	if (bc->bc_errno) {
3263fa9e4066Sahrens 		/*
3264fa9e4066Sahrens 		 * We can't scrub this block, but we can continue to scrub
3265fa9e4066Sahrens 		 * the rest of the pool.  Note the error and move along.
3266fa9e4066Sahrens 		 */
3267fa9e4066Sahrens 		mutex_enter(&spa->spa_scrub_lock);
3268fa9e4066Sahrens 		spa->spa_scrub_errors++;
3269fa9e4066Sahrens 		mutex_exit(&spa->spa_scrub_lock);
3270fa9e4066Sahrens 
327144cd46caSbillm 		mutex_enter(&vd->vdev_stat_lock);
327244cd46caSbillm 		vd->vdev_stat.vs_scrub_errors++;
327344cd46caSbillm 		mutex_exit(&vd->vdev_stat_lock);
3274fa9e4066Sahrens 
3275fa9e4066Sahrens 		return (ERESTART);
3276fa9e4066Sahrens 	}
3277fa9e4066Sahrens 
3278fa9e4066Sahrens 	ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg);
3279fa9e4066Sahrens 
328044cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
328144cd46caSbillm 		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]));
3282fa9e4066Sahrens 
328344cd46caSbillm 		ASSERT(vd != NULL);
328444cd46caSbillm 
328544cd46caSbillm 		/*
328644cd46caSbillm 		 * Keep track of how much data we've examined so that
328744cd46caSbillm 		 * zpool(1M) status can make useful progress reports.
328844cd46caSbillm 		 */
328944cd46caSbillm 		mutex_enter(&vd->vdev_stat_lock);
329044cd46caSbillm 		vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]);
329144cd46caSbillm 		mutex_exit(&vd->vdev_stat_lock);
329244cd46caSbillm 
329344cd46caSbillm 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) {
329444cd46caSbillm 			if (DVA_GET_GANG(&dva[d])) {
329544cd46caSbillm 				/*
329644cd46caSbillm 				 * Gang members may be spread across multiple
329744cd46caSbillm 				 * vdevs, so the best we can do is look at the
329844cd46caSbillm 				 * pool-wide DTL.
329944cd46caSbillm 				 * XXX -- it would be better to change our
330044cd46caSbillm 				 * allocation policy to ensure that this can't
330144cd46caSbillm 				 * happen.
330244cd46caSbillm 				 */
330344cd46caSbillm 				vd = spa->spa_root_vdev;
330444cd46caSbillm 			}
330544cd46caSbillm 			if (vdev_dtl_contains(&vd->vdev_dtl_map,
330644cd46caSbillm 			    bp->blk_birth, 1))
330744cd46caSbillm 				needs_resilver = B_TRUE;
3308fa9e4066Sahrens 		}
330944cd46caSbillm 	}
331044cd46caSbillm 
331144cd46caSbillm 	if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING)
3312fa9e4066Sahrens 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB,
3313ea8dc4b6Seschrock 		    ZIO_FLAG_SCRUB, &bc->bc_bookmark);
331444cd46caSbillm 	else if (needs_resilver)
331544cd46caSbillm 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER,
331644cd46caSbillm 		    ZIO_FLAG_RESILVER, &bc->bc_bookmark);
3317fa9e4066Sahrens 
3318fa9e4066Sahrens 	return (0);
3319fa9e4066Sahrens }
3320fa9e4066Sahrens 
3321fa9e4066Sahrens static void
3322fa9e4066Sahrens spa_scrub_thread(spa_t *spa)
3323fa9e4066Sahrens {
3324fa9e4066Sahrens 	callb_cpr_t cprinfo;
3325fa9e4066Sahrens 	traverse_handle_t *th = spa->spa_scrub_th;
3326fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3327fa9e4066Sahrens 	pool_scrub_type_t scrub_type = spa->spa_scrub_type;
3328fa9e4066Sahrens 	int error = 0;
3329fa9e4066Sahrens 	boolean_t complete;
3330fa9e4066Sahrens 
3331fa9e4066Sahrens 	CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG);
3332fa9e4066Sahrens 
3333f0aa80d4Sbonwick 	/*
3334f0aa80d4Sbonwick 	 * If we're restarting due to a snapshot create/delete,
3335f0aa80d4Sbonwick 	 * wait for that to complete.
3336f0aa80d4Sbonwick 	 */
3337f0aa80d4Sbonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
3338f0aa80d4Sbonwick 
3339ea8dc4b6Seschrock 	dprintf("start %s mintxg=%llu maxtxg=%llu\n",
3340ea8dc4b6Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
3341ea8dc4b6Seschrock 	    spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg);
3342ea8dc4b6Seschrock 
3343ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
3344ea8dc4b6Seschrock 	vdev_reopen(rvd);		/* purge all vdev caches */
3345fa9e4066Sahrens 	vdev_config_dirty(rvd);		/* rewrite all disk labels */
3346fa9e4066Sahrens 	vdev_scrub_stat_update(rvd, scrub_type, B_FALSE);
3347ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
3348fa9e4066Sahrens 
3349fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3350fa9e4066Sahrens 	spa->spa_scrub_errors = 0;
3351fa9e4066Sahrens 	spa->spa_scrub_active = 1;
3352ea8dc4b6Seschrock 	ASSERT(spa->spa_scrub_inflight == 0);
3353fa9e4066Sahrens 
3354fa9e4066Sahrens 	while (!spa->spa_scrub_stop) {
3355fa9e4066Sahrens 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3356ea8dc4b6Seschrock 		while (spa->spa_scrub_suspended) {
3357fa9e4066Sahrens 			spa->spa_scrub_active = 0;
3358fa9e4066Sahrens 			cv_broadcast(&spa->spa_scrub_cv);
3359fa9e4066Sahrens 			cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3360fa9e4066Sahrens 			spa->spa_scrub_active = 1;
3361fa9e4066Sahrens 		}
3362fa9e4066Sahrens 		CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock);
3363fa9e4066Sahrens 
3364fa9e4066Sahrens 		if (spa->spa_scrub_restart_txg != 0)
3365fa9e4066Sahrens 			break;
3366fa9e4066Sahrens 
3367fa9e4066Sahrens 		mutex_exit(&spa->spa_scrub_lock);
3368fa9e4066Sahrens 		error = traverse_more(th);
3369fa9e4066Sahrens 		mutex_enter(&spa->spa_scrub_lock);
3370fa9e4066Sahrens 		if (error != EAGAIN)
3371fa9e4066Sahrens 			break;
3372fa9e4066Sahrens 	}
3373fa9e4066Sahrens 
3374fa9e4066Sahrens 	while (spa->spa_scrub_inflight)
3375fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3376fa9e4066Sahrens 
33775dabedeeSbonwick 	spa->spa_scrub_active = 0;
33785dabedeeSbonwick 	cv_broadcast(&spa->spa_scrub_cv);
33795dabedeeSbonwick 
33805dabedeeSbonwick 	mutex_exit(&spa->spa_scrub_lock);
33815dabedeeSbonwick 
33825dabedeeSbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
33835dabedeeSbonwick 
33845dabedeeSbonwick 	mutex_enter(&spa->spa_scrub_lock);
33855dabedeeSbonwick 
33865dabedeeSbonwick 	/*
33875dabedeeSbonwick 	 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock
33885dabedeeSbonwick 	 * AND the spa config lock to synchronize with any config changes
33895dabedeeSbonwick 	 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
33905dabedeeSbonwick 	 */
3391fa9e4066Sahrens 	if (spa->spa_scrub_restart_txg != 0)
3392fa9e4066Sahrens 		error = ERESTART;
3393fa9e4066Sahrens 
3394ea8dc4b6Seschrock 	if (spa->spa_scrub_stop)
3395ea8dc4b6Seschrock 		error = EINTR;
3396ea8dc4b6Seschrock 
3397fa9e4066Sahrens 	/*
3398ea8dc4b6Seschrock 	 * Even if there were uncorrectable errors, we consider the scrub
3399ea8dc4b6Seschrock 	 * completed.  The downside is that if there is a transient error during
3400ea8dc4b6Seschrock 	 * a resilver, we won't resilver the data properly to the target.  But
3401ea8dc4b6Seschrock 	 * if the damage is permanent (more likely) we will resilver forever,
3402ea8dc4b6Seschrock 	 * which isn't really acceptable.  Since there is enough information for
3403ea8dc4b6Seschrock 	 * the user to know what has failed and why, this seems like a more
3404ea8dc4b6Seschrock 	 * tractable approach.
3405fa9e4066Sahrens 	 */
3406ea8dc4b6Seschrock 	complete = (error == 0);
3407fa9e4066Sahrens 
3408ea8dc4b6Seschrock 	dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
3409ea8dc4b6Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
3410fa9e4066Sahrens 	    spa->spa_scrub_maxtxg, complete ? "done" : "FAILED",
3411fa9e4066Sahrens 	    error, spa->spa_scrub_errors, spa->spa_scrub_stop);
3412fa9e4066Sahrens 
3413fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3414fa9e4066Sahrens 
3415fa9e4066Sahrens 	/*
3416fa9e4066Sahrens 	 * If the scrub/resilver completed, update all DTLs to reflect this.
3417fa9e4066Sahrens 	 * Whether it succeeded or not, vacate all temporary scrub DTLs.
3418fa9e4066Sahrens 	 */
3419fa9e4066Sahrens 	vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1,
3420fa9e4066Sahrens 	    complete ? spa->spa_scrub_maxtxg : 0, B_TRUE);
3421fa9e4066Sahrens 	vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete);
3422ea8dc4b6Seschrock 	spa_errlog_rotate(spa);
34235dabedeeSbonwick 
34243d7072f8Seschrock 	if (scrub_type == POOL_SCRUB_RESILVER && complete)
34253d7072f8Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH);
34263d7072f8Seschrock 
3427ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
3428fa9e4066Sahrens 
3429fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3430fa9e4066Sahrens 
3431ea8dc4b6Seschrock 	/*
3432ea8dc4b6Seschrock 	 * We may have finished replacing a device.
3433ea8dc4b6Seschrock 	 * Let the async thread assess this and handle the detach.
3434ea8dc4b6Seschrock 	 */
34353d7072f8Seschrock 	spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3436fa9e4066Sahrens 
3437fa9e4066Sahrens 	/*
3438fa9e4066Sahrens 	 * If we were told to restart, our final act is to start a new scrub.
3439fa9e4066Sahrens 	 */
3440fa9e4066Sahrens 	if (error == ERESTART)
3441ea8dc4b6Seschrock 		spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ?
3442ea8dc4b6Seschrock 		    SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB);
3443fa9e4066Sahrens 
3444ea8dc4b6Seschrock 	spa->spa_scrub_type = POOL_SCRUB_NONE;
3445ea8dc4b6Seschrock 	spa->spa_scrub_active = 0;
3446ea8dc4b6Seschrock 	spa->spa_scrub_thread = NULL;
3447ea8dc4b6Seschrock 	cv_broadcast(&spa->spa_scrub_cv);
3448fa9e4066Sahrens 	CALLB_CPR_EXIT(&cprinfo);	/* drops &spa->spa_scrub_lock */
3449fa9e4066Sahrens 	thread_exit();
3450fa9e4066Sahrens }
3451fa9e4066Sahrens 
3452fa9e4066Sahrens void
3453fa9e4066Sahrens spa_scrub_suspend(spa_t *spa)
3454fa9e4066Sahrens {
3455fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3456ea8dc4b6Seschrock 	spa->spa_scrub_suspended++;
3457fa9e4066Sahrens 	while (spa->spa_scrub_active) {
3458fa9e4066Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3459fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3460fa9e4066Sahrens 	}
3461fa9e4066Sahrens 	while (spa->spa_scrub_inflight)
3462fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3463fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3464fa9e4066Sahrens }
3465fa9e4066Sahrens 
3466fa9e4066Sahrens void
3467fa9e4066Sahrens spa_scrub_resume(spa_t *spa)
3468fa9e4066Sahrens {
3469fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3470ea8dc4b6Seschrock 	ASSERT(spa->spa_scrub_suspended != 0);
3471ea8dc4b6Seschrock 	if (--spa->spa_scrub_suspended == 0)
3472fa9e4066Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3473fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3474fa9e4066Sahrens }
3475fa9e4066Sahrens 
3476fa9e4066Sahrens void
3477fa9e4066Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg)
3478fa9e4066Sahrens {
3479fa9e4066Sahrens 	/*
3480fa9e4066Sahrens 	 * Something happened (e.g. snapshot create/delete) that means
3481fa9e4066Sahrens 	 * we must restart any in-progress scrubs.  The itinerary will
3482fa9e4066Sahrens 	 * fix this properly.
3483fa9e4066Sahrens 	 */
3484fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
3485fa9e4066Sahrens 	spa->spa_scrub_restart_txg = txg;
3486fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
3487fa9e4066Sahrens }
3488fa9e4066Sahrens 
3489ea8dc4b6Seschrock int
3490ea8dc4b6Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force)
3491fa9e4066Sahrens {
3492fa9e4066Sahrens 	space_seg_t *ss;
3493fa9e4066Sahrens 	uint64_t mintxg, maxtxg;
3494fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3495fa9e4066Sahrens 
3496bb8b5132Sek 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3497bb8b5132Sek 	ASSERT(!spa_config_held(spa, RW_WRITER));
3498bb8b5132Sek 
3499fa9e4066Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3500fa9e4066Sahrens 		return (ENOTSUP);
3501fa9e4066Sahrens 
3502ea8dc4b6Seschrock 	mutex_enter(&spa->spa_scrub_lock);
3503ea8dc4b6Seschrock 
3504fa9e4066Sahrens 	/*
3505fa9e4066Sahrens 	 * If there's a scrub or resilver already in progress, stop it.
3506fa9e4066Sahrens 	 */
3507fa9e4066Sahrens 	while (spa->spa_scrub_thread != NULL) {
3508fa9e4066Sahrens 		/*
3509fa9e4066Sahrens 		 * Don't stop a resilver unless forced.
3510fa9e4066Sahrens 		 */
3511ea8dc4b6Seschrock 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) {
3512ea8dc4b6Seschrock 			mutex_exit(&spa->spa_scrub_lock);
3513fa9e4066Sahrens 			return (EBUSY);
3514ea8dc4b6Seschrock 		}
3515fa9e4066Sahrens 		spa->spa_scrub_stop = 1;
3516fa9e4066Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3517fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3518fa9e4066Sahrens 	}
3519fa9e4066Sahrens 
3520fa9e4066Sahrens 	/*
3521fa9e4066Sahrens 	 * Terminate the previous traverse.
3522fa9e4066Sahrens 	 */
3523fa9e4066Sahrens 	if (spa->spa_scrub_th != NULL) {
3524fa9e4066Sahrens 		traverse_fini(spa->spa_scrub_th);
3525fa9e4066Sahrens 		spa->spa_scrub_th = NULL;
3526fa9e4066Sahrens 	}
3527fa9e4066Sahrens 
3528ea8dc4b6Seschrock 	if (rvd == NULL) {
3529ea8dc4b6Seschrock 		ASSERT(spa->spa_scrub_stop == 0);
3530ea8dc4b6Seschrock 		ASSERT(spa->spa_scrub_type == type);
3531ea8dc4b6Seschrock 		ASSERT(spa->spa_scrub_restart_txg == 0);
3532ea8dc4b6Seschrock 		mutex_exit(&spa->spa_scrub_lock);
3533ea8dc4b6Seschrock 		return (0);
3534ea8dc4b6Seschrock 	}
3535fa9e4066Sahrens 
3536fa9e4066Sahrens 	mintxg = TXG_INITIAL - 1;
3537fa9e4066Sahrens 	maxtxg = spa_last_synced_txg(spa) + 1;
3538fa9e4066Sahrens 
3539ea8dc4b6Seschrock 	mutex_enter(&rvd->vdev_dtl_lock);
3540fa9e4066Sahrens 
3541ea8dc4b6Seschrock 	if (rvd->vdev_dtl_map.sm_space == 0) {
3542ea8dc4b6Seschrock 		/*
3543ea8dc4b6Seschrock 		 * The pool-wide DTL is empty.
3544ecc2d604Sbonwick 		 * If this is a resilver, there's nothing to do except
3545ecc2d604Sbonwick 		 * check whether any in-progress replacements have completed.
3546ea8dc4b6Seschrock 		 */
3547ecc2d604Sbonwick 		if (type == POOL_SCRUB_RESILVER) {
3548ea8dc4b6Seschrock 			type = POOL_SCRUB_NONE;
35493d7072f8Seschrock 			spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3550ecc2d604Sbonwick 		}
3551ea8dc4b6Seschrock 	} else {
3552ea8dc4b6Seschrock 		/*
3553ea8dc4b6Seschrock 		 * The pool-wide DTL is non-empty.
3554ea8dc4b6Seschrock 		 * If this is a normal scrub, upgrade to a resilver instead.
3555ea8dc4b6Seschrock 		 */
3556ea8dc4b6Seschrock 		if (type == POOL_SCRUB_EVERYTHING)
3557ea8dc4b6Seschrock 			type = POOL_SCRUB_RESILVER;
3558ea8dc4b6Seschrock 	}
3559fa9e4066Sahrens 
3560ea8dc4b6Seschrock 	if (type == POOL_SCRUB_RESILVER) {
3561fa9e4066Sahrens 		/*
3562fa9e4066Sahrens 		 * Determine the resilvering boundaries.
3563fa9e4066Sahrens 		 *
3564fa9e4066Sahrens 		 * Note: (mintxg, maxtxg) is an open interval,
3565fa9e4066Sahrens 		 * i.e. mintxg and maxtxg themselves are not included.
3566fa9e4066Sahrens 		 *
3567fa9e4066Sahrens 		 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
3568fa9e4066Sahrens 		 * so we don't claim to resilver a txg that's still changing.
3569fa9e4066Sahrens 		 */
3570fa9e4066Sahrens 		ss = avl_first(&rvd->vdev_dtl_map.sm_root);
3571ea8dc4b6Seschrock 		mintxg = ss->ss_start - 1;
3572fa9e4066Sahrens 		ss = avl_last(&rvd->vdev_dtl_map.sm_root);
3573ea8dc4b6Seschrock 		maxtxg = MIN(ss->ss_end, maxtxg);
35743d7072f8Seschrock 
35753d7072f8Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
3576fa9e4066Sahrens 	}
3577fa9e4066Sahrens 
3578ea8dc4b6Seschrock 	mutex_exit(&rvd->vdev_dtl_lock);
3579ea8dc4b6Seschrock 
3580ea8dc4b6Seschrock 	spa->spa_scrub_stop = 0;
3581ea8dc4b6Seschrock 	spa->spa_scrub_type = type;
3582ea8dc4b6Seschrock 	spa->spa_scrub_restart_txg = 0;
3583ea8dc4b6Seschrock 
3584ea8dc4b6Seschrock 	if (type != POOL_SCRUB_NONE) {
3585ea8dc4b6Seschrock 		spa->spa_scrub_mintxg = mintxg;
3586fa9e4066Sahrens 		spa->spa_scrub_maxtxg = maxtxg;
3587fa9e4066Sahrens 		spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL,
35880373e76bSbonwick 		    ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL,
35890373e76bSbonwick 		    ZIO_FLAG_CANFAIL);
3590fa9e4066Sahrens 		traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg);
3591fa9e4066Sahrens 		spa->spa_scrub_thread = thread_create(NULL, 0,
3592fa9e4066Sahrens 		    spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri);
3593fa9e4066Sahrens 	}
3594fa9e4066Sahrens 
3595ea8dc4b6Seschrock 	mutex_exit(&spa->spa_scrub_lock);
3596ea8dc4b6Seschrock 
3597fa9e4066Sahrens 	return (0);
3598fa9e4066Sahrens }
3599fa9e4066Sahrens 
3600ea8dc4b6Seschrock /*
3601ea8dc4b6Seschrock  * ==========================================================================
3602ea8dc4b6Seschrock  * SPA async task processing
3603ea8dc4b6Seschrock  * ==========================================================================
3604ea8dc4b6Seschrock  */
3605ea8dc4b6Seschrock 
3606ea8dc4b6Seschrock static void
36073d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3608fa9e4066Sahrens {
3609ea8dc4b6Seschrock 	vdev_t *tvd;
3610ea8dc4b6Seschrock 	int c;
3611fa9e4066Sahrens 
36123d7072f8Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
36133d7072f8Seschrock 		tvd = vd->vdev_child[c];
36143d7072f8Seschrock 		if (tvd->vdev_remove_wanted) {
36153d7072f8Seschrock 			tvd->vdev_remove_wanted = 0;
36163d7072f8Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
36173d7072f8Seschrock 			    VDEV_AUX_NONE);
36180a4e9518Sgw 			vdev_clear(spa, tvd, B_TRUE);
36193d7072f8Seschrock 			vdev_config_dirty(tvd->vdev_top);
3620ea8dc4b6Seschrock 		}
36213d7072f8Seschrock 		spa_async_remove(spa, tvd);
3622ea8dc4b6Seschrock 	}
3623ea8dc4b6Seschrock }
3624fa9e4066Sahrens 
3625ea8dc4b6Seschrock static void
3626ea8dc4b6Seschrock spa_async_thread(spa_t *spa)
3627ea8dc4b6Seschrock {
3628ea8dc4b6Seschrock 	int tasks;
36293d7072f8Seschrock 	uint64_t txg;
3630ea8dc4b6Seschrock 
3631ea8dc4b6Seschrock 	ASSERT(spa->spa_sync_on);
3632ea8dc4b6Seschrock 
3633ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3634ea8dc4b6Seschrock 	tasks = spa->spa_async_tasks;
3635ea8dc4b6Seschrock 	spa->spa_async_tasks = 0;
3636ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3637ea8dc4b6Seschrock 
36380373e76bSbonwick 	/*
36390373e76bSbonwick 	 * See if the config needs to be updated.
36400373e76bSbonwick 	 */
36410373e76bSbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
36420373e76bSbonwick 		mutex_enter(&spa_namespace_lock);
36430373e76bSbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
36440373e76bSbonwick 		mutex_exit(&spa_namespace_lock);
36450373e76bSbonwick 	}
36460373e76bSbonwick 
3647ea8dc4b6Seschrock 	/*
36483d7072f8Seschrock 	 * See if any devices need to be marked REMOVED.
36490a4e9518Sgw 	 *
36500a4e9518Sgw 	 * XXX - We avoid doing this when we are in
36510a4e9518Sgw 	 * I/O failure state since spa_vdev_enter() grabs
36520a4e9518Sgw 	 * the namespace lock and would not be able to obtain
36530a4e9518Sgw 	 * the writer config lock.
3654ea8dc4b6Seschrock 	 */
36550a4e9518Sgw 	if (tasks & SPA_ASYNC_REMOVE &&
36560a4e9518Sgw 	    spa_state(spa) != POOL_STATE_IO_FAILURE) {
36573d7072f8Seschrock 		txg = spa_vdev_enter(spa);
36583d7072f8Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
36593d7072f8Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
36603d7072f8Seschrock 	}
3661ea8dc4b6Seschrock 
3662ea8dc4b6Seschrock 	/*
3663ea8dc4b6Seschrock 	 * If any devices are done replacing, detach them.
3664ea8dc4b6Seschrock 	 */
36653d7072f8Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
36663d7072f8Seschrock 		spa_vdev_resilver_done(spa);
3667fa9e4066Sahrens 
3668ea8dc4b6Seschrock 	/*
36693d7072f8Seschrock 	 * Kick off a scrub.  When starting a RESILVER scrub (or an EVERYTHING
36703d7072f8Seschrock 	 * scrub which can become a resilver), we need to hold
36713d7072f8Seschrock 	 * spa_namespace_lock() because the sysevent we post via
36723d7072f8Seschrock 	 * spa_event_notify() needs to get the name of the pool.
3673ea8dc4b6Seschrock 	 */
36743d7072f8Seschrock 	if (tasks & SPA_ASYNC_SCRUB) {
36753d7072f8Seschrock 		mutex_enter(&spa_namespace_lock);
3676ea8dc4b6Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0);
36773d7072f8Seschrock 		mutex_exit(&spa_namespace_lock);
36783d7072f8Seschrock 	}
3679ea8dc4b6Seschrock 
3680ea8dc4b6Seschrock 	/*
3681ea8dc4b6Seschrock 	 * Kick off a resilver.
3682ea8dc4b6Seschrock 	 */
36833d7072f8Seschrock 	if (tasks & SPA_ASYNC_RESILVER) {
36843d7072f8Seschrock 		mutex_enter(&spa_namespace_lock);
3685ea8dc4b6Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
36863d7072f8Seschrock 		mutex_exit(&spa_namespace_lock);
36873d7072f8Seschrock 	}
3688ea8dc4b6Seschrock 
3689ea8dc4b6Seschrock 	/*
3690ea8dc4b6Seschrock 	 * Let the world know that we're done.
3691ea8dc4b6Seschrock 	 */
3692ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3693ea8dc4b6Seschrock 	spa->spa_async_thread = NULL;
3694ea8dc4b6Seschrock 	cv_broadcast(&spa->spa_async_cv);
3695ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3696ea8dc4b6Seschrock 	thread_exit();
3697ea8dc4b6Seschrock }
3698ea8dc4b6Seschrock 
3699ea8dc4b6Seschrock void
3700ea8dc4b6Seschrock spa_async_suspend(spa_t *spa)
3701ea8dc4b6Seschrock {
3702ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3703ea8dc4b6Seschrock 	spa->spa_async_suspended++;
3704ea8dc4b6Seschrock 	while (spa->spa_async_thread != NULL)
3705ea8dc4b6Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
3706ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3707ea8dc4b6Seschrock }
3708ea8dc4b6Seschrock 
3709ea8dc4b6Seschrock void
3710ea8dc4b6Seschrock spa_async_resume(spa_t *spa)
3711ea8dc4b6Seschrock {
3712ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3713ea8dc4b6Seschrock 	ASSERT(spa->spa_async_suspended != 0);
3714ea8dc4b6Seschrock 	spa->spa_async_suspended--;
3715ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3716ea8dc4b6Seschrock }
3717ea8dc4b6Seschrock 
3718ea8dc4b6Seschrock static void
3719ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa)
3720ea8dc4b6Seschrock {
3721ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3722ea8dc4b6Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
37230373e76bSbonwick 	    spa->spa_async_thread == NULL &&
37240373e76bSbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
3725ea8dc4b6Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
3726ea8dc4b6Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
3727ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3728ea8dc4b6Seschrock }
3729ea8dc4b6Seschrock 
3730ea8dc4b6Seschrock void
3731ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task)
3732ea8dc4b6Seschrock {
3733ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3734ea8dc4b6Seschrock 	spa->spa_async_tasks |= task;
3735ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3736fa9e4066Sahrens }
3737fa9e4066Sahrens 
3738fa9e4066Sahrens /*
3739fa9e4066Sahrens  * ==========================================================================
3740fa9e4066Sahrens  * SPA syncing routines
3741fa9e4066Sahrens  * ==========================================================================
3742fa9e4066Sahrens  */
3743fa9e4066Sahrens 
3744fa9e4066Sahrens static void
3745fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3746fa9e4066Sahrens {
3747fa9e4066Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3748fa9e4066Sahrens 	dmu_tx_t *tx;
3749fa9e4066Sahrens 	blkptr_t blk;
3750fa9e4066Sahrens 	uint64_t itor = 0;
3751fa9e4066Sahrens 	zio_t *zio;
3752fa9e4066Sahrens 	int error;
3753fa9e4066Sahrens 	uint8_t c = 1;
3754fa9e4066Sahrens 
3755fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3756fa9e4066Sahrens 
3757fa9e4066Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3758fa9e4066Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3759fa9e4066Sahrens 
3760fa9e4066Sahrens 	error = zio_wait(zio);
3761fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
3762fa9e4066Sahrens 
3763fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3764fa9e4066Sahrens 	bplist_vacate(bpl, tx);
3765fa9e4066Sahrens 
3766fa9e4066Sahrens 	/*
3767fa9e4066Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3768fa9e4066Sahrens 	 * (Usually only the first block is needed.)
3769fa9e4066Sahrens 	 */
3770fa9e4066Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3771fa9e4066Sahrens 	dmu_tx_commit(tx);
3772fa9e4066Sahrens }
3773fa9e4066Sahrens 
3774fa9e4066Sahrens static void
377599653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
3776fa9e4066Sahrens {
3777fa9e4066Sahrens 	char *packed = NULL;
3778fa9e4066Sahrens 	size_t nvsize = 0;
3779fa9e4066Sahrens 	dmu_buf_t *db;
3780fa9e4066Sahrens 
378199653d4eSeschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
3782fa9e4066Sahrens 
3783fa9e4066Sahrens 	packed = kmem_alloc(nvsize, KM_SLEEP);
3784fa9e4066Sahrens 
378599653d4eSeschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
3786ea8dc4b6Seschrock 	    KM_SLEEP) == 0);
3787fa9e4066Sahrens 
378899653d4eSeschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
3789fa9e4066Sahrens 
3790fa9e4066Sahrens 	kmem_free(packed, nvsize);
3791fa9e4066Sahrens 
379299653d4eSeschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
3793fa9e4066Sahrens 	dmu_buf_will_dirty(db, tx);
3794fa9e4066Sahrens 	*(uint64_t *)db->db_data = nvsize;
3795ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
3796fa9e4066Sahrens }
3797fa9e4066Sahrens 
379899653d4eSeschrock static void
3799fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
3800fa94a07fSbrendan     const char *config, const char *entry)
380199653d4eSeschrock {
380299653d4eSeschrock 	nvlist_t *nvroot;
3803fa94a07fSbrendan 	nvlist_t **list;
380499653d4eSeschrock 	int i;
380599653d4eSeschrock 
3806fa94a07fSbrendan 	if (!sav->sav_sync)
380799653d4eSeschrock 		return;
380899653d4eSeschrock 
380999653d4eSeschrock 	/*
3810fa94a07fSbrendan 	 * Update the MOS nvlist describing the list of available devices.
3811fa94a07fSbrendan 	 * spa_validate_aux() will have already made sure this nvlist is
38123d7072f8Seschrock 	 * valid and the vdevs are labeled appropriately.
381399653d4eSeschrock 	 */
3814fa94a07fSbrendan 	if (sav->sav_object == 0) {
3815fa94a07fSbrendan 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
3816fa94a07fSbrendan 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
3817fa94a07fSbrendan 		    sizeof (uint64_t), tx);
381899653d4eSeschrock 		VERIFY(zap_update(spa->spa_meta_objset,
3819fa94a07fSbrendan 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
3820fa94a07fSbrendan 		    &sav->sav_object, tx) == 0);
382199653d4eSeschrock 	}
382299653d4eSeschrock 
382399653d4eSeschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3824fa94a07fSbrendan 	if (sav->sav_count == 0) {
3825fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
382699653d4eSeschrock 	} else {
3827fa94a07fSbrendan 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
3828fa94a07fSbrendan 		for (i = 0; i < sav->sav_count; i++)
3829fa94a07fSbrendan 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
3830fa94a07fSbrendan 			    B_FALSE, B_FALSE, B_TRUE);
3831fa94a07fSbrendan 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
3832fa94a07fSbrendan 		    sav->sav_count) == 0);
3833fa94a07fSbrendan 		for (i = 0; i < sav->sav_count; i++)
3834fa94a07fSbrendan 			nvlist_free(list[i]);
3835fa94a07fSbrendan 		kmem_free(list, sav->sav_count * sizeof (void *));
383699653d4eSeschrock 	}
383799653d4eSeschrock 
3838fa94a07fSbrendan 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
383906eeb2adSek 	nvlist_free(nvroot);
384099653d4eSeschrock 
3841fa94a07fSbrendan 	sav->sav_sync = B_FALSE;
384299653d4eSeschrock }
384399653d4eSeschrock 
384499653d4eSeschrock static void
384599653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
384699653d4eSeschrock {
384799653d4eSeschrock 	nvlist_t *config;
384899653d4eSeschrock 
384999653d4eSeschrock 	if (list_is_empty(&spa->spa_dirty_list))
385099653d4eSeschrock 		return;
385199653d4eSeschrock 
385299653d4eSeschrock 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
385399653d4eSeschrock 
385499653d4eSeschrock 	if (spa->spa_config_syncing)
385599653d4eSeschrock 		nvlist_free(spa->spa_config_syncing);
385699653d4eSeschrock 	spa->spa_config_syncing = config;
385799653d4eSeschrock 
385899653d4eSeschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
385999653d4eSeschrock }
386099653d4eSeschrock 
3861990b4856Slling /*
3862990b4856Slling  * Set zpool properties.
3863990b4856Slling  */
3864b1b8ab34Slling static void
3865ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
3866b1b8ab34Slling {
3867b1b8ab34Slling 	spa_t *spa = arg1;
3868b1b8ab34Slling 	objset_t *mos = spa->spa_meta_objset;
3869990b4856Slling 	nvlist_t *nvp = arg2;
3870990b4856Slling 	nvpair_t *elem;
38713d7072f8Seschrock 	uint64_t intval;
38722f8aaab3Seschrock 	char *strval, *slash;
3873990b4856Slling 	zpool_prop_t prop;
3874990b4856Slling 	const char *propname;
3875990b4856Slling 	zprop_type_t proptype;
3876b1b8ab34Slling 
3877990b4856Slling 	elem = NULL;
3878990b4856Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
3879990b4856Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
3880990b4856Slling 		case ZPOOL_PROP_VERSION:
3881990b4856Slling 			/*
3882990b4856Slling 			 * Only set version for non-zpool-creation cases
3883990b4856Slling 			 * (set/import). spa_create() needs special care
3884990b4856Slling 			 * for version setting.
3885990b4856Slling 			 */
3886990b4856Slling 			if (tx->tx_txg != TXG_INITIAL) {
3887990b4856Slling 				VERIFY(nvpair_value_uint64(elem,
3888990b4856Slling 				    &intval) == 0);
3889990b4856Slling 				ASSERT(intval <= SPA_VERSION);
3890990b4856Slling 				ASSERT(intval >= spa_version(spa));
3891990b4856Slling 				spa->spa_uberblock.ub_version = intval;
3892990b4856Slling 				vdev_config_dirty(spa->spa_root_vdev);
3893990b4856Slling 			}
3894ecd6cf80Smarks 			break;
3895990b4856Slling 
3896990b4856Slling 		case ZPOOL_PROP_ALTROOT:
3897990b4856Slling 			/*
3898990b4856Slling 			 * 'altroot' is a non-persistent property. It should
3899990b4856Slling 			 * have been set temporarily at creation or import time.
3900990b4856Slling 			 */
3901990b4856Slling 			ASSERT(spa->spa_root != NULL);
3902b1b8ab34Slling 			break;
39033d7072f8Seschrock 
39042f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
3905990b4856Slling 			/*
39062f8aaab3Seschrock 			 * 'cachefile' is a non-persistent property, but note
39072f8aaab3Seschrock 			 * an async request that the config cache needs to be
39082f8aaab3Seschrock 			 * udpated.
3909990b4856Slling 			 */
39102f8aaab3Seschrock 			VERIFY(nvpair_value_string(elem, &strval) == 0);
39112f8aaab3Seschrock 			if (spa->spa_config_dir)
39122f8aaab3Seschrock 				spa_strfree(spa->spa_config_dir);
39132f8aaab3Seschrock 			if (spa->spa_config_file)
39142f8aaab3Seschrock 				spa_strfree(spa->spa_config_file);
39152f8aaab3Seschrock 
39162f8aaab3Seschrock 			if (strval[0] == '\0') {
39172f8aaab3Seschrock 				spa->spa_config_dir = NULL;
39182f8aaab3Seschrock 				spa->spa_config_file = NULL;
39192f8aaab3Seschrock 			} else if (strcmp(strval, "none") == 0) {
39202f8aaab3Seschrock 				spa->spa_config_dir = spa_strdup(strval);
39212f8aaab3Seschrock 				spa->spa_config_file = NULL;
39222f8aaab3Seschrock 			} else {
39232c32020fSeschrock 				/*
39242c32020fSeschrock 				 * If the cachefile is in the root directory,
39252c32020fSeschrock 				 * we will end up with an empty string for
39262c32020fSeschrock 				 * spa_config_dir.  This value is only ever
39272c32020fSeschrock 				 * used when concatenated with '/', so an empty
39282c32020fSeschrock 				 * string still behaves correctly and keeps the
39292c32020fSeschrock 				 * rest of the code simple.
39302c32020fSeschrock 				 */
39312f8aaab3Seschrock 				slash = strrchr(strval, '/');
39322f8aaab3Seschrock 				ASSERT(slash != NULL);
39332f8aaab3Seschrock 				*slash = '\0';
39342c32020fSeschrock 				if (strcmp(strval, spa_config_dir) == 0 &&
39352c32020fSeschrock 				    strcmp(slash + 1, ZPOOL_CACHE_FILE) == 0) {
39362c32020fSeschrock 					spa->spa_config_dir = NULL;
39372c32020fSeschrock 					spa->spa_config_file = NULL;
39382c32020fSeschrock 				} else {
39392c32020fSeschrock 					spa->spa_config_dir =
39402c32020fSeschrock 					    spa_strdup(strval);
39412c32020fSeschrock 					spa->spa_config_file =
39422c32020fSeschrock 					    spa_strdup(slash + 1);
39432c32020fSeschrock 				}
39442f8aaab3Seschrock 			}
39452f8aaab3Seschrock 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
39463d7072f8Seschrock 			break;
3947990b4856Slling 		default:
3948990b4856Slling 			/*
3949990b4856Slling 			 * Set pool property values in the poolprops mos object.
3950990b4856Slling 			 */
3951990b4856Slling 			mutex_enter(&spa->spa_props_lock);
3952990b4856Slling 			if (spa->spa_pool_props_object == 0) {
3953990b4856Slling 				objset_t *mos = spa->spa_meta_objset;
3954990b4856Slling 
3955990b4856Slling 				VERIFY((spa->spa_pool_props_object =
3956990b4856Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
3957990b4856Slling 				    DMU_OT_NONE, 0, tx)) > 0);
3958990b4856Slling 
3959990b4856Slling 				VERIFY(zap_update(mos,
3960990b4856Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
3961990b4856Slling 				    8, 1, &spa->spa_pool_props_object, tx)
3962990b4856Slling 				    == 0);
3963990b4856Slling 			}
3964990b4856Slling 			mutex_exit(&spa->spa_props_lock);
3965990b4856Slling 
3966990b4856Slling 			/* normalize the property name */
3967990b4856Slling 			propname = zpool_prop_to_name(prop);
3968990b4856Slling 			proptype = zpool_prop_get_type(prop);
3969990b4856Slling 
3970990b4856Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
3971990b4856Slling 				ASSERT(proptype == PROP_TYPE_STRING);
3972990b4856Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
3973990b4856Slling 				VERIFY(zap_update(mos,
3974990b4856Slling 				    spa->spa_pool_props_object, propname,
3975990b4856Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
3976990b4856Slling 
3977990b4856Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
3978990b4856Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3979990b4856Slling 
3980990b4856Slling 				if (proptype == PROP_TYPE_INDEX) {
3981990b4856Slling 					const char *unused;
3982990b4856Slling 					VERIFY(zpool_prop_index_to_string(
3983990b4856Slling 					    prop, intval, &unused) == 0);
3984990b4856Slling 				}
3985990b4856Slling 				VERIFY(zap_update(mos,
3986990b4856Slling 				    spa->spa_pool_props_object, propname,
3987990b4856Slling 				    8, 1, &intval, tx) == 0);
3988990b4856Slling 			} else {
3989990b4856Slling 				ASSERT(0); /* not allowed */
3990990b4856Slling 			}
3991990b4856Slling 
39920a4e9518Sgw 			switch (prop) {
39930a4e9518Sgw 			case ZPOOL_PROP_DELEGATION:
3994990b4856Slling 				spa->spa_delegation = intval;
39950a4e9518Sgw 				break;
39960a4e9518Sgw 			case ZPOOL_PROP_BOOTFS:
3997990b4856Slling 				spa->spa_bootfs = intval;
39980a4e9518Sgw 				break;
39990a4e9518Sgw 			case ZPOOL_PROP_FAILUREMODE:
40000a4e9518Sgw 				spa->spa_failmode = intval;
40010a4e9518Sgw 				break;
40020a4e9518Sgw 			default:
40030a4e9518Sgw 				break;
40040a4e9518Sgw 			}
4005990b4856Slling 		}
4006990b4856Slling 
4007990b4856Slling 		/* log internal history if this is not a zpool create */
4008990b4856Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
4009990b4856Slling 		    tx->tx_txg != TXG_INITIAL) {
4010990b4856Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
4011990b4856Slling 			    spa, tx, cr, "%s %lld %s",
4012990b4856Slling 			    nvpair_name(elem), intval, spa->spa_name);
4013b1b8ab34Slling 		}
4014b1b8ab34Slling 	}
4015b1b8ab34Slling }
4016b1b8ab34Slling 
4017fa9e4066Sahrens /*
4018fa9e4066Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
4019fa9e4066Sahrens  * part of the process, so we iterate until it converges.
4020fa9e4066Sahrens  */
4021fa9e4066Sahrens void
4022fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg)
4023fa9e4066Sahrens {
4024fa9e4066Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
4025fa9e4066Sahrens 	objset_t *mos = spa->spa_meta_objset;
4026fa9e4066Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
40270373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4028fa9e4066Sahrens 	vdev_t *vd;
402917f17c2dSbonwick 	vdev_t *svd[SPA_DVAS_PER_BP];
403017f17c2dSbonwick 	int svdcount = 0;
4031fa9e4066Sahrens 	dmu_tx_t *tx;
4032fa9e4066Sahrens 	int dirty_vdevs;
4033fa9e4066Sahrens 
4034fa9e4066Sahrens 	/*
4035fa9e4066Sahrens 	 * Lock out configuration changes.
4036fa9e4066Sahrens 	 */
4037ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
4038fa9e4066Sahrens 
4039fa9e4066Sahrens 	spa->spa_syncing_txg = txg;
4040fa9e4066Sahrens 	spa->spa_sync_pass = 0;
4041fa9e4066Sahrens 
4042ea8dc4b6Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4043fa9e4066Sahrens 
404499653d4eSeschrock 	tx = dmu_tx_create_assigned(dp, txg);
404599653d4eSeschrock 
404699653d4eSeschrock 	/*
4047e7437265Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
404899653d4eSeschrock 	 * set spa_deflate if we have no raid-z vdevs.
404999653d4eSeschrock 	 */
4050e7437265Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
4051e7437265Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
405299653d4eSeschrock 		int i;
405399653d4eSeschrock 
405499653d4eSeschrock 		for (i = 0; i < rvd->vdev_children; i++) {
405599653d4eSeschrock 			vd = rvd->vdev_child[i];
405699653d4eSeschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
405799653d4eSeschrock 				break;
405899653d4eSeschrock 		}
405999653d4eSeschrock 		if (i == rvd->vdev_children) {
406099653d4eSeschrock 			spa->spa_deflate = TRUE;
406199653d4eSeschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
406299653d4eSeschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
406399653d4eSeschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
406499653d4eSeschrock 		}
406599653d4eSeschrock 	}
406699653d4eSeschrock 
4067fa9e4066Sahrens 	/*
4068fa9e4066Sahrens 	 * If anything has changed in this txg, push the deferred frees
4069fa9e4066Sahrens 	 * from the previous txg.  If not, leave them alone so that we
4070fa9e4066Sahrens 	 * don't generate work on an otherwise idle system.
4071fa9e4066Sahrens 	 */
4072fa9e4066Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
40731615a317Sek 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
40741615a317Sek 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4075fa9e4066Sahrens 		spa_sync_deferred_frees(spa, txg);
4076fa9e4066Sahrens 
4077fa9e4066Sahrens 	/*
4078fa9e4066Sahrens 	 * Iterate to convergence.
4079fa9e4066Sahrens 	 */
4080fa9e4066Sahrens 	do {
4081fa9e4066Sahrens 		spa->spa_sync_pass++;
4082fa9e4066Sahrens 
4083fa9e4066Sahrens 		spa_sync_config_object(spa, tx);
4084fa94a07fSbrendan 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
4085fa94a07fSbrendan 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
4086fa94a07fSbrendan 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
4087fa94a07fSbrendan 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
4088ea8dc4b6Seschrock 		spa_errlog_sync(spa, txg);
4089fa9e4066Sahrens 		dsl_pool_sync(dp, txg);
4090fa9e4066Sahrens 
4091fa9e4066Sahrens 		dirty_vdevs = 0;
4092fa9e4066Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4093fa9e4066Sahrens 			vdev_sync(vd, txg);
4094fa9e4066Sahrens 			dirty_vdevs++;
4095fa9e4066Sahrens 		}
4096fa9e4066Sahrens 
4097fa9e4066Sahrens 		bplist_sync(bpl, tx);
4098fa9e4066Sahrens 	} while (dirty_vdevs);
4099fa9e4066Sahrens 
4100fa9e4066Sahrens 	bplist_close(bpl);
4101fa9e4066Sahrens 
4102fa9e4066Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4103fa9e4066Sahrens 
4104fa9e4066Sahrens 	/*
4105fa9e4066Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
4106fa9e4066Sahrens 	 * to commit the transaction group.
41070373e76bSbonwick 	 *
410817f17c2dSbonwick 	 * If there are no dirty vdevs, we sync the uberblock to a few
410917f17c2dSbonwick 	 * random top-level vdevs that are known to be visible in the
411017f17c2dSbonwick 	 * config cache (see spa_vdev_add() for details).  If there *are*
411117f17c2dSbonwick 	 * dirty vdevs -- or if the sync to our random subset fails --
411217f17c2dSbonwick 	 * then sync the uberblock to all vdevs.
41130373e76bSbonwick 	 */
411417f17c2dSbonwick 	if (list_is_empty(&spa->spa_dirty_list)) {
41150373e76bSbonwick 		int children = rvd->vdev_children;
41160373e76bSbonwick 		int c0 = spa_get_random(children);
41170373e76bSbonwick 		int c;
41180373e76bSbonwick 
41190373e76bSbonwick 		for (c = 0; c < children; c++) {
41200373e76bSbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
412117f17c2dSbonwick 			if (vd->vdev_ms_array == 0 || vd->vdev_islog)
41220373e76bSbonwick 				continue;
412317f17c2dSbonwick 			svd[svdcount++] = vd;
412417f17c2dSbonwick 			if (svdcount == SPA_DVAS_PER_BP)
41250373e76bSbonwick 				break;
41260373e76bSbonwick 		}
41270373e76bSbonwick 	}
412817f17c2dSbonwick 	if (svdcount == 0 || vdev_config_sync(svd, svdcount, txg) != 0)
412917f17c2dSbonwick 		VERIFY3U(vdev_config_sync(rvd->vdev_child,
413017f17c2dSbonwick 		    rvd->vdev_children, txg), ==, 0);
41310373e76bSbonwick 
413299653d4eSeschrock 	dmu_tx_commit(tx);
413399653d4eSeschrock 
41340373e76bSbonwick 	/*
41350373e76bSbonwick 	 * Clear the dirty config list.
4136fa9e4066Sahrens 	 */
41370373e76bSbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
41380373e76bSbonwick 		vdev_config_clean(vd);
41390373e76bSbonwick 
41400373e76bSbonwick 	/*
41410373e76bSbonwick 	 * Now that the new config has synced transactionally,
41420373e76bSbonwick 	 * let it become visible to the config cache.
41430373e76bSbonwick 	 */
41440373e76bSbonwick 	if (spa->spa_config_syncing != NULL) {
41450373e76bSbonwick 		spa_config_set(spa, spa->spa_config_syncing);
41460373e76bSbonwick 		spa->spa_config_txg = txg;
41470373e76bSbonwick 		spa->spa_config_syncing = NULL;
41480373e76bSbonwick 	}
4149fa9e4066Sahrens 
4150fa9e4066Sahrens 	/*
4151fa9e4066Sahrens 	 * Make a stable copy of the fully synced uberblock.
4152fa9e4066Sahrens 	 * We use this as the root for pool traversals.
4153fa9e4066Sahrens 	 */
4154fa9e4066Sahrens 	spa->spa_traverse_wanted = 1;	/* tells traverse_more() to stop */
4155fa9e4066Sahrens 
4156fa9e4066Sahrens 	spa_scrub_suspend(spa);		/* stop scrubbing and finish I/Os */
4157fa9e4066Sahrens 
4158fa9e4066Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
4159fa9e4066Sahrens 	spa->spa_traverse_wanted = 0;
4160fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
4161fa9e4066Sahrens 	rw_exit(&spa->spa_traverse_lock);
4162fa9e4066Sahrens 
4163fa9e4066Sahrens 	spa_scrub_resume(spa);		/* resume scrub with new ubsync */
4164fa9e4066Sahrens 
4165fa9e4066Sahrens 	/*
4166fa9e4066Sahrens 	 * Clean up the ZIL records for the synced txg.
4167fa9e4066Sahrens 	 */
4168fa9e4066Sahrens 	dsl_pool_zil_clean(dp);
4169fa9e4066Sahrens 
4170fa9e4066Sahrens 	/*
4171fa9e4066Sahrens 	 * Update usable space statistics.
4172fa9e4066Sahrens 	 */
4173fa9e4066Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4174fa9e4066Sahrens 		vdev_sync_done(vd, txg);
4175fa9e4066Sahrens 
4176fa9e4066Sahrens 	/*
4177fa9e4066Sahrens 	 * It had better be the case that we didn't dirty anything
417899653d4eSeschrock 	 * since vdev_config_sync().
4179fa9e4066Sahrens 	 */
4180fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4181fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4182fa9e4066Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4183fa9e4066Sahrens 	ASSERT(bpl->bpl_queue == NULL);
4184fa9e4066Sahrens 
4185ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
4186ea8dc4b6Seschrock 
4187ea8dc4b6Seschrock 	/*
4188ea8dc4b6Seschrock 	 * If any async tasks have been requested, kick them off.
4189ea8dc4b6Seschrock 	 */
4190ea8dc4b6Seschrock 	spa_async_dispatch(spa);
4191fa9e4066Sahrens }
4192fa9e4066Sahrens 
4193fa9e4066Sahrens /*
4194fa9e4066Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
4195fa9e4066Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
4196fa9e4066Sahrens  * sync.
4197fa9e4066Sahrens  */
4198fa9e4066Sahrens void
4199fa9e4066Sahrens spa_sync_allpools(void)
4200fa9e4066Sahrens {
4201fa9e4066Sahrens 	spa_t *spa = NULL;
4202fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
4203fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
4204fa9e4066Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
4205fa9e4066Sahrens 			continue;
4206fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
4207fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
4208fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
4209fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
4210fa9e4066Sahrens 		spa_close(spa, FTAG);
4211fa9e4066Sahrens 	}
4212fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
4213fa9e4066Sahrens }
4214fa9e4066Sahrens 
4215fa9e4066Sahrens /*
4216fa9e4066Sahrens  * ==========================================================================
4217fa9e4066Sahrens  * Miscellaneous routines
4218fa9e4066Sahrens  * ==========================================================================
4219fa9e4066Sahrens  */
4220fa9e4066Sahrens 
4221fa9e4066Sahrens /*
4222fa9e4066Sahrens  * Remove all pools in the system.
4223fa9e4066Sahrens  */
4224fa9e4066Sahrens void
4225fa9e4066Sahrens spa_evict_all(void)
4226fa9e4066Sahrens {
4227fa9e4066Sahrens 	spa_t *spa;
4228fa9e4066Sahrens 
4229fa9e4066Sahrens 	/*
4230fa9e4066Sahrens 	 * Remove all cached state.  All pools should be closed now,
4231fa9e4066Sahrens 	 * so every spa in the AVL tree should be unreferenced.
4232fa9e4066Sahrens 	 */
4233fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
4234fa9e4066Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
4235fa9e4066Sahrens 		/*
4236ea8dc4b6Seschrock 		 * Stop async tasks.  The async thread may need to detach
4237ea8dc4b6Seschrock 		 * a device that's been replaced, which requires grabbing
4238ea8dc4b6Seschrock 		 * spa_namespace_lock, so we must drop it here.
4239fa9e4066Sahrens 		 */
4240fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
4241fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
4242ea8dc4b6Seschrock 		spa_async_suspend(spa);
4243fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
4244bb8b5132Sek 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
4245fa9e4066Sahrens 		spa_close(spa, FTAG);
4246fa9e4066Sahrens 
4247fa9e4066Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4248fa9e4066Sahrens 			spa_unload(spa);
4249fa9e4066Sahrens 			spa_deactivate(spa);
4250fa9e4066Sahrens 		}
4251fa9e4066Sahrens 		spa_remove(spa);
4252fa9e4066Sahrens 	}
4253fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
4254fa9e4066Sahrens }
4255ea8dc4b6Seschrock 
4256ea8dc4b6Seschrock vdev_t *
4257ea8dc4b6Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid)
4258ea8dc4b6Seschrock {
4259ea8dc4b6Seschrock 	return (vdev_lookup_by_guid(spa->spa_root_vdev, guid));
4260ea8dc4b6Seschrock }
4261eaca9bbdSeschrock 
4262eaca9bbdSeschrock void
4263990b4856Slling spa_upgrade(spa_t *spa, uint64_t version)
4264eaca9bbdSeschrock {
4265eaca9bbdSeschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
4266eaca9bbdSeschrock 
4267eaca9bbdSeschrock 	/*
4268eaca9bbdSeschrock 	 * This should only be called for a non-faulted pool, and since a
4269eaca9bbdSeschrock 	 * future version would result in an unopenable pool, this shouldn't be
4270eaca9bbdSeschrock 	 * possible.
4271eaca9bbdSeschrock 	 */
4272e7437265Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
4273990b4856Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
4274eaca9bbdSeschrock 
4275990b4856Slling 	spa->spa_uberblock.ub_version = version;
4276eaca9bbdSeschrock 	vdev_config_dirty(spa->spa_root_vdev);
4277eaca9bbdSeschrock 
4278eaca9bbdSeschrock 	spa_config_exit(spa, FTAG);
427999653d4eSeschrock 
428099653d4eSeschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
428199653d4eSeschrock }
428299653d4eSeschrock 
428399653d4eSeschrock boolean_t
428499653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid)
428599653d4eSeschrock {
428699653d4eSeschrock 	int i;
428739c23413Seschrock 	uint64_t spareguid;
4288fa94a07fSbrendan 	spa_aux_vdev_t *sav = &spa->spa_spares;
428999653d4eSeschrock 
4290fa94a07fSbrendan 	for (i = 0; i < sav->sav_count; i++)
4291fa94a07fSbrendan 		if (sav->sav_vdevs[i]->vdev_guid == guid)
429299653d4eSeschrock 			return (B_TRUE);
429399653d4eSeschrock 
4294fa94a07fSbrendan 	for (i = 0; i < sav->sav_npending; i++) {
4295fa94a07fSbrendan 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
4296fa94a07fSbrendan 		    &spareguid) == 0 && spareguid == guid)
429739c23413Seschrock 			return (B_TRUE);
429839c23413Seschrock 	}
429939c23413Seschrock 
430099653d4eSeschrock 	return (B_FALSE);
4301eaca9bbdSeschrock }
4302b1b8ab34Slling 
43033d7072f8Seschrock /*
43043d7072f8Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
43053d7072f8Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
43063d7072f8Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
43073d7072f8Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
43083d7072f8Seschrock  * or zdb as real changes.
43093d7072f8Seschrock  */
43103d7072f8Seschrock void
43113d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
43123d7072f8Seschrock {
43133d7072f8Seschrock #ifdef _KERNEL
43143d7072f8Seschrock 	sysevent_t		*ev;
43153d7072f8Seschrock 	sysevent_attr_list_t	*attr = NULL;
43163d7072f8Seschrock 	sysevent_value_t	value;
43173d7072f8Seschrock 	sysevent_id_t		eid;
43183d7072f8Seschrock 
43193d7072f8Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
43203d7072f8Seschrock 	    SE_SLEEP);
43213d7072f8Seschrock 
43223d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
43233d7072f8Seschrock 	value.value.sv_string = spa_name(spa);
43243d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
43253d7072f8Seschrock 		goto done;
43263d7072f8Seschrock 
43273d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
43283d7072f8Seschrock 	value.value.sv_uint64 = spa_guid(spa);
43293d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
43303d7072f8Seschrock 		goto done;
43313d7072f8Seschrock 
43323d7072f8Seschrock 	if (vd) {
43333d7072f8Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
43343d7072f8Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
43353d7072f8Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
43363d7072f8Seschrock 		    SE_SLEEP) != 0)
43373d7072f8Seschrock 			goto done;
43383d7072f8Seschrock 
43393d7072f8Seschrock 		if (vd->vdev_path) {
43403d7072f8Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
43413d7072f8Seschrock 			value.value.sv_string = vd->vdev_path;
43423d7072f8Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
43433d7072f8Seschrock 			    &value, SE_SLEEP) != 0)
43443d7072f8Seschrock 				goto done;
43453d7072f8Seschrock 		}
43463d7072f8Seschrock 	}
43473d7072f8Seschrock 
4348*b01c3b58Seschrock 	if (sysevent_attach_attributes(ev, attr) != 0)
4349*b01c3b58Seschrock 		goto done;
4350*b01c3b58Seschrock 	attr = NULL;
4351*b01c3b58Seschrock 
43523d7072f8Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
43533d7072f8Seschrock 
43543d7072f8Seschrock done:
43553d7072f8Seschrock 	if (attr)
43563d7072f8Seschrock 		sysevent_free_attr(attr);
43573d7072f8Seschrock 	sysevent_free(ev);
43583d7072f8Seschrock #endif
43593d7072f8Seschrock }
4360