xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 1733:a7c3bc84e012)
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) ||
278*1733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
2791544Seschrock 		error = EINVAL;
2801544Seschrock 		goto out;
2811544Seschrock 	}
282789Sahrens 
283*1733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
284*1733Sbonwick 	    &spa->spa_config_txg);
285*1733Sbonwick 
2861635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2871544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
2881544Seschrock 		error = EEXIST;
2891544Seschrock 		goto out;
2901544Seschrock 	}
291789Sahrens 
292789Sahrens 	/*
293789Sahrens 	 * Parse the configuration into a vdev tree.
294789Sahrens 	 */
2951544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
296789Sahrens 	rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
2971544Seschrock 	spa_config_exit(spa, FTAG);
298789Sahrens 
2991544Seschrock 	if (rvd == NULL) {
3001544Seschrock 		error = EINVAL;
3011544Seschrock 		goto out;
3021544Seschrock 	}
303789Sahrens 
3041585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
305789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
306789Sahrens 
307789Sahrens 	/*
308789Sahrens 	 * Try to open all vdevs, loading each label in the process.
309789Sahrens 	 */
3101544Seschrock 	if (vdev_open(rvd) != 0) {
3111544Seschrock 		error = ENXIO;
3121544Seschrock 		goto out;
3131544Seschrock 	}
314789Sahrens 
315789Sahrens 	/*
316789Sahrens 	 * Find the best uberblock.
317789Sahrens 	 */
318789Sahrens 	bzero(ub, sizeof (uberblock_t));
319789Sahrens 
320789Sahrens 	zio = zio_root(spa, NULL, NULL,
321789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
322789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
323789Sahrens 	error = zio_wait(zio);
324789Sahrens 
325789Sahrens 	/*
326789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
327789Sahrens 	 */
328789Sahrens 	if (ub->ub_txg == 0) {
3291544Seschrock 		error = ENXIO;
3301544Seschrock 		goto out;
3311544Seschrock 	}
3321544Seschrock 
3331544Seschrock 	/*
3341544Seschrock 	 * If the pool is newer than the code, we can't open it.
3351544Seschrock 	 */
3361544Seschrock 	if (ub->ub_version > UBERBLOCK_VERSION) {
3371544Seschrock 		error = ENOTSUP;
3381544Seschrock 		goto out;
339789Sahrens 	}
340789Sahrens 
341789Sahrens 	/*
342789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
343789Sahrens 	 * incomplete configuration.
344789Sahrens 	 */
3451732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
3461544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3471544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
3481544Seschrock 		error = ENXIO;
3491544Seschrock 		goto out;
350789Sahrens 	}
351789Sahrens 
352789Sahrens 	/*
353789Sahrens 	 * Initialize internal SPA structures.
354789Sahrens 	 */
355789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
356789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
357789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
3581544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
3591544Seschrock 	if (error) {
3601544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3611544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
3621544Seschrock 		goto out;
3631544Seschrock 	}
364789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
365789Sahrens 
3661544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
367789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3681544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
3691544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3701544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
3711544Seschrock 		error = EIO;
3721544Seschrock 		goto out;
3731544Seschrock 	}
374789Sahrens 
375789Sahrens 	if (!mosconfig) {
376789Sahrens 		dmu_buf_t *db;
377789Sahrens 		char *packed = NULL;
378789Sahrens 		size_t nvsize = 0;
379789Sahrens 		nvlist_t *newconfig = NULL;
380789Sahrens 
3811544Seschrock 		VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset,
3821544Seschrock 		    spa->spa_config_object, FTAG, &db));
383789Sahrens 		nvsize = *(uint64_t *)db->db_data;
3841544Seschrock 		dmu_buf_rele(db, FTAG);
385789Sahrens 
386789Sahrens 		packed = kmem_alloc(nvsize, KM_SLEEP);
3871544Seschrock 		error = dmu_read(spa->spa_meta_objset,
388789Sahrens 		    spa->spa_config_object, 0, nvsize, packed);
389789Sahrens 		if (error == 0)
390789Sahrens 			error = nvlist_unpack(packed, nvsize, &newconfig, 0);
391789Sahrens 		kmem_free(packed, nvsize);
392789Sahrens 
3931544Seschrock 		if (error) {
3941544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
3951544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
3961544Seschrock 			error = EIO;
3971544Seschrock 			goto out;
3981544Seschrock 		}
399789Sahrens 
400789Sahrens 		spa_config_set(spa, newconfig);
401789Sahrens 
402789Sahrens 		spa_unload(spa);
403789Sahrens 		spa_deactivate(spa);
404789Sahrens 		spa_activate(spa);
405789Sahrens 
4061544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
4071544Seschrock 	}
4081544Seschrock 
4091544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
4101544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
4111544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
4121544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
4131544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
4141544Seschrock 		error = EIO;
4151544Seschrock 		goto out;
416789Sahrens 	}
417789Sahrens 
4181544Seschrock 	/*
4191544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
4201544Seschrock 	 * not be present.
4211544Seschrock 	 */
4221544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
4231544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
4241544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
4251544Seschrock 	if (error != 0 &&error != ENOENT) {
4261544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
4271544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
4281544Seschrock 		error = EIO;
4291544Seschrock 		goto out;
4301544Seschrock 	}
4311544Seschrock 
4321544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
4331544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
4341544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
4351544Seschrock 	if (error != 0 && error != ENOENT) {
4361544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
4371544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
4381544Seschrock 		error = EIO;
4391544Seschrock 		goto out;
4401544Seschrock 	}
441789Sahrens 
442789Sahrens 	/*
4431544Seschrock 	 * Load the vdev state for all top level vdevs.  We need to grab the
4441544Seschrock 	 * config lock because all label I/O is done with the
4451544Seschrock 	 * ZIO_FLAG_CONFIG_HELD flag.
446789Sahrens 	 */
4471544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
4481635Sbonwick 	error = vdev_load(rvd);
4491635Sbonwick 	spa_config_exit(spa, FTAG);
4501635Sbonwick 
4511635Sbonwick 	if (error)
4521544Seschrock 		goto out;
453789Sahrens 
454789Sahrens 	/*
455789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
456789Sahrens 	 */
4571544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
458789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
4591544Seschrock 	spa_config_exit(spa, FTAG);
460789Sahrens 
461789Sahrens 	/*
462789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
463789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
464789Sahrens 	 */
4651544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
4661544Seschrock 		error = ENXIO;
4671544Seschrock 		goto out;
4681544Seschrock 	}
469789Sahrens 
4701544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
4711635Sbonwick 		dmu_tx_t *tx;
4721635Sbonwick 		int need_update = B_FALSE;
4731585Sbonwick 		int c;
4741601Sbonwick 
4751635Sbonwick 		/*
4761635Sbonwick 		 * Claim log blocks that haven't been committed yet.
4771635Sbonwick 		 * This must all happen in a single txg.
4781635Sbonwick 		 */
4791601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
480789Sahrens 		    spa_first_txg(spa));
481789Sahrens 		dmu_objset_find(spa->spa_name, zil_claim, tx, 0);
482789Sahrens 		dmu_tx_commit(tx);
483789Sahrens 
484789Sahrens 		spa->spa_sync_on = B_TRUE;
485789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
486789Sahrens 
487789Sahrens 		/*
488789Sahrens 		 * Wait for all claims to sync.
489789Sahrens 		 */
490789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
4911585Sbonwick 
4921585Sbonwick 		/*
4931635Sbonwick 		 * If the config cache is stale, or we have uninitialized
4941635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
4951585Sbonwick 		 */
4961635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
4971635Sbonwick 		    state == SPA_LOAD_IMPORT)
4981635Sbonwick 			need_update = B_TRUE;
4991635Sbonwick 
5001635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
5011635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
5021635Sbonwick 				need_update = B_TRUE;
5031585Sbonwick 
5041585Sbonwick 		/*
5051635Sbonwick 		 * Update the config cache asychronously in case we're the
5061635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
5071585Sbonwick 		 */
5081635Sbonwick 		if (need_update)
5091635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
510789Sahrens 	}
511789Sahrens 
5121544Seschrock 	error = 0;
5131544Seschrock out:
5141544Seschrock 	if (error)
5151544Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
5161544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
5171544Seschrock 	spa->spa_ena = 0;
5181544Seschrock 
5191544Seschrock 	return (error);
520789Sahrens }
521789Sahrens 
522789Sahrens /*
523789Sahrens  * Pool Open/Import
524789Sahrens  *
525789Sahrens  * The import case is identical to an open except that the configuration is sent
526789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
527789Sahrens  * case of an open, the pool configuration will exist in the
528789Sahrens  * POOL_STATE_UNITIALIZED state.
529789Sahrens  *
530789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
531789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
532789Sahrens  * ambiguous state.
533789Sahrens  */
534789Sahrens static int
535789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
536789Sahrens {
537789Sahrens 	spa_t *spa;
538789Sahrens 	int error;
539789Sahrens 	int loaded = B_FALSE;
540789Sahrens 	int locked = B_FALSE;
541789Sahrens 
542789Sahrens 	*spapp = NULL;
543789Sahrens 
544789Sahrens 	/*
545789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
546789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
547789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
548789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
549789Sahrens 	 */
550789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
551789Sahrens 		mutex_enter(&spa_namespace_lock);
552789Sahrens 		locked = B_TRUE;
553789Sahrens 	}
554789Sahrens 
555789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
556789Sahrens 		if (locked)
557789Sahrens 			mutex_exit(&spa_namespace_lock);
558789Sahrens 		return (ENOENT);
559789Sahrens 	}
560789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
561789Sahrens 
562789Sahrens 		spa_activate(spa);
563789Sahrens 
5641635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
565789Sahrens 
566789Sahrens 		if (error == EBADF) {
567789Sahrens 			/*
568789Sahrens 			 * If vdev_load() returns EBADF, it indicates that one
569789Sahrens 			 * of the vdevs indicates that the pool has been
570789Sahrens 			 * exported or destroyed.  If this is the case, the
571789Sahrens 			 * config cache is out of sync and we should remove the
572789Sahrens 			 * pool from the namespace.
573789Sahrens 			 */
574789Sahrens 			spa_unload(spa);
575789Sahrens 			spa_deactivate(spa);
576789Sahrens 			spa_remove(spa);
577789Sahrens 			spa_config_sync();
578789Sahrens 			if (locked)
579789Sahrens 				mutex_exit(&spa_namespace_lock);
580789Sahrens 			return (ENOENT);
5811544Seschrock 		}
5821544Seschrock 
5831544Seschrock 		if (error) {
584789Sahrens 			/*
585789Sahrens 			 * We can't open the pool, but we still have useful
586789Sahrens 			 * information: the state of each vdev after the
587789Sahrens 			 * attempted vdev_open().  Return this to the user.
588789Sahrens 			 */
5891635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
5901635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
591789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
592789Sahrens 				    B_TRUE);
5931635Sbonwick 				spa_config_exit(spa, FTAG);
5941635Sbonwick 			}
595789Sahrens 			spa_unload(spa);
596789Sahrens 			spa_deactivate(spa);
5971544Seschrock 			spa->spa_last_open_failed = B_TRUE;
598789Sahrens 			if (locked)
599789Sahrens 				mutex_exit(&spa_namespace_lock);
600789Sahrens 			*spapp = NULL;
601789Sahrens 			return (error);
6021544Seschrock 		} else {
6031544Seschrock 			zfs_post_ok(spa, NULL);
6041544Seschrock 			spa->spa_last_open_failed = B_FALSE;
605789Sahrens 		}
606789Sahrens 
607789Sahrens 		loaded = B_TRUE;
608789Sahrens 	}
609789Sahrens 
610789Sahrens 	spa_open_ref(spa, tag);
611789Sahrens 	if (locked)
612789Sahrens 		mutex_exit(&spa_namespace_lock);
613789Sahrens 
614789Sahrens 	*spapp = spa;
615789Sahrens 
616789Sahrens 	if (config != NULL) {
6171544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
618789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
6191544Seschrock 		spa_config_exit(spa, FTAG);
620789Sahrens 	}
621789Sahrens 
622789Sahrens 	/*
623789Sahrens 	 * If we just loaded the pool, resilver anything that's out of date.
624789Sahrens 	 */
625789Sahrens 	if (loaded && (spa_mode & FWRITE))
626789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
627789Sahrens 
628789Sahrens 	return (0);
629789Sahrens }
630789Sahrens 
631789Sahrens int
632789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
633789Sahrens {
634789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
635789Sahrens }
636789Sahrens 
6371544Seschrock /*
6381544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
6391544Seschrock  * preventing it from being exported or destroyed.
6401544Seschrock  */
6411544Seschrock spa_t *
6421544Seschrock spa_inject_addref(char *name)
6431544Seschrock {
6441544Seschrock 	spa_t *spa;
6451544Seschrock 
6461544Seschrock 	mutex_enter(&spa_namespace_lock);
6471544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
6481544Seschrock 		mutex_exit(&spa_namespace_lock);
6491544Seschrock 		return (NULL);
6501544Seschrock 	}
6511544Seschrock 	spa->spa_inject_ref++;
6521544Seschrock 	mutex_exit(&spa_namespace_lock);
6531544Seschrock 
6541544Seschrock 	return (spa);
6551544Seschrock }
6561544Seschrock 
6571544Seschrock void
6581544Seschrock spa_inject_delref(spa_t *spa)
6591544Seschrock {
6601544Seschrock 	mutex_enter(&spa_namespace_lock);
6611544Seschrock 	spa->spa_inject_ref--;
6621544Seschrock 	mutex_exit(&spa_namespace_lock);
6631544Seschrock }
6641544Seschrock 
665789Sahrens int
6661544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
667789Sahrens {
668789Sahrens 	int error;
669789Sahrens 	spa_t *spa;
670789Sahrens 
671789Sahrens 	*config = NULL;
672789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
673789Sahrens 
6741544Seschrock 	if (spa && *config != NULL)
6751544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
6761544Seschrock 		    spa_get_errlog_size(spa)) == 0);
6771544Seschrock 
6781544Seschrock 	/*
6791544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
6801544Seschrock 	 * and call spa_lookup() directly.
6811544Seschrock 	 */
6821544Seschrock 	if (altroot) {
6831544Seschrock 		if (spa == NULL) {
6841544Seschrock 			mutex_enter(&spa_namespace_lock);
6851544Seschrock 			spa = spa_lookup(name);
6861544Seschrock 			if (spa)
6871544Seschrock 				spa_altroot(spa, altroot, buflen);
6881544Seschrock 			else
6891544Seschrock 				altroot[0] = '\0';
6901544Seschrock 			spa = NULL;
6911544Seschrock 			mutex_exit(&spa_namespace_lock);
6921544Seschrock 		} else {
6931544Seschrock 			spa_altroot(spa, altroot, buflen);
6941544Seschrock 		}
6951544Seschrock 	}
6961544Seschrock 
697789Sahrens 	if (spa != NULL)
698789Sahrens 		spa_close(spa, FTAG);
699789Sahrens 
700789Sahrens 	return (error);
701789Sahrens }
702789Sahrens 
703789Sahrens /*
704789Sahrens  * Pool Creation
705789Sahrens  */
706789Sahrens int
7071635Sbonwick spa_create(const char *pool, nvlist_t *nvroot, const char *altroot)
708789Sahrens {
709789Sahrens 	spa_t *spa;
7101635Sbonwick 	vdev_t *rvd;
711789Sahrens 	dsl_pool_t *dp;
712789Sahrens 	dmu_tx_t *tx;
7131635Sbonwick 	int c, error;
714789Sahrens 	uint64_t txg = TXG_INITIAL;
715789Sahrens 
716789Sahrens 	/*
717789Sahrens 	 * If this pool already exists, return failure.
718789Sahrens 	 */
719789Sahrens 	mutex_enter(&spa_namespace_lock);
720789Sahrens 	if (spa_lookup(pool) != NULL) {
721789Sahrens 		mutex_exit(&spa_namespace_lock);
722789Sahrens 		return (EEXIST);
723789Sahrens 	}
724789Sahrens 
725789Sahrens 	/*
726789Sahrens 	 * Allocate a new spa_t structure.
727789Sahrens 	 */
7281635Sbonwick 	spa = spa_add(pool, altroot);
729789Sahrens 	spa_activate(spa);
730789Sahrens 
731789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
732789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
733789Sahrens 
7341635Sbonwick 	/*
7351635Sbonwick 	 * Create the root vdev.
7361635Sbonwick 	 */
7371635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
7381635Sbonwick 
7391635Sbonwick 	rvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD);
7401635Sbonwick 
7411635Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
7421635Sbonwick 
7431635Sbonwick 	if (rvd == NULL) {
7441635Sbonwick 		error = EINVAL;
7451635Sbonwick 	} else {
7461635Sbonwick 		if ((error = vdev_create(rvd, txg)) == 0) {
7471635Sbonwick 			for (c = 0; c < rvd->vdev_children; c++)
7481635Sbonwick 				vdev_init(rvd->vdev_child[c], txg);
7491635Sbonwick 			vdev_config_dirty(rvd);
7501635Sbonwick 		}
7511635Sbonwick 	}
7521635Sbonwick 
7531635Sbonwick 	spa_config_exit(spa, FTAG);
754789Sahrens 
755789Sahrens 	if (error) {
756789Sahrens 		spa_unload(spa);
757789Sahrens 		spa_deactivate(spa);
758789Sahrens 		spa_remove(spa);
759789Sahrens 		mutex_exit(&spa_namespace_lock);
760789Sahrens 		return (error);
761789Sahrens 	}
762789Sahrens 
763789Sahrens 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg);
764789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
765789Sahrens 
766789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
767789Sahrens 
768789Sahrens 	/*
769789Sahrens 	 * Create the pool config object.
770789Sahrens 	 */
771789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
772789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
773789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
774789Sahrens 
7751544Seschrock 	if (zap_add(spa->spa_meta_objset,
776789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
7771544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
7781544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
7791544Seschrock 	}
780789Sahrens 
781789Sahrens 	/*
782789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
783789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
784789Sahrens 	 * keeps changing.
785789Sahrens 	 */
786789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
787789Sahrens 	    1 << 14, tx);
788789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
789789Sahrens 	    ZIO_COMPRESS_OFF, tx);
790789Sahrens 
7911544Seschrock 	if (zap_add(spa->spa_meta_objset,
792789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
7931544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
7941544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
7951544Seschrock 	}
796789Sahrens 
797789Sahrens 	dmu_tx_commit(tx);
798789Sahrens 
799789Sahrens 	spa->spa_sync_on = B_TRUE;
800789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
801789Sahrens 
802789Sahrens 	/*
803789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
804789Sahrens 	 * bean counters are appropriately updated.
805789Sahrens 	 */
806789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
807789Sahrens 
808789Sahrens 	spa_config_sync();
809789Sahrens 
810789Sahrens 	mutex_exit(&spa_namespace_lock);
811789Sahrens 
812789Sahrens 	return (0);
813789Sahrens }
814789Sahrens 
815789Sahrens /*
816789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
817789Sahrens  * then call spa_load() to do the dirty work.
818789Sahrens  */
819789Sahrens int
8201635Sbonwick spa_import(const char *pool, nvlist_t *config, const char *altroot)
821789Sahrens {
822789Sahrens 	spa_t *spa;
823789Sahrens 	int error;
824789Sahrens 
825789Sahrens 	if (!(spa_mode & FWRITE))
826789Sahrens 		return (EROFS);
827789Sahrens 
828789Sahrens 	/*
829789Sahrens 	 * If a pool with this name exists, return failure.
830789Sahrens 	 */
831789Sahrens 	mutex_enter(&spa_namespace_lock);
832789Sahrens 	if (spa_lookup(pool) != NULL) {
833789Sahrens 		mutex_exit(&spa_namespace_lock);
834789Sahrens 		return (EEXIST);
835789Sahrens 	}
836789Sahrens 
837789Sahrens 	/*
8381635Sbonwick 	 * Create and initialize the spa structure.
839789Sahrens 	 */
8401635Sbonwick 	spa = spa_add(pool, altroot);
841789Sahrens 	spa_activate(spa);
842789Sahrens 
843789Sahrens 	/*
8441635Sbonwick 	 * Pass off the heavy lifting to spa_load().
8451732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
8461732Sbonwick 	 * is actually the one to trust when doing an import.
8471601Sbonwick 	 */
8481732Sbonwick 	error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE);
849789Sahrens 
850789Sahrens 	if (error) {
851789Sahrens 		spa_unload(spa);
852789Sahrens 		spa_deactivate(spa);
853789Sahrens 		spa_remove(spa);
854789Sahrens 		mutex_exit(&spa_namespace_lock);
855789Sahrens 		return (error);
856789Sahrens 	}
857789Sahrens 
8581635Sbonwick 	/*
8591635Sbonwick 	 * Update the config cache to include the newly-imported pool.
8601635Sbonwick 	 */
8611635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
8621635Sbonwick 
863789Sahrens 	mutex_exit(&spa_namespace_lock);
864789Sahrens 
865789Sahrens 	/*
866789Sahrens 	 * Resilver anything that's out of date.
867789Sahrens 	 */
868789Sahrens 	if (spa_mode & FWRITE)
869789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
870789Sahrens 
871789Sahrens 	return (0);
872789Sahrens }
873789Sahrens 
874789Sahrens /*
875789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
876789Sahrens  * to get the vdev stats associated with the imported devices.
877789Sahrens  */
878789Sahrens #define	TRYIMPORT_NAME	"$import"
879789Sahrens 
880789Sahrens nvlist_t *
881789Sahrens spa_tryimport(nvlist_t *tryconfig)
882789Sahrens {
883789Sahrens 	nvlist_t *config = NULL;
884789Sahrens 	char *poolname;
885789Sahrens 	spa_t *spa;
886789Sahrens 	uint64_t state;
887789Sahrens 
888789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
889789Sahrens 		return (NULL);
890789Sahrens 
891789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
892789Sahrens 		return (NULL);
893789Sahrens 
8941635Sbonwick 	/*
8951635Sbonwick 	 * Create and initialize the spa structure.
8961635Sbonwick 	 */
897789Sahrens 	mutex_enter(&spa_namespace_lock);
8981635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
899789Sahrens 	spa_activate(spa);
900789Sahrens 
901789Sahrens 	/*
9021635Sbonwick 	 * Pass off the heavy lifting to spa_load().
9031732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
9041732Sbonwick 	 * is actually the one to trust when doing an import.
905789Sahrens 	 */
9061732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
907789Sahrens 
908789Sahrens 	/*
909789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
910789Sahrens 	 */
911789Sahrens 	if (spa->spa_root_vdev != NULL) {
9121635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
913789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
9141635Sbonwick 		spa_config_exit(spa, FTAG);
915789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
916789Sahrens 		    poolname) == 0);
917789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
918789Sahrens 		    state) == 0);
919789Sahrens 	}
920789Sahrens 
921789Sahrens 	spa_unload(spa);
922789Sahrens 	spa_deactivate(spa);
923789Sahrens 	spa_remove(spa);
924789Sahrens 	mutex_exit(&spa_namespace_lock);
925789Sahrens 
926789Sahrens 	return (config);
927789Sahrens }
928789Sahrens 
929789Sahrens /*
930789Sahrens  * Pool export/destroy
931789Sahrens  *
932789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
933789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
934789Sahrens  * update the pool state and sync all the labels to disk, removing the
935789Sahrens  * configuration from the cache afterwards.
936789Sahrens  */
937789Sahrens static int
938789Sahrens spa_export_common(char *pool, int new_state)
939789Sahrens {
940789Sahrens 	spa_t *spa;
941789Sahrens 
942789Sahrens 	if (!(spa_mode & FWRITE))
943789Sahrens 		return (EROFS);
944789Sahrens 
945789Sahrens 	mutex_enter(&spa_namespace_lock);
946789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
947789Sahrens 		mutex_exit(&spa_namespace_lock);
948789Sahrens 		return (ENOENT);
949789Sahrens 	}
950789Sahrens 
951789Sahrens 	/*
9521544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
9531544Seschrock 	 * reacquire the namespace lock, and see if we can export.
9541544Seschrock 	 */
9551544Seschrock 	spa_open_ref(spa, FTAG);
9561544Seschrock 	mutex_exit(&spa_namespace_lock);
9571544Seschrock 	spa_async_suspend(spa);
9581544Seschrock 	mutex_enter(&spa_namespace_lock);
9591544Seschrock 	spa_close(spa, FTAG);
9601544Seschrock 
9611544Seschrock 	/*
962789Sahrens 	 * The pool will be in core if it's openable,
963789Sahrens 	 * in which case we can modify its state.
964789Sahrens 	 */
965789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
966789Sahrens 		/*
967789Sahrens 		 * Objsets may be open only because they're dirty, so we
968789Sahrens 		 * have to force it to sync before checking spa_refcnt.
969789Sahrens 		 */
970789Sahrens 		spa_scrub_suspend(spa);
971789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
972789Sahrens 
9731544Seschrock 		/*
9741544Seschrock 		 * A pool cannot be exported or destroyed if there are active
9751544Seschrock 		 * references.  If we are resetting a pool, allow references by
9761544Seschrock 		 * fault injection handlers.
9771544Seschrock 		 */
9781544Seschrock 		if (!spa_refcount_zero(spa) ||
9791544Seschrock 		    (spa->spa_inject_ref != 0 &&
9801544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
981789Sahrens 			spa_scrub_resume(spa);
9821544Seschrock 			spa_async_resume(spa);
983789Sahrens 			mutex_exit(&spa_namespace_lock);
984789Sahrens 			return (EBUSY);
985789Sahrens 		}
986789Sahrens 
987789Sahrens 		spa_scrub_resume(spa);
988789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
989789Sahrens 
990789Sahrens 		/*
991789Sahrens 		 * We want this to be reflected on every label,
992789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
993789Sahrens 		 * final sync that pushes these changes out.
994789Sahrens 		 */
9951544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
9961601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
9971544Seschrock 			spa->spa_state = new_state;
9981635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
9991544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
10001601Sbonwick 			spa_config_exit(spa, FTAG);
10011544Seschrock 		}
1002789Sahrens 	}
1003789Sahrens 
1004789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
1005789Sahrens 		spa_unload(spa);
1006789Sahrens 		spa_deactivate(spa);
1007789Sahrens 	}
1008789Sahrens 
10091544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
10101544Seschrock 		spa_remove(spa);
10111544Seschrock 		spa_config_sync();
10121544Seschrock 	}
1013789Sahrens 	mutex_exit(&spa_namespace_lock);
1014789Sahrens 
1015789Sahrens 	return (0);
1016789Sahrens }
1017789Sahrens 
1018789Sahrens /*
1019789Sahrens  * Destroy a storage pool.
1020789Sahrens  */
1021789Sahrens int
1022789Sahrens spa_destroy(char *pool)
1023789Sahrens {
1024789Sahrens 	return (spa_export_common(pool, POOL_STATE_DESTROYED));
1025789Sahrens }
1026789Sahrens 
1027789Sahrens /*
1028789Sahrens  * Export a storage pool.
1029789Sahrens  */
1030789Sahrens int
1031789Sahrens spa_export(char *pool)
1032789Sahrens {
1033789Sahrens 	return (spa_export_common(pool, POOL_STATE_EXPORTED));
1034789Sahrens }
1035789Sahrens 
1036789Sahrens /*
10371544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
10381544Seschrock  * from the namespace in any way.
10391544Seschrock  */
10401544Seschrock int
10411544Seschrock spa_reset(char *pool)
10421544Seschrock {
10431544Seschrock 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED));
10441544Seschrock }
10451544Seschrock 
10461544Seschrock 
10471544Seschrock /*
1048789Sahrens  * ==========================================================================
1049789Sahrens  * Device manipulation
1050789Sahrens  * ==========================================================================
1051789Sahrens  */
1052789Sahrens 
1053789Sahrens /*
1054789Sahrens  * Add capacity to a storage pool.
1055789Sahrens  */
1056789Sahrens int
1057789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
1058789Sahrens {
1059789Sahrens 	uint64_t txg;
10601635Sbonwick 	int c, error;
1061789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
10621585Sbonwick 	vdev_t *vd, *tvd;
1063789Sahrens 
1064789Sahrens 	txg = spa_vdev_enter(spa);
1065789Sahrens 
1066789Sahrens 	vd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD);
1067789Sahrens 
1068789Sahrens 	if (vd == NULL)
1069789Sahrens 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
1070789Sahrens 
1071789Sahrens 	if ((error = vdev_create(vd, txg)) != 0)
1072789Sahrens 		return (spa_vdev_exit(spa, vd, txg, error));
1073789Sahrens 
1074789Sahrens 	/*
10751585Sbonwick 	 * Transfer each new top-level vdev from vd to rvd.
1076789Sahrens 	 */
10771635Sbonwick 	for (c = 0; c < vd->vdev_children; c++) {
10781585Sbonwick 		tvd = vd->vdev_child[c];
10791635Sbonwick 		vdev_remove_child(vd, tvd);
10801635Sbonwick 		tvd->vdev_id = rvd->vdev_children;
10811635Sbonwick 		vdev_add_child(rvd, tvd);
1082789Sahrens 		vdev_config_dirty(tvd);
1083789Sahrens 	}
1084789Sahrens 
1085789Sahrens 	/*
10861585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
10871585Sbonwick 	 * If other threads start allocating from these vdevs before we
10881585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
10891585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
10901585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
10911585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
10921635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
10931585Sbonwick 	 *
10941585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
10951585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
10961585Sbonwick 	 * steps will be completed the next time we load the pool.
1097789Sahrens 	 */
10981635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
10991585Sbonwick 
11001635Sbonwick 	mutex_enter(&spa_namespace_lock);
11011635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
11021635Sbonwick 	mutex_exit(&spa_namespace_lock);
1103789Sahrens 
11041635Sbonwick 	return (0);
1105789Sahrens }
1106789Sahrens 
1107789Sahrens /*
1108789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
1109789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
1110789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
1111789Sahrens  *
1112789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
1113789Sahrens  * existing device; in this case the two devices are made into their own
1114789Sahrens  * mirror using the 'replacing' vdev, which is functionally idendical to
1115789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
1116789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
1117789Sahrens  * completion of resilvering, the first disk (the one being replaced)
1118789Sahrens  * is automatically detached.
1119789Sahrens  */
1120789Sahrens int
11211544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
1122789Sahrens {
1123789Sahrens 	uint64_t txg, open_txg;
1124789Sahrens 	int error;
1125789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1126789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
1127789Sahrens 	vdev_ops_t *pvops = replacing ? &vdev_replacing_ops : &vdev_mirror_ops;
1128789Sahrens 
1129789Sahrens 	txg = spa_vdev_enter(spa);
1130789Sahrens 
11311544Seschrock 	oldvd = vdev_lookup_by_guid(rvd, guid);
1132789Sahrens 
1133789Sahrens 	if (oldvd == NULL)
1134789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1135789Sahrens 
11361585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
11371585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
11381585Sbonwick 
1139789Sahrens 	pvd = oldvd->vdev_parent;
1140789Sahrens 
1141789Sahrens 	/*
1142789Sahrens 	 * The parent must be a mirror or the root, unless we're replacing;
1143789Sahrens 	 * in that case, the parent can be anything but another replacing vdev.
1144789Sahrens 	 */
1145789Sahrens 	if (pvd->vdev_ops != &vdev_mirror_ops &&
1146789Sahrens 	    pvd->vdev_ops != &vdev_root_ops &&
1147789Sahrens 	    (!replacing || pvd->vdev_ops == &vdev_replacing_ops))
1148789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1149789Sahrens 
1150789Sahrens 	newrootvd = spa_config_parse(spa, nvroot, NULL, 0, VDEV_ALLOC_ADD);
1151789Sahrens 
1152789Sahrens 	if (newrootvd == NULL || newrootvd->vdev_children != 1)
1153789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
1154789Sahrens 
1155789Sahrens 	newvd = newrootvd->vdev_child[0];
1156789Sahrens 
1157789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
1158789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
1159789Sahrens 
1160789Sahrens 	if ((error = vdev_create(newrootvd, txg)) != 0)
1161789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
1162789Sahrens 
11631175Slling 	/*
11641175Slling 	 * Compare the new device size with the replaceable/attachable
11651175Slling 	 * device size.
11661175Slling 	 */
11671175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
1168789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
1169789Sahrens 
11701732Sbonwick 	/*
11711732Sbonwick 	 * The new device cannot have a higher alignment requirement
11721732Sbonwick 	 * than the top-level vdev.
11731732Sbonwick 	 */
11741732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
1175789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
1176789Sahrens 
1177789Sahrens 	/*
1178789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
1179789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
1180789Sahrens 	 */
1181789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
1182789Sahrens 		spa_strfree(oldvd->vdev_path);
1183789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
1184789Sahrens 		    KM_SLEEP);
1185789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
1186789Sahrens 		    newvd->vdev_path, "old");
1187789Sahrens 		if (oldvd->vdev_devid != NULL) {
1188789Sahrens 			spa_strfree(oldvd->vdev_devid);
1189789Sahrens 			oldvd->vdev_devid = NULL;
1190789Sahrens 		}
1191789Sahrens 	}
1192789Sahrens 
1193789Sahrens 	/*
1194789Sahrens 	 * If the parent is not a mirror, or if we're replacing,
1195789Sahrens 	 * insert the new mirror/replacing vdev above oldvd.
1196789Sahrens 	 */
1197789Sahrens 	if (pvd->vdev_ops != pvops)
1198789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
1199789Sahrens 
1200789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
1201789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
1202789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
1203789Sahrens 
1204789Sahrens 	/*
1205789Sahrens 	 * Extract the new device from its root and add it to pvd.
1206789Sahrens 	 */
1207789Sahrens 	vdev_remove_child(newrootvd, newvd);
1208789Sahrens 	newvd->vdev_id = pvd->vdev_children;
1209789Sahrens 	vdev_add_child(pvd, newvd);
1210789Sahrens 
12111544Seschrock 	/*
12121544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
12131544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
12141544Seschrock 	 */
12151544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
12161544Seschrock 
1217789Sahrens 	tvd = newvd->vdev_top;
1218789Sahrens 	ASSERT(pvd->vdev_top == tvd);
1219789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
1220789Sahrens 
1221789Sahrens 	vdev_config_dirty(tvd);
1222789Sahrens 
1223789Sahrens 	/*
1224789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
1225789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
1226789Sahrens 	 */
1227789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
1228789Sahrens 
1229789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
1230789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
1231789Sahrens 	    open_txg - TXG_INITIAL + 1);
1232789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
1233789Sahrens 
12341544Seschrock 	dprintf("attached %s in txg %llu\n", newvd->vdev_path, txg);
12351544Seschrock 
1236789Sahrens 	/*
1237789Sahrens 	 * Mark newvd's DTL dirty in this txg.
1238789Sahrens 	 */
12391732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
1240789Sahrens 
1241789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
1242789Sahrens 
1243789Sahrens 	/*
1244789Sahrens 	 * Kick off a resilver to update newvd.
1245789Sahrens 	 */
1246789Sahrens 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1247789Sahrens 
1248789Sahrens 	return (0);
1249789Sahrens }
1250789Sahrens 
1251789Sahrens /*
1252789Sahrens  * Detach a device from a mirror or replacing vdev.
1253789Sahrens  * If 'replace_done' is specified, only detach if the parent
1254789Sahrens  * is a replacing vdev.
1255789Sahrens  */
1256789Sahrens int
12571544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
1258789Sahrens {
1259789Sahrens 	uint64_t txg;
1260789Sahrens 	int c, t, error;
1261789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1262789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
1263789Sahrens 
1264789Sahrens 	txg = spa_vdev_enter(spa);
1265789Sahrens 
12661544Seschrock 	vd = vdev_lookup_by_guid(rvd, guid);
1267789Sahrens 
1268789Sahrens 	if (vd == NULL)
1269789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1270789Sahrens 
12711585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
12721585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
12731585Sbonwick 
1274789Sahrens 	pvd = vd->vdev_parent;
1275789Sahrens 
1276789Sahrens 	/*
1277789Sahrens 	 * If replace_done is specified, only remove this device if it's
1278789Sahrens 	 * the first child of a replacing vdev.
1279789Sahrens 	 */
1280789Sahrens 	if (replace_done &&
1281789Sahrens 	    (vd->vdev_id != 0 || pvd->vdev_ops != &vdev_replacing_ops))
1282789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1283789Sahrens 
1284789Sahrens 	/*
1285789Sahrens 	 * Only mirror and replacing vdevs support detach.
1286789Sahrens 	 */
1287789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
1288789Sahrens 	    pvd->vdev_ops != &vdev_mirror_ops)
1289789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1290789Sahrens 
1291789Sahrens 	/*
1292789Sahrens 	 * If there's only one replica, you can't detach it.
1293789Sahrens 	 */
1294789Sahrens 	if (pvd->vdev_children <= 1)
1295789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1296789Sahrens 
1297789Sahrens 	/*
1298789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
1299789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
1300789Sahrens 	 *
1301789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
1302789Sahrens 	 * precise DTL check.
1303789Sahrens 	 */
1304789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
1305789Sahrens 		uint64_t dirty;
1306789Sahrens 
1307789Sahrens 		cvd = pvd->vdev_child[c];
1308789Sahrens 		if (cvd == vd)
1309789Sahrens 			continue;
1310789Sahrens 		if (vdev_is_dead(cvd))
1311789Sahrens 			continue;
1312789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
1313789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
1314789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
1315789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
1316789Sahrens 		if (!dirty)
1317789Sahrens 			break;
1318789Sahrens 	}
1319789Sahrens 	if (c == pvd->vdev_children)
1320789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1321789Sahrens 
1322789Sahrens 	/*
1323789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
1324789Sahrens 	 * This must be done after all other error cases are handled,
1325789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
1326789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
1327789Sahrens 	 * it may be that the unwritability of the disk is the reason
1328789Sahrens 	 * it's being detached!
1329789Sahrens 	 */
1330789Sahrens 	error = vdev_label_init(vd, 0);
1331789Sahrens 	if (error)
1332789Sahrens 		dprintf("unable to erase labels on %s\n", vdev_description(vd));
1333789Sahrens 
1334789Sahrens 	/*
1335789Sahrens 	 * Remove vd from its parent and compact the parent's children.
1336789Sahrens 	 */
1337789Sahrens 	vdev_remove_child(pvd, vd);
1338789Sahrens 	vdev_compact_children(pvd);
1339789Sahrens 
1340789Sahrens 	/*
1341789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
1342789Sahrens 	 */
1343789Sahrens 	cvd = pvd->vdev_child[0];
1344789Sahrens 
1345789Sahrens 	/*
1346789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
1347789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
1348789Sahrens 	 */
1349789Sahrens 	if (pvd->vdev_children == 1)
1350789Sahrens 		vdev_remove_parent(cvd);
1351789Sahrens 
1352789Sahrens 	/*
1353789Sahrens 	 * We don't set tvd until now because the parent we just removed
1354789Sahrens 	 * may have been the previous top-level vdev.
1355789Sahrens 	 */
1356789Sahrens 	tvd = cvd->vdev_top;
1357789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
1358789Sahrens 
1359789Sahrens 	/*
1360789Sahrens 	 * Reopen this top-level vdev to reassess health after detach.
1361789Sahrens 	 */
13621544Seschrock 	vdev_reopen(tvd);
1363789Sahrens 
1364789Sahrens 	/*
1365789Sahrens 	 * If the device we just detached was smaller than the others,
13661732Sbonwick 	 * it may be possible to add metaslabs (i.e. grow the pool).
13671732Sbonwick 	 * vdev_metaslab_init() can't fail because the existing metaslabs
13681732Sbonwick 	 * are already in core, so there's nothing to read from disk.
1369789Sahrens 	 */
13701732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
1371789Sahrens 
1372789Sahrens 	vdev_config_dirty(tvd);
1373789Sahrens 
1374789Sahrens 	/*
1375789Sahrens 	 * Mark vd's DTL as dirty in this txg.
1376789Sahrens 	 * vdev_dtl_sync() will see that vd->vdev_detached is set
1377789Sahrens 	 * and free vd's DTL object in syncing context.
1378789Sahrens 	 * But first make sure we're not on any *other* txg's DTL list,
1379789Sahrens 	 * to prevent vd from being accessed after it's freed.
1380789Sahrens 	 */
1381789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
1382789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
13831732Sbonwick 	vd->vdev_detached = B_TRUE;
13841732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
1385789Sahrens 
13861544Seschrock 	dprintf("detached %s in txg %llu\n", vd->vdev_path, txg);
1387789Sahrens 
1388789Sahrens 	return (spa_vdev_exit(spa, vd, txg, 0));
1389789Sahrens }
1390789Sahrens 
1391789Sahrens /*
13921544Seschrock  * Find any device that's done replacing, so we can detach it.
1393789Sahrens  */
13941544Seschrock static vdev_t *
13951544Seschrock spa_vdev_replace_done_hunt(vdev_t *vd)
1396789Sahrens {
13971544Seschrock 	vdev_t *newvd, *oldvd;
1398789Sahrens 	int c;
1399789Sahrens 
14001544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
14011544Seschrock 		oldvd = spa_vdev_replace_done_hunt(vd->vdev_child[c]);
14021544Seschrock 		if (oldvd != NULL)
14031544Seschrock 			return (oldvd);
14041544Seschrock 	}
1405789Sahrens 
1406789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
14071544Seschrock 		oldvd = vd->vdev_child[0];
14081544Seschrock 		newvd = vd->vdev_child[1];
1409789Sahrens 
14101544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
14111544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
14121544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
14131544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
14141544Seschrock 			return (oldvd);
14151544Seschrock 		}
14161544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
14171544Seschrock 	}
1418789Sahrens 
14191544Seschrock 	return (NULL);
1420789Sahrens }
1421789Sahrens 
14221544Seschrock static void
1423789Sahrens spa_vdev_replace_done(spa_t *spa)
1424789Sahrens {
14251544Seschrock 	vdev_t *vd;
14261544Seschrock 	uint64_t guid;
1427789Sahrens 
14281544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
1429789Sahrens 
14301544Seschrock 	while ((vd = spa_vdev_replace_done_hunt(spa->spa_root_vdev)) != NULL) {
14311544Seschrock 		guid = vd->vdev_guid;
14321544Seschrock 		spa_config_exit(spa, FTAG);
14331544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
14341544Seschrock 			return;
14351544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1436789Sahrens 	}
1437789Sahrens 
14381544Seschrock 	spa_config_exit(spa, FTAG);
1439789Sahrens }
1440789Sahrens 
1441789Sahrens /*
14421354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
14431354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
14441354Seschrock  */
14451354Seschrock int
14461354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
14471354Seschrock {
14481354Seschrock 	vdev_t *rvd, *vd;
14491354Seschrock 	uint64_t txg;
14501354Seschrock 
14511354Seschrock 	rvd = spa->spa_root_vdev;
14521354Seschrock 
14531354Seschrock 	txg = spa_vdev_enter(spa);
14541354Seschrock 
14551354Seschrock 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
14561354Seschrock 		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
14571354Seschrock 
14581585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
14591585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
14601585Sbonwick 
14611354Seschrock 	spa_strfree(vd->vdev_path);
14621354Seschrock 	vd->vdev_path = spa_strdup(newpath);
14631354Seschrock 
14641354Seschrock 	vdev_config_dirty(vd->vdev_top);
14651354Seschrock 
14661354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
14671354Seschrock }
14681354Seschrock 
14691354Seschrock /*
1470789Sahrens  * ==========================================================================
1471789Sahrens  * SPA Scrubbing
1472789Sahrens  * ==========================================================================
1473789Sahrens  */
1474789Sahrens 
14751544Seschrock void
14761544Seschrock spa_scrub_throttle(spa_t *spa, int direction)
14771544Seschrock {
14781544Seschrock 	mutex_enter(&spa->spa_scrub_lock);
14791544Seschrock 	spa->spa_scrub_throttled += direction;
14801544Seschrock 	ASSERT(spa->spa_scrub_throttled >= 0);
14811544Seschrock 	if (spa->spa_scrub_throttled == 0)
14821544Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
14831544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
14841544Seschrock }
1485789Sahrens 
1486789Sahrens static void
1487789Sahrens spa_scrub_io_done(zio_t *zio)
1488789Sahrens {
1489789Sahrens 	spa_t *spa = zio->io_spa;
1490789Sahrens 
1491789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
1492789Sahrens 
1493789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
14941544Seschrock 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
14951544Seschrock 		vdev_t *vd = zio->io_vd;
1496789Sahrens 		spa->spa_scrub_errors++;
1497789Sahrens 		mutex_enter(&vd->vdev_stat_lock);
1498789Sahrens 		vd->vdev_stat.vs_scrub_errors++;
1499789Sahrens 		mutex_exit(&vd->vdev_stat_lock);
1500789Sahrens 	}
15011544Seschrock 	if (--spa->spa_scrub_inflight == 0) {
15021544Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
15031544Seschrock 		ASSERT(spa->spa_scrub_throttled == 0);
15041544Seschrock 	}
15051544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
1506789Sahrens }
1507789Sahrens 
1508789Sahrens static void
15091544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags,
15101544Seschrock     zbookmark_t *zb)
1511789Sahrens {
1512789Sahrens 	size_t size = BP_GET_LSIZE(bp);
1513789Sahrens 	void *data = zio_buf_alloc(size);
1514789Sahrens 
1515789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1516789Sahrens 	spa->spa_scrub_inflight++;
1517789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1518789Sahrens 
15191544Seschrock 	if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
15201544Seschrock 		flags |= ZIO_FLAG_SPECULATIVE;	/* intent log block */
15211544Seschrock 
15221544Seschrock 	flags |= ZIO_FLAG_CANFAIL;
15231544Seschrock 
1524789Sahrens 	zio_nowait(zio_read(NULL, spa, bp, data, size,
15251544Seschrock 	    spa_scrub_io_done, NULL, priority, flags, zb));
1526789Sahrens }
1527789Sahrens 
1528789Sahrens /* ARGSUSED */
1529789Sahrens static int
1530789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
1531789Sahrens {
1532789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
1533789Sahrens 	vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[0]));
1534789Sahrens 
1535789Sahrens 	if (bc->bc_errno || vd == NULL) {
1536789Sahrens 		/*
1537789Sahrens 		 * We can't scrub this block, but we can continue to scrub
1538789Sahrens 		 * the rest of the pool.  Note the error and move along.
1539789Sahrens 		 */
1540789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
1541789Sahrens 		spa->spa_scrub_errors++;
1542789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
1543789Sahrens 
1544789Sahrens 		if (vd != NULL) {
1545789Sahrens 			mutex_enter(&vd->vdev_stat_lock);
1546789Sahrens 			vd->vdev_stat.vs_scrub_errors++;
1547789Sahrens 			mutex_exit(&vd->vdev_stat_lock);
1548789Sahrens 		}
1549789Sahrens 
1550789Sahrens 		return (ERESTART);
1551789Sahrens 	}
1552789Sahrens 
1553789Sahrens 	ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg);
1554789Sahrens 
1555789Sahrens 	/*
1556789Sahrens 	 * Keep track of how much data we've examined so that
1557789Sahrens 	 * zpool(1M) status can make useful progress reports.
1558789Sahrens 	 */
1559789Sahrens 	mutex_enter(&vd->vdev_stat_lock);
1560789Sahrens 	vd->vdev_stat.vs_scrub_examined += BP_GET_ASIZE(bp);
1561789Sahrens 	mutex_exit(&vd->vdev_stat_lock);
1562789Sahrens 
1563789Sahrens 	if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) {
1564789Sahrens 		if (DVA_GET_GANG(&bp->blk_dva[0])) {
1565789Sahrens 			/*
1566789Sahrens 			 * Gang members may be spread across multiple vdevs,
1567789Sahrens 			 * so the best we can do is look at the pool-wide DTL.
1568789Sahrens 			 * XXX -- it would be better to change our allocation
1569789Sahrens 			 * policy to ensure that this can't happen.
1570789Sahrens 			 */
1571789Sahrens 			vd = spa->spa_root_vdev;
1572789Sahrens 		}
1573789Sahrens 		if (vdev_dtl_contains(&vd->vdev_dtl_map, bp->blk_birth, 1)) {
1574789Sahrens 			spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER,
15751544Seschrock 			    ZIO_FLAG_RESILVER, &bc->bc_bookmark);
1576789Sahrens 		}
1577789Sahrens 	} else {
1578789Sahrens 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB,
15791544Seschrock 		    ZIO_FLAG_SCRUB, &bc->bc_bookmark);
1580789Sahrens 	}
1581789Sahrens 
1582789Sahrens 	return (0);
1583789Sahrens }
1584789Sahrens 
1585789Sahrens static void
1586789Sahrens spa_scrub_thread(spa_t *spa)
1587789Sahrens {
1588789Sahrens 	callb_cpr_t cprinfo;
1589789Sahrens 	traverse_handle_t *th = spa->spa_scrub_th;
1590789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1591789Sahrens 	pool_scrub_type_t scrub_type = spa->spa_scrub_type;
1592789Sahrens 	int error = 0;
1593789Sahrens 	boolean_t complete;
1594789Sahrens 
1595789Sahrens 	CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG);
1596789Sahrens 
1597797Sbonwick 	/*
1598797Sbonwick 	 * If we're restarting due to a snapshot create/delete,
1599797Sbonwick 	 * wait for that to complete.
1600797Sbonwick 	 */
1601797Sbonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
1602797Sbonwick 
16031544Seschrock 	dprintf("start %s mintxg=%llu maxtxg=%llu\n",
16041544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
16051544Seschrock 	    spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg);
16061544Seschrock 
16071544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
16081544Seschrock 	vdev_reopen(rvd);		/* purge all vdev caches */
1609789Sahrens 	vdev_config_dirty(rvd);		/* rewrite all disk labels */
1610789Sahrens 	vdev_scrub_stat_update(rvd, scrub_type, B_FALSE);
16111544Seschrock 	spa_config_exit(spa, FTAG);
1612789Sahrens 
1613789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1614789Sahrens 	spa->spa_scrub_errors = 0;
1615789Sahrens 	spa->spa_scrub_active = 1;
16161544Seschrock 	ASSERT(spa->spa_scrub_inflight == 0);
16171544Seschrock 	ASSERT(spa->spa_scrub_throttled == 0);
1618789Sahrens 
1619789Sahrens 	while (!spa->spa_scrub_stop) {
1620789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
16211544Seschrock 		while (spa->spa_scrub_suspended) {
1622789Sahrens 			spa->spa_scrub_active = 0;
1623789Sahrens 			cv_broadcast(&spa->spa_scrub_cv);
1624789Sahrens 			cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
1625789Sahrens 			spa->spa_scrub_active = 1;
1626789Sahrens 		}
1627789Sahrens 		CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock);
1628789Sahrens 
1629789Sahrens 		if (spa->spa_scrub_restart_txg != 0)
1630789Sahrens 			break;
1631789Sahrens 
1632789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
1633789Sahrens 		error = traverse_more(th);
1634789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
1635789Sahrens 		if (error != EAGAIN)
1636789Sahrens 			break;
16371544Seschrock 
16381544Seschrock 		while (spa->spa_scrub_throttled > 0)
16391544Seschrock 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1640789Sahrens 	}
1641789Sahrens 
1642789Sahrens 	while (spa->spa_scrub_inflight)
1643789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1644789Sahrens 
16451601Sbonwick 	spa->spa_scrub_active = 0;
16461601Sbonwick 	cv_broadcast(&spa->spa_scrub_cv);
16471601Sbonwick 
16481601Sbonwick 	mutex_exit(&spa->spa_scrub_lock);
16491601Sbonwick 
16501601Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
16511601Sbonwick 
16521601Sbonwick 	mutex_enter(&spa->spa_scrub_lock);
16531601Sbonwick 
16541601Sbonwick 	/*
16551601Sbonwick 	 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock
16561601Sbonwick 	 * AND the spa config lock to synchronize with any config changes
16571601Sbonwick 	 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
16581601Sbonwick 	 */
1659789Sahrens 	if (spa->spa_scrub_restart_txg != 0)
1660789Sahrens 		error = ERESTART;
1661789Sahrens 
16621544Seschrock 	if (spa->spa_scrub_stop)
16631544Seschrock 		error = EINTR;
16641544Seschrock 
1665789Sahrens 	/*
16661544Seschrock 	 * Even if there were uncorrectable errors, we consider the scrub
16671544Seschrock 	 * completed.  The downside is that if there is a transient error during
16681544Seschrock 	 * a resilver, we won't resilver the data properly to the target.  But
16691544Seschrock 	 * if the damage is permanent (more likely) we will resilver forever,
16701544Seschrock 	 * which isn't really acceptable.  Since there is enough information for
16711544Seschrock 	 * the user to know what has failed and why, this seems like a more
16721544Seschrock 	 * tractable approach.
1673789Sahrens 	 */
16741544Seschrock 	complete = (error == 0);
1675789Sahrens 
16761544Seschrock 	dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
16771544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
1678789Sahrens 	    spa->spa_scrub_maxtxg, complete ? "done" : "FAILED",
1679789Sahrens 	    error, spa->spa_scrub_errors, spa->spa_scrub_stop);
1680789Sahrens 
1681789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1682789Sahrens 
1683789Sahrens 	/*
1684789Sahrens 	 * If the scrub/resilver completed, update all DTLs to reflect this.
1685789Sahrens 	 * Whether it succeeded or not, vacate all temporary scrub DTLs.
1686789Sahrens 	 */
1687789Sahrens 	vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1,
1688789Sahrens 	    complete ? spa->spa_scrub_maxtxg : 0, B_TRUE);
1689789Sahrens 	vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete);
16901544Seschrock 	spa_errlog_rotate(spa);
16911601Sbonwick 
16921544Seschrock 	spa_config_exit(spa, FTAG);
1693789Sahrens 
1694789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1695789Sahrens 
16961544Seschrock 	/*
16971544Seschrock 	 * We may have finished replacing a device.
16981544Seschrock 	 * Let the async thread assess this and handle the detach.
16991544Seschrock 	 */
17001544Seschrock 	spa_async_request(spa, SPA_ASYNC_REPLACE_DONE);
1701789Sahrens 
1702789Sahrens 	/*
1703789Sahrens 	 * If we were told to restart, our final act is to start a new scrub.
1704789Sahrens 	 */
1705789Sahrens 	if (error == ERESTART)
17061544Seschrock 		spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ?
17071544Seschrock 		    SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB);
1708789Sahrens 
17091544Seschrock 	spa->spa_scrub_type = POOL_SCRUB_NONE;
17101544Seschrock 	spa->spa_scrub_active = 0;
17111544Seschrock 	spa->spa_scrub_thread = NULL;
17121544Seschrock 	cv_broadcast(&spa->spa_scrub_cv);
1713789Sahrens 	CALLB_CPR_EXIT(&cprinfo);	/* drops &spa->spa_scrub_lock */
1714789Sahrens 	thread_exit();
1715789Sahrens }
1716789Sahrens 
1717789Sahrens void
1718789Sahrens spa_scrub_suspend(spa_t *spa)
1719789Sahrens {
1720789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
17211544Seschrock 	spa->spa_scrub_suspended++;
1722789Sahrens 	while (spa->spa_scrub_active) {
1723789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
1724789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
1725789Sahrens 	}
1726789Sahrens 	while (spa->spa_scrub_inflight)
1727789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1728789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1729789Sahrens }
1730789Sahrens 
1731789Sahrens void
1732789Sahrens spa_scrub_resume(spa_t *spa)
1733789Sahrens {
1734789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
17351544Seschrock 	ASSERT(spa->spa_scrub_suspended != 0);
17361544Seschrock 	if (--spa->spa_scrub_suspended == 0)
1737789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
1738789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1739789Sahrens }
1740789Sahrens 
1741789Sahrens void
1742789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg)
1743789Sahrens {
1744789Sahrens 	/*
1745789Sahrens 	 * Something happened (e.g. snapshot create/delete) that means
1746789Sahrens 	 * we must restart any in-progress scrubs.  The itinerary will
1747789Sahrens 	 * fix this properly.
1748789Sahrens 	 */
1749789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
1750789Sahrens 	spa->spa_scrub_restart_txg = txg;
1751789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
1752789Sahrens }
1753789Sahrens 
17541544Seschrock int
17551544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force)
1756789Sahrens {
1757789Sahrens 	space_seg_t *ss;
1758789Sahrens 	uint64_t mintxg, maxtxg;
1759789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
1760789Sahrens 
1761789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
1762789Sahrens 		return (ENOTSUP);
1763789Sahrens 
17641544Seschrock 	mutex_enter(&spa->spa_scrub_lock);
17651544Seschrock 
1766789Sahrens 	/*
1767789Sahrens 	 * If there's a scrub or resilver already in progress, stop it.
1768789Sahrens 	 */
1769789Sahrens 	while (spa->spa_scrub_thread != NULL) {
1770789Sahrens 		/*
1771789Sahrens 		 * Don't stop a resilver unless forced.
1772789Sahrens 		 */
17731544Seschrock 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) {
17741544Seschrock 			mutex_exit(&spa->spa_scrub_lock);
1775789Sahrens 			return (EBUSY);
17761544Seschrock 		}
1777789Sahrens 		spa->spa_scrub_stop = 1;
1778789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
1779789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
1780789Sahrens 	}
1781789Sahrens 
1782789Sahrens 	/*
1783789Sahrens 	 * Terminate the previous traverse.
1784789Sahrens 	 */
1785789Sahrens 	if (spa->spa_scrub_th != NULL) {
1786789Sahrens 		traverse_fini(spa->spa_scrub_th);
1787789Sahrens 		spa->spa_scrub_th = NULL;
1788789Sahrens 	}
1789789Sahrens 
17901544Seschrock 	if (rvd == NULL) {
17911544Seschrock 		ASSERT(spa->spa_scrub_stop == 0);
17921544Seschrock 		ASSERT(spa->spa_scrub_type == type);
17931544Seschrock 		ASSERT(spa->spa_scrub_restart_txg == 0);
17941544Seschrock 		mutex_exit(&spa->spa_scrub_lock);
17951544Seschrock 		return (0);
17961544Seschrock 	}
1797789Sahrens 
1798789Sahrens 	mintxg = TXG_INITIAL - 1;
1799789Sahrens 	maxtxg = spa_last_synced_txg(spa) + 1;
1800789Sahrens 
18011544Seschrock 	mutex_enter(&rvd->vdev_dtl_lock);
1802789Sahrens 
18031544Seschrock 	if (rvd->vdev_dtl_map.sm_space == 0) {
18041544Seschrock 		/*
18051544Seschrock 		 * The pool-wide DTL is empty.
18061732Sbonwick 		 * If this is a resilver, there's nothing to do except
18071732Sbonwick 		 * check whether any in-progress replacements have completed.
18081544Seschrock 		 */
18091732Sbonwick 		if (type == POOL_SCRUB_RESILVER) {
18101544Seschrock 			type = POOL_SCRUB_NONE;
18111732Sbonwick 			spa_async_request(spa, SPA_ASYNC_REPLACE_DONE);
18121732Sbonwick 		}
18131544Seschrock 	} else {
18141544Seschrock 		/*
18151544Seschrock 		 * The pool-wide DTL is non-empty.
18161544Seschrock 		 * If this is a normal scrub, upgrade to a resilver instead.
18171544Seschrock 		 */
18181544Seschrock 		if (type == POOL_SCRUB_EVERYTHING)
18191544Seschrock 			type = POOL_SCRUB_RESILVER;
18201544Seschrock 	}
1821789Sahrens 
18221544Seschrock 	if (type == POOL_SCRUB_RESILVER) {
1823789Sahrens 		/*
1824789Sahrens 		 * Determine the resilvering boundaries.
1825789Sahrens 		 *
1826789Sahrens 		 * Note: (mintxg, maxtxg) is an open interval,
1827789Sahrens 		 * i.e. mintxg and maxtxg themselves are not included.
1828789Sahrens 		 *
1829789Sahrens 		 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
1830789Sahrens 		 * so we don't claim to resilver a txg that's still changing.
1831789Sahrens 		 */
1832789Sahrens 		ss = avl_first(&rvd->vdev_dtl_map.sm_root);
18331544Seschrock 		mintxg = ss->ss_start - 1;
1834789Sahrens 		ss = avl_last(&rvd->vdev_dtl_map.sm_root);
18351544Seschrock 		maxtxg = MIN(ss->ss_end, maxtxg);
1836789Sahrens 	}
1837789Sahrens 
18381544Seschrock 	mutex_exit(&rvd->vdev_dtl_lock);
18391544Seschrock 
18401544Seschrock 	spa->spa_scrub_stop = 0;
18411544Seschrock 	spa->spa_scrub_type = type;
18421544Seschrock 	spa->spa_scrub_restart_txg = 0;
18431544Seschrock 
18441544Seschrock 	if (type != POOL_SCRUB_NONE) {
18451544Seschrock 		spa->spa_scrub_mintxg = mintxg;
1846789Sahrens 		spa->spa_scrub_maxtxg = maxtxg;
1847789Sahrens 		spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL,
18481635Sbonwick 		    ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL,
18491635Sbonwick 		    ZIO_FLAG_CANFAIL);
1850789Sahrens 		traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg);
1851789Sahrens 		spa->spa_scrub_thread = thread_create(NULL, 0,
1852789Sahrens 		    spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri);
1853789Sahrens 	}
1854789Sahrens 
18551544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
18561544Seschrock 
1857789Sahrens 	return (0);
1858789Sahrens }
1859789Sahrens 
18601544Seschrock /*
18611544Seschrock  * ==========================================================================
18621544Seschrock  * SPA async task processing
18631544Seschrock  * ==========================================================================
18641544Seschrock  */
18651544Seschrock 
18661544Seschrock static void
18671544Seschrock spa_async_reopen(spa_t *spa)
1868789Sahrens {
18691544Seschrock 	vdev_t *rvd = spa->spa_root_vdev;
18701544Seschrock 	vdev_t *tvd;
18711544Seschrock 	int c;
18721544Seschrock 
18731544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
18741544Seschrock 
18751544Seschrock 	for (c = 0; c < rvd->vdev_children; c++) {
18761544Seschrock 		tvd = rvd->vdev_child[c];
18771544Seschrock 		if (tvd->vdev_reopen_wanted) {
18781544Seschrock 			tvd->vdev_reopen_wanted = 0;
18791544Seschrock 			vdev_reopen(tvd);
18801544Seschrock 		}
18811544Seschrock 	}
1882789Sahrens 
18831544Seschrock 	spa_config_exit(spa, FTAG);
18841544Seschrock }
18851544Seschrock 
18861544Seschrock static void
18871544Seschrock spa_async_thread(spa_t *spa)
18881544Seschrock {
18891544Seschrock 	int tasks;
18901544Seschrock 
18911544Seschrock 	ASSERT(spa->spa_sync_on);
1892789Sahrens 
18931544Seschrock 	mutex_enter(&spa->spa_async_lock);
18941544Seschrock 	tasks = spa->spa_async_tasks;
18951544Seschrock 	spa->spa_async_tasks = 0;
18961544Seschrock 	mutex_exit(&spa->spa_async_lock);
18971544Seschrock 
18981544Seschrock 	/*
18991635Sbonwick 	 * See if the config needs to be updated.
19001635Sbonwick 	 */
19011635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
19021635Sbonwick 		mutex_enter(&spa_namespace_lock);
19031635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
19041635Sbonwick 		mutex_exit(&spa_namespace_lock);
19051635Sbonwick 	}
19061635Sbonwick 
19071635Sbonwick 	/*
19081544Seschrock 	 * See if any devices need to be reopened.
19091544Seschrock 	 */
19101544Seschrock 	if (tasks & SPA_ASYNC_REOPEN)
19111544Seschrock 		spa_async_reopen(spa);
19121544Seschrock 
19131544Seschrock 	/*
19141544Seschrock 	 * If any devices are done replacing, detach them.
19151544Seschrock 	 */
19161544Seschrock 	if (tasks & SPA_ASYNC_REPLACE_DONE)
1917789Sahrens 		spa_vdev_replace_done(spa);
1918789Sahrens 
19191544Seschrock 	/*
19201544Seschrock 	 * Kick off a scrub.
19211544Seschrock 	 */
19221544Seschrock 	if (tasks & SPA_ASYNC_SCRUB)
19231544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0);
19241544Seschrock 
19251544Seschrock 	/*
19261544Seschrock 	 * Kick off a resilver.
19271544Seschrock 	 */
19281544Seschrock 	if (tasks & SPA_ASYNC_RESILVER)
19291544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
19301544Seschrock 
19311544Seschrock 	/*
19321544Seschrock 	 * Let the world know that we're done.
19331544Seschrock 	 */
19341544Seschrock 	mutex_enter(&spa->spa_async_lock);
19351544Seschrock 	spa->spa_async_thread = NULL;
19361544Seschrock 	cv_broadcast(&spa->spa_async_cv);
19371544Seschrock 	mutex_exit(&spa->spa_async_lock);
19381544Seschrock 	thread_exit();
19391544Seschrock }
19401544Seschrock 
19411544Seschrock void
19421544Seschrock spa_async_suspend(spa_t *spa)
19431544Seschrock {
19441544Seschrock 	mutex_enter(&spa->spa_async_lock);
19451544Seschrock 	spa->spa_async_suspended++;
19461544Seschrock 	while (spa->spa_async_thread != NULL)
19471544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
19481544Seschrock 	mutex_exit(&spa->spa_async_lock);
19491544Seschrock }
19501544Seschrock 
19511544Seschrock void
19521544Seschrock spa_async_resume(spa_t *spa)
19531544Seschrock {
19541544Seschrock 	mutex_enter(&spa->spa_async_lock);
19551544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
19561544Seschrock 	spa->spa_async_suspended--;
19571544Seschrock 	mutex_exit(&spa->spa_async_lock);
19581544Seschrock }
19591544Seschrock 
19601544Seschrock static void
19611544Seschrock spa_async_dispatch(spa_t *spa)
19621544Seschrock {
19631544Seschrock 	mutex_enter(&spa->spa_async_lock);
19641544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
19651635Sbonwick 	    spa->spa_async_thread == NULL &&
19661635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
19671544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
19681544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
19691544Seschrock 	mutex_exit(&spa->spa_async_lock);
19701544Seschrock }
19711544Seschrock 
19721544Seschrock void
19731544Seschrock spa_async_request(spa_t *spa, int task)
19741544Seschrock {
19751544Seschrock 	mutex_enter(&spa->spa_async_lock);
19761544Seschrock 	spa->spa_async_tasks |= task;
19771544Seschrock 	mutex_exit(&spa->spa_async_lock);
1978789Sahrens }
1979789Sahrens 
1980789Sahrens /*
1981789Sahrens  * ==========================================================================
1982789Sahrens  * SPA syncing routines
1983789Sahrens  * ==========================================================================
1984789Sahrens  */
1985789Sahrens 
1986789Sahrens static void
1987789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
1988789Sahrens {
1989789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
1990789Sahrens 	dmu_tx_t *tx;
1991789Sahrens 	blkptr_t blk;
1992789Sahrens 	uint64_t itor = 0;
1993789Sahrens 	zio_t *zio;
1994789Sahrens 	int error;
1995789Sahrens 	uint8_t c = 1;
1996789Sahrens 
1997789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
1998789Sahrens 
1999789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
2000789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
2001789Sahrens 
2002789Sahrens 	error = zio_wait(zio);
2003789Sahrens 	ASSERT3U(error, ==, 0);
2004789Sahrens 
2005789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2006789Sahrens 	bplist_vacate(bpl, tx);
2007789Sahrens 
2008789Sahrens 	/*
2009789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
2010789Sahrens 	 * (Usually only the first block is needed.)
2011789Sahrens 	 */
2012789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
2013789Sahrens 	dmu_tx_commit(tx);
2014789Sahrens }
2015789Sahrens 
2016789Sahrens static void
2017789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
2018789Sahrens {
2019789Sahrens 	nvlist_t *config;
2020789Sahrens 	char *packed = NULL;
2021789Sahrens 	size_t nvsize = 0;
2022789Sahrens 	dmu_buf_t *db;
2023789Sahrens 
2024789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
2025789Sahrens 		return;
2026789Sahrens 
2027789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
2028789Sahrens 
20291635Sbonwick 	if (spa->spa_config_syncing)
20301635Sbonwick 		nvlist_free(spa->spa_config_syncing);
20311635Sbonwick 	spa->spa_config_syncing = config;
2032789Sahrens 
2033789Sahrens 	VERIFY(nvlist_size(config, &nvsize, NV_ENCODE_XDR) == 0);
2034789Sahrens 
2035789Sahrens 	packed = kmem_alloc(nvsize, KM_SLEEP);
2036789Sahrens 
20371544Seschrock 	VERIFY(nvlist_pack(config, &packed, &nvsize, NV_ENCODE_XDR,
20381544Seschrock 	    KM_SLEEP) == 0);
2039789Sahrens 
2040789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_config_object, 0, nvsize,
2041789Sahrens 	    packed, tx);
2042789Sahrens 
2043789Sahrens 	kmem_free(packed, nvsize);
2044789Sahrens 
20451544Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset,
20461544Seschrock 	    spa->spa_config_object, FTAG, &db));
2047789Sahrens 	dmu_buf_will_dirty(db, tx);
2048789Sahrens 	*(uint64_t *)db->db_data = nvsize;
20491544Seschrock 	dmu_buf_rele(db, FTAG);
2050789Sahrens }
2051789Sahrens 
2052789Sahrens /*
2053789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
2054789Sahrens  * part of the process, so we iterate until it converges.
2055789Sahrens  */
2056789Sahrens void
2057789Sahrens spa_sync(spa_t *spa, uint64_t txg)
2058789Sahrens {
2059789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
2060789Sahrens 	objset_t *mos = spa->spa_meta_objset;
2061789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
20621635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
2063789Sahrens 	vdev_t *vd;
2064789Sahrens 	dmu_tx_t *tx;
2065789Sahrens 	int dirty_vdevs;
2066789Sahrens 
2067789Sahrens 	/*
2068789Sahrens 	 * Lock out configuration changes.
2069789Sahrens 	 */
20701544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2071789Sahrens 
2072789Sahrens 	spa->spa_syncing_txg = txg;
2073789Sahrens 	spa->spa_sync_pass = 0;
2074789Sahrens 
20751544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
2076789Sahrens 
2077789Sahrens 	/*
2078789Sahrens 	 * If anything has changed in this txg, push the deferred frees
2079789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
2080789Sahrens 	 * don't generate work on an otherwise idle system.
2081789Sahrens 	 */
2082789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
2083789Sahrens 	    !txg_list_empty(&dp->dp_dirty_dirs, txg))
2084789Sahrens 		spa_sync_deferred_frees(spa, txg);
2085789Sahrens 
2086789Sahrens 	/*
2087789Sahrens 	 * Iterate to convergence.
2088789Sahrens 	 */
2089789Sahrens 	do {
2090789Sahrens 		spa->spa_sync_pass++;
2091789Sahrens 
2092789Sahrens 		tx = dmu_tx_create_assigned(dp, txg);
2093789Sahrens 		spa_sync_config_object(spa, tx);
2094789Sahrens 		dmu_tx_commit(tx);
2095789Sahrens 
20961544Seschrock 		spa_errlog_sync(spa, txg);
20971544Seschrock 
2098789Sahrens 		dsl_pool_sync(dp, txg);
2099789Sahrens 
2100789Sahrens 		dirty_vdevs = 0;
2101789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
2102789Sahrens 			vdev_sync(vd, txg);
2103789Sahrens 			dirty_vdevs++;
2104789Sahrens 		}
2105789Sahrens 
2106789Sahrens 		tx = dmu_tx_create_assigned(dp, txg);
2107789Sahrens 		bplist_sync(bpl, tx);
2108789Sahrens 		dmu_tx_commit(tx);
2109789Sahrens 
2110789Sahrens 	} while (dirty_vdevs);
2111789Sahrens 
2112789Sahrens 	bplist_close(bpl);
2113789Sahrens 
2114789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
2115789Sahrens 
2116789Sahrens 	/*
2117789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
2118789Sahrens 	 * to commit the transaction group.
21191635Sbonwick 	 *
21201635Sbonwick 	 * If there are any dirty vdevs, sync the uberblock to all vdevs.
21211635Sbonwick 	 * Otherwise, pick a random top-level vdev that's known to be
21221635Sbonwick 	 * visible in the config cache (see spa_vdev_add() for details).
21231635Sbonwick 	 * If the write fails, try the next vdev until we're tried them all.
2124789Sahrens 	 */
21251635Sbonwick 	if (!list_is_empty(&spa->spa_dirty_list)) {
21261635Sbonwick 		VERIFY(vdev_config_sync(rvd, txg) == 0);
21271635Sbonwick 	} else {
21281635Sbonwick 		int children = rvd->vdev_children;
21291635Sbonwick 		int c0 = spa_get_random(children);
21301635Sbonwick 		int c;
21311635Sbonwick 
21321635Sbonwick 		for (c = 0; c < children; c++) {
21331635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
21341635Sbonwick 			if (vd->vdev_ms_array == 0)
21351635Sbonwick 				continue;
21361635Sbonwick 			if (vdev_config_sync(vd, txg) == 0)
21371635Sbonwick 				break;
21381635Sbonwick 		}
21391635Sbonwick 		if (c == children)
21401635Sbonwick 			VERIFY(vdev_config_sync(rvd, txg) == 0);
21411635Sbonwick 	}
21421635Sbonwick 
21431635Sbonwick 	/*
21441635Sbonwick 	 * Clear the dirty config list.
21451635Sbonwick 	 */
21461635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
21471635Sbonwick 		vdev_config_clean(vd);
21481635Sbonwick 
21491635Sbonwick 	/*
21501635Sbonwick 	 * Now that the new config has synced transactionally,
21511635Sbonwick 	 * let it become visible to the config cache.
21521635Sbonwick 	 */
21531635Sbonwick 	if (spa->spa_config_syncing != NULL) {
21541635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
21551635Sbonwick 		spa->spa_config_txg = txg;
21561635Sbonwick 		spa->spa_config_syncing = NULL;
21571635Sbonwick 	}
2158789Sahrens 
2159789Sahrens 	/*
2160789Sahrens 	 * Make a stable copy of the fully synced uberblock.
2161789Sahrens 	 * We use this as the root for pool traversals.
2162789Sahrens 	 */
2163789Sahrens 	spa->spa_traverse_wanted = 1;	/* tells traverse_more() to stop */
2164789Sahrens 
2165789Sahrens 	spa_scrub_suspend(spa);		/* stop scrubbing and finish I/Os */
2166789Sahrens 
2167789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
2168789Sahrens 	spa->spa_traverse_wanted = 0;
2169789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
2170789Sahrens 	rw_exit(&spa->spa_traverse_lock);
2171789Sahrens 
2172789Sahrens 	spa_scrub_resume(spa);		/* resume scrub with new ubsync */
2173789Sahrens 
2174789Sahrens 	/*
2175789Sahrens 	 * Clean up the ZIL records for the synced txg.
2176789Sahrens 	 */
2177789Sahrens 	dsl_pool_zil_clean(dp);
2178789Sahrens 
2179789Sahrens 	/*
2180789Sahrens 	 * Update usable space statistics.
2181789Sahrens 	 */
2182789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
2183789Sahrens 		vdev_sync_done(vd, txg);
2184789Sahrens 
2185789Sahrens 	/*
2186789Sahrens 	 * It had better be the case that we didn't dirty anything
2187789Sahrens 	 * since spa_sync_labels().
2188789Sahrens 	 */
2189789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
2190789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
2191789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
2192789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
2193789Sahrens 
21941544Seschrock 	spa_config_exit(spa, FTAG);
21951544Seschrock 
21961544Seschrock 	/*
21971544Seschrock 	 * If any async tasks have been requested, kick them off.
21981544Seschrock 	 */
21991544Seschrock 	spa_async_dispatch(spa);
2200789Sahrens }
2201789Sahrens 
2202789Sahrens /*
2203789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
2204789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
2205789Sahrens  * sync.
2206789Sahrens  */
2207789Sahrens void
2208789Sahrens spa_sync_allpools(void)
2209789Sahrens {
2210789Sahrens 	spa_t *spa = NULL;
2211789Sahrens 	mutex_enter(&spa_namespace_lock);
2212789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
2213789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
2214789Sahrens 			continue;
2215789Sahrens 		spa_open_ref(spa, FTAG);
2216789Sahrens 		mutex_exit(&spa_namespace_lock);
2217789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
2218789Sahrens 		mutex_enter(&spa_namespace_lock);
2219789Sahrens 		spa_close(spa, FTAG);
2220789Sahrens 	}
2221789Sahrens 	mutex_exit(&spa_namespace_lock);
2222789Sahrens }
2223789Sahrens 
2224789Sahrens /*
2225789Sahrens  * ==========================================================================
2226789Sahrens  * Miscellaneous routines
2227789Sahrens  * ==========================================================================
2228789Sahrens  */
2229789Sahrens 
2230789Sahrens /*
2231789Sahrens  * Remove all pools in the system.
2232789Sahrens  */
2233789Sahrens void
2234789Sahrens spa_evict_all(void)
2235789Sahrens {
2236789Sahrens 	spa_t *spa;
2237789Sahrens 
2238789Sahrens 	/*
2239789Sahrens 	 * Remove all cached state.  All pools should be closed now,
2240789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
2241789Sahrens 	 */
2242789Sahrens 	mutex_enter(&spa_namespace_lock);
2243789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
2244789Sahrens 		/*
22451544Seschrock 		 * Stop async tasks.  The async thread may need to detach
22461544Seschrock 		 * a device that's been replaced, which requires grabbing
22471544Seschrock 		 * spa_namespace_lock, so we must drop it here.
2248789Sahrens 		 */
2249789Sahrens 		spa_open_ref(spa, FTAG);
2250789Sahrens 		mutex_exit(&spa_namespace_lock);
22511544Seschrock 		spa_async_suspend(spa);
2252789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
2253789Sahrens 		mutex_enter(&spa_namespace_lock);
2254789Sahrens 		spa_close(spa, FTAG);
2255789Sahrens 
2256789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2257789Sahrens 			spa_unload(spa);
2258789Sahrens 			spa_deactivate(spa);
2259789Sahrens 		}
2260789Sahrens 		spa_remove(spa);
2261789Sahrens 	}
2262789Sahrens 	mutex_exit(&spa_namespace_lock);
2263789Sahrens }
22641544Seschrock 
22651544Seschrock vdev_t *
22661544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid)
22671544Seschrock {
22681544Seschrock 	return (vdev_lookup_by_guid(spa->spa_root_vdev, guid));
22691544Seschrock }
2270