xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 1732)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221354Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens /*
29789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
30789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
31789Sahrens  * pool.
32789Sahrens  */
33789Sahrens 
34789Sahrens #include <sys/zfs_context.h>
351544Seschrock #include <sys/fm/fs/zfs.h>
36789Sahrens #include <sys/spa_impl.h>
37789Sahrens #include <sys/zio.h>
38789Sahrens #include <sys/zio_checksum.h>
39789Sahrens #include <sys/zio_compress.h>
40789Sahrens #include <sys/dmu.h>
41789Sahrens #include <sys/dmu_tx.h>
42789Sahrens #include <sys/zap.h>
43789Sahrens #include <sys/zil.h>
44789Sahrens #include <sys/vdev_impl.h>
45789Sahrens #include <sys/metaslab.h>
46789Sahrens #include <sys/uberblock_impl.h>
47789Sahrens #include <sys/txg.h>
48789Sahrens #include <sys/avl.h>
49789Sahrens #include <sys/dmu_traverse.h>
50789Sahrens #include <sys/unique.h>
51789Sahrens #include <sys/dsl_pool.h>
52789Sahrens #include <sys/dsl_dir.h>
53789Sahrens #include <sys/dsl_prop.h>
54789Sahrens #include <sys/fs/zfs.h>
55789Sahrens #include <sys/callb.h>
56789Sahrens 
57789Sahrens /*
58789Sahrens  * ==========================================================================
59789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
60789Sahrens  * ==========================================================================
61789Sahrens  */
62789Sahrens 
631544Seschrock static int
641544Seschrock spa_error_entry_compare(const void *a, const void *b)
651544Seschrock {
661544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
671544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
681544Seschrock 	int ret;
691544Seschrock 
701544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
711544Seschrock 	    sizeof (zbookmark_t));
721544Seschrock 
731544Seschrock 	if (ret < 0)
741544Seschrock 		return (-1);
751544Seschrock 	else if (ret > 0)
761544Seschrock 		return (1);
771544Seschrock 	else
781544Seschrock 		return (0);
791544Seschrock }
801544Seschrock 
811544Seschrock /*
821544Seschrock  * Utility function which retrieves copies of the current logs and
831544Seschrock  * re-initializes them in the process.
841544Seschrock  */
851544Seschrock void
861544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
871544Seschrock {
881544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
891544Seschrock 
901544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
911544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
921544Seschrock 
931544Seschrock 	avl_create(&spa->spa_errlist_scrub,
941544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
951544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
961544Seschrock 	avl_create(&spa->spa_errlist_last,
971544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
981544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
991544Seschrock }
1001544Seschrock 
101789Sahrens /*
102789Sahrens  * Activate an uninitialized pool.
103789Sahrens  */
104789Sahrens static void
105789Sahrens spa_activate(spa_t *spa)
106789Sahrens {
107789Sahrens 	int t;
108789Sahrens 
109789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
110789Sahrens 
111789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
112789Sahrens 
113789Sahrens 	spa->spa_normal_class = metaslab_class_create();
114789Sahrens 
115789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
116789Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
117789Sahrens 		    8, maxclsyspri, 50, INT_MAX,
118789Sahrens 		    TASKQ_PREPOPULATE);
119789Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
120789Sahrens 		    8, maxclsyspri, 50, INT_MAX,
121789Sahrens 		    TASKQ_PREPOPULATE);
122789Sahrens 	}
123789Sahrens 
124789Sahrens 	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
125789Sahrens 
126789Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
127789Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
128789Sahrens 
129789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
130789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
1311544Seschrock 
1321544Seschrock 	avl_create(&spa->spa_errlist_scrub,
1331544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1341544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
1351544Seschrock 	avl_create(&spa->spa_errlist_last,
1361544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1371544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
138789Sahrens }
139789Sahrens 
140789Sahrens /*
141789Sahrens  * Opposite of spa_activate().
142789Sahrens  */
143789Sahrens static void
144789Sahrens spa_deactivate(spa_t *spa)
145789Sahrens {
146789Sahrens 	int t;
147789Sahrens 
148789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
149789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
150789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
151789Sahrens 
152789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
153789Sahrens 
154789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
155789Sahrens 
156789Sahrens 	list_destroy(&spa->spa_dirty_list);
157789Sahrens 
158789Sahrens 	rw_destroy(&spa->spa_traverse_lock);
159789Sahrens 
160789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
161789Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
162789Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
163789Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
164789Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
165789Sahrens 	}
166789Sahrens 
167789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
168789Sahrens 	spa->spa_normal_class = NULL;
169789Sahrens 
1701544Seschrock 	/*
1711544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
1721544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
1731544Seschrock 	 */
1741544Seschrock 	spa_errlog_drain(spa);
1751544Seschrock 
1761544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
1771544Seschrock 	avl_destroy(&spa->spa_errlist_last);
1781544Seschrock 
179789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
180789Sahrens }
181789Sahrens 
182789Sahrens /*
183789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
184789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
185789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
186789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
187789Sahrens  */
188789Sahrens static vdev_t *
189789Sahrens spa_config_parse(spa_t *spa, nvlist_t *nv, vdev_t *parent, uint_t id, int atype)
190789Sahrens {
191789Sahrens 	nvlist_t **child;
192789Sahrens 	uint_t c, children;
193789Sahrens 	vdev_t *vd;
194789Sahrens 
195789Sahrens 	if ((vd = vdev_alloc(spa, nv, parent, id, atype)) == NULL)
196789Sahrens 		return (NULL);
197789Sahrens 
198789Sahrens 	if (vd->vdev_ops->vdev_op_leaf)
199789Sahrens 		return (vd);
200789Sahrens 
201789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
202789Sahrens 	    &child, &children) != 0) {
203789Sahrens 		vdev_free(vd);
204789Sahrens 		return (NULL);
205789Sahrens 	}
206789Sahrens 
207789Sahrens 	for (c = 0; c < children; c++) {
208789Sahrens 		if (spa_config_parse(spa, child[c], vd, c, atype) == NULL) {
209789Sahrens 			vdev_free(vd);
210789Sahrens 			return (NULL);
211789Sahrens 		}
212789Sahrens 	}
213789Sahrens 
214789Sahrens 	return (vd);
215789Sahrens }
216789Sahrens 
217789Sahrens /*
218789Sahrens  * Opposite of spa_load().
219789Sahrens  */
220789Sahrens static void
221789Sahrens spa_unload(spa_t *spa)
222789Sahrens {
223789Sahrens 	/*
2241544Seschrock 	 * Stop async tasks.
2251544Seschrock 	 */
2261544Seschrock 	spa_async_suspend(spa);
2271544Seschrock 
2281544Seschrock 	/*
229789Sahrens 	 * Stop syncing.
230789Sahrens 	 */
231789Sahrens 	if (spa->spa_sync_on) {
232789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
233789Sahrens 		spa->spa_sync_on = B_FALSE;
234789Sahrens 	}
235789Sahrens 
236789Sahrens 	/*
237789Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
238789Sahrens 	 */
2391544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
2401544Seschrock 	spa_config_exit(spa, FTAG);
241789Sahrens 
242789Sahrens 	/*
243789Sahrens 	 * Close the dsl pool.
244789Sahrens 	 */
245789Sahrens 	if (spa->spa_dsl_pool) {
246789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
247789Sahrens 		spa->spa_dsl_pool = NULL;
248789Sahrens 	}
249789Sahrens 
250789Sahrens 	/*
251789Sahrens 	 * Close all vdevs.
252789Sahrens 	 */
2531585Sbonwick 	if (spa->spa_root_vdev)
254789Sahrens 		vdev_free(spa->spa_root_vdev);
2551585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
2561544Seschrock 
2571544Seschrock 	spa->spa_async_suspended = 0;
258789Sahrens }
259789Sahrens 
260789Sahrens /*
261789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
2621544Seschrock  * source of configuration information.
263789Sahrens  */
264789Sahrens static int
2651544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
266789Sahrens {
267789Sahrens 	int error = 0;
268789Sahrens 	nvlist_t *nvroot = NULL;
269789Sahrens 	vdev_t *rvd;
270789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
2711635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
272789Sahrens 	uint64_t pool_guid;
273789Sahrens 	zio_t *zio;
274789Sahrens 
2751544Seschrock 	spa->spa_load_state = state;
2761635Sbonwick 
277789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
2781635Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid) ||
2791635Sbonwick 	    (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2801635Sbonwick 	    &spa->spa_config_txg) && mosconfig)) {
2811544Seschrock 		error = EINVAL;
2821544Seschrock 		goto out;
2831544Seschrock 	}
284789Sahrens 
2851635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2861544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
2871544Seschrock 		error = EEXIST;
2881544Seschrock 		goto out;
2891544Seschrock 	}
290789Sahrens 
291789Sahrens 	/*
292789Sahrens 	 * Parse the configuration into a vdev tree.
293789Sahrens 	 */
2941544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
295789Sahrens 	rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
2961544Seschrock 	spa_config_exit(spa, FTAG);
297789Sahrens 
2981544Seschrock 	if (rvd == NULL) {
2991544Seschrock 		error = EINVAL;
3001544Seschrock 		goto out;
3011544Seschrock 	}
302789Sahrens 
3031585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
304789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
305789Sahrens 
306789Sahrens 	/*
307789Sahrens 	 * Try to open all vdevs, loading each label in the process.
308789Sahrens 	 */
3091544Seschrock 	if (vdev_open(rvd) != 0) {
3101544Seschrock 		error = ENXIO;
3111544Seschrock 		goto out;
3121544Seschrock 	}
313789Sahrens 
314789Sahrens 	/*
315789Sahrens 	 * Find the best uberblock.
316789Sahrens 	 */
317789Sahrens 	bzero(ub, sizeof (uberblock_t));
318789Sahrens 
319789Sahrens 	zio = zio_root(spa, NULL, NULL,
320789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
321789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
322789Sahrens 	error = zio_wait(zio);
323789Sahrens 
324789Sahrens 	/*
325789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
326789Sahrens 	 */
327789Sahrens 	if (ub->ub_txg == 0) {
3281544Seschrock 		error = ENXIO;
3291544Seschrock 		goto out;
3301544Seschrock 	}
3311544Seschrock 
3321544Seschrock 	/*
3331544Seschrock 	 * If the pool is newer than the code, we can't open it.
3341544Seschrock 	 */
3351544Seschrock 	if (ub->ub_version > UBERBLOCK_VERSION) {
3361544Seschrock 		error = ENOTSUP;
3371544Seschrock 		goto out;
338789Sahrens 	}
339789Sahrens 
340789Sahrens 	/*
341789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
342789Sahrens 	 * incomplete configuration.
343789Sahrens 	 */
344*1732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
3451544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3461544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
3471544Seschrock 		error = ENXIO;
3481544Seschrock 		goto out;
349789Sahrens 	}
350789Sahrens 
351789Sahrens 	/*
352789Sahrens 	 * Initialize internal SPA structures.
353789Sahrens 	 */
354789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
355789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
356789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
3571544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
3581544Seschrock 	if (error) {
3591544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3601544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
3611544Seschrock 		goto out;
3621544Seschrock 	}
363789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
364789Sahrens 
3651544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
366789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3671544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
3681544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3691544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
3701544Seschrock 		error = EIO;
3711544Seschrock 		goto out;
3721544Seschrock 	}
373789Sahrens 
374789Sahrens 	if (!mosconfig) {
375789Sahrens 		dmu_buf_t *db;
376789Sahrens 		char *packed = NULL;
377789Sahrens 		size_t nvsize = 0;
378789Sahrens 		nvlist_t *newconfig = NULL;
379789Sahrens 
3801544Seschrock 		VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset,
3811544Seschrock 		    spa->spa_config_object, FTAG, &db));
382789Sahrens 		nvsize = *(uint64_t *)db->db_data;
3831544Seschrock 		dmu_buf_rele(db, FTAG);
384789Sahrens 
385789Sahrens 		packed = kmem_alloc(nvsize, KM_SLEEP);
3861544Seschrock 		error = dmu_read(spa->spa_meta_objset,
387789Sahrens 		    spa->spa_config_object, 0, nvsize, packed);
388789Sahrens 		if (error == 0)
389789Sahrens 			error = nvlist_unpack(packed, nvsize, &newconfig, 0);
390789Sahrens 		kmem_free(packed, nvsize);
391789Sahrens 
3921544Seschrock 		if (error) {
3931544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3941544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
3951544Seschrock 			error = EIO;
3961544Seschrock 			goto out;
3971544Seschrock 		}
398789Sahrens 
399789Sahrens 		spa_config_set(spa, newconfig);
400789Sahrens 
401789Sahrens 		spa_unload(spa);
402789Sahrens 		spa_deactivate(spa);
403789Sahrens 		spa_activate(spa);
404789Sahrens 
4051544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
4061544Seschrock 	}
4071544Seschrock 
4081544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
4091544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
4101544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
4111544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
4121544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
4131544Seschrock 		error = EIO;
4141544Seschrock 		goto out;
415789Sahrens 	}
416789Sahrens 
4171544Seschrock 	/*
4181544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
4191544Seschrock 	 * not be present.
4201544Seschrock 	 */
4211544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
4221544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
4231544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
4241544Seschrock 	if (error != 0 &&error != ENOENT) {
4251544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
4261544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
4271544Seschrock 		error = EIO;
4281544Seschrock 		goto out;
4291544Seschrock 	}
4301544Seschrock 
4311544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
4321544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
4331544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
4341544Seschrock 	if (error != 0 && error != ENOENT) {
4351544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
4361544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
4371544Seschrock 		error = EIO;
4381544Seschrock 		goto out;
4391544Seschrock 	}
440789Sahrens 
441789Sahrens 	/*
4421544Seschrock 	 * Load the vdev state for all top level vdevs.  We need to grab the
4431544Seschrock 	 * config lock because all label I/O is done with the
4441544Seschrock 	 * ZIO_FLAG_CONFIG_HELD flag.
445789Sahrens 	 */
4461544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
4471635Sbonwick 	error = vdev_load(rvd);
4481635Sbonwick 	spa_config_exit(spa, FTAG);
4491635Sbonwick 
4501635Sbonwick 	if (error)
4511544Seschrock 		goto out;
452789Sahrens 
453789Sahrens 	/*
454789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
455789Sahrens 	 */
4561544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
457789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
4581544Seschrock 	spa_config_exit(spa, FTAG);
459789Sahrens 
460789Sahrens 	/*
461789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
462789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
463789Sahrens 	 */
4641544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
4651544Seschrock 		error = ENXIO;
4661544Seschrock 		goto out;
4671544Seschrock 	}
468789Sahrens 
4691544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
4701635Sbonwick 		dmu_tx_t *tx;
4711635Sbonwick 		int need_update = B_FALSE;
4721585Sbonwick 		int c;
4731601Sbonwick 
4741635Sbonwick 		/*
4751635Sbonwick 		 * Claim log blocks that haven't been committed yet.
4761635Sbonwick 		 * This must all happen in a single txg.
4771635Sbonwick 		 */
4781601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
479789Sahrens 		    spa_first_txg(spa));
480789Sahrens 		dmu_objset_find(spa->spa_name, zil_claim, tx, 0);
481789Sahrens 		dmu_tx_commit(tx);
482789Sahrens 
483789Sahrens 		spa->spa_sync_on = B_TRUE;
484789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
485789Sahrens 
486789Sahrens 		/*
487789Sahrens 		 * Wait for all claims to sync.
488789Sahrens 		 */
489789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
4901585Sbonwick 
4911585Sbonwick 		/*
4921635Sbonwick 		 * If the config cache is stale, or we have uninitialized
4931635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
4941585Sbonwick 		 */
4951635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
4961635Sbonwick 		    state == SPA_LOAD_IMPORT)
4971635Sbonwick 			need_update = B_TRUE;
4981635Sbonwick 
4991635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
5001635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
5011635Sbonwick 				need_update = B_TRUE;
5021585Sbonwick 
5031585Sbonwick 		/*
5041635Sbonwick 		 * Update the config cache asychronously in case we're the
5051635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
5061585Sbonwick 		 */
5071635Sbonwick 		if (need_update)
5081635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
509789Sahrens 	}
510789Sahrens 
5111544Seschrock 	error = 0;
5121544Seschrock out:
5131544Seschrock 	if (error)
5141544Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
5151544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
5161544Seschrock 	spa->spa_ena = 0;
5171544Seschrock 
5181544Seschrock 	return (error);
519789Sahrens }
520789Sahrens 
521789Sahrens /*
522789Sahrens  * Pool Open/Import
523789Sahrens  *
524789Sahrens  * The import case is identical to an open except that the configuration is sent
525789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
526789Sahrens  * case of an open, the pool configuration will exist in the
527789Sahrens  * POOL_STATE_UNITIALIZED state.
528789Sahrens  *
529789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
530789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
531789Sahrens  * ambiguous state.
532789Sahrens  */
533789Sahrens static int
534789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
535789Sahrens {
536789Sahrens 	spa_t *spa;
537789Sahrens 	int error;
538789Sahrens 	int loaded = B_FALSE;
539789Sahrens 	int locked = B_FALSE;
540789Sahrens 
541789Sahrens 	*spapp = NULL;
542789Sahrens 
543789Sahrens 	/*
544789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
545789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
546789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
547789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
548789Sahrens 	 */
549789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
550789Sahrens 		mutex_enter(&spa_namespace_lock);
551789Sahrens 		locked = B_TRUE;
552789Sahrens 	}
553789Sahrens 
554789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
555789Sahrens 		if (locked)
556789Sahrens 			mutex_exit(&spa_namespace_lock);
557789Sahrens 		return (ENOENT);
558789Sahrens 	}
559789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
560789Sahrens 
561789Sahrens 		spa_activate(spa);
562789Sahrens 
5631635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
564789Sahrens 
565789Sahrens 		if (error == EBADF) {
566789Sahrens 			/*
567789Sahrens 			 * If vdev_load() returns EBADF, it indicates that one
568789Sahrens 			 * of the vdevs indicates that the pool has been
569789Sahrens 			 * exported or destroyed.  If this is the case, the
570789Sahrens 			 * config cache is out of sync and we should remove the
571789Sahrens 			 * pool from the namespace.
572789Sahrens 			 */
573789Sahrens 			spa_unload(spa);
574789Sahrens 			spa_deactivate(spa);
575789Sahrens 			spa_remove(spa);
576789Sahrens 			spa_config_sync();
577789Sahrens 			if (locked)
578789Sahrens 				mutex_exit(&spa_namespace_lock);
579789Sahrens 			return (ENOENT);
5801544Seschrock 		}
5811544Seschrock 
5821544Seschrock 		if (error) {
583789Sahrens 			/*
584789Sahrens 			 * We can't open the pool, but we still have useful
585789Sahrens 			 * information: the state of each vdev after the
586789Sahrens 			 * attempted vdev_open().  Return this to the user.
587789Sahrens 			 */
5881635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
5891635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
590789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
591789Sahrens 				    B_TRUE);
5921635Sbonwick 				spa_config_exit(spa, FTAG);
5931635Sbonwick 			}
594789Sahrens 			spa_unload(spa);
595789Sahrens 			spa_deactivate(spa);
5961544Seschrock 			spa->spa_last_open_failed = B_TRUE;
597789Sahrens 			if (locked)
598789Sahrens 				mutex_exit(&spa_namespace_lock);
599789Sahrens 			*spapp = NULL;
600789Sahrens 			return (error);
6011544Seschrock 		} else {
6021544Seschrock 			zfs_post_ok(spa, NULL);
6031544Seschrock 			spa->spa_last_open_failed = B_FALSE;
604789Sahrens 		}
605789Sahrens 
606789Sahrens 		loaded = B_TRUE;
607789Sahrens 	}
608789Sahrens 
609789Sahrens 	spa_open_ref(spa, tag);
610789Sahrens 	if (locked)
611789Sahrens 		mutex_exit(&spa_namespace_lock);
612789Sahrens 
613789Sahrens 	*spapp = spa;
614789Sahrens 
615789Sahrens 	if (config != NULL) {
6161544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
617789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
6181544Seschrock 		spa_config_exit(spa, FTAG);
619789Sahrens 	}
620789Sahrens 
621789Sahrens 	/*
622789Sahrens 	 * If we just loaded the pool, resilver anything that's out of date.
623789Sahrens 	 */
624789Sahrens 	if (loaded && (spa_mode & FWRITE))
625789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
626789Sahrens 
627789Sahrens 	return (0);
628789Sahrens }
629789Sahrens 
630789Sahrens int
631789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
632789Sahrens {
633789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
634789Sahrens }
635789Sahrens 
6361544Seschrock /*
6371544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
6381544Seschrock  * preventing it from being exported or destroyed.
6391544Seschrock  */
6401544Seschrock spa_t *
6411544Seschrock spa_inject_addref(char *name)
6421544Seschrock {
6431544Seschrock 	spa_t *spa;
6441544Seschrock 
6451544Seschrock 	mutex_enter(&spa_namespace_lock);
6461544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
6471544Seschrock 		mutex_exit(&spa_namespace_lock);
6481544Seschrock 		return (NULL);
6491544Seschrock 	}
6501544Seschrock 	spa->spa_inject_ref++;
6511544Seschrock 	mutex_exit(&spa_namespace_lock);
6521544Seschrock 
6531544Seschrock 	return (spa);
6541544Seschrock }
6551544Seschrock 
6561544Seschrock void
6571544Seschrock spa_inject_delref(spa_t *spa)
6581544Seschrock {
6591544Seschrock 	mutex_enter(&spa_namespace_lock);
6601544Seschrock 	spa->spa_inject_ref--;
6611544Seschrock 	mutex_exit(&spa_namespace_lock);
6621544Seschrock }
6631544Seschrock 
664789Sahrens int
6651544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
666789Sahrens {
667789Sahrens 	int error;
668789Sahrens 	spa_t *spa;
669789Sahrens 
670789Sahrens 	*config = NULL;
671789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
672789Sahrens 
6731544Seschrock 	if (spa && *config != NULL)
6741544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
6751544Seschrock 		    spa_get_errlog_size(spa)) == 0);
6761544Seschrock 
6771544Seschrock 	/*
6781544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
6791544Seschrock 	 * and call spa_lookup() directly.
6801544Seschrock 	 */
6811544Seschrock 	if (altroot) {
6821544Seschrock 		if (spa == NULL) {
6831544Seschrock 			mutex_enter(&spa_namespace_lock);
6841544Seschrock 			spa = spa_lookup(name);
6851544Seschrock 			if (spa)
6861544Seschrock 				spa_altroot(spa, altroot, buflen);
6871544Seschrock 			else
6881544Seschrock 				altroot[0] = '\0';
6891544Seschrock 			spa = NULL;
6901544Seschrock 			mutex_exit(&spa_namespace_lock);
6911544Seschrock 		} else {
6921544Seschrock 			spa_altroot(spa, altroot, buflen);
6931544Seschrock 		}
6941544Seschrock 	}
6951544Seschrock 
696789Sahrens 	if (spa != NULL)
697789Sahrens 		spa_close(spa, FTAG);
698789Sahrens 
699789Sahrens 	return (error);
700789Sahrens }
701789Sahrens 
702789Sahrens /*
703789Sahrens  * Pool Creation
704789Sahrens  */
705789Sahrens int
7061635Sbonwick spa_create(const char *pool, nvlist_t *nvroot, const char *altroot)
707789Sahrens {
708789Sahrens 	spa_t *spa;
7091635Sbonwick 	vdev_t *rvd;
710789Sahrens 	dsl_pool_t *dp;
711789Sahrens 	dmu_tx_t *tx;
7121635Sbonwick 	int c, error;
713789Sahrens 	uint64_t txg = TXG_INITIAL;
714789Sahrens 
715789Sahrens 	/*
716789Sahrens 	 * If this pool already exists, return failure.
717789Sahrens 	 */
718789Sahrens 	mutex_enter(&spa_namespace_lock);
719789Sahrens 	if (spa_lookup(pool) != NULL) {
720789Sahrens 		mutex_exit(&spa_namespace_lock);
721789Sahrens 		return (EEXIST);
722789Sahrens 	}
723789Sahrens 
724789Sahrens 	/*
725789Sahrens 	 * Allocate a new spa_t structure.
726789Sahrens 	 */
7271635Sbonwick 	spa = spa_add(pool, altroot);
728789Sahrens 	spa_activate(spa);
729789Sahrens 
730789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
731789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
732789Sahrens 
7331635Sbonwick 	/*
7341635Sbonwick 	 * Create the root vdev.
7351635Sbonwick 	 */
7361635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
7371635Sbonwick 
7381635Sbonwick 	rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD);
7391635Sbonwick 
7401635Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
7411635Sbonwick 
7421635Sbonwick 	if (rvd == NULL) {
7431635Sbonwick 		error = EINVAL;
7441635Sbonwick 	} else {
7451635Sbonwick 		if ((error = vdev_create(rvd, txg)) == 0) {
7461635Sbonwick 			for (c = 0; c < rvd->vdev_children; c++)
7471635Sbonwick 				vdev_init(rvd->vdev_child[c], txg);
7481635Sbonwick 			vdev_config_dirty(rvd);
7491635Sbonwick 		}
7501635Sbonwick 	}
7511635Sbonwick 
7521635Sbonwick 	spa_config_exit(spa, FTAG);
753789Sahrens 
754789Sahrens 	if (error) {
755789Sahrens 		spa_unload(spa);
756789Sahrens 		spa_deactivate(spa);
757789Sahrens 		spa_remove(spa);
758789Sahrens 		mutex_exit(&spa_namespace_lock);
759789Sahrens 		return (error);
760789Sahrens 	}
761789Sahrens 
762789Sahrens 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg);
763789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
764789Sahrens 
765789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
766789Sahrens 
767789Sahrens 	/*
768789Sahrens 	 * Create the pool config object.
769789Sahrens 	 */
770789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
771789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
772789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
773789Sahrens 
7741544Seschrock 	if (zap_add(spa->spa_meta_objset,
775789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
7761544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
7771544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
7781544Seschrock 	}
779789Sahrens 
780789Sahrens 	/*
781789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
782789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
783789Sahrens 	 * keeps changing.
784789Sahrens 	 */
785789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
786789Sahrens 	    1 << 14, tx);
787789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
788789Sahrens 	    ZIO_COMPRESS_OFF, tx);
789789Sahrens 
7901544Seschrock 	if (zap_add(spa->spa_meta_objset,
791789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
7921544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
7931544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
7941544Seschrock 	}
795789Sahrens 
796789Sahrens 	dmu_tx_commit(tx);
797789Sahrens 
798789Sahrens 	spa->spa_sync_on = B_TRUE;
799789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
800789Sahrens 
801789Sahrens 	/*
802789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
803789Sahrens 	 * bean counters are appropriately updated.
804789Sahrens 	 */
805789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
806789Sahrens 
807789Sahrens 	spa_config_sync();
808789Sahrens 
809789Sahrens 	mutex_exit(&spa_namespace_lock);
810789Sahrens 
811789Sahrens 	return (0);
812789Sahrens }
813789Sahrens 
814789Sahrens /*
815789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
816789Sahrens  * then call spa_load() to do the dirty work.
817789Sahrens  */
818789Sahrens int
8191635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot)
820789Sahrens {
821789Sahrens 	spa_t *spa;
822789Sahrens 	int error;
823789Sahrens 
824789Sahrens 	if (!(spa_mode & FWRITE))
825789Sahrens 		return (EROFS);
826789Sahrens 
827789Sahrens 	/*
828789Sahrens 	 * If a pool with this name exists, return failure.
829789Sahrens 	 */
830789Sahrens 	mutex_enter(&spa_namespace_lock);
831789Sahrens 	if (spa_lookup(pool) != NULL) {
832789Sahrens 		mutex_exit(&spa_namespace_lock);
833789Sahrens 		return (EEXIST);
834789Sahrens 	}
835789Sahrens 
836789Sahrens 	/*
8371635Sbonwick 	 * Create and initialize the spa structure.
838789Sahrens 	 */
8391635Sbonwick 	spa = spa_add(pool, altroot);
840789Sahrens 	spa_activate(spa);
841789Sahrens 
842789Sahrens 	/*
8431635Sbonwick 	 * Pass off the heavy lifting to spa_load().
844*1732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
845*1732Sbonwick 	 * is actually the one to trust when doing an import.
8461601Sbonwick 	 */
847*1732Sbonwick 	error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE);
848789Sahrens 
849789Sahrens 	if (error) {
850789Sahrens 		spa_unload(spa);
851789Sahrens 		spa_deactivate(spa);
852789Sahrens 		spa_remove(spa);
853789Sahrens 		mutex_exit(&spa_namespace_lock);
854789Sahrens 		return (error);
855789Sahrens 	}
856789Sahrens 
8571635Sbonwick 	/*
8581635Sbonwick 	 * Update the config cache to include the newly-imported pool.
8591635Sbonwick 	 */
8601635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
8611635Sbonwick 
862789Sahrens 	mutex_exit(&spa_namespace_lock);
863789Sahrens 
864789Sahrens 	/*
865789Sahrens 	 * Resilver anything that's out of date.
866789Sahrens 	 */
867789Sahrens 	if (spa_mode & FWRITE)
868789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
869789Sahrens 
870789Sahrens 	return (0);
871789Sahrens }
872789Sahrens 
873789Sahrens /*
874789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
875789Sahrens  * to get the vdev stats associated with the imported devices.
876789Sahrens  */
877789Sahrens #define	TRYIMPORT_NAME	"$import"
878789Sahrens 
879789Sahrens nvlist_t *
880789Sahrens spa_tryimport(nvlist_t *tryconfig)
881789Sahrens {
882789Sahrens 	nvlist_t *config = NULL;
883789Sahrens 	char *poolname;
884789Sahrens 	spa_t *spa;
885789Sahrens 	uint64_t state;
886789Sahrens 
887789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
888789Sahrens 		return (NULL);
889789Sahrens 
890789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
891789Sahrens 		return (NULL);
892789Sahrens 
8931635Sbonwick 	/*
8941635Sbonwick 	 * Create and initialize the spa structure.
8951635Sbonwick 	 */
896789Sahrens 	mutex_enter(&spa_namespace_lock);
8971635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
898789Sahrens 	spa_activate(spa);
899789Sahrens 
900789Sahrens 	/*
9011635Sbonwick 	 * Pass off the heavy lifting to spa_load().
902*1732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
903*1732Sbonwick 	 * is actually the one to trust when doing an import.
904789Sahrens 	 */
905*1732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
906789Sahrens 
907789Sahrens 	/*
908789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
909789Sahrens 	 */
910789Sahrens 	if (spa->spa_root_vdev != NULL) {
9111635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
912789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
9131635Sbonwick 		spa_config_exit(spa, FTAG);
914789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
915789Sahrens 		    poolname) == 0);
916789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
917789Sahrens 		    state) == 0);
918789Sahrens 	}
919789Sahrens 
920789Sahrens 	spa_unload(spa);
921789Sahrens 	spa_deactivate(spa);
922789Sahrens 	spa_remove(spa);
923789Sahrens 	mutex_exit(&spa_namespace_lock);
924789Sahrens 
925789Sahrens 	return (config);
926789Sahrens }
927789Sahrens 
928789Sahrens /*
929789Sahrens  * Pool export/destroy
930789Sahrens  *
931789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
932789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
933789Sahrens  * update the pool state and sync all the labels to disk, removing the
934789Sahrens  * configuration from the cache afterwards.
935789Sahrens  */
936789Sahrens static int
937789Sahrens spa_export_common(char *pool, int new_state)
938789Sahrens {
939789Sahrens 	spa_t *spa;
940789Sahrens 
941789Sahrens 	if (!(spa_mode & FWRITE))
942789Sahrens 		return (EROFS);
943789Sahrens 
944789Sahrens 	mutex_enter(&spa_namespace_lock);
945789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
946789Sahrens 		mutex_exit(&spa_namespace_lock);
947789Sahrens 		return (ENOENT);
948789Sahrens 	}
949789Sahrens 
950789Sahrens 	/*
9511544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
9521544Seschrock 	 * reacquire the namespace lock, and see if we can export.
9531544Seschrock 	 */
9541544Seschrock 	spa_open_ref(spa, FTAG);
9551544Seschrock 	mutex_exit(&spa_namespace_lock);
9561544Seschrock 	spa_async_suspend(spa);
9571544Seschrock 	mutex_enter(&spa_namespace_lock);
9581544Seschrock 	spa_close(spa, FTAG);
9591544Seschrock 
9601544Seschrock 	/*
961789Sahrens 	 * The pool will be in core if it's openable,
962789Sahrens 	 * in which case we can modify its state.
963789Sahrens 	 */
964789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
965789Sahrens 		/*
966789Sahrens 		 * Objsets may be open only because they're dirty, so we
967789Sahrens 		 * have to force it to sync before checking spa_refcnt.
968789Sahrens 		 */
969789Sahrens 		spa_scrub_suspend(spa);
970789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
971789Sahrens 
9721544Seschrock 		/*
9731544Seschrock 		 * A pool cannot be exported or destroyed if there are active
9741544Seschrock 		 * references.  If we are resetting a pool, allow references by
9751544Seschrock 		 * fault injection handlers.
9761544Seschrock 		 */
9771544Seschrock 		if (!spa_refcount_zero(spa) ||
9781544Seschrock 		    (spa->spa_inject_ref != 0 &&
9791544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
980789Sahrens 			spa_scrub_resume(spa);
9811544Seschrock 			spa_async_resume(spa);
982789Sahrens 			mutex_exit(&spa_namespace_lock);
983789Sahrens 			return (EBUSY);
984789Sahrens 		}
985789Sahrens 
986789Sahrens 		spa_scrub_resume(spa);
987789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
988789Sahrens 
989789Sahrens 		/*
990789Sahrens 		 * We want this to be reflected on every label,
991789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
992789Sahrens 		 * final sync that pushes these changes out.
993789Sahrens 		 */
9941544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
9951601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
9961544Seschrock 			spa->spa_state = new_state;
9971635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
9981544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
9991601Sbonwick 			spa_config_exit(spa, FTAG);
10001544Seschrock 		}
1001789Sahrens 	}
1002789Sahrens 
1003789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
1004789Sahrens 		spa_unload(spa);
1005789Sahrens 		spa_deactivate(spa);
1006789Sahrens 	}
1007789Sahrens 
10081544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
10091544Seschrock 		spa_remove(spa);
10101544Seschrock 		spa_config_sync();
10111544Seschrock 	}
1012789Sahrens 	mutex_exit(&spa_namespace_lock);
1013789Sahrens 
1014789Sahrens 	return (0);
1015789Sahrens }
1016789Sahrens 
1017789Sahrens /*
1018789Sahrens  * Destroy a storage pool.
1019789Sahrens  */
1020789Sahrens int
1021789Sahrens spa_destroy(char *pool)
1022789Sahrens {
1023789Sahrens 	return (spa_export_common(pool, POOL_STATE_DESTROYED));
1024789Sahrens }
1025789Sahrens 
1026789Sahrens /*
1027789Sahrens  * Export a storage pool.
1028789Sahrens  */
1029789Sahrens int
1030789Sahrens spa_export(char *pool)
1031789Sahrens {
1032789Sahrens 	return (spa_export_common(pool, POOL_STATE_EXPORTED));
1033789Sahrens }
1034789Sahrens 
1035789Sahrens /*
10361544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
10371544Seschrock  * from the namespace in any way.
10381544Seschrock  */
10391544Seschrock int
10401544Seschrock spa_reset(char *pool)
10411544Seschrock {
10421544Seschrock 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED));
10431544Seschrock }
10441544Seschrock 
10451544Seschrock 
10461544Seschrock /*
1047789Sahrens  * ==========================================================================
1048789Sahrens  * Device manipulation
1049789Sahrens  * ==========================================================================
1050789Sahrens  */
1051789Sahrens 
1052789Sahrens /*
1053789Sahrens  * Add capacity to a storage pool.
1054789Sahrens  */
1055789Sahrens int
1056789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
1057789Sahrens {
1058789Sahrens 	uint64_t txg;
10591635Sbonwick 	int c, error;
1060789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
10611585Sbonwick 	vdev_t *vd, *tvd;
1062789Sahrens 
1063789Sahrens 	txg = spa_vdev_enter(spa);
1064789Sahrens 
1065789Sahrens 	vd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD);
1066789Sahrens 
1067789Sahrens 	if (vd == NULL)
1068789Sahrens 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
1069789Sahrens 
1070789Sahrens 	if ((error = vdev_create(vd, txg)) != 0)
1071789Sahrens 		return (spa_vdev_exit(spa, vd, txg, error));
1072789Sahrens 
1073789Sahrens 	/*
10741585Sbonwick 	 * Transfer each new top-level vdev from vd to rvd.
1075789Sahrens 	 */
10761635Sbonwick 	for (c = 0; c < vd->vdev_children; c++) {
10771585Sbonwick 		tvd = vd->vdev_child[c];
10781635Sbonwick 		vdev_remove_child(vd, tvd);
10791635Sbonwick 		tvd->vdev_id = rvd->vdev_children;
10801635Sbonwick 		vdev_add_child(rvd, tvd);
1081789Sahrens 		vdev_config_dirty(tvd);
1082789Sahrens 	}
1083789Sahrens 
1084789Sahrens 	/*
10851585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
10861585Sbonwick 	 * If other threads start allocating from these vdevs before we
10871585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
10881585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
10891585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
10901585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
10911635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
10921585Sbonwick 	 *
10931585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
10941585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
10951585Sbonwick 	 * steps will be completed the next time we load the pool.
1096789Sahrens 	 */
10971635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
10981585Sbonwick 
10991635Sbonwick 	mutex_enter(&spa_namespace_lock);
11001635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
11011635Sbonwick 	mutex_exit(&spa_namespace_lock);
1102789Sahrens 
11031635Sbonwick 	return (0);
1104789Sahrens }
1105789Sahrens 
1106789Sahrens /*
1107789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
1108789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
1109789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
1110789Sahrens  *
1111789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
1112789Sahrens  * existing device; in this case the two devices are made into their own
1113789Sahrens  * mirror using the 'replacing' vdev, which is functionally idendical to
1114789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
1115789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
1116789Sahrens  * completion of resilvering, the first disk (the one being replaced)
1117789Sahrens  * is automatically detached.
1118789Sahrens  */
1119789Sahrens int
11201544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
1121789Sahrens {
1122789Sahrens 	uint64_t txg, open_txg;
1123789Sahrens 	int error;
1124789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1125789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
1126789Sahrens 	vdev_ops_t *pvops = replacing ? &vdev_replacing_ops : &vdev_mirror_ops;
1127789Sahrens 
1128789Sahrens 	txg = spa_vdev_enter(spa);
1129789Sahrens 
11301544Seschrock 	oldvd = vdev_lookup_by_guid(rvd, guid);
1131789Sahrens 
1132789Sahrens 	if (oldvd == NULL)
1133789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1134789Sahrens 
11351585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
11361585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
11371585Sbonwick 
1138789Sahrens 	pvd = oldvd->vdev_parent;
1139789Sahrens 
1140789Sahrens 	/*
1141789Sahrens 	 * The parent must be a mirror or the root, unless we're replacing;
1142789Sahrens 	 * in that case, the parent can be anything but another replacing vdev.
1143789Sahrens 	 */
1144789Sahrens 	if (pvd->vdev_ops != &vdev_mirror_ops &&
1145789Sahrens 	    pvd->vdev_ops != &vdev_root_ops &&
1146789Sahrens 	    (!replacing || pvd->vdev_ops == &vdev_replacing_ops))
1147789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1148789Sahrens 
1149789Sahrens 	newrootvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD);
1150789Sahrens 
1151789Sahrens 	if (newrootvd == NULL || newrootvd->vdev_children != 1)
1152789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
1153789Sahrens 
1154789Sahrens 	newvd = newrootvd->vdev_child[0];
1155789Sahrens 
1156789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
1157789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
1158789Sahrens 
1159789Sahrens 	if ((error = vdev_create(newrootvd, txg)) != 0)
1160789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
1161789Sahrens 
11621175Slling 	/*
11631175Slling 	 * Compare the new device size with the replaceable/attachable
11641175Slling 	 * device size.
11651175Slling 	 */
11661175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
1167789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
1168789Sahrens 
1169*1732Sbonwick 	/*
1170*1732Sbonwick 	 * The new device cannot have a higher alignment requirement
1171*1732Sbonwick 	 * than the top-level vdev.
1172*1732Sbonwick 	 */
1173*1732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
1174789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
1175789Sahrens 
1176789Sahrens 	/*
1177789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
1178789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
1179789Sahrens 	 */
1180789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
1181789Sahrens 		spa_strfree(oldvd->vdev_path);
1182789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
1183789Sahrens 		    KM_SLEEP);
1184789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
1185789Sahrens 		    newvd->vdev_path, "old");
1186789Sahrens 		if (oldvd->vdev_devid != NULL) {
1187789Sahrens 			spa_strfree(oldvd->vdev_devid);
1188789Sahrens 			oldvd->vdev_devid = NULL;
1189789Sahrens 		}
1190789Sahrens 	}
1191789Sahrens 
1192789Sahrens 	/*
1193789Sahrens 	 * If the parent is not a mirror, or if we're replacing,
1194789Sahrens 	 * insert the new mirror/replacing vdev above oldvd.
1195789Sahrens 	 */
1196789Sahrens 	if (pvd->vdev_ops != pvops)
1197789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
1198789Sahrens 
1199789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
1200789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
1201789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
1202789Sahrens 
1203789Sahrens 	/*
1204789Sahrens 	 * Extract the new device from its root and add it to pvd.
1205789Sahrens 	 */
1206789Sahrens 	vdev_remove_child(newrootvd, newvd);
1207789Sahrens 	newvd->vdev_id = pvd->vdev_children;
1208789Sahrens 	vdev_add_child(pvd, newvd);
1209789Sahrens 
12101544Seschrock 	/*
12111544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
12121544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
12131544Seschrock 	 */
12141544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
12151544Seschrock 
1216789Sahrens 	tvd = newvd->vdev_top;
1217789Sahrens 	ASSERT(pvd->vdev_top == tvd);
1218789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
1219789Sahrens 
1220789Sahrens 	vdev_config_dirty(tvd);
1221789Sahrens 
1222789Sahrens 	/*
1223789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
1224789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
1225789Sahrens 	 */
1226789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
1227789Sahrens 
1228789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
1229789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
1230789Sahrens 	    open_txg - TXG_INITIAL + 1);
1231789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
1232789Sahrens 
12331544Seschrock 	dprintf("attached %s in txg %llu\n", newvd->vdev_path, txg);
12341544Seschrock 
1235789Sahrens 	/*
1236789Sahrens 	 * Mark newvd's DTL dirty in this txg.
1237789Sahrens 	 */
1238*1732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
1239789Sahrens 
1240789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
1241789Sahrens 
1242789Sahrens 	/*
1243789Sahrens 	 * Kick off a resilver to update newvd.
1244789Sahrens 	 */
1245789Sahrens 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1246789Sahrens 
1247789Sahrens 	return (0);
1248789Sahrens }
1249789Sahrens 
1250789Sahrens /*
1251789Sahrens  * Detach a device from a mirror or replacing vdev.
1252789Sahrens  * If 'replace_done' is specified, only detach if the parent
1253789Sahrens  * is a replacing vdev.
1254789Sahrens  */
1255789Sahrens int
12561544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
1257789Sahrens {
1258789Sahrens 	uint64_t txg;
1259789Sahrens 	int c, t, error;
1260789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1261789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
1262789Sahrens 
1263789Sahrens 	txg = spa_vdev_enter(spa);
1264789Sahrens 
12651544Seschrock 	vd = vdev_lookup_by_guid(rvd, guid);
1266789Sahrens 
1267789Sahrens 	if (vd == NULL)
1268789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1269789Sahrens 
12701585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
12711585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
12721585Sbonwick 
1273789Sahrens 	pvd = vd->vdev_parent;
1274789Sahrens 
1275789Sahrens 	/*
1276789Sahrens 	 * If replace_done is specified, only remove this device if it's
1277789Sahrens 	 * the first child of a replacing vdev.
1278789Sahrens 	 */
1279789Sahrens 	if (replace_done &&
1280789Sahrens 	    (vd->vdev_id != 0 || pvd->vdev_ops != &vdev_replacing_ops))
1281789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1282789Sahrens 
1283789Sahrens 	/*
1284789Sahrens 	 * Only mirror and replacing vdevs support detach.
1285789Sahrens 	 */
1286789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
1287789Sahrens 	    pvd->vdev_ops != &vdev_mirror_ops)
1288789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1289789Sahrens 
1290789Sahrens 	/*
1291789Sahrens 	 * If there's only one replica, you can't detach it.
1292789Sahrens 	 */
1293789Sahrens 	if (pvd->vdev_children <= 1)
1294789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1295789Sahrens 
1296789Sahrens 	/*
1297789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
1298789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
1299789Sahrens 	 *
1300789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
1301789Sahrens 	 * precise DTL check.
1302789Sahrens 	 */
1303789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
1304789Sahrens 		uint64_t dirty;
1305789Sahrens 
1306789Sahrens 		cvd = pvd->vdev_child[c];
1307789Sahrens 		if (cvd == vd)
1308789Sahrens 			continue;
1309789Sahrens 		if (vdev_is_dead(cvd))
1310789Sahrens 			continue;
1311789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
1312789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
1313789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
1314789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
1315789Sahrens 		if (!dirty)
1316789Sahrens 			break;
1317789Sahrens 	}
1318789Sahrens 	if (c == pvd->vdev_children)
1319789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1320789Sahrens 
1321789Sahrens 	/*
1322789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
1323789Sahrens 	 * This must be done after all other error cases are handled,
1324789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
1325789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
1326789Sahrens 	 * it may be that the unwritability of the disk is the reason
1327789Sahrens 	 * it's being detached!
1328789Sahrens 	 */
1329789Sahrens 	error = vdev_label_init(vd, 0);
1330789Sahrens 	if (error)
1331789Sahrens 		dprintf("unable to erase labels on %s\n", vdev_description(vd));
1332789Sahrens 
1333789Sahrens 	/*
1334789Sahrens 	 * Remove vd from its parent and compact the parent's children.
1335789Sahrens 	 */
1336789Sahrens 	vdev_remove_child(pvd, vd);
1337789Sahrens 	vdev_compact_children(pvd);
1338789Sahrens 
1339789Sahrens 	/*
1340789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
1341789Sahrens 	 */
1342789Sahrens 	cvd = pvd->vdev_child[0];
1343789Sahrens 
1344789Sahrens 	/*
1345789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
1346789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
1347789Sahrens 	 */
1348789Sahrens 	if (pvd->vdev_children == 1)
1349789Sahrens 		vdev_remove_parent(cvd);
1350789Sahrens 
1351789Sahrens 	/*
1352789Sahrens 	 * We don't set tvd until now because the parent we just removed
1353789Sahrens 	 * may have been the previous top-level vdev.
1354789Sahrens 	 */
1355789Sahrens 	tvd = cvd->vdev_top;
1356789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
1357789Sahrens 
1358789Sahrens 	/*
1359789Sahrens 	 * Reopen this top-level vdev to reassess health after detach.
1360789Sahrens 	 */
13611544Seschrock 	vdev_reopen(tvd);
1362789Sahrens 
1363789Sahrens 	/*
1364789Sahrens 	 * If the device we just detached was smaller than the others,
1365*1732Sbonwick 	 * it may be possible to add metaslabs (i.e. grow the pool).
1366*1732Sbonwick 	 * vdev_metaslab_init() can't fail because the existing metaslabs
1367*1732Sbonwick 	 * are already in core, so there's nothing to read from disk.
1368789Sahrens 	 */
1369*1732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
1370789Sahrens 
1371789Sahrens 	vdev_config_dirty(tvd);
1372789Sahrens 
1373789Sahrens 	/*
1374789Sahrens 	 * Mark vd's DTL as dirty in this txg.
1375789Sahrens 	 * vdev_dtl_sync() will see that vd->vdev_detached is set
1376789Sahrens 	 * and free vd's DTL object in syncing context.
1377789Sahrens 	 * But first make sure we're not on any *other* txg's DTL list,
1378789Sahrens 	 * to prevent vd from being accessed after it's freed.
1379789Sahrens 	 */
1380789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
1381789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
1382*1732Sbonwick 	vd->vdev_detached = B_TRUE;
1383*1732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
1384789Sahrens 
13851544Seschrock 	dprintf("detached %s in txg %llu\n", vd->vdev_path, txg);
1386789Sahrens 
1387789Sahrens 	return (spa_vdev_exit(spa, vd, txg, 0));
1388789Sahrens }
1389789Sahrens 
1390789Sahrens /*
13911544Seschrock  * Find any device that's done replacing, so we can detach it.
1392789Sahrens  */
13931544Seschrock static vdev_t *
13941544Seschrock spa_vdev_replace_done_hunt(vdev_t *vd)
1395789Sahrens {
13961544Seschrock 	vdev_t *newvd, *oldvd;
1397789Sahrens 	int c;
1398789Sahrens 
13991544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
14001544Seschrock 		oldvd = spa_vdev_replace_done_hunt(vd->vdev_child[c]);
14011544Seschrock 		if (oldvd != NULL)
14021544Seschrock 			return (oldvd);
14031544Seschrock 	}
1404789Sahrens 
1405789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
14061544Seschrock 		oldvd = vd->vdev_child[0];
14071544Seschrock 		newvd = vd->vdev_child[1];
1408789Sahrens 
14091544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
14101544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
14111544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
14121544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
14131544Seschrock 			return (oldvd);
14141544Seschrock 		}
14151544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
14161544Seschrock 	}
1417789Sahrens 
14181544Seschrock 	return (NULL);
1419789Sahrens }
1420789Sahrens 
14211544Seschrock static void
1422789Sahrens spa_vdev_replace_done(spa_t *spa)
1423789Sahrens {
14241544Seschrock 	vdev_t *vd;
14251544Seschrock 	uint64_t guid;
1426789Sahrens 
14271544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
1428789Sahrens 
14291544Seschrock 	while ((vd = spa_vdev_replace_done_hunt(spa->spa_root_vdev)) != NULL) {
14301544Seschrock 		guid = vd->vdev_guid;
14311544Seschrock 		spa_config_exit(spa, FTAG);
14321544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
14331544Seschrock 			return;
14341544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1435789Sahrens 	}
1436789Sahrens 
14371544Seschrock 	spa_config_exit(spa, FTAG);
1438789Sahrens }
1439789Sahrens 
1440789Sahrens /*
14411354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
14421354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
14431354Seschrock  */
14441354Seschrock int
14451354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
14461354Seschrock {
14471354Seschrock 	vdev_t *rvd, *vd;
14481354Seschrock 	uint64_t txg;
14491354Seschrock 
14501354Seschrock 	rvd = spa->spa_root_vdev;
14511354Seschrock 
14521354Seschrock 	txg = spa_vdev_enter(spa);
14531354Seschrock 
14541354Seschrock 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
14551354Seschrock 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
14561354Seschrock 
14571585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
14581585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
14591585Sbonwick 
14601354Seschrock 	spa_strfree(vd->vdev_path);
14611354Seschrock 	vd->vdev_path = spa_strdup(newpath);
14621354Seschrock 
14631354Seschrock 	vdev_config_dirty(vd->vdev_top);
14641354Seschrock 
14651354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
14661354Seschrock }
14671354Seschrock 
14681354Seschrock /*
1469789Sahrens  * ==========================================================================
1470789Sahrens  * SPA Scrubbing
1471789Sahrens  * ==========================================================================
1472789Sahrens  */
1473789Sahrens 
14741544Seschrock void
14751544Seschrock spa_scrub_throttle(spa_t *spa, int direction)
14761544Seschrock {
14771544Seschrock 	mutex_enter(&spa->spa_scrub_lock);
14781544Seschrock 	spa->spa_scrub_throttled += direction;
14791544Seschrock 	ASSERT(spa->spa_scrub_throttled >= 0);
14801544Seschrock 	if (spa->spa_scrub_throttled == 0)
14811544Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
14821544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
14831544Seschrock }
1484789Sahrens 
1485789Sahrens static void
1486789Sahrens spa_scrub_io_done(zio_t *zio)
1487789Sahrens {
1488789Sahrens 	spa_t *spa = zio->io_spa;
1489789Sahrens 
1490789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
1491789Sahrens 
1492789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
14931544Seschrock 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
14941544Seschrock 		vdev_t *vd = zio->io_vd;
1495789Sahrens 		spa->spa_scrub_errors++;
1496789Sahrens 		mutex_enter(&vd->vdev_stat_lock);
1497789Sahrens 		vd->vdev_stat.vs_scrub_errors++;
1498789Sahrens 		mutex_exit(&vd->vdev_stat_lock);
1499789Sahrens 	}
15001544Seschrock 	if (--spa->spa_scrub_inflight == 0) {
15011544Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
15021544Seschrock 		ASSERT(spa->spa_scrub_throttled == 0);
15031544Seschrock 	}
15041544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
1505789Sahrens }
1506789Sahrens 
1507789Sahrens static void
15081544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags,
15091544Seschrock     zbookmark_t *zb)
1510789Sahrens {
1511789Sahrens 	size_t size = BP_GET_LSIZE(bp);
1512789Sahrens 	void *data = zio_buf_alloc(size);
1513789Sahrens 
1514789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1515789Sahrens 	spa->spa_scrub_inflight++;
1516789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1517789Sahrens 
15181544Seschrock 	if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
15191544Seschrock 		flags |= ZIO_FLAG_SPECULATIVE;	/* intent log block */
15201544Seschrock 
15211544Seschrock 	flags |= ZIO_FLAG_CANFAIL;
15221544Seschrock 
1523789Sahrens 	zio_nowait(zio_read(NULL, spa, bp, data, size,
15241544Seschrock 	    spa_scrub_io_done, NULL, priority, flags, zb));
1525789Sahrens }
1526789Sahrens 
1527789Sahrens /* ARGSUSED */
1528789Sahrens static int
1529789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
1530789Sahrens {
1531789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
1532789Sahrens 	vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[0]));
1533789Sahrens 
1534789Sahrens 	if (bc->bc_errno || vd == NULL) {
1535789Sahrens 		/*
1536789Sahrens 		 * We can't scrub this block, but we can continue to scrub
1537789Sahrens 		 * the rest of the pool.  Note the error and move along.
1538789Sahrens 		 */
1539789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
1540789Sahrens 		spa->spa_scrub_errors++;
1541789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
1542789Sahrens 
1543789Sahrens 		if (vd != NULL) {
1544789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
1545789Sahrens 			vd->vdev_stat.vs_scrub_errors++;
1546789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
1547789Sahrens 		}
1548789Sahrens 
1549789Sahrens 		return (ERESTART);
1550789Sahrens 	}
1551789Sahrens 
1552789Sahrens 	ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg);
1553789Sahrens 
1554789Sahrens 	/*
1555789Sahrens 	 * Keep track of how much data we've examined so that
1556789Sahrens 	 * zpool(1M) status can make useful progress reports.
1557789Sahrens 	 */
1558789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
1559789Sahrens 	vd->vdev_stat.vs_scrub_examined += BP_GET_ASIZE(bp);
1560789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
1561789Sahrens 
1562789Sahrens 	if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) {
1563789Sahrens 		if (DVA_GET_GANG(&bp->blk_dva[0])) {
1564789Sahrens 			/*
1565789Sahrens 			 * Gang members may be spread across multiple vdevs,
1566789Sahrens 			 * so the best we can do is look at the pool-wide DTL.
1567789Sahrens 			 * XXX -- it would be better to change our allocation
1568789Sahrens 			 * policy to ensure that this can't happen.
1569789Sahrens 			 */
1570789Sahrens 			vd = spa->spa_root_vdev;
1571789Sahrens 		}
1572789Sahrens 		if (vdev_dtl_contains(&vd->vdev_dtl_map, bp->blk_birth, 1)) {
1573789Sahrens 			spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER,
15741544Seschrock 			    ZIO_FLAG_RESILVER, &bc->bc_bookmark);
1575789Sahrens 		}
1576789Sahrens 	} else {
1577789Sahrens 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB,
15781544Seschrock 		    ZIO_FLAG_SCRUB, &bc->bc_bookmark);
1579789Sahrens 	}
1580789Sahrens 
1581789Sahrens 	return (0);
1582789Sahrens }
1583789Sahrens 
1584789Sahrens static void
1585789Sahrens spa_scrub_thread(spa_t *spa)
1586789Sahrens {
1587789Sahrens 	callb_cpr_t cprinfo;
1588789Sahrens 	traverse_handle_t *th = spa->spa_scrub_th;
1589789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1590789Sahrens 	pool_scrub_type_t scrub_type = spa->spa_scrub_type;
1591789Sahrens 	int error = 0;
1592789Sahrens 	boolean_t complete;
1593789Sahrens 
1594789Sahrens 	CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG);
1595789Sahrens 
1596797Sbonwick 	/*
1597797Sbonwick 	 * If we're restarting due to a snapshot create/delete,
1598797Sbonwick 	 * wait for that to complete.
1599797Sbonwick 	 */
1600797Sbonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
1601797Sbonwick 
16021544Seschrock 	dprintf("start %s mintxg=%llu maxtxg=%llu\n",
16031544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
16041544Seschrock 	    spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg);
16051544Seschrock 
16061544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
16071544Seschrock 	vdev_reopen(rvd);		/* purge all vdev caches */
1608789Sahrens 	vdev_config_dirty(rvd);		/* rewrite all disk labels */
1609789Sahrens 	vdev_scrub_stat_update(rvd, scrub_type, B_FALSE);
16101544Seschrock 	spa_config_exit(spa, FTAG);
1611789Sahrens 
1612789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1613789Sahrens 	spa->spa_scrub_errors = 0;
1614789Sahrens 	spa->spa_scrub_active = 1;
16151544Seschrock 	ASSERT(spa->spa_scrub_inflight == 0);
16161544Seschrock 	ASSERT(spa->spa_scrub_throttled == 0);
1617789Sahrens 
1618789Sahrens 	while (!spa->spa_scrub_stop) {
1619789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
16201544Seschrock 		while (spa->spa_scrub_suspended) {
1621789Sahrens 			spa->spa_scrub_active = 0;
1622789Sahrens 			cv_broadcast(&spa->spa_scrub_cv);
1623789Sahrens 			cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
1624789Sahrens 			spa->spa_scrub_active = 1;
1625789Sahrens 		}
1626789Sahrens 		CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock);
1627789Sahrens 
1628789Sahrens 		if (spa->spa_scrub_restart_txg != 0)
1629789Sahrens 			break;
1630789Sahrens 
1631789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
1632789Sahrens 		error = traverse_more(th);
1633789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
1634789Sahrens 		if (error != EAGAIN)
1635789Sahrens 			break;
16361544Seschrock 
16371544Seschrock 		while (spa->spa_scrub_throttled > 0)
16381544Seschrock 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1639789Sahrens 	}
1640789Sahrens 
1641789Sahrens 	while (spa->spa_scrub_inflight)
1642789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1643789Sahrens 
16441601Sbonwick 	spa->spa_scrub_active = 0;
16451601Sbonwick 	cv_broadcast(&spa->spa_scrub_cv);
16461601Sbonwick 
16471601Sbonwick 	mutex_exit(&spa->spa_scrub_lock);
16481601Sbonwick 
16491601Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
16501601Sbonwick 
16511601Sbonwick 	mutex_enter(&spa->spa_scrub_lock);
16521601Sbonwick 
16531601Sbonwick 	/*
16541601Sbonwick 	 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock
16551601Sbonwick 	 * AND the spa config lock to synchronize with any config changes
16561601Sbonwick 	 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
16571601Sbonwick 	 */
1658789Sahrens 	if (spa->spa_scrub_restart_txg != 0)
1659789Sahrens 		error = ERESTART;
1660789Sahrens 
16611544Seschrock 	if (spa->spa_scrub_stop)
16621544Seschrock 		error = EINTR;
16631544Seschrock 
1664789Sahrens 	/*
16651544Seschrock 	 * Even if there were uncorrectable errors, we consider the scrub
16661544Seschrock 	 * completed.  The downside is that if there is a transient error during
16671544Seschrock 	 * a resilver, we won't resilver the data properly to the target.  But
16681544Seschrock 	 * if the damage is permanent (more likely) we will resilver forever,
16691544Seschrock 	 * which isn't really acceptable.  Since there is enough information for
16701544Seschrock 	 * the user to know what has failed and why, this seems like a more
16711544Seschrock 	 * tractable approach.
1672789Sahrens 	 */
16731544Seschrock 	complete = (error == 0);
1674789Sahrens 
16751544Seschrock 	dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
16761544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
1677789Sahrens 	    spa->spa_scrub_maxtxg, complete ? "done" : "FAILED",
1678789Sahrens 	    error, spa->spa_scrub_errors, spa->spa_scrub_stop);
1679789Sahrens 
1680789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1681789Sahrens 
1682789Sahrens 	/*
1683789Sahrens 	 * If the scrub/resilver completed, update all DTLs to reflect this.
1684789Sahrens 	 * Whether it succeeded or not, vacate all temporary scrub DTLs.
1685789Sahrens 	 */
1686789Sahrens 	vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1,
1687789Sahrens 	    complete ? spa->spa_scrub_maxtxg : 0, B_TRUE);
1688789Sahrens 	vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete);
16891544Seschrock 	spa_errlog_rotate(spa);
16901601Sbonwick 
16911544Seschrock 	spa_config_exit(spa, FTAG);
1692789Sahrens 
1693789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1694789Sahrens 
16951544Seschrock 	/*
16961544Seschrock 	 * We may have finished replacing a device.
16971544Seschrock 	 * Let the async thread assess this and handle the detach.
16981544Seschrock 	 */
16991544Seschrock 	spa_async_request(spa, SPA_ASYNC_REPLACE_DONE);
1700789Sahrens 
1701789Sahrens 	/*
1702789Sahrens 	 * If we were told to restart, our final act is to start a new scrub.
1703789Sahrens 	 */
1704789Sahrens 	if (error == ERESTART)
17051544Seschrock 		spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ?
17061544Seschrock 		    SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB);
1707789Sahrens 
17081544Seschrock 	spa->spa_scrub_type = POOL_SCRUB_NONE;
17091544Seschrock 	spa->spa_scrub_active = 0;
17101544Seschrock 	spa->spa_scrub_thread = NULL;
17111544Seschrock 	cv_broadcast(&spa->spa_scrub_cv);
1712789Sahrens 	CALLB_CPR_EXIT(&cprinfo);	/* drops &spa->spa_scrub_lock */
1713789Sahrens 	thread_exit();
1714789Sahrens }
1715789Sahrens 
1716789Sahrens void
1717789Sahrens spa_scrub_suspend(spa_t *spa)
1718789Sahrens {
1719789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
17201544Seschrock 	spa->spa_scrub_suspended++;
1721789Sahrens 	while (spa->spa_scrub_active) {
1722789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
1723789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
1724789Sahrens 	}
1725789Sahrens 	while (spa->spa_scrub_inflight)
1726789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1727789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1728789Sahrens }
1729789Sahrens 
1730789Sahrens void
1731789Sahrens spa_scrub_resume(spa_t *spa)
1732789Sahrens {
1733789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
17341544Seschrock 	ASSERT(spa->spa_scrub_suspended != 0);
17351544Seschrock 	if (--spa->spa_scrub_suspended == 0)
1736789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
1737789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1738789Sahrens }
1739789Sahrens 
1740789Sahrens void
1741789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg)
1742789Sahrens {
1743789Sahrens 	/*
1744789Sahrens 	 * Something happened (e.g. snapshot create/delete) that means
1745789Sahrens 	 * we must restart any in-progress scrubs.  The itinerary will
1746789Sahrens 	 * fix this properly.
1747789Sahrens 	 */
1748789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1749789Sahrens 	spa->spa_scrub_restart_txg = txg;
1750789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1751789Sahrens }
1752789Sahrens 
17531544Seschrock int
17541544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force)
1755789Sahrens {
1756789Sahrens 	space_seg_t *ss;
1757789Sahrens 	uint64_t mintxg, maxtxg;
1758789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1759789Sahrens 
1760789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
1761789Sahrens 		return (ENOTSUP);
1762789Sahrens 
17631544Seschrock 	mutex_enter(&spa->spa_scrub_lock);
17641544Seschrock 
1765789Sahrens 	/*
1766789Sahrens 	 * If there's a scrub or resilver already in progress, stop it.
1767789Sahrens 	 */
1768789Sahrens 	while (spa->spa_scrub_thread != NULL) {
1769789Sahrens 		/*
1770789Sahrens 		 * Don't stop a resilver unless forced.
1771789Sahrens 		 */
17721544Seschrock 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) {
17731544Seschrock 			mutex_exit(&spa->spa_scrub_lock);
1774789Sahrens 			return (EBUSY);
17751544Seschrock 		}
1776789Sahrens 		spa->spa_scrub_stop = 1;
1777789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
1778789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
1779789Sahrens 	}
1780789Sahrens 
1781789Sahrens 	/*
1782789Sahrens 	 * Terminate the previous traverse.
1783789Sahrens 	 */
1784789Sahrens 	if (spa->spa_scrub_th != NULL) {
1785789Sahrens 		traverse_fini(spa->spa_scrub_th);
1786789Sahrens 		spa->spa_scrub_th = NULL;
1787789Sahrens 	}
1788789Sahrens 
17891544Seschrock 	if (rvd == NULL) {
17901544Seschrock 		ASSERT(spa->spa_scrub_stop == 0);
17911544Seschrock 		ASSERT(spa->spa_scrub_type == type);
17921544Seschrock 		ASSERT(spa->spa_scrub_restart_txg == 0);
17931544Seschrock 		mutex_exit(&spa->spa_scrub_lock);
17941544Seschrock 		return (0);
17951544Seschrock 	}
1796789Sahrens 
1797789Sahrens 	mintxg = TXG_INITIAL - 1;
1798789Sahrens 	maxtxg = spa_last_synced_txg(spa) + 1;
1799789Sahrens 
18001544Seschrock 	mutex_enter(&rvd->vdev_dtl_lock);
1801789Sahrens 
18021544Seschrock 	if (rvd->vdev_dtl_map.sm_space == 0) {
18031544Seschrock 		/*
18041544Seschrock 		 * The pool-wide DTL is empty.
1805*1732Sbonwick 		 * If this is a resilver, there's nothing to do except
1806*1732Sbonwick 		 * check whether any in-progress replacements have completed.
18071544Seschrock 		 */
1808*1732Sbonwick 		if (type == POOL_SCRUB_RESILVER) {
18091544Seschrock 			type = POOL_SCRUB_NONE;
1810*1732Sbonwick 			spa_async_request(spa, SPA_ASYNC_REPLACE_DONE);
1811*1732Sbonwick 		}
18121544Seschrock 	} else {
18131544Seschrock 		/*
18141544Seschrock 		 * The pool-wide DTL is non-empty.
18151544Seschrock 		 * If this is a normal scrub, upgrade to a resilver instead.
18161544Seschrock 		 */
18171544Seschrock 		if (type == POOL_SCRUB_EVERYTHING)
18181544Seschrock 			type = POOL_SCRUB_RESILVER;
18191544Seschrock 	}
1820789Sahrens 
18211544Seschrock 	if (type == POOL_SCRUB_RESILVER) {
1822789Sahrens 		/*
1823789Sahrens 		 * Determine the resilvering boundaries.
1824789Sahrens 		 *
1825789Sahrens 		 * Note: (mintxg, maxtxg) is an open interval,
1826789Sahrens 		 * i.e. mintxg and maxtxg themselves are not included.
1827789Sahrens 		 *
1828789Sahrens 		 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
1829789Sahrens 		 * so we don't claim to resilver a txg that's still changing.
1830789Sahrens 		 */
1831789Sahrens 		ss = avl_first(&rvd->vdev_dtl_map.sm_root);
18321544Seschrock 		mintxg = ss->ss_start - 1;
1833789Sahrens 		ss = avl_last(&rvd->vdev_dtl_map.sm_root);
18341544Seschrock 		maxtxg = MIN(ss->ss_end, maxtxg);
1835789Sahrens 	}
1836789Sahrens 
18371544Seschrock 	mutex_exit(&rvd->vdev_dtl_lock);
18381544Seschrock 
18391544Seschrock 	spa->spa_scrub_stop = 0;
18401544Seschrock 	spa->spa_scrub_type = type;
18411544Seschrock 	spa->spa_scrub_restart_txg = 0;
18421544Seschrock 
18431544Seschrock 	if (type != POOL_SCRUB_NONE) {
18441544Seschrock 		spa->spa_scrub_mintxg = mintxg;
1845789Sahrens 		spa->spa_scrub_maxtxg = maxtxg;
1846789Sahrens 		spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL,
18471635Sbonwick 		    ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL,
18481635Sbonwick 		    ZIO_FLAG_CANFAIL);
1849789Sahrens 		traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg);
1850789Sahrens 		spa->spa_scrub_thread = thread_create(NULL, 0,
1851789Sahrens 		    spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri);
1852789Sahrens 	}
1853789Sahrens 
18541544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
18551544Seschrock 
1856789Sahrens 	return (0);
1857789Sahrens }
1858789Sahrens 
18591544Seschrock /*
18601544Seschrock  * ==========================================================================
18611544Seschrock  * SPA async task processing
18621544Seschrock  * ==========================================================================
18631544Seschrock  */
18641544Seschrock 
18651544Seschrock static void
18661544Seschrock spa_async_reopen(spa_t *spa)
1867789Sahrens {
18681544Seschrock 	vdev_t *rvd = spa->spa_root_vdev;
18691544Seschrock 	vdev_t *tvd;
18701544Seschrock 	int c;
18711544Seschrock 
18721544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
18731544Seschrock 
18741544Seschrock 	for (c = 0; c < rvd->vdev_children; c++) {
18751544Seschrock 		tvd = rvd->vdev_child[c];
18761544Seschrock 		if (tvd->vdev_reopen_wanted) {
18771544Seschrock 			tvd->vdev_reopen_wanted = 0;
18781544Seschrock 			vdev_reopen(tvd);
18791544Seschrock 		}
18801544Seschrock 	}
1881789Sahrens 
18821544Seschrock 	spa_config_exit(spa, FTAG);
18831544Seschrock }
18841544Seschrock 
18851544Seschrock static void
18861544Seschrock spa_async_thread(spa_t *spa)
18871544Seschrock {
18881544Seschrock 	int tasks;
18891544Seschrock 
18901544Seschrock 	ASSERT(spa->spa_sync_on);
1891789Sahrens 
18921544Seschrock 	mutex_enter(&spa->spa_async_lock);
18931544Seschrock 	tasks = spa->spa_async_tasks;
18941544Seschrock 	spa->spa_async_tasks = 0;
18951544Seschrock 	mutex_exit(&spa->spa_async_lock);
18961544Seschrock 
18971544Seschrock 	/*
18981635Sbonwick 	 * See if the config needs to be updated.
18991635Sbonwick 	 */
19001635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
19011635Sbonwick 		mutex_enter(&spa_namespace_lock);
19021635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
19031635Sbonwick 		mutex_exit(&spa_namespace_lock);
19041635Sbonwick 	}
19051635Sbonwick 
19061635Sbonwick 	/*
19071544Seschrock 	 * See if any devices need to be reopened.
19081544Seschrock 	 */
19091544Seschrock 	if (tasks & SPA_ASYNC_REOPEN)
19101544Seschrock 		spa_async_reopen(spa);
19111544Seschrock 
19121544Seschrock 	/*
19131544Seschrock 	 * If any devices are done replacing, detach them.
19141544Seschrock 	 */
19151544Seschrock 	if (tasks & SPA_ASYNC_REPLACE_DONE)
1916789Sahrens 		spa_vdev_replace_done(spa);
1917789Sahrens 
19181544Seschrock 	/*
19191544Seschrock 	 * Kick off a scrub.
19201544Seschrock 	 */
19211544Seschrock 	if (tasks & SPA_ASYNC_SCRUB)
19221544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0);
19231544Seschrock 
19241544Seschrock 	/*
19251544Seschrock 	 * Kick off a resilver.
19261544Seschrock 	 */
19271544Seschrock 	if (tasks & SPA_ASYNC_RESILVER)
19281544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
19291544Seschrock 
19301544Seschrock 	/*
19311544Seschrock 	 * Let the world know that we're done.
19321544Seschrock 	 */
19331544Seschrock 	mutex_enter(&spa->spa_async_lock);
19341544Seschrock 	spa->spa_async_thread = NULL;
19351544Seschrock 	cv_broadcast(&spa->spa_async_cv);
19361544Seschrock 	mutex_exit(&spa->spa_async_lock);
19371544Seschrock 	thread_exit();
19381544Seschrock }
19391544Seschrock 
19401544Seschrock void
19411544Seschrock spa_async_suspend(spa_t *spa)
19421544Seschrock {
19431544Seschrock 	mutex_enter(&spa->spa_async_lock);
19441544Seschrock 	spa->spa_async_suspended++;
19451544Seschrock 	while (spa->spa_async_thread != NULL)
19461544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
19471544Seschrock 	mutex_exit(&spa->spa_async_lock);
19481544Seschrock }
19491544Seschrock 
19501544Seschrock void
19511544Seschrock spa_async_resume(spa_t *spa)
19521544Seschrock {
19531544Seschrock 	mutex_enter(&spa->spa_async_lock);
19541544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
19551544Seschrock 	spa->spa_async_suspended--;
19561544Seschrock 	mutex_exit(&spa->spa_async_lock);
19571544Seschrock }
19581544Seschrock 
19591544Seschrock static void
19601544Seschrock spa_async_dispatch(spa_t *spa)
19611544Seschrock {
19621544Seschrock 	mutex_enter(&spa->spa_async_lock);
19631544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
19641635Sbonwick 	    spa->spa_async_thread == NULL &&
19651635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
19661544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
19671544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
19681544Seschrock 	mutex_exit(&spa->spa_async_lock);
19691544Seschrock }
19701544Seschrock 
19711544Seschrock void
19721544Seschrock spa_async_request(spa_t *spa, int task)
19731544Seschrock {
19741544Seschrock 	mutex_enter(&spa->spa_async_lock);
19751544Seschrock 	spa->spa_async_tasks |= task;
19761544Seschrock 	mutex_exit(&spa->spa_async_lock);
1977789Sahrens }
1978789Sahrens 
1979789Sahrens /*
1980789Sahrens  * ==========================================================================
1981789Sahrens  * SPA syncing routines
1982789Sahrens  * ==========================================================================
1983789Sahrens  */
1984789Sahrens 
1985789Sahrens static void
1986789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
1987789Sahrens {
1988789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
1989789Sahrens 	dmu_tx_t *tx;
1990789Sahrens 	blkptr_t blk;
1991789Sahrens 	uint64_t itor = 0;
1992789Sahrens 	zio_t *zio;
1993789Sahrens 	int error;
1994789Sahrens 	uint8_t c = 1;
1995789Sahrens 
1996789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
1997789Sahrens 
1998789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
1999789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
2000789Sahrens 
2001789Sahrens 	error = zio_wait(zio);
2002789Sahrens 	ASSERT3U(error, ==, 0);
2003789Sahrens 
2004789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2005789Sahrens 	bplist_vacate(bpl, tx);
2006789Sahrens 
2007789Sahrens 	/*
2008789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
2009789Sahrens 	 * (Usually only the first block is needed.)
2010789Sahrens 	 */
2011789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
2012789Sahrens 	dmu_tx_commit(tx);
2013789Sahrens }
2014789Sahrens 
2015789Sahrens static void
2016789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
2017789Sahrens {
2018789Sahrens 	nvlist_t *config;
2019789Sahrens 	char *packed = NULL;
2020789Sahrens 	size_t nvsize = 0;
2021789Sahrens 	dmu_buf_t *db;
2022789Sahrens 
2023789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
2024789Sahrens 		return;
2025789Sahrens 
2026789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
2027789Sahrens 
20281635Sbonwick 	if (spa->spa_config_syncing)
20291635Sbonwick 		nvlist_free(spa->spa_config_syncing);
20301635Sbonwick 	spa->spa_config_syncing = config;
2031789Sahrens 
2032789Sahrens 	VERIFY(nvlist_size(config, &nvsize, NV_ENCODE_XDR) == 0);
2033789Sahrens 
2034789Sahrens 	packed = kmem_alloc(nvsize, KM_SLEEP);
2035789Sahrens 
20361544Seschrock 	VERIFY(nvlist_pack(config, &packed, &nvsize, NV_ENCODE_XDR,
20371544Seschrock 	    KM_SLEEP) == 0);
2038789Sahrens 
2039789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_config_object, 0, nvsize,
2040789Sahrens 	    packed, tx);
2041789Sahrens 
2042789Sahrens 	kmem_free(packed, nvsize);
2043789Sahrens 
20441544Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset,
20451544Seschrock 	    spa->spa_config_object, FTAG, &db));
2046789Sahrens 	dmu_buf_will_dirty(db, tx);
2047789Sahrens 	*(uint64_t *)db->db_data = nvsize;
20481544Seschrock 	dmu_buf_rele(db, FTAG);
2049789Sahrens }
2050789Sahrens 
2051789Sahrens /*
2052789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
2053789Sahrens  * part of the process, so we iterate until it converges.
2054789Sahrens  */
2055789Sahrens void
2056789Sahrens spa_sync(spa_t *spa, uint64_t txg)
2057789Sahrens {
2058789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
2059789Sahrens 	objset_t *mos = spa->spa_meta_objset;
2060789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
20611635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
2062789Sahrens 	vdev_t *vd;
2063789Sahrens 	dmu_tx_t *tx;
2064789Sahrens 	int dirty_vdevs;
2065789Sahrens 
2066789Sahrens 	/*
2067789Sahrens 	 * Lock out configuration changes.
2068789Sahrens 	 */
20691544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2070789Sahrens 
2071789Sahrens 	spa->spa_syncing_txg = txg;
2072789Sahrens 	spa->spa_sync_pass = 0;
2073789Sahrens 
20741544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
2075789Sahrens 
2076789Sahrens 	/*
2077789Sahrens 	 * If anything has changed in this txg, push the deferred frees
2078789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
2079789Sahrens 	 * don't generate work on an otherwise idle system.
2080789Sahrens 	 */
2081789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
2082789Sahrens 	    !txg_list_empty(&dp->dp_dirty_dirs, txg))
2083789Sahrens 		spa_sync_deferred_frees(spa, txg);
2084789Sahrens 
2085789Sahrens 	/*
2086789Sahrens 	 * Iterate to convergence.
2087789Sahrens 	 */
2088789Sahrens 	do {
2089789Sahrens 		spa->spa_sync_pass++;
2090789Sahrens 
2091789Sahrens 		tx = dmu_tx_create_assigned(dp, txg);
2092789Sahrens 		spa_sync_config_object(spa, tx);
2093789Sahrens 		dmu_tx_commit(tx);
2094789Sahrens 
20951544Seschrock 		spa_errlog_sync(spa, txg);
20961544Seschrock 
2097789Sahrens 		dsl_pool_sync(dp, txg);
2098789Sahrens 
2099789Sahrens 		dirty_vdevs = 0;
2100789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
2101789Sahrens 			vdev_sync(vd, txg);
2102789Sahrens 			dirty_vdevs++;
2103789Sahrens 		}
2104789Sahrens 
2105789Sahrens 		tx = dmu_tx_create_assigned(dp, txg);
2106789Sahrens 		bplist_sync(bpl, tx);
2107789Sahrens 		dmu_tx_commit(tx);
2108789Sahrens 
2109789Sahrens 	} while (dirty_vdevs);
2110789Sahrens 
2111789Sahrens 	bplist_close(bpl);
2112789Sahrens 
2113789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
2114789Sahrens 
2115789Sahrens 	/*
2116789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
2117789Sahrens 	 * to commit the transaction group.
21181635Sbonwick 	 *
21191635Sbonwick 	 * If there are any dirty vdevs, sync the uberblock to all vdevs.
21201635Sbonwick 	 * Otherwise, pick a random top-level vdev that's known to be
21211635Sbonwick 	 * visible in the config cache (see spa_vdev_add() for details).
21221635Sbonwick 	 * If the write fails, try the next vdev until we're tried them all.
2123789Sahrens 	 */
21241635Sbonwick 	if (!list_is_empty(&spa->spa_dirty_list)) {
21251635Sbonwick 		VERIFY(vdev_config_sync(rvd, txg) == 0);
21261635Sbonwick 	} else {
21271635Sbonwick 		int children = rvd->vdev_children;
21281635Sbonwick 		int c0 = spa_get_random(children);
21291635Sbonwick 		int c;
21301635Sbonwick 
21311635Sbonwick 		for (c = 0; c < children; c++) {
21321635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
21331635Sbonwick 			if (vd->vdev_ms_array == 0)
21341635Sbonwick 				continue;
21351635Sbonwick 			if (vdev_config_sync(vd, txg) == 0)
21361635Sbonwick 				break;
21371635Sbonwick 		}
21381635Sbonwick 		if (c == children)
21391635Sbonwick 			VERIFY(vdev_config_sync(rvd, txg) == 0);
21401635Sbonwick 	}
21411635Sbonwick 
21421635Sbonwick 	/*
21431635Sbonwick 	 * Clear the dirty config list.
21441635Sbonwick 	 */
21451635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
21461635Sbonwick 		vdev_config_clean(vd);
21471635Sbonwick 
21481635Sbonwick 	/*
21491635Sbonwick 	 * Now that the new config has synced transactionally,
21501635Sbonwick 	 * let it become visible to the config cache.
21511635Sbonwick 	 */
21521635Sbonwick 	if (spa->spa_config_syncing != NULL) {
21531635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
21541635Sbonwick 		spa->spa_config_txg = txg;
21551635Sbonwick 		spa->spa_config_syncing = NULL;
21561635Sbonwick 	}
2157789Sahrens 
2158789Sahrens 	/*
2159789Sahrens 	 * Make a stable copy of the fully synced uberblock.
2160789Sahrens 	 * We use this as the root for pool traversals.
2161789Sahrens 	 */
2162789Sahrens 	spa->spa_traverse_wanted = 1;	/* tells traverse_more() to stop */
2163789Sahrens 
2164789Sahrens 	spa_scrub_suspend(spa);		/* stop scrubbing and finish I/Os */
2165789Sahrens 
2166789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
2167789Sahrens 	spa->spa_traverse_wanted = 0;
2168789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2169789Sahrens 	rw_exit(&spa->spa_traverse_lock);
2170789Sahrens 
2171789Sahrens 	spa_scrub_resume(spa);		/* resume scrub with new ubsync */
2172789Sahrens 
2173789Sahrens 	/*
2174789Sahrens 	 * Clean up the ZIL records for the synced txg.
2175789Sahrens 	 */
2176789Sahrens 	dsl_pool_zil_clean(dp);
2177789Sahrens 
2178789Sahrens 	/*
2179789Sahrens 	 * Update usable space statistics.
2180789Sahrens 	 */
2181789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
2182789Sahrens 		vdev_sync_done(vd, txg);
2183789Sahrens 
2184789Sahrens 	/*
2185789Sahrens 	 * It had better be the case that we didn't dirty anything
2186789Sahrens 	 * since spa_sync_labels().
2187789Sahrens 	 */
2188789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
2189789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
2190789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
2191789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
2192789Sahrens 
21931544Seschrock 	spa_config_exit(spa, FTAG);
21941544Seschrock 
21951544Seschrock 	/*
21961544Seschrock 	 * If any async tasks have been requested, kick them off.
21971544Seschrock 	 */
21981544Seschrock 	spa_async_dispatch(spa);
2199789Sahrens }
2200789Sahrens 
2201789Sahrens /*
2202789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
2203789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
2204789Sahrens  * sync.
2205789Sahrens  */
2206789Sahrens void
2207789Sahrens spa_sync_allpools(void)
2208789Sahrens {
2209789Sahrens 	spa_t *spa = NULL;
2210789Sahrens 	mutex_enter(&spa_namespace_lock);
2211789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
2212789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
2213789Sahrens 			continue;
2214789Sahrens 		spa_open_ref(spa, FTAG);
2215789Sahrens 		mutex_exit(&spa_namespace_lock);
2216789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
2217789Sahrens 		mutex_enter(&spa_namespace_lock);
2218789Sahrens 		spa_close(spa, FTAG);
2219789Sahrens 	}
2220789Sahrens 	mutex_exit(&spa_namespace_lock);
2221789Sahrens }
2222789Sahrens 
2223789Sahrens /*
2224789Sahrens  * ==========================================================================
2225789Sahrens  * Miscellaneous routines
2226789Sahrens  * ==========================================================================
2227789Sahrens  */
2228789Sahrens 
2229789Sahrens /*
2230789Sahrens  * Remove all pools in the system.
2231789Sahrens  */
2232789Sahrens void
2233789Sahrens spa_evict_all(void)
2234789Sahrens {
2235789Sahrens 	spa_t *spa;
2236789Sahrens 
2237789Sahrens 	/*
2238789Sahrens 	 * Remove all cached state.  All pools should be closed now,
2239789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
2240789Sahrens 	 */
2241789Sahrens 	mutex_enter(&spa_namespace_lock);
2242789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
2243789Sahrens 		/*
22441544Seschrock 		 * Stop async tasks.  The async thread may need to detach
22451544Seschrock 		 * a device that's been replaced, which requires grabbing
22461544Seschrock 		 * spa_namespace_lock, so we must drop it here.
2247789Sahrens 		 */
2248789Sahrens 		spa_open_ref(spa, FTAG);
2249789Sahrens 		mutex_exit(&spa_namespace_lock);
22501544Seschrock 		spa_async_suspend(spa);
2251789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
2252789Sahrens 		mutex_enter(&spa_namespace_lock);
2253789Sahrens 		spa_close(spa, FTAG);
2254789Sahrens 
2255789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2256789Sahrens 			spa_unload(spa);
2257789Sahrens 			spa_deactivate(spa);
2258789Sahrens 		}
2259789Sahrens 		spa_remove(spa);
2260789Sahrens 	}
2261789Sahrens 	mutex_exit(&spa_namespace_lock);
2262789Sahrens }
22631544Seschrock 
22641544Seschrock vdev_t *
22651544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid)
22661544Seschrock {
22671544Seschrock 	return (vdev_lookup_by_guid(spa->spa_root_vdev, guid));
22681544Seschrock }
2269