xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa.c (revision 5094:71a3e95fb9e2)
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  */
212082Seschrock 
22789Sahrens /*
233377Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens /*
30789Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
31789Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
32789Sahrens  * pool.
33789Sahrens  */
34789Sahrens 
35789Sahrens #include <sys/zfs_context.h>
361544Seschrock #include <sys/fm/fs/zfs.h>
37789Sahrens #include <sys/spa_impl.h>
38789Sahrens #include <sys/zio.h>
39789Sahrens #include <sys/zio_checksum.h>
40789Sahrens #include <sys/zio_compress.h>
41789Sahrens #include <sys/dmu.h>
42789Sahrens #include <sys/dmu_tx.h>
43789Sahrens #include <sys/zap.h>
44789Sahrens #include <sys/zil.h>
45789Sahrens #include <sys/vdev_impl.h>
46789Sahrens #include <sys/metaslab.h>
47789Sahrens #include <sys/uberblock_impl.h>
48789Sahrens #include <sys/txg.h>
49789Sahrens #include <sys/avl.h>
50789Sahrens #include <sys/dmu_traverse.h>
513912Slling #include <sys/dmu_objset.h>
52789Sahrens #include <sys/unique.h>
53789Sahrens #include <sys/dsl_pool.h>
543912Slling #include <sys/dsl_dataset.h>
55789Sahrens #include <sys/dsl_dir.h>
56789Sahrens #include <sys/dsl_prop.h>
573912Slling #include <sys/dsl_synctask.h>
58789Sahrens #include <sys/fs/zfs.h>
59789Sahrens #include <sys/callb.h>
603975Sek110237 #include <sys/systeminfo.h>
613975Sek110237 #include <sys/sunddi.h>
62789Sahrens 
63*5094Slling #include "zfs_prop.h"
64*5094Slling 
652986Sek110237 int zio_taskq_threads = 8;
662986Sek110237 
67*5094Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
68*5094Slling 
69*5094Slling /*
70*5094Slling  * ==========================================================================
71*5094Slling  * SPA properties routines
72*5094Slling  * ==========================================================================
73*5094Slling  */
74*5094Slling 
75*5094Slling /*
76*5094Slling  * Add a (source=src, propname=propval) list to an nvlist.
77*5094Slling  */
78*5094Slling static int
79*5094Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
80*5094Slling     uint64_t intval, zprop_source_t src)
81*5094Slling {
82*5094Slling 	const char *propname = zpool_prop_to_name(prop);
83*5094Slling 	nvlist_t *propval;
84*5094Slling 	int err = 0;
85*5094Slling 
86*5094Slling 	if (err = nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP))
87*5094Slling 		return (err);
88*5094Slling 
89*5094Slling 	if (err = nvlist_add_uint64(propval, ZPROP_SOURCE, src))
90*5094Slling 		goto out;
91*5094Slling 
92*5094Slling 	if (strval != NULL) {
93*5094Slling 		if (err = nvlist_add_string(propval, ZPROP_VALUE, strval))
94*5094Slling 			goto out;
95*5094Slling 	} else {
96*5094Slling 		if (err = nvlist_add_uint64(propval, ZPROP_VALUE, intval))
97*5094Slling 			goto out;
98*5094Slling 	}
99*5094Slling 
100*5094Slling 	err = nvlist_add_nvlist(nvl, propname, propval);
101*5094Slling out:
102*5094Slling 	nvlist_free(propval);
103*5094Slling 	return (err);
104*5094Slling }
105*5094Slling 
106*5094Slling /*
107*5094Slling  * Get property values from the spa configuration.
108*5094Slling  */
109*5094Slling static int
110*5094Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
111*5094Slling {
112*5094Slling 	uint64_t size = spa_get_space(spa);
113*5094Slling 	uint64_t used = spa_get_alloc(spa);
114*5094Slling 	uint64_t cap, version;
115*5094Slling 	zprop_source_t src = ZPROP_SRC_NONE;
116*5094Slling 	int err;
117*5094Slling 
118*5094Slling 	/*
119*5094Slling 	 * readonly properties
120*5094Slling 	 */
121*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name,
122*5094Slling 	    0, src))
123*5094Slling 		return (err);
124*5094Slling 
125*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src))
126*5094Slling 		return (err);
127*5094Slling 
128*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src))
129*5094Slling 		return (err);
130*5094Slling 
131*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
132*5094Slling 	    size - used, src))
133*5094Slling 		return (err);
134*5094Slling 
135*5094Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
136*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src))
137*5094Slling 		return (err);
138*5094Slling 
139*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL,
140*5094Slling 	    spa_guid(spa), src))
141*5094Slling 		return (err);
142*5094Slling 
143*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
144*5094Slling 	    spa->spa_root_vdev->vdev_state, src))
145*5094Slling 		return (err);
146*5094Slling 
147*5094Slling 	/*
148*5094Slling 	 * settable properties that are not stored in the pool property object.
149*5094Slling 	 */
150*5094Slling 	version = spa_version(spa);
151*5094Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
152*5094Slling 		src = ZPROP_SRC_DEFAULT;
153*5094Slling 	else
154*5094Slling 		src = ZPROP_SRC_LOCAL;
155*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
156*5094Slling 	    version, src))
157*5094Slling 		return (err);
158*5094Slling 
159*5094Slling 	if (spa->spa_root != NULL) {
160*5094Slling 		src = ZPROP_SRC_LOCAL;
161*5094Slling 		if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT,
162*5094Slling 		    spa->spa_root, 0, src))
163*5094Slling 			return (err);
164*5094Slling 	}
165*5094Slling 
166*5094Slling 	if (spa->spa_temporary ==
167*5094Slling 	    zpool_prop_default_numeric(ZPOOL_PROP_TEMPORARY))
168*5094Slling 		src = ZPROP_SRC_DEFAULT;
169*5094Slling 	else
170*5094Slling 		src = ZPROP_SRC_LOCAL;
171*5094Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_TEMPORARY, NULL,
172*5094Slling 	    spa->spa_temporary, src))
173*5094Slling 		return (err);
174*5094Slling 
175*5094Slling 	return (0);
176*5094Slling }
177*5094Slling 
178*5094Slling /*
179*5094Slling  * Get zpool property values.
180*5094Slling  */
181*5094Slling int
182*5094Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
183*5094Slling {
184*5094Slling 	zap_cursor_t zc;
185*5094Slling 	zap_attribute_t za;
186*5094Slling 	objset_t *mos = spa->spa_meta_objset;
187*5094Slling 	int err;
188*5094Slling 
189*5094Slling 	if (err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP))
190*5094Slling 		return (err);
191*5094Slling 
192*5094Slling 	/*
193*5094Slling 	 * Get properties from the spa config.
194*5094Slling 	 */
195*5094Slling 	if (err = spa_prop_get_config(spa, nvp))
196*5094Slling 		goto out;
197*5094Slling 
198*5094Slling 	mutex_enter(&spa->spa_props_lock);
199*5094Slling 	/* If no pool property object, no more prop to get. */
200*5094Slling 	if (spa->spa_pool_props_object == 0) {
201*5094Slling 		mutex_exit(&spa->spa_props_lock);
202*5094Slling 		return (0);
203*5094Slling 	}
204*5094Slling 
205*5094Slling 	/*
206*5094Slling 	 * Get properties from the MOS pool property object.
207*5094Slling 	 */
208*5094Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
209*5094Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
210*5094Slling 	    zap_cursor_advance(&zc)) {
211*5094Slling 		uint64_t intval = 0;
212*5094Slling 		char *strval = NULL;
213*5094Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
214*5094Slling 		zpool_prop_t prop;
215*5094Slling 
216*5094Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
217*5094Slling 			continue;
218*5094Slling 
219*5094Slling 		switch (za.za_integer_length) {
220*5094Slling 		case 8:
221*5094Slling 			/* integer property */
222*5094Slling 			if (za.za_first_integer !=
223*5094Slling 			    zpool_prop_default_numeric(prop))
224*5094Slling 				src = ZPROP_SRC_LOCAL;
225*5094Slling 
226*5094Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
227*5094Slling 				dsl_pool_t *dp;
228*5094Slling 				dsl_dataset_t *ds = NULL;
229*5094Slling 
230*5094Slling 				dp = spa_get_dsl(spa);
231*5094Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
232*5094Slling 				if (err = dsl_dataset_open_obj(dp,
233*5094Slling 				    za.za_first_integer, NULL, DS_MODE_NONE,
234*5094Slling 				    FTAG, &ds)) {
235*5094Slling 					rw_exit(&dp->dp_config_rwlock);
236*5094Slling 					break;
237*5094Slling 				}
238*5094Slling 
239*5094Slling 				strval = kmem_alloc(
240*5094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
241*5094Slling 				    KM_SLEEP);
242*5094Slling 				dsl_dataset_name(ds, strval);
243*5094Slling 				dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
244*5094Slling 				rw_exit(&dp->dp_config_rwlock);
245*5094Slling 			} else {
246*5094Slling 				strval = NULL;
247*5094Slling 				intval = za.za_first_integer;
248*5094Slling 			}
249*5094Slling 
250*5094Slling 			err = spa_prop_add_list(*nvp, prop, strval,
251*5094Slling 			    intval, src);
252*5094Slling 
253*5094Slling 			if (strval != NULL)
254*5094Slling 				kmem_free(strval,
255*5094Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
256*5094Slling 
257*5094Slling 			break;
258*5094Slling 
259*5094Slling 		case 1:
260*5094Slling 			/* string property */
261*5094Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
262*5094Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
263*5094Slling 			    za.za_name, 1, za.za_num_integers, strval);
264*5094Slling 			if (err) {
265*5094Slling 				kmem_free(strval, za.za_num_integers);
266*5094Slling 				break;
267*5094Slling 			}
268*5094Slling 			err = spa_prop_add_list(*nvp, prop, strval, 0, src);
269*5094Slling 			kmem_free(strval, za.za_num_integers);
270*5094Slling 			break;
271*5094Slling 
272*5094Slling 		default:
273*5094Slling 			break;
274*5094Slling 		}
275*5094Slling 	}
276*5094Slling 	zap_cursor_fini(&zc);
277*5094Slling 	mutex_exit(&spa->spa_props_lock);
278*5094Slling out:
279*5094Slling 	if (err && err != ENOENT) {
280*5094Slling 		nvlist_free(*nvp);
281*5094Slling 		return (err);
282*5094Slling 	}
283*5094Slling 
284*5094Slling 	return (0);
285*5094Slling }
286*5094Slling 
287*5094Slling /*
288*5094Slling  * Validate the given pool properties nvlist and modify the list
289*5094Slling  * for the property values to be set.
290*5094Slling  */
291*5094Slling static int
292*5094Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
293*5094Slling {
294*5094Slling 	nvpair_t *elem;
295*5094Slling 	int error = 0, reset_bootfs = 0;
296*5094Slling 	uint64_t objnum;
297*5094Slling 
298*5094Slling 	elem = NULL;
299*5094Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
300*5094Slling 		zpool_prop_t prop;
301*5094Slling 		char *propname, *strval;
302*5094Slling 		uint64_t intval;
303*5094Slling 		vdev_t *rvdev;
304*5094Slling 		char *vdev_type;
305*5094Slling 		objset_t *os;
306*5094Slling 
307*5094Slling 		propname = nvpair_name(elem);
308*5094Slling 
309*5094Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
310*5094Slling 			return (EINVAL);
311*5094Slling 
312*5094Slling 		switch (prop) {
313*5094Slling 		case ZPOOL_PROP_VERSION:
314*5094Slling 			error = nvpair_value_uint64(elem, &intval);
315*5094Slling 			if (!error &&
316*5094Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
317*5094Slling 				error = EINVAL;
318*5094Slling 			break;
319*5094Slling 
320*5094Slling 		case ZPOOL_PROP_DELEGATION:
321*5094Slling 		case ZPOOL_PROP_AUTOREPLACE:
322*5094Slling 			error = nvpair_value_uint64(elem, &intval);
323*5094Slling 			if (!error && intval > 1)
324*5094Slling 				error = EINVAL;
325*5094Slling 			break;
326*5094Slling 
327*5094Slling 		case ZPOOL_PROP_BOOTFS:
328*5094Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
329*5094Slling 				error = ENOTSUP;
330*5094Slling 				break;
331*5094Slling 			}
332*5094Slling 
333*5094Slling 			/*
334*5094Slling 			 * A bootable filesystem can not be on a RAIDZ pool
335*5094Slling 			 * nor a striped pool with more than 1 device.
336*5094Slling 			 */
337*5094Slling 			rvdev = spa->spa_root_vdev;
338*5094Slling 			vdev_type =
339*5094Slling 			    rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
340*5094Slling 			if (rvdev->vdev_children > 1 ||
341*5094Slling 			    strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
342*5094Slling 			    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
343*5094Slling 				error = ENOTSUP;
344*5094Slling 				break;
345*5094Slling 			}
346*5094Slling 
347*5094Slling 			reset_bootfs = 1;
348*5094Slling 
349*5094Slling 			error = nvpair_value_string(elem, &strval);
350*5094Slling 
351*5094Slling 			if (!error) {
352*5094Slling 				if (strval == NULL || strval[0] == '\0') {
353*5094Slling 					objnum = zpool_prop_default_numeric(
354*5094Slling 					    ZPOOL_PROP_BOOTFS);
355*5094Slling 					break;
356*5094Slling 				}
357*5094Slling 
358*5094Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
359*5094Slling 				    DS_MODE_STANDARD | DS_MODE_READONLY, &os))
360*5094Slling 					break;
361*5094Slling 				objnum = dmu_objset_id(os);
362*5094Slling 				dmu_objset_close(os);
363*5094Slling 			}
364*5094Slling 			break;
365*5094Slling 		}
366*5094Slling 
367*5094Slling 		if (error)
368*5094Slling 			break;
369*5094Slling 	}
370*5094Slling 
371*5094Slling 	if (!error && reset_bootfs) {
372*5094Slling 		error = nvlist_remove(props,
373*5094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
374*5094Slling 
375*5094Slling 		if (!error) {
376*5094Slling 			error = nvlist_add_uint64(props,
377*5094Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
378*5094Slling 		}
379*5094Slling 	}
380*5094Slling 
381*5094Slling 	return (error);
382*5094Slling }
383*5094Slling 
384*5094Slling int
385*5094Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
386*5094Slling {
387*5094Slling 	int error;
388*5094Slling 
389*5094Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
390*5094Slling 		return (error);
391*5094Slling 
392*5094Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
393*5094Slling 	    spa, nvp, 3));
394*5094Slling }
395*5094Slling 
396*5094Slling /*
397*5094Slling  * If the bootfs property value is dsobj, clear it.
398*5094Slling  */
399*5094Slling void
400*5094Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
401*5094Slling {
402*5094Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
403*5094Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
404*5094Slling 		    spa->spa_pool_props_object,
405*5094Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
406*5094Slling 		spa->spa_bootfs = 0;
407*5094Slling 	}
408*5094Slling }
409*5094Slling 
410789Sahrens /*
411789Sahrens  * ==========================================================================
412789Sahrens  * SPA state manipulation (open/create/destroy/import/export)
413789Sahrens  * ==========================================================================
414789Sahrens  */
415789Sahrens 
4161544Seschrock static int
4171544Seschrock spa_error_entry_compare(const void *a, const void *b)
4181544Seschrock {
4191544Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
4201544Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
4211544Seschrock 	int ret;
4221544Seschrock 
4231544Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
4241544Seschrock 	    sizeof (zbookmark_t));
4251544Seschrock 
4261544Seschrock 	if (ret < 0)
4271544Seschrock 		return (-1);
4281544Seschrock 	else if (ret > 0)
4291544Seschrock 		return (1);
4301544Seschrock 	else
4311544Seschrock 		return (0);
4321544Seschrock }
4331544Seschrock 
4341544Seschrock /*
4351544Seschrock  * Utility function which retrieves copies of the current logs and
4361544Seschrock  * re-initializes them in the process.
4371544Seschrock  */
4381544Seschrock void
4391544Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
4401544Seschrock {
4411544Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
4421544Seschrock 
4431544Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
4441544Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
4451544Seschrock 
4461544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4471544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4481544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4491544Seschrock 	avl_create(&spa->spa_errlist_last,
4501544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4511544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4521544Seschrock }
4531544Seschrock 
454789Sahrens /*
455789Sahrens  * Activate an uninitialized pool.
456789Sahrens  */
457789Sahrens static void
458789Sahrens spa_activate(spa_t *spa)
459789Sahrens {
460789Sahrens 	int t;
461789Sahrens 
462789Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
463789Sahrens 
464789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
465789Sahrens 
466789Sahrens 	spa->spa_normal_class = metaslab_class_create();
4674527Sperrin 	spa->spa_log_class = metaslab_class_create();
468789Sahrens 
469789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
470789Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
4712986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
472789Sahrens 		    TASKQ_PREPOPULATE);
473789Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
4742986Sek110237 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
475789Sahrens 		    TASKQ_PREPOPULATE);
476789Sahrens 	}
477789Sahrens 
478789Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
479789Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
480789Sahrens 
481789Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
482789Sahrens 	    offsetof(struct vdev, vdev_txg_node));
4831544Seschrock 
4841544Seschrock 	avl_create(&spa->spa_errlist_scrub,
4851544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4861544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
4871544Seschrock 	avl_create(&spa->spa_errlist_last,
4881544Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
4891544Seschrock 	    offsetof(spa_error_entry_t, se_avl));
490789Sahrens }
491789Sahrens 
492789Sahrens /*
493789Sahrens  * Opposite of spa_activate().
494789Sahrens  */
495789Sahrens static void
496789Sahrens spa_deactivate(spa_t *spa)
497789Sahrens {
498789Sahrens 	int t;
499789Sahrens 
500789Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
501789Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
502789Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
503789Sahrens 
504789Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
505789Sahrens 
506789Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
507789Sahrens 
508789Sahrens 	list_destroy(&spa->spa_dirty_list);
509789Sahrens 
510789Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
511789Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
512789Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
513789Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
514789Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
515789Sahrens 	}
516789Sahrens 
517789Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
518789Sahrens 	spa->spa_normal_class = NULL;
519789Sahrens 
5204527Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5214527Sperrin 	spa->spa_log_class = NULL;
5224527Sperrin 
5231544Seschrock 	/*
5241544Seschrock 	 * If this was part of an import or the open otherwise failed, we may
5251544Seschrock 	 * still have errors left in the queues.  Empty them just in case.
5261544Seschrock 	 */
5271544Seschrock 	spa_errlog_drain(spa);
5281544Seschrock 
5291544Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
5301544Seschrock 	avl_destroy(&spa->spa_errlist_last);
5311544Seschrock 
532789Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
533789Sahrens }
534789Sahrens 
535789Sahrens /*
536789Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
537789Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
538789Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
539789Sahrens  * All vdev validation is done by the vdev_alloc() routine.
540789Sahrens  */
5412082Seschrock static int
5422082Seschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
5432082Seschrock     uint_t id, int atype)
544789Sahrens {
545789Sahrens 	nvlist_t **child;
546789Sahrens 	uint_t c, children;
5472082Seschrock 	int error;
5482082Seschrock 
5492082Seschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
5502082Seschrock 		return (error);
5512082Seschrock 
5522082Seschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
5532082Seschrock 		return (0);
554789Sahrens 
555789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
556789Sahrens 	    &child, &children) != 0) {
5572082Seschrock 		vdev_free(*vdp);
5582082Seschrock 		*vdp = NULL;
5592082Seschrock 		return (EINVAL);
560789Sahrens 	}
561789Sahrens 
562789Sahrens 	for (c = 0; c < children; c++) {
5632082Seschrock 		vdev_t *vd;
5642082Seschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
5652082Seschrock 		    atype)) != 0) {
5662082Seschrock 			vdev_free(*vdp);
5672082Seschrock 			*vdp = NULL;
5682082Seschrock 			return (error);
569789Sahrens 		}
570789Sahrens 	}
571789Sahrens 
5722082Seschrock 	ASSERT(*vdp != NULL);
5732082Seschrock 
5742082Seschrock 	return (0);
575789Sahrens }
576789Sahrens 
577789Sahrens /*
578789Sahrens  * Opposite of spa_load().
579789Sahrens  */
580789Sahrens static void
581789Sahrens spa_unload(spa_t *spa)
582789Sahrens {
5832082Seschrock 	int i;
5842082Seschrock 
585789Sahrens 	/*
5861544Seschrock 	 * Stop async tasks.
5871544Seschrock 	 */
5881544Seschrock 	spa_async_suspend(spa);
5891544Seschrock 
5901544Seschrock 	/*
591789Sahrens 	 * Stop syncing.
592789Sahrens 	 */
593789Sahrens 	if (spa->spa_sync_on) {
594789Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
595789Sahrens 		spa->spa_sync_on = B_FALSE;
596789Sahrens 	}
597789Sahrens 
598789Sahrens 	/*
599789Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
600789Sahrens 	 */
6011544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
6021544Seschrock 	spa_config_exit(spa, FTAG);
603789Sahrens 
604789Sahrens 	/*
605789Sahrens 	 * Close the dsl pool.
606789Sahrens 	 */
607789Sahrens 	if (spa->spa_dsl_pool) {
608789Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
609789Sahrens 		spa->spa_dsl_pool = NULL;
610789Sahrens 	}
611789Sahrens 
612789Sahrens 	/*
613789Sahrens 	 * Close all vdevs.
614789Sahrens 	 */
6151585Sbonwick 	if (spa->spa_root_vdev)
616789Sahrens 		vdev_free(spa->spa_root_vdev);
6171585Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
6181544Seschrock 
6192082Seschrock 	for (i = 0; i < spa->spa_nspares; i++)
6202082Seschrock 		vdev_free(spa->spa_spares[i]);
6212082Seschrock 	if (spa->spa_spares) {
6222082Seschrock 		kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *));
6232082Seschrock 		spa->spa_spares = NULL;
6242082Seschrock 	}
6252082Seschrock 	if (spa->spa_sparelist) {
6262082Seschrock 		nvlist_free(spa->spa_sparelist);
6272082Seschrock 		spa->spa_sparelist = NULL;
6282082Seschrock 	}
6292082Seschrock 
6301544Seschrock 	spa->spa_async_suspended = 0;
631789Sahrens }
632789Sahrens 
633789Sahrens /*
6342082Seschrock  * Load (or re-load) the current list of vdevs describing the active spares for
6352082Seschrock  * this pool.  When this is called, we have some form of basic information in
6362082Seschrock  * 'spa_sparelist'.  We parse this into vdevs, try to open them, and then
6372082Seschrock  * re-generate a more complete list including status information.
6382082Seschrock  */
6392082Seschrock static void
6402082Seschrock spa_load_spares(spa_t *spa)
6412082Seschrock {
6422082Seschrock 	nvlist_t **spares;
6432082Seschrock 	uint_t nspares;
6442082Seschrock 	int i;
6453377Seschrock 	vdev_t *vd, *tvd;
6462082Seschrock 
6472082Seschrock 	/*
6482082Seschrock 	 * First, close and free any existing spare vdevs.
6492082Seschrock 	 */
6502082Seschrock 	for (i = 0; i < spa->spa_nspares; i++) {
6513377Seschrock 		vd = spa->spa_spares[i];
6523377Seschrock 
6533377Seschrock 		/* Undo the call to spa_activate() below */
6543377Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL &&
6553377Seschrock 		    tvd->vdev_isspare)
6563377Seschrock 			spa_spare_remove(tvd);
6573377Seschrock 		vdev_close(vd);
6583377Seschrock 		vdev_free(vd);
6592082Seschrock 	}
6603377Seschrock 
6612082Seschrock 	if (spa->spa_spares)
6622082Seschrock 		kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *));
6632082Seschrock 
6642082Seschrock 	if (spa->spa_sparelist == NULL)
6652082Seschrock 		nspares = 0;
6662082Seschrock 	else
6672082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
6682082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
6692082Seschrock 
6702082Seschrock 	spa->spa_nspares = (int)nspares;
6712082Seschrock 	spa->spa_spares = NULL;
6722082Seschrock 
6732082Seschrock 	if (nspares == 0)
6742082Seschrock 		return;
6752082Seschrock 
6762082Seschrock 	/*
6772082Seschrock 	 * Construct the array of vdevs, opening them to get status in the
6783377Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
6793377Seschrock 	 * structures associated with it: one in the list of spares (used only
6803377Seschrock 	 * for basic validation purposes) and one in the active vdev
6813377Seschrock 	 * configuration (if it's spared in).  During this phase we open and
6823377Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
6833377Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
6842082Seschrock 	 */
6852082Seschrock 	spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP);
6862082Seschrock 	for (i = 0; i < spa->spa_nspares; i++) {
6872082Seschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
6882082Seschrock 		    VDEV_ALLOC_SPARE) == 0);
6892082Seschrock 		ASSERT(vd != NULL);
6902082Seschrock 
6912082Seschrock 		spa->spa_spares[i] = vd;
6922082Seschrock 
6933377Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) {
6943377Seschrock 			if (!tvd->vdev_isspare)
6953377Seschrock 				spa_spare_add(tvd);
6963377Seschrock 
6973377Seschrock 			/*
6983377Seschrock 			 * We only mark the spare active if we were successfully
6993377Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
7003377Seschrock 			 * with a bad active spare would result in strange
7013377Seschrock 			 * behavior, because multiple pool would think the spare
7023377Seschrock 			 * is actively in use.
7033377Seschrock 			 *
7043377Seschrock 			 * There is a vulnerability here to an equally bizarre
7053377Seschrock 			 * circumstance, where a dead active spare is later
7063377Seschrock 			 * brought back to life (onlined or otherwise).  Given
7073377Seschrock 			 * the rarity of this scenario, and the extra complexity
7083377Seschrock 			 * it adds, we ignore the possibility.
7093377Seschrock 			 */
7103377Seschrock 			if (!vdev_is_dead(tvd))
7113377Seschrock 				spa_spare_activate(tvd);
7123377Seschrock 		}
7133377Seschrock 
7142082Seschrock 		if (vdev_open(vd) != 0)
7152082Seschrock 			continue;
7162082Seschrock 
7172082Seschrock 		vd->vdev_top = vd;
7182082Seschrock 		(void) vdev_validate_spare(vd);
7192082Seschrock 	}
7202082Seschrock 
7212082Seschrock 	/*
7222082Seschrock 	 * Recompute the stashed list of spares, with status information
7232082Seschrock 	 * this time.
7242082Seschrock 	 */
7252082Seschrock 	VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
7262082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
7272082Seschrock 
7282082Seschrock 	spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP);
7292082Seschrock 	for (i = 0; i < spa->spa_nspares; i++)
7302082Seschrock 		spares[i] = vdev_config_generate(spa, spa->spa_spares[i],
7312082Seschrock 		    B_TRUE, B_TRUE);
7322082Seschrock 	VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
7332082Seschrock 	    spares, spa->spa_nspares) == 0);
7342082Seschrock 	for (i = 0; i < spa->spa_nspares; i++)
7352082Seschrock 		nvlist_free(spares[i]);
7362082Seschrock 	kmem_free(spares, spa->spa_nspares * sizeof (void *));
7372082Seschrock }
7382082Seschrock 
7392082Seschrock static int
7402082Seschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
7412082Seschrock {
7422082Seschrock 	dmu_buf_t *db;
7432082Seschrock 	char *packed = NULL;
7442082Seschrock 	size_t nvsize = 0;
7452082Seschrock 	int error;
7462082Seschrock 	*value = NULL;
7472082Seschrock 
7482082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
7492082Seschrock 	nvsize = *(uint64_t *)db->db_data;
7502082Seschrock 	dmu_buf_rele(db, FTAG);
7512082Seschrock 
7522082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
7532082Seschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
7542082Seschrock 	if (error == 0)
7552082Seschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
7562082Seschrock 	kmem_free(packed, nvsize);
7572082Seschrock 
7582082Seschrock 	return (error);
7592082Seschrock }
7602082Seschrock 
7612082Seschrock /*
7624451Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
7634451Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
7644451Seschrock  */
7654451Seschrock static void
7664451Seschrock spa_check_removed(vdev_t *vd)
7674451Seschrock {
7684451Seschrock 	int c;
7694451Seschrock 
7704451Seschrock 	for (c = 0; c < vd->vdev_children; c++)
7714451Seschrock 		spa_check_removed(vd->vdev_child[c]);
7724451Seschrock 
7734451Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
7744451Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
7754451Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
7764451Seschrock 	}
7774451Seschrock }
7784451Seschrock 
7794451Seschrock /*
780789Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
7811544Seschrock  * source of configuration information.
782789Sahrens  */
783789Sahrens static int
7841544Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
785789Sahrens {
786789Sahrens 	int error = 0;
787789Sahrens 	nvlist_t *nvroot = NULL;
788789Sahrens 	vdev_t *rvd;
789789Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
7901635Sbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
791789Sahrens 	uint64_t pool_guid;
7922082Seschrock 	uint64_t version;
793789Sahrens 	zio_t *zio;
7944451Seschrock 	uint64_t autoreplace = 0;
795789Sahrens 
7961544Seschrock 	spa->spa_load_state = state;
7971635Sbonwick 
798789Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
7991733Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
8001544Seschrock 		error = EINVAL;
8011544Seschrock 		goto out;
8021544Seschrock 	}
803789Sahrens 
8042082Seschrock 	/*
8052082Seschrock 	 * Versioning wasn't explicitly added to the label until later, so if
8062082Seschrock 	 * it's not present treat it as the initial version.
8072082Seschrock 	 */
8082082Seschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
8094577Sahrens 		version = SPA_VERSION_INITIAL;
8102082Seschrock 
8111733Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
8121733Sbonwick 	    &spa->spa_config_txg);
8131733Sbonwick 
8141635Sbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
8151544Seschrock 	    spa_guid_exists(pool_guid, 0)) {
8161544Seschrock 		error = EEXIST;
8171544Seschrock 		goto out;
8181544Seschrock 	}
819789Sahrens 
8202174Seschrock 	spa->spa_load_guid = pool_guid;
8212174Seschrock 
822789Sahrens 	/*
8232082Seschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
8242082Seschrock 	 * value that will be returned by spa_version() since parsing the
8252082Seschrock 	 * configuration requires knowing the version number.
826789Sahrens 	 */
8271544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
8282082Seschrock 	spa->spa_ubsync.ub_version = version;
8292082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
8301544Seschrock 	spa_config_exit(spa, FTAG);
831789Sahrens 
8322082Seschrock 	if (error != 0)
8331544Seschrock 		goto out;
834789Sahrens 
8351585Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
836789Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
837789Sahrens 
838789Sahrens 	/*
839789Sahrens 	 * Try to open all vdevs, loading each label in the process.
840789Sahrens 	 */
8414070Smc142369 	error = vdev_open(rvd);
8424070Smc142369 	if (error != 0)
8431544Seschrock 		goto out;
844789Sahrens 
845789Sahrens 	/*
8461986Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
8471986Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
8481986Seschrock 	 * flag.
8491986Seschrock 	 */
8501986Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
8511986Seschrock 	error = vdev_validate(rvd);
8521986Seschrock 	spa_config_exit(spa, FTAG);
8531986Seschrock 
8544070Smc142369 	if (error != 0)
8551986Seschrock 		goto out;
8561986Seschrock 
8571986Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
8581986Seschrock 		error = ENXIO;
8591986Seschrock 		goto out;
8601986Seschrock 	}
8611986Seschrock 
8621986Seschrock 	/*
863789Sahrens 	 * Find the best uberblock.
864789Sahrens 	 */
865789Sahrens 	bzero(ub, sizeof (uberblock_t));
866789Sahrens 
867789Sahrens 	zio = zio_root(spa, NULL, NULL,
868789Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
869789Sahrens 	vdev_uberblock_load(zio, rvd, ub);
870789Sahrens 	error = zio_wait(zio);
871789Sahrens 
872789Sahrens 	/*
873789Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
874789Sahrens 	 */
875789Sahrens 	if (ub->ub_txg == 0) {
8761760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
8771760Seschrock 		    VDEV_AUX_CORRUPT_DATA);
8781544Seschrock 		error = ENXIO;
8791544Seschrock 		goto out;
8801544Seschrock 	}
8811544Seschrock 
8821544Seschrock 	/*
8831544Seschrock 	 * If the pool is newer than the code, we can't open it.
8841544Seschrock 	 */
8854577Sahrens 	if (ub->ub_version > SPA_VERSION) {
8861760Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
8871760Seschrock 		    VDEV_AUX_VERSION_NEWER);
8881544Seschrock 		error = ENOTSUP;
8891544Seschrock 		goto out;
890789Sahrens 	}
891789Sahrens 
892789Sahrens 	/*
893789Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
894789Sahrens 	 * incomplete configuration.
895789Sahrens 	 */
8961732Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
8971544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
8981544Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
8991544Seschrock 		error = ENXIO;
9001544Seschrock 		goto out;
901789Sahrens 	}
902789Sahrens 
903789Sahrens 	/*
904789Sahrens 	 * Initialize internal SPA structures.
905789Sahrens 	 */
906789Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
907789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
908789Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
9091544Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
9101544Seschrock 	if (error) {
9111544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
9121544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
9131544Seschrock 		goto out;
9141544Seschrock 	}
915789Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
916789Sahrens 
9171544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
918789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
9191544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
9201544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
9211544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
9221544Seschrock 		error = EIO;
9231544Seschrock 		goto out;
9241544Seschrock 	}
925789Sahrens 
926789Sahrens 	if (!mosconfig) {
9272082Seschrock 		nvlist_t *newconfig;
9283975Sek110237 		uint64_t hostid;
9292082Seschrock 
9302082Seschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
9311544Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
9321544Seschrock 			    VDEV_AUX_CORRUPT_DATA);
9331544Seschrock 			error = EIO;
9341544Seschrock 			goto out;
9351544Seschrock 		}
936789Sahrens 
9373975Sek110237 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
9383975Sek110237 		    &hostid) == 0) {
9393975Sek110237 			char *hostname;
9403975Sek110237 			unsigned long myhostid = 0;
9413975Sek110237 
9423975Sek110237 			VERIFY(nvlist_lookup_string(newconfig,
9433975Sek110237 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
9443975Sek110237 
9453975Sek110237 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
9464178Slling 			if (hostid != 0 && myhostid != 0 &&
9474178Slling 			    (unsigned long)hostid != myhostid) {
9483975Sek110237 				cmn_err(CE_WARN, "pool '%s' could not be "
9493975Sek110237 				    "loaded as it was last accessed by "
9503975Sek110237 				    "another system (host: %s hostid: 0x%lx).  "
9513975Sek110237 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
9523975Sek110237 				    spa->spa_name, hostname,
9533975Sek110237 				    (unsigned long)hostid);
9543975Sek110237 				error = EBADF;
9553975Sek110237 				goto out;
9563975Sek110237 			}
9573975Sek110237 		}
9583975Sek110237 
959789Sahrens 		spa_config_set(spa, newconfig);
960789Sahrens 		spa_unload(spa);
961789Sahrens 		spa_deactivate(spa);
962789Sahrens 		spa_activate(spa);
963789Sahrens 
9641544Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
9651544Seschrock 	}
9661544Seschrock 
9671544Seschrock 	if (zap_lookup(spa->spa_meta_objset,
9681544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
9691544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
9701544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
9711544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
9721544Seschrock 		error = EIO;
9731544Seschrock 		goto out;
974789Sahrens 	}
975789Sahrens 
9761544Seschrock 	/*
9772082Seschrock 	 * Load the bit that tells us to use the new accounting function
9782082Seschrock 	 * (raid-z deflation).  If we have an older pool, this will not
9792082Seschrock 	 * be present.
9802082Seschrock 	 */
9812082Seschrock 	error = zap_lookup(spa->spa_meta_objset,
9822082Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
9832082Seschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
9842082Seschrock 	if (error != 0 && error != ENOENT) {
9852082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
9862082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
9872082Seschrock 		error = EIO;
9882082Seschrock 		goto out;
9892082Seschrock 	}
9902082Seschrock 
9912082Seschrock 	/*
9921544Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
9931544Seschrock 	 * not be present.
9941544Seschrock 	 */
9951544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
9961544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
9971544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
9981807Sbonwick 	if (error != 0 && error != ENOENT) {
9991544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10001544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10011544Seschrock 		error = EIO;
10021544Seschrock 		goto out;
10031544Seschrock 	}
10041544Seschrock 
10051544Seschrock 	error = zap_lookup(spa->spa_meta_objset,
10061544Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
10071544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
10081544Seschrock 	if (error != 0 && error != ENOENT) {
10091544Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10101544Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10111544Seschrock 		error = EIO;
10121544Seschrock 		goto out;
10131544Seschrock 	}
1014789Sahrens 
1015789Sahrens 	/*
10162926Sek110237 	 * Load the history object.  If we have an older pool, this
10172926Sek110237 	 * will not be present.
10182926Sek110237 	 */
10192926Sek110237 	error = zap_lookup(spa->spa_meta_objset,
10202926Sek110237 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
10212926Sek110237 	    sizeof (uint64_t), 1, &spa->spa_history);
10222926Sek110237 	if (error != 0 && error != ENOENT) {
10232926Sek110237 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10242926Sek110237 		    VDEV_AUX_CORRUPT_DATA);
10252926Sek110237 		error = EIO;
10262926Sek110237 		goto out;
10272926Sek110237 	}
10282926Sek110237 
10292926Sek110237 	/*
10302082Seschrock 	 * Load any hot spares for this pool.
10312082Seschrock 	 */
10322082Seschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
10332082Seschrock 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object);
10342082Seschrock 	if (error != 0 && error != ENOENT) {
10352082Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10362082Seschrock 		    VDEV_AUX_CORRUPT_DATA);
10372082Seschrock 		error = EIO;
10382082Seschrock 		goto out;
10392082Seschrock 	}
10402082Seschrock 	if (error == 0) {
10414577Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
10422082Seschrock 		if (load_nvlist(spa, spa->spa_spares_object,
10432082Seschrock 		    &spa->spa_sparelist) != 0) {
10442082Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10452082Seschrock 			    VDEV_AUX_CORRUPT_DATA);
10462082Seschrock 			error = EIO;
10472082Seschrock 			goto out;
10482082Seschrock 		}
10492082Seschrock 
10502082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
10512082Seschrock 		spa_load_spares(spa);
10522082Seschrock 		spa_config_exit(spa, FTAG);
10532082Seschrock 	}
10542082Seschrock 
1055*5094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
10564543Smarks 
10573912Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
10583912Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
10593912Slling 
10603912Slling 	if (error && error != ENOENT) {
10613912Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
10623912Slling 		    VDEV_AUX_CORRUPT_DATA);
10633912Slling 		error = EIO;
10643912Slling 		goto out;
10653912Slling 	}
10663912Slling 
10673912Slling 	if (error == 0) {
10683912Slling 		(void) zap_lookup(spa->spa_meta_objset,
10693912Slling 		    spa->spa_pool_props_object,
10704451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
10713912Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
10724451Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
10734451Seschrock 		    spa->spa_pool_props_object,
10744451Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
10754451Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
10764543Smarks 		(void) zap_lookup(spa->spa_meta_objset,
10774543Smarks 		    spa->spa_pool_props_object,
10784543Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
10794543Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
10803912Slling 	}
10813912Slling 
10822082Seschrock 	/*
10834451Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
10844451Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
10854451Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
10864451Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
10874451Seschrock 	 * over.
10884451Seschrock 	 */
10894451Seschrock 	if (autoreplace)
10904451Seschrock 		spa_check_removed(spa->spa_root_vdev);
10914451Seschrock 
10924451Seschrock 	/*
10931986Seschrock 	 * Load the vdev state for all toplevel vdevs.
1094789Sahrens 	 */
10951986Seschrock 	vdev_load(rvd);
1096789Sahrens 
1097789Sahrens 	/*
1098789Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1099789Sahrens 	 */
11001544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1101789Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
11021544Seschrock 	spa_config_exit(spa, FTAG);
1103789Sahrens 
1104789Sahrens 	/*
1105789Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1106789Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1107789Sahrens 	 */
11081544Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
11091544Seschrock 		error = ENXIO;
11101544Seschrock 		goto out;
11111544Seschrock 	}
1112789Sahrens 
11131544Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
11141635Sbonwick 		dmu_tx_t *tx;
11151635Sbonwick 		int need_update = B_FALSE;
11161585Sbonwick 		int c;
11171601Sbonwick 
11181635Sbonwick 		/*
11191635Sbonwick 		 * Claim log blocks that haven't been committed yet.
11201635Sbonwick 		 * This must all happen in a single txg.
11211635Sbonwick 		 */
11221601Sbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1123789Sahrens 		    spa_first_txg(spa));
11242417Sahrens 		(void) dmu_objset_find(spa->spa_name,
11252417Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1126789Sahrens 		dmu_tx_commit(tx);
1127789Sahrens 
1128789Sahrens 		spa->spa_sync_on = B_TRUE;
1129789Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1130789Sahrens 
1131789Sahrens 		/*
1132789Sahrens 		 * Wait for all claims to sync.
1133789Sahrens 		 */
1134789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
11351585Sbonwick 
11361585Sbonwick 		/*
11371635Sbonwick 		 * If the config cache is stale, or we have uninitialized
11381635Sbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
11391585Sbonwick 		 */
11401635Sbonwick 		if (config_cache_txg != spa->spa_config_txg ||
11411635Sbonwick 		    state == SPA_LOAD_IMPORT)
11421635Sbonwick 			need_update = B_TRUE;
11431635Sbonwick 
11441635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++)
11451635Sbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
11461635Sbonwick 				need_update = B_TRUE;
11471585Sbonwick 
11481585Sbonwick 		/*
11491635Sbonwick 		 * Update the config cache asychronously in case we're the
11501635Sbonwick 		 * root pool, in which case the config cache isn't writable yet.
11511585Sbonwick 		 */
11521635Sbonwick 		if (need_update)
11531635Sbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1154789Sahrens 	}
1155789Sahrens 
11561544Seschrock 	error = 0;
11571544Seschrock out:
11582082Seschrock 	if (error && error != EBADF)
11591544Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
11601544Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
11611544Seschrock 	spa->spa_ena = 0;
11621544Seschrock 
11631544Seschrock 	return (error);
1164789Sahrens }
1165789Sahrens 
1166789Sahrens /*
1167789Sahrens  * Pool Open/Import
1168789Sahrens  *
1169789Sahrens  * The import case is identical to an open except that the configuration is sent
1170789Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1171789Sahrens  * case of an open, the pool configuration will exist in the
11724451Seschrock  * POOL_STATE_UNINITIALIZED state.
1173789Sahrens  *
1174789Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1175789Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1176789Sahrens  * ambiguous state.
1177789Sahrens  */
1178789Sahrens static int
1179789Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1180789Sahrens {
1181789Sahrens 	spa_t *spa;
1182789Sahrens 	int error;
1183789Sahrens 	int loaded = B_FALSE;
1184789Sahrens 	int locked = B_FALSE;
1185789Sahrens 
1186789Sahrens 	*spapp = NULL;
1187789Sahrens 
1188789Sahrens 	/*
1189789Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1190789Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1191789Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1192789Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1193789Sahrens 	 */
1194789Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1195789Sahrens 		mutex_enter(&spa_namespace_lock);
1196789Sahrens 		locked = B_TRUE;
1197789Sahrens 	}
1198789Sahrens 
1199789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1200789Sahrens 		if (locked)
1201789Sahrens 			mutex_exit(&spa_namespace_lock);
1202789Sahrens 		return (ENOENT);
1203789Sahrens 	}
1204789Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1205789Sahrens 
1206789Sahrens 		spa_activate(spa);
1207789Sahrens 
12081635Sbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1209789Sahrens 
1210789Sahrens 		if (error == EBADF) {
1211789Sahrens 			/*
12121986Seschrock 			 * If vdev_validate() returns failure (indicated by
12131986Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
12141986Seschrock 			 * that the pool has been exported or destroyed.  If
12151986Seschrock 			 * this is the case, the config cache is out of sync and
12161986Seschrock 			 * we should remove the pool from the namespace.
1217789Sahrens 			 */
12182082Seschrock 			zfs_post_ok(spa, NULL);
1219789Sahrens 			spa_unload(spa);
1220789Sahrens 			spa_deactivate(spa);
1221789Sahrens 			spa_remove(spa);
1222789Sahrens 			spa_config_sync();
1223789Sahrens 			if (locked)
1224789Sahrens 				mutex_exit(&spa_namespace_lock);
1225789Sahrens 			return (ENOENT);
12261544Seschrock 		}
12271544Seschrock 
12281544Seschrock 		if (error) {
1229789Sahrens 			/*
1230789Sahrens 			 * We can't open the pool, but we still have useful
1231789Sahrens 			 * information: the state of each vdev after the
1232789Sahrens 			 * attempted vdev_open().  Return this to the user.
1233789Sahrens 			 */
12341635Sbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
12351635Sbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1236789Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1237789Sahrens 				    B_TRUE);
12381635Sbonwick 				spa_config_exit(spa, FTAG);
12391635Sbonwick 			}
1240789Sahrens 			spa_unload(spa);
1241789Sahrens 			spa_deactivate(spa);
12421544Seschrock 			spa->spa_last_open_failed = B_TRUE;
1243789Sahrens 			if (locked)
1244789Sahrens 				mutex_exit(&spa_namespace_lock);
1245789Sahrens 			*spapp = NULL;
1246789Sahrens 			return (error);
12471544Seschrock 		} else {
12481544Seschrock 			zfs_post_ok(spa, NULL);
12491544Seschrock 			spa->spa_last_open_failed = B_FALSE;
1250789Sahrens 		}
1251789Sahrens 
1252789Sahrens 		loaded = B_TRUE;
1253789Sahrens 	}
1254789Sahrens 
1255789Sahrens 	spa_open_ref(spa, tag);
12564451Seschrock 
12574451Seschrock 	/*
12584451Seschrock 	 * If we just loaded the pool, resilver anything that's out of date.
12594451Seschrock 	 */
12604451Seschrock 	if (loaded && (spa_mode & FWRITE))
12614451Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
12624451Seschrock 
1263789Sahrens 	if (locked)
1264789Sahrens 		mutex_exit(&spa_namespace_lock);
1265789Sahrens 
1266789Sahrens 	*spapp = spa;
1267789Sahrens 
1268789Sahrens 	if (config != NULL) {
12691544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1270789Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
12711544Seschrock 		spa_config_exit(spa, FTAG);
1272789Sahrens 	}
1273789Sahrens 
1274789Sahrens 	return (0);
1275789Sahrens }
1276789Sahrens 
1277789Sahrens int
1278789Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1279789Sahrens {
1280789Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1281789Sahrens }
1282789Sahrens 
12831544Seschrock /*
12841544Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
12851544Seschrock  * preventing it from being exported or destroyed.
12861544Seschrock  */
12871544Seschrock spa_t *
12881544Seschrock spa_inject_addref(char *name)
12891544Seschrock {
12901544Seschrock 	spa_t *spa;
12911544Seschrock 
12921544Seschrock 	mutex_enter(&spa_namespace_lock);
12931544Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
12941544Seschrock 		mutex_exit(&spa_namespace_lock);
12951544Seschrock 		return (NULL);
12961544Seschrock 	}
12971544Seschrock 	spa->spa_inject_ref++;
12981544Seschrock 	mutex_exit(&spa_namespace_lock);
12991544Seschrock 
13001544Seschrock 	return (spa);
13011544Seschrock }
13021544Seschrock 
13031544Seschrock void
13041544Seschrock spa_inject_delref(spa_t *spa)
13051544Seschrock {
13061544Seschrock 	mutex_enter(&spa_namespace_lock);
13071544Seschrock 	spa->spa_inject_ref--;
13081544Seschrock 	mutex_exit(&spa_namespace_lock);
13091544Seschrock }
13101544Seschrock 
13112082Seschrock static void
13122082Seschrock spa_add_spares(spa_t *spa, nvlist_t *config)
13132082Seschrock {
13142082Seschrock 	nvlist_t **spares;
13152082Seschrock 	uint_t i, nspares;
13162082Seschrock 	nvlist_t *nvroot;
13172082Seschrock 	uint64_t guid;
13182082Seschrock 	vdev_stat_t *vs;
13192082Seschrock 	uint_t vsc;
13203377Seschrock 	uint64_t pool;
13212082Seschrock 
13222082Seschrock 	if (spa->spa_nspares == 0)
13232082Seschrock 		return;
13242082Seschrock 
13252082Seschrock 	VERIFY(nvlist_lookup_nvlist(config,
13262082Seschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
13272082Seschrock 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
13282082Seschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
13292082Seschrock 	if (nspares != 0) {
13302082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
13312082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
13322082Seschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
13332082Seschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
13342082Seschrock 
13352082Seschrock 		/*
13362082Seschrock 		 * Go through and find any spares which have since been
13372082Seschrock 		 * repurposed as an active spare.  If this is the case, update
13382082Seschrock 		 * their status appropriately.
13392082Seschrock 		 */
13402082Seschrock 		for (i = 0; i < nspares; i++) {
13412082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
13422082Seschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
13433377Seschrock 			if (spa_spare_exists(guid, &pool) && pool != 0ULL) {
13442082Seschrock 				VERIFY(nvlist_lookup_uint64_array(
13452082Seschrock 				    spares[i], ZPOOL_CONFIG_STATS,
13462082Seschrock 				    (uint64_t **)&vs, &vsc) == 0);
13472082Seschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
13482082Seschrock 				vs->vs_aux = VDEV_AUX_SPARED;
13492082Seschrock 			}
13502082Seschrock 		}
13512082Seschrock 	}
13522082Seschrock }
13532082Seschrock 
1354789Sahrens int
13551544Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1356789Sahrens {
1357789Sahrens 	int error;
1358789Sahrens 	spa_t *spa;
1359789Sahrens 
1360789Sahrens 	*config = NULL;
1361789Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1362789Sahrens 
13632082Seschrock 	if (spa && *config != NULL) {
13641544Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
13651544Seschrock 		    spa_get_errlog_size(spa)) == 0);
13661544Seschrock 
13672082Seschrock 		spa_add_spares(spa, *config);
13682082Seschrock 	}
13692082Seschrock 
13701544Seschrock 	/*
13711544Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
13721544Seschrock 	 * and call spa_lookup() directly.
13731544Seschrock 	 */
13741544Seschrock 	if (altroot) {
13751544Seschrock 		if (spa == NULL) {
13761544Seschrock 			mutex_enter(&spa_namespace_lock);
13771544Seschrock 			spa = spa_lookup(name);
13781544Seschrock 			if (spa)
13791544Seschrock 				spa_altroot(spa, altroot, buflen);
13801544Seschrock 			else
13811544Seschrock 				altroot[0] = '\0';
13821544Seschrock 			spa = NULL;
13831544Seschrock 			mutex_exit(&spa_namespace_lock);
13841544Seschrock 		} else {
13851544Seschrock 			spa_altroot(spa, altroot, buflen);
13861544Seschrock 		}
13871544Seschrock 	}
13881544Seschrock 
1389789Sahrens 	if (spa != NULL)
1390789Sahrens 		spa_close(spa, FTAG);
1391789Sahrens 
1392789Sahrens 	return (error);
1393789Sahrens }
1394789Sahrens 
1395789Sahrens /*
13962082Seschrock  * Validate that the 'spares' array is well formed.  We must have an array of
13973377Seschrock  * nvlists, each which describes a valid leaf vdev.  If this is an import (mode
13983377Seschrock  * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long
13993377Seschrock  * as they are well-formed.
14002082Seschrock  */
14012082Seschrock static int
14022082Seschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
14032082Seschrock {
14042082Seschrock 	nvlist_t **spares;
14052082Seschrock 	uint_t i, nspares;
14062082Seschrock 	vdev_t *vd;
14072082Seschrock 	int error;
14082082Seschrock 
14092082Seschrock 	/*
14102082Seschrock 	 * It's acceptable to have no spares specified.
14112082Seschrock 	 */
14122082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
14132082Seschrock 	    &spares, &nspares) != 0)
14142082Seschrock 		return (0);
14152082Seschrock 
14162082Seschrock 	if (nspares == 0)
14172082Seschrock 		return (EINVAL);
14182082Seschrock 
14192082Seschrock 	/*
14202082Seschrock 	 * Make sure the pool is formatted with a version that supports hot
14212082Seschrock 	 * spares.
14222082Seschrock 	 */
14234577Sahrens 	if (spa_version(spa) < SPA_VERSION_SPARES)
14242082Seschrock 		return (ENOTSUP);
14252082Seschrock 
14263377Seschrock 	/*
14273377Seschrock 	 * Set the pending spare list so we correctly handle device in-use
14283377Seschrock 	 * checking.
14293377Seschrock 	 */
14303377Seschrock 	spa->spa_pending_spares = spares;
14313377Seschrock 	spa->spa_pending_nspares = nspares;
14323377Seschrock 
14332082Seschrock 	for (i = 0; i < nspares; i++) {
14342082Seschrock 		if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0,
14352082Seschrock 		    mode)) != 0)
14363377Seschrock 			goto out;
14372082Seschrock 
14382082Seschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
14392082Seschrock 			vdev_free(vd);
14403377Seschrock 			error = EINVAL;
14413377Seschrock 			goto out;
14422082Seschrock 		}
14432082Seschrock 
14442082Seschrock 		vd->vdev_top = vd;
14453377Seschrock 
14463377Seschrock 		if ((error = vdev_open(vd)) == 0 &&
14473377Seschrock 		    (error = vdev_label_init(vd, crtxg,
14483377Seschrock 		    VDEV_LABEL_SPARE)) == 0) {
14493377Seschrock 			VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID,
14503377Seschrock 			    vd->vdev_guid) == 0);
14512082Seschrock 		}
14522082Seschrock 
14532082Seschrock 		vdev_free(vd);
14543377Seschrock 
14553377Seschrock 		if (error && mode != VDEV_ALLOC_SPARE)
14563377Seschrock 			goto out;
14573377Seschrock 		else
14583377Seschrock 			error = 0;
14592082Seschrock 	}
14602082Seschrock 
14613377Seschrock out:
14623377Seschrock 	spa->spa_pending_spares = NULL;
14633377Seschrock 	spa->spa_pending_nspares = 0;
14643377Seschrock 	return (error);
14652082Seschrock }
14662082Seschrock 
14672082Seschrock /*
1468789Sahrens  * Pool Creation
1469789Sahrens  */
1470789Sahrens int
1471*5094Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
14724715Sek110237     const char *history_str)
1473789Sahrens {
1474789Sahrens 	spa_t *spa;
1475*5094Slling 	char *altroot = NULL;
14761635Sbonwick 	vdev_t *rvd;
1477789Sahrens 	dsl_pool_t *dp;
1478789Sahrens 	dmu_tx_t *tx;
14792082Seschrock 	int c, error = 0;
1480789Sahrens 	uint64_t txg = TXG_INITIAL;
14812082Seschrock 	nvlist_t **spares;
14822082Seschrock 	uint_t nspares;
1483*5094Slling 	uint64_t version;
1484789Sahrens 
1485789Sahrens 	/*
1486789Sahrens 	 * If this pool already exists, return failure.
1487789Sahrens 	 */
1488789Sahrens 	mutex_enter(&spa_namespace_lock);
1489789Sahrens 	if (spa_lookup(pool) != NULL) {
1490789Sahrens 		mutex_exit(&spa_namespace_lock);
1491789Sahrens 		return (EEXIST);
1492789Sahrens 	}
1493789Sahrens 
1494789Sahrens 	/*
1495789Sahrens 	 * Allocate a new spa_t structure.
1496789Sahrens 	 */
1497*5094Slling 	(void) nvlist_lookup_string(props,
1498*5094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
14991635Sbonwick 	spa = spa_add(pool, altroot);
1500789Sahrens 	spa_activate(spa);
1501789Sahrens 
1502789Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
1503*5094Slling 
1504*5094Slling 	if (props && (error = spa_prop_validate(spa, props))) {
1505*5094Slling 		spa_unload(spa);
1506*5094Slling 		spa_deactivate(spa);
1507*5094Slling 		spa_remove(spa);
1508*5094Slling 		return (error);
1509*5094Slling 	}
1510*5094Slling 
1511*5094Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
1512*5094Slling 	    &version) != 0)
1513*5094Slling 		version = SPA_VERSION;
1514*5094Slling 	ASSERT(version <= SPA_VERSION);
1515*5094Slling 	spa->spa_uberblock.ub_version = version;
1516789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1517789Sahrens 
15181635Sbonwick 	/*
15191635Sbonwick 	 * Create the root vdev.
15201635Sbonwick 	 */
15211635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
15221635Sbonwick 
15232082Seschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
15242082Seschrock 
15252082Seschrock 	ASSERT(error != 0 || rvd != NULL);
15262082Seschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
15272082Seschrock 
15282082Seschrock 	if (error == 0 && rvd->vdev_children == 0)
15291635Sbonwick 		error = EINVAL;
15302082Seschrock 
15312082Seschrock 	if (error == 0 &&
15322082Seschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
15332082Seschrock 	    (error = spa_validate_spares(spa, nvroot, txg,
15342082Seschrock 	    VDEV_ALLOC_ADD)) == 0) {
15352082Seschrock 		for (c = 0; c < rvd->vdev_children; c++)
15362082Seschrock 			vdev_init(rvd->vdev_child[c], txg);
15372082Seschrock 		vdev_config_dirty(rvd);
15381635Sbonwick 	}
15391635Sbonwick 
15401635Sbonwick 	spa_config_exit(spa, FTAG);
1541789Sahrens 
15422082Seschrock 	if (error != 0) {
1543789Sahrens 		spa_unload(spa);
1544789Sahrens 		spa_deactivate(spa);
1545789Sahrens 		spa_remove(spa);
1546789Sahrens 		mutex_exit(&spa_namespace_lock);
1547789Sahrens 		return (error);
1548789Sahrens 	}
1549789Sahrens 
15502082Seschrock 	/*
15512082Seschrock 	 * Get the list of spares, if specified.
15522082Seschrock 	 */
15532082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
15542082Seschrock 	    &spares, &nspares) == 0) {
15552082Seschrock 		VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME,
15562082Seschrock 		    KM_SLEEP) == 0);
15572082Seschrock 		VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
15582082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
15592082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
15602082Seschrock 		spa_load_spares(spa);
15612082Seschrock 		spa_config_exit(spa, FTAG);
15622082Seschrock 		spa->spa_sync_spares = B_TRUE;
15632082Seschrock 	}
15642082Seschrock 
1565789Sahrens 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg);
1566789Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1567789Sahrens 
1568789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1569789Sahrens 
1570789Sahrens 	/*
1571789Sahrens 	 * Create the pool config object.
1572789Sahrens 	 */
1573789Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1574789Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1575789Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1576789Sahrens 
15771544Seschrock 	if (zap_add(spa->spa_meta_objset,
1578789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
15791544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
15801544Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
15811544Seschrock 	}
1582789Sahrens 
1583*5094Slling 	/* Newly created pools with the right version are always deflated. */
1584*5094Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
1585*5094Slling 		spa->spa_deflate = TRUE;
1586*5094Slling 		if (zap_add(spa->spa_meta_objset,
1587*5094Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
1588*5094Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
1589*5094Slling 			cmn_err(CE_PANIC, "failed to add deflate");
1590*5094Slling 		}
15912082Seschrock 	}
15922082Seschrock 
1593789Sahrens 	/*
1594789Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
1595789Sahrens 	 * because sync-to-convergence takes longer if the blocksize
1596789Sahrens 	 * keeps changing.
1597789Sahrens 	 */
1598789Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
1599789Sahrens 	    1 << 14, tx);
1600789Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
1601789Sahrens 	    ZIO_COMPRESS_OFF, tx);
1602789Sahrens 
16031544Seschrock 	if (zap_add(spa->spa_meta_objset,
1604789Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
16051544Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
16061544Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
16071544Seschrock 	}
1608789Sahrens 
16092926Sek110237 	/*
16102926Sek110237 	 * Create the pool's history object.
16112926Sek110237 	 */
1612*5094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
1613*5094Slling 		spa_history_create_obj(spa, tx);
1614*5094Slling 
1615*5094Slling 	/*
1616*5094Slling 	 * Set pool properties.
1617*5094Slling 	 */
1618*5094Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
1619*5094Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1620*5094Slling 	spa->spa_temporary = zpool_prop_default_numeric(ZPOOL_PROP_TEMPORARY);
1621*5094Slling 	if (props)
1622*5094Slling 		spa_sync_props(spa, props, CRED(), tx);
16232926Sek110237 
1624789Sahrens 	dmu_tx_commit(tx);
1625789Sahrens 
1626789Sahrens 	spa->spa_sync_on = B_TRUE;
1627789Sahrens 	txg_sync_start(spa->spa_dsl_pool);
1628789Sahrens 
1629789Sahrens 	/*
1630789Sahrens 	 * We explicitly wait for the first transaction to complete so that our
1631789Sahrens 	 * bean counters are appropriately updated.
1632789Sahrens 	 */
1633789Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
1634789Sahrens 
1635789Sahrens 	spa_config_sync();
1636789Sahrens 
1637*5094Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
16384715Sek110237 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
16394715Sek110237 
1640789Sahrens 	mutex_exit(&spa_namespace_lock);
1641789Sahrens 
1642789Sahrens 	return (0);
1643789Sahrens }
1644789Sahrens 
1645789Sahrens /*
1646789Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
1647789Sahrens  * then call spa_load() to do the dirty work.
1648789Sahrens  */
1649789Sahrens int
1650*5094Slling spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
1651789Sahrens {
1652789Sahrens 	spa_t *spa;
1653*5094Slling 	char *altroot = NULL;
1654789Sahrens 	int error;
16552082Seschrock 	nvlist_t *nvroot;
16562082Seschrock 	nvlist_t **spares;
16572082Seschrock 	uint_t nspares;
1658789Sahrens 
1659789Sahrens 	/*
1660789Sahrens 	 * If a pool with this name exists, return failure.
1661789Sahrens 	 */
1662789Sahrens 	mutex_enter(&spa_namespace_lock);
1663789Sahrens 	if (spa_lookup(pool) != NULL) {
1664789Sahrens 		mutex_exit(&spa_namespace_lock);
1665789Sahrens 		return (EEXIST);
1666789Sahrens 	}
1667789Sahrens 
1668789Sahrens 	/*
16691635Sbonwick 	 * Create and initialize the spa structure.
1670789Sahrens 	 */
1671*5094Slling 	(void) nvlist_lookup_string(props,
1672*5094Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
16731635Sbonwick 	spa = spa_add(pool, altroot);
1674789Sahrens 	spa_activate(spa);
1675789Sahrens 
1676789Sahrens 	/*
16771635Sbonwick 	 * Pass off the heavy lifting to spa_load().
16781732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
16791732Sbonwick 	 * is actually the one to trust when doing an import.
16801601Sbonwick 	 */
16811732Sbonwick 	error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE);
1682789Sahrens 
16832082Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
16842082Seschrock 	/*
16852082Seschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
16862082Seschrock 	 * and conflicts with spa_has_spare().
16872082Seschrock 	 */
16882082Seschrock 	if (spa->spa_sparelist) {
16892082Seschrock 		nvlist_free(spa->spa_sparelist);
16902082Seschrock 		spa->spa_sparelist = NULL;
16912082Seschrock 		spa_load_spares(spa);
16922082Seschrock 	}
16932082Seschrock 
16942082Seschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
16952082Seschrock 	    &nvroot) == 0);
1696*5094Slling 	if (error == 0) {
16972082Seschrock 		error = spa_validate_spares(spa, nvroot, -1ULL,
16982082Seschrock 		    VDEV_ALLOC_SPARE);
1699*5094Slling 	}
17002082Seschrock 	spa_config_exit(spa, FTAG);
17012082Seschrock 
1702*5094Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
1703789Sahrens 		spa_unload(spa);
1704789Sahrens 		spa_deactivate(spa);
1705789Sahrens 		spa_remove(spa);
1706789Sahrens 		mutex_exit(&spa_namespace_lock);
1707789Sahrens 		return (error);
1708789Sahrens 	}
1709789Sahrens 
17101635Sbonwick 	/*
17112082Seschrock 	 * Override any spares as specified by the user, as these may have
17122082Seschrock 	 * correct device names/devids, etc.
17132082Seschrock 	 */
17142082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
17152082Seschrock 	    &spares, &nspares) == 0) {
17162082Seschrock 		if (spa->spa_sparelist)
17172082Seschrock 			VERIFY(nvlist_remove(spa->spa_sparelist,
17182082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
17192082Seschrock 		else
17202082Seschrock 			VERIFY(nvlist_alloc(&spa->spa_sparelist,
17212082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
17222082Seschrock 		VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
17232082Seschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
17242082Seschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
17252082Seschrock 		spa_load_spares(spa);
17262082Seschrock 		spa_config_exit(spa, FTAG);
17272082Seschrock 		spa->spa_sync_spares = B_TRUE;
17282082Seschrock 	}
17292082Seschrock 
17302082Seschrock 	/*
17311635Sbonwick 	 * Update the config cache to include the newly-imported pool.
17321635Sbonwick 	 */
17334627Sck153898 	if (spa_mode & FWRITE)
17344627Sck153898 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
17351635Sbonwick 
1736789Sahrens 	/*
1737789Sahrens 	 * Resilver anything that's out of date.
1738789Sahrens 	 */
1739789Sahrens 	if (spa_mode & FWRITE)
1740789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1741789Sahrens 
17424451Seschrock 	mutex_exit(&spa_namespace_lock);
17434451Seschrock 
1744789Sahrens 	return (0);
1745789Sahrens }
1746789Sahrens 
1747789Sahrens /*
1748789Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
1749789Sahrens  * to get the vdev stats associated with the imported devices.
1750789Sahrens  */
1751789Sahrens #define	TRYIMPORT_NAME	"$import"
1752789Sahrens 
1753789Sahrens nvlist_t *
1754789Sahrens spa_tryimport(nvlist_t *tryconfig)
1755789Sahrens {
1756789Sahrens 	nvlist_t *config = NULL;
1757789Sahrens 	char *poolname;
1758789Sahrens 	spa_t *spa;
1759789Sahrens 	uint64_t state;
1760789Sahrens 
1761789Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
1762789Sahrens 		return (NULL);
1763789Sahrens 
1764789Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
1765789Sahrens 		return (NULL);
1766789Sahrens 
17671635Sbonwick 	/*
17681635Sbonwick 	 * Create and initialize the spa structure.
17691635Sbonwick 	 */
1770789Sahrens 	mutex_enter(&spa_namespace_lock);
17711635Sbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
1772789Sahrens 	spa_activate(spa);
1773789Sahrens 
1774789Sahrens 	/*
17751635Sbonwick 	 * Pass off the heavy lifting to spa_load().
17761732Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
17771732Sbonwick 	 * is actually the one to trust when doing an import.
1778789Sahrens 	 */
17791732Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
1780789Sahrens 
1781789Sahrens 	/*
1782789Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
1783789Sahrens 	 */
1784789Sahrens 	if (spa->spa_root_vdev != NULL) {
17851635Sbonwick 		spa_config_enter(spa, RW_READER, FTAG);
1786789Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
17871635Sbonwick 		spa_config_exit(spa, FTAG);
1788789Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
1789789Sahrens 		    poolname) == 0);
1790789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1791789Sahrens 		    state) == 0);
17923975Sek110237 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
17933975Sek110237 		    spa->spa_uberblock.ub_timestamp) == 0);
17942082Seschrock 
17952082Seschrock 		/*
17962082Seschrock 		 * Add the list of hot spares.
17972082Seschrock 		 */
17982082Seschrock 		spa_add_spares(spa, config);
1799789Sahrens 	}
1800789Sahrens 
1801789Sahrens 	spa_unload(spa);
1802789Sahrens 	spa_deactivate(spa);
1803789Sahrens 	spa_remove(spa);
1804789Sahrens 	mutex_exit(&spa_namespace_lock);
1805789Sahrens 
1806789Sahrens 	return (config);
1807789Sahrens }
1808789Sahrens 
1809789Sahrens /*
1810789Sahrens  * Pool export/destroy
1811789Sahrens  *
1812789Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
1813789Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
1814789Sahrens  * update the pool state and sync all the labels to disk, removing the
1815789Sahrens  * configuration from the cache afterwards.
1816789Sahrens  */
1817789Sahrens static int
18181775Sbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig)
1819789Sahrens {
1820789Sahrens 	spa_t *spa;
1821789Sahrens 
18221775Sbillm 	if (oldconfig)
18231775Sbillm 		*oldconfig = NULL;
18241775Sbillm 
1825789Sahrens 	if (!(spa_mode & FWRITE))
1826789Sahrens 		return (EROFS);
1827789Sahrens 
1828789Sahrens 	mutex_enter(&spa_namespace_lock);
1829789Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1830789Sahrens 		mutex_exit(&spa_namespace_lock);
1831789Sahrens 		return (ENOENT);
1832789Sahrens 	}
1833789Sahrens 
1834789Sahrens 	/*
18351544Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
18361544Seschrock 	 * reacquire the namespace lock, and see if we can export.
18371544Seschrock 	 */
18381544Seschrock 	spa_open_ref(spa, FTAG);
18391544Seschrock 	mutex_exit(&spa_namespace_lock);
18401544Seschrock 	spa_async_suspend(spa);
18411544Seschrock 	mutex_enter(&spa_namespace_lock);
18421544Seschrock 	spa_close(spa, FTAG);
18431544Seschrock 
18441544Seschrock 	/*
1845789Sahrens 	 * The pool will be in core if it's openable,
1846789Sahrens 	 * in which case we can modify its state.
1847789Sahrens 	 */
1848789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
1849789Sahrens 		/*
1850789Sahrens 		 * Objsets may be open only because they're dirty, so we
1851789Sahrens 		 * have to force it to sync before checking spa_refcnt.
1852789Sahrens 		 */
1853789Sahrens 		spa_scrub_suspend(spa);
1854789Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
1855789Sahrens 
18561544Seschrock 		/*
18571544Seschrock 		 * A pool cannot be exported or destroyed if there are active
18581544Seschrock 		 * references.  If we are resetting a pool, allow references by
18591544Seschrock 		 * fault injection handlers.
18601544Seschrock 		 */
18611544Seschrock 		if (!spa_refcount_zero(spa) ||
18621544Seschrock 		    (spa->spa_inject_ref != 0 &&
18631544Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
1864789Sahrens 			spa_scrub_resume(spa);
18651544Seschrock 			spa_async_resume(spa);
1866789Sahrens 			mutex_exit(&spa_namespace_lock);
1867789Sahrens 			return (EBUSY);
1868789Sahrens 		}
1869789Sahrens 
1870789Sahrens 		spa_scrub_resume(spa);
1871789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
1872789Sahrens 
1873789Sahrens 		/*
1874789Sahrens 		 * We want this to be reflected on every label,
1875789Sahrens 		 * so mark them all dirty.  spa_unload() will do the
1876789Sahrens 		 * final sync that pushes these changes out.
1877789Sahrens 		 */
18781544Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
18791601Sbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
18801544Seschrock 			spa->spa_state = new_state;
18811635Sbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
18821544Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
18831601Sbonwick 			spa_config_exit(spa, FTAG);
18841544Seschrock 		}
1885789Sahrens 	}
1886789Sahrens 
18874451Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
18884451Seschrock 
1889789Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
1890789Sahrens 		spa_unload(spa);
1891789Sahrens 		spa_deactivate(spa);
1892789Sahrens 	}
1893789Sahrens 
18941775Sbillm 	if (oldconfig && spa->spa_config)
18951775Sbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
18961775Sbillm 
18971544Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
18981544Seschrock 		spa_remove(spa);
18991544Seschrock 		spa_config_sync();
19001544Seschrock 	}
1901789Sahrens 	mutex_exit(&spa_namespace_lock);
1902789Sahrens 
1903789Sahrens 	return (0);
1904789Sahrens }
1905789Sahrens 
1906789Sahrens /*
1907789Sahrens  * Destroy a storage pool.
1908789Sahrens  */
1909789Sahrens int
1910789Sahrens spa_destroy(char *pool)
1911789Sahrens {
19121775Sbillm 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL));
1913789Sahrens }
1914789Sahrens 
1915789Sahrens /*
1916789Sahrens  * Export a storage pool.
1917789Sahrens  */
1918789Sahrens int
19191775Sbillm spa_export(char *pool, nvlist_t **oldconfig)
1920789Sahrens {
19211775Sbillm 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig));
1922789Sahrens }
1923789Sahrens 
1924789Sahrens /*
19251544Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
19261544Seschrock  * from the namespace in any way.
19271544Seschrock  */
19281544Seschrock int
19291544Seschrock spa_reset(char *pool)
19301544Seschrock {
19311775Sbillm 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL));
19321544Seschrock }
19331544Seschrock 
19341544Seschrock 
19351544Seschrock /*
1936789Sahrens  * ==========================================================================
1937789Sahrens  * Device manipulation
1938789Sahrens  * ==========================================================================
1939789Sahrens  */
1940789Sahrens 
1941789Sahrens /*
19424527Sperrin  * Add a device to a storage pool.
1943789Sahrens  */
1944789Sahrens int
1945789Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
1946789Sahrens {
1947789Sahrens 	uint64_t txg;
19481635Sbonwick 	int c, error;
1949789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
19501585Sbonwick 	vdev_t *vd, *tvd;
19512082Seschrock 	nvlist_t **spares;
19522082Seschrock 	uint_t i, nspares;
1953789Sahrens 
1954789Sahrens 	txg = spa_vdev_enter(spa);
1955789Sahrens 
19562082Seschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
19572082Seschrock 	    VDEV_ALLOC_ADD)) != 0)
19582082Seschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
19592082Seschrock 
19603377Seschrock 	spa->spa_pending_vdev = vd;
1961789Sahrens 
19622082Seschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
19632082Seschrock 	    &spares, &nspares) != 0)
19642082Seschrock 		nspares = 0;
19652082Seschrock 
19663377Seschrock 	if (vd->vdev_children == 0 && nspares == 0) {
19673377Seschrock 		spa->spa_pending_vdev = NULL;
19682082Seschrock 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
19693377Seschrock 	}
19702082Seschrock 
19712082Seschrock 	if (vd->vdev_children != 0) {
19723377Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
19733377Seschrock 			spa->spa_pending_vdev = NULL;
19742082Seschrock 			return (spa_vdev_exit(spa, vd, txg, error));
19752082Seschrock 		}
19762082Seschrock 	}
19772082Seschrock 
19783377Seschrock 	/*
19793377Seschrock 	 * We must validate the spares after checking the children.  Otherwise,
19803377Seschrock 	 * vdev_inuse() will blindly overwrite the spare.
19813377Seschrock 	 */
19823377Seschrock 	if ((error = spa_validate_spares(spa, nvroot, txg,
19833377Seschrock 	    VDEV_ALLOC_ADD)) != 0) {
19843377Seschrock 		spa->spa_pending_vdev = NULL;
19853377Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
19863377Seschrock 	}
19873377Seschrock 
19883377Seschrock 	spa->spa_pending_vdev = NULL;
19893377Seschrock 
19903377Seschrock 	/*
19913377Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
19923377Seschrock 	 */
19933377Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
19943377Seschrock 		tvd = vd->vdev_child[c];
19953377Seschrock 		vdev_remove_child(vd, tvd);
19963377Seschrock 		tvd->vdev_id = rvd->vdev_children;
19973377Seschrock 		vdev_add_child(rvd, tvd);
19983377Seschrock 		vdev_config_dirty(tvd);
19993377Seschrock 	}
20003377Seschrock 
20012082Seschrock 	if (nspares != 0) {
20022082Seschrock 		if (spa->spa_sparelist != NULL) {
20032082Seschrock 			nvlist_t **oldspares;
20042082Seschrock 			uint_t oldnspares;
20052082Seschrock 			nvlist_t **newspares;
20062082Seschrock 
20072082Seschrock 			VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
20082082Seschrock 			    ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0);
20092082Seschrock 
20102082Seschrock 			newspares = kmem_alloc(sizeof (void *) *
20112082Seschrock 			    (nspares + oldnspares), KM_SLEEP);
20122082Seschrock 			for (i = 0; i < oldnspares; i++)
20132082Seschrock 				VERIFY(nvlist_dup(oldspares[i],
20142082Seschrock 				    &newspares[i], KM_SLEEP) == 0);
20152082Seschrock 			for (i = 0; i < nspares; i++)
20162082Seschrock 				VERIFY(nvlist_dup(spares[i],
20172082Seschrock 				    &newspares[i + oldnspares],
20182082Seschrock 				    KM_SLEEP) == 0);
20192082Seschrock 
20202082Seschrock 			VERIFY(nvlist_remove(spa->spa_sparelist,
20212082Seschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
20222082Seschrock 
20232082Seschrock 			VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
20242082Seschrock 			    ZPOOL_CONFIG_SPARES, newspares,
20252082Seschrock 			    nspares + oldnspares) == 0);
20262082Seschrock 			for (i = 0; i < oldnspares + nspares; i++)
20272082Seschrock 				nvlist_free(newspares[i]);
20282082Seschrock 			kmem_free(newspares, (oldnspares + nspares) *
20292082Seschrock 			    sizeof (void *));
20302082Seschrock 		} else {
20312082Seschrock 			VERIFY(nvlist_alloc(&spa->spa_sparelist,
20322082Seschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
20332082Seschrock 			VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
20342082Seschrock 			    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
20352082Seschrock 		}
20362082Seschrock 
20372082Seschrock 		spa_load_spares(spa);
20382082Seschrock 		spa->spa_sync_spares = B_TRUE;
2039789Sahrens 	}
2040789Sahrens 
2041789Sahrens 	/*
20421585Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
20431585Sbonwick 	 * If other threads start allocating from these vdevs before we
20441585Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
20451585Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
20461585Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
20471585Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
20481635Sbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
20491585Sbonwick 	 *
20501585Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
20511585Sbonwick 	 * if we lose power at any point in this sequence, the remaining
20521585Sbonwick 	 * steps will be completed the next time we load the pool.
2053789Sahrens 	 */
20541635Sbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
20551585Sbonwick 
20561635Sbonwick 	mutex_enter(&spa_namespace_lock);
20571635Sbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
20581635Sbonwick 	mutex_exit(&spa_namespace_lock);
2059789Sahrens 
20601635Sbonwick 	return (0);
2061789Sahrens }
2062789Sahrens 
2063789Sahrens /*
2064789Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2065789Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2066789Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2067789Sahrens  *
2068789Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2069789Sahrens  * existing device; in this case the two devices are made into their own
20704451Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2071789Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2072789Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2073789Sahrens  * completion of resilvering, the first disk (the one being replaced)
2074789Sahrens  * is automatically detached.
2075789Sahrens  */
2076789Sahrens int
20771544Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2078789Sahrens {
2079789Sahrens 	uint64_t txg, open_txg;
2080789Sahrens 	int error;
2081789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2082789Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
20832082Seschrock 	vdev_ops_t *pvops;
20844527Sperrin 	int is_log;
2085789Sahrens 
2086789Sahrens 	txg = spa_vdev_enter(spa);
2087789Sahrens 
20881544Seschrock 	oldvd = vdev_lookup_by_guid(rvd, guid);
2089789Sahrens 
2090789Sahrens 	if (oldvd == NULL)
2091789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2092789Sahrens 
20931585Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
20941585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
20951585Sbonwick 
2096789Sahrens 	pvd = oldvd->vdev_parent;
2097789Sahrens 
20982082Seschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
20994451Seschrock 	    VDEV_ALLOC_ADD)) != 0)
21004451Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
21014451Seschrock 
21024451Seschrock 	if (newrootvd->vdev_children != 1)
2103789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2104789Sahrens 
2105789Sahrens 	newvd = newrootvd->vdev_child[0];
2106789Sahrens 
2107789Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2108789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2109789Sahrens 
21102082Seschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2111789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2112789Sahrens 
21134527Sperrin 	/*
21144527Sperrin 	 * Spares can't replace logs
21154527Sperrin 	 */
21164527Sperrin 	is_log = oldvd->vdev_islog;
21174527Sperrin 	if (is_log && newvd->vdev_isspare)
21184527Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
21194527Sperrin 
21202082Seschrock 	if (!replacing) {
21212082Seschrock 		/*
21222082Seschrock 		 * For attach, the only allowable parent is a mirror or the root
21232082Seschrock 		 * vdev.
21242082Seschrock 		 */
21252082Seschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
21262082Seschrock 		    pvd->vdev_ops != &vdev_root_ops)
21272082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
21282082Seschrock 
21292082Seschrock 		pvops = &vdev_mirror_ops;
21302082Seschrock 	} else {
21312082Seschrock 		/*
21322082Seschrock 		 * Active hot spares can only be replaced by inactive hot
21332082Seschrock 		 * spares.
21342082Seschrock 		 */
21352082Seschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
21362082Seschrock 		    pvd->vdev_child[1] == oldvd &&
21372082Seschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
21382082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
21392082Seschrock 
21402082Seschrock 		/*
21412082Seschrock 		 * If the source is a hot spare, and the parent isn't already a
21422082Seschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
21433377Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
21443377Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
21453377Seschrock 		 * the same (spare replaces spare, non-spare replaces
21463377Seschrock 		 * non-spare).
21472082Seschrock 		 */
21482082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
21492082Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
21503377Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
21513377Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
21523377Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
21532082Seschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
21542082Seschrock 		    newvd->vdev_isspare)
21552082Seschrock 			pvops = &vdev_spare_ops;
21562082Seschrock 		else
21572082Seschrock 			pvops = &vdev_replacing_ops;
21582082Seschrock 	}
21592082Seschrock 
21601175Slling 	/*
21611175Slling 	 * Compare the new device size with the replaceable/attachable
21621175Slling 	 * device size.
21631175Slling 	 */
21641175Slling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2165789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2166789Sahrens 
21671732Sbonwick 	/*
21681732Sbonwick 	 * The new device cannot have a higher alignment requirement
21691732Sbonwick 	 * than the top-level vdev.
21701732Sbonwick 	 */
21711732Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2172789Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2173789Sahrens 
2174789Sahrens 	/*
2175789Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2176789Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2177789Sahrens 	 */
2178789Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2179789Sahrens 		spa_strfree(oldvd->vdev_path);
2180789Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2181789Sahrens 		    KM_SLEEP);
2182789Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2183789Sahrens 		    newvd->vdev_path, "old");
2184789Sahrens 		if (oldvd->vdev_devid != NULL) {
2185789Sahrens 			spa_strfree(oldvd->vdev_devid);
2186789Sahrens 			oldvd->vdev_devid = NULL;
2187789Sahrens 		}
2188789Sahrens 	}
2189789Sahrens 
2190789Sahrens 	/*
21912082Seschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
21922082Seschrock 	 * mirror/replacing/spare vdev above oldvd.
2193789Sahrens 	 */
2194789Sahrens 	if (pvd->vdev_ops != pvops)
2195789Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2196789Sahrens 
2197789Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2198789Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2199789Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2200789Sahrens 
2201789Sahrens 	/*
2202789Sahrens 	 * Extract the new device from its root and add it to pvd.
2203789Sahrens 	 */
2204789Sahrens 	vdev_remove_child(newrootvd, newvd);
2205789Sahrens 	newvd->vdev_id = pvd->vdev_children;
2206789Sahrens 	vdev_add_child(pvd, newvd);
2207789Sahrens 
22081544Seschrock 	/*
22091544Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
22101544Seschrock 	 * the addition of newvd may have decreased our parent's asize.
22111544Seschrock 	 */
22121544Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
22131544Seschrock 
2214789Sahrens 	tvd = newvd->vdev_top;
2215789Sahrens 	ASSERT(pvd->vdev_top == tvd);
2216789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2217789Sahrens 
2218789Sahrens 	vdev_config_dirty(tvd);
2219789Sahrens 
2220789Sahrens 	/*
2221789Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2222789Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2223789Sahrens 	 */
2224789Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2225789Sahrens 
2226789Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2227789Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2228789Sahrens 	    open_txg - TXG_INITIAL + 1);
2229789Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2230789Sahrens 
22313377Seschrock 	if (newvd->vdev_isspare)
22323377Seschrock 		spa_spare_activate(newvd);
22331544Seschrock 
2234789Sahrens 	/*
2235789Sahrens 	 * Mark newvd's DTL dirty in this txg.
2236789Sahrens 	 */
22371732Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2238789Sahrens 
2239789Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2240789Sahrens 
2241789Sahrens 	/*
22424451Seschrock 	 * Kick off a resilver to update newvd.  We need to grab the namespace
22434451Seschrock 	 * lock because spa_scrub() needs to post a sysevent with the pool name.
2244789Sahrens 	 */
22454451Seschrock 	mutex_enter(&spa_namespace_lock);
2246789Sahrens 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
22474451Seschrock 	mutex_exit(&spa_namespace_lock);
2248789Sahrens 
2249789Sahrens 	return (0);
2250789Sahrens }
2251789Sahrens 
2252789Sahrens /*
2253789Sahrens  * Detach a device from a mirror or replacing vdev.
2254789Sahrens  * If 'replace_done' is specified, only detach if the parent
2255789Sahrens  * is a replacing vdev.
2256789Sahrens  */
2257789Sahrens int
22581544Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2259789Sahrens {
2260789Sahrens 	uint64_t txg;
2261789Sahrens 	int c, t, error;
2262789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2263789Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
22642082Seschrock 	boolean_t unspare = B_FALSE;
22652082Seschrock 	uint64_t unspare_guid;
2266789Sahrens 
2267789Sahrens 	txg = spa_vdev_enter(spa);
2268789Sahrens 
22691544Seschrock 	vd = vdev_lookup_by_guid(rvd, guid);
2270789Sahrens 
2271789Sahrens 	if (vd == NULL)
2272789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2273789Sahrens 
22741585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
22751585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
22761585Sbonwick 
2277789Sahrens 	pvd = vd->vdev_parent;
2278789Sahrens 
2279789Sahrens 	/*
2280789Sahrens 	 * If replace_done is specified, only remove this device if it's
22812082Seschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
22822082Seschrock 	 * disk can be removed.
2283789Sahrens 	 */
22842082Seschrock 	if (replace_done) {
22852082Seschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
22862082Seschrock 			if (vd->vdev_id != 0)
22872082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
22882082Seschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
22892082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
22902082Seschrock 		}
22912082Seschrock 	}
22922082Seschrock 
22932082Seschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
22944577Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
2295789Sahrens 
2296789Sahrens 	/*
22972082Seschrock 	 * Only mirror, replacing, and spare vdevs support detach.
2298789Sahrens 	 */
2299789Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
23002082Seschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
23012082Seschrock 	    pvd->vdev_ops != &vdev_spare_ops)
2302789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2303789Sahrens 
2304789Sahrens 	/*
2305789Sahrens 	 * If there's only one replica, you can't detach it.
2306789Sahrens 	 */
2307789Sahrens 	if (pvd->vdev_children <= 1)
2308789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2309789Sahrens 
2310789Sahrens 	/*
2311789Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
2312789Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
2313789Sahrens 	 *
2314789Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
2315789Sahrens 	 * precise DTL check.
2316789Sahrens 	 */
2317789Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
2318789Sahrens 		uint64_t dirty;
2319789Sahrens 
2320789Sahrens 		cvd = pvd->vdev_child[c];
2321789Sahrens 		if (cvd == vd)
2322789Sahrens 			continue;
2323789Sahrens 		if (vdev_is_dead(cvd))
2324789Sahrens 			continue;
2325789Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
2326789Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
2327789Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
2328789Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
2329789Sahrens 		if (!dirty)
2330789Sahrens 			break;
2331789Sahrens 	}
23322082Seschrock 
23332082Seschrock 	/*
23342082Seschrock 	 * If we are a replacing or spare vdev, then we can always detach the
23352082Seschrock 	 * latter child, as that is how one cancels the operation.
23362082Seschrock 	 */
23372082Seschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
23382082Seschrock 	    c == pvd->vdev_children)
2339789Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2340789Sahrens 
2341789Sahrens 	/*
23422082Seschrock 	 * If we are detaching the original disk from a spare, then it implies
23432082Seschrock 	 * that the spare should become a real disk, and be removed from the
23442082Seschrock 	 * active spare list for the pool.
23452082Seschrock 	 */
23462082Seschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
23472082Seschrock 	    vd->vdev_id == 0)
23482082Seschrock 		unspare = B_TRUE;
23492082Seschrock 
23502082Seschrock 	/*
2351789Sahrens 	 * Erase the disk labels so the disk can be used for other things.
2352789Sahrens 	 * This must be done after all other error cases are handled,
2353789Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
2354789Sahrens 	 * But if we can't do it, don't treat the error as fatal --
2355789Sahrens 	 * it may be that the unwritability of the disk is the reason
2356789Sahrens 	 * it's being detached!
2357789Sahrens 	 */
23583377Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
2359789Sahrens 
2360789Sahrens 	/*
2361789Sahrens 	 * Remove vd from its parent and compact the parent's children.
2362789Sahrens 	 */
2363789Sahrens 	vdev_remove_child(pvd, vd);
2364789Sahrens 	vdev_compact_children(pvd);
2365789Sahrens 
2366789Sahrens 	/*
2367789Sahrens 	 * Remember one of the remaining children so we can get tvd below.
2368789Sahrens 	 */
2369789Sahrens 	cvd = pvd->vdev_child[0];
2370789Sahrens 
2371789Sahrens 	/*
23722082Seschrock 	 * If we need to remove the remaining child from the list of hot spares,
23732082Seschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
23742082Seschrock 	 * must do this before vdev_remove_parent(), because that can change the
23752082Seschrock 	 * GUID if it creates a new toplevel GUID.
23762082Seschrock 	 */
23772082Seschrock 	if (unspare) {
23782082Seschrock 		ASSERT(cvd->vdev_isspare);
23793377Seschrock 		spa_spare_remove(cvd);
23802082Seschrock 		unspare_guid = cvd->vdev_guid;
23812082Seschrock 	}
23822082Seschrock 
23832082Seschrock 	/*
2384789Sahrens 	 * If the parent mirror/replacing vdev only has one child,
2385789Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
2386789Sahrens 	 */
2387789Sahrens 	if (pvd->vdev_children == 1)
2388789Sahrens 		vdev_remove_parent(cvd);
2389789Sahrens 
2390789Sahrens 	/*
2391789Sahrens 	 * We don't set tvd until now because the parent we just removed
2392789Sahrens 	 * may have been the previous top-level vdev.
2393789Sahrens 	 */
2394789Sahrens 	tvd = cvd->vdev_top;
2395789Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2396789Sahrens 
2397789Sahrens 	/*
23983377Seschrock 	 * Reevaluate the parent vdev state.
2399789Sahrens 	 */
24004451Seschrock 	vdev_propagate_state(cvd);
2401789Sahrens 
2402789Sahrens 	/*
24033377Seschrock 	 * If the device we just detached was smaller than the others, it may be
24043377Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
24053377Seschrock 	 * can't fail because the existing metaslabs are already in core, so
24063377Seschrock 	 * there's nothing to read from disk.
2407789Sahrens 	 */
24081732Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
2409789Sahrens 
2410789Sahrens 	vdev_config_dirty(tvd);
2411789Sahrens 
2412789Sahrens 	/*
24133377Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
24143377Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
24153377Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
24163377Seschrock 	 * prevent vd from being accessed after it's freed.
2417789Sahrens 	 */
2418789Sahrens 	for (t = 0; t < TXG_SIZE; t++)
2419789Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
24201732Sbonwick 	vd->vdev_detached = B_TRUE;
24211732Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
2422789Sahrens 
24234451Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
24244451Seschrock 
24252082Seschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
24262082Seschrock 
24272082Seschrock 	/*
24283377Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
24293377Seschrock 	 * then we want to go through and remove the device from the hot spare
24303377Seschrock 	 * list of every other pool.
24312082Seschrock 	 */
24322082Seschrock 	if (unspare) {
24332082Seschrock 		spa = NULL;
24342082Seschrock 		mutex_enter(&spa_namespace_lock);
24352082Seschrock 		while ((spa = spa_next(spa)) != NULL) {
24362082Seschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
24372082Seschrock 				continue;
24382082Seschrock 
24392082Seschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
24402082Seschrock 		}
24412082Seschrock 		mutex_exit(&spa_namespace_lock);
24422082Seschrock 	}
24432082Seschrock 
24442082Seschrock 	return (error);
24452082Seschrock }
24462082Seschrock 
24472082Seschrock /*
24482082Seschrock  * Remove a device from the pool.  Currently, this supports removing only hot
24492082Seschrock  * spares.
24502082Seschrock  */
24512082Seschrock int
24522082Seschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
24532082Seschrock {
24542082Seschrock 	vdev_t *vd;
24552082Seschrock 	nvlist_t **spares, *nv, **newspares;
24562082Seschrock 	uint_t i, j, nspares;
24572082Seschrock 	int ret = 0;
24582082Seschrock 
24592082Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
24602082Seschrock 
24612082Seschrock 	vd = spa_lookup_by_guid(spa, guid);
24622082Seschrock 
24632082Seschrock 	nv = NULL;
24642082Seschrock 	if (spa->spa_spares != NULL &&
24652082Seschrock 	    nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
24662082Seschrock 	    &spares, &nspares) == 0) {
24672082Seschrock 		for (i = 0; i < nspares; i++) {
24682082Seschrock 			uint64_t theguid;
24692082Seschrock 
24702082Seschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
24712082Seschrock 			    ZPOOL_CONFIG_GUID, &theguid) == 0);
24722082Seschrock 			if (theguid == guid) {
24732082Seschrock 				nv = spares[i];
24742082Seschrock 				break;
24752082Seschrock 			}
24762082Seschrock 		}
24772082Seschrock 	}
24782082Seschrock 
24792082Seschrock 	/*
24802082Seschrock 	 * We only support removing a hot spare, and only if it's not currently
24812082Seschrock 	 * in use in this pool.
24822082Seschrock 	 */
24832082Seschrock 	if (nv == NULL && vd == NULL) {
24842082Seschrock 		ret = ENOENT;
24852082Seschrock 		goto out;
24862082Seschrock 	}
24872082Seschrock 
24882082Seschrock 	if (nv == NULL && vd != NULL) {
24892082Seschrock 		ret = ENOTSUP;
24902082Seschrock 		goto out;
24912082Seschrock 	}
24922082Seschrock 
24932082Seschrock 	if (!unspare && nv != NULL && vd != NULL) {
24942082Seschrock 		ret = EBUSY;
24952082Seschrock 		goto out;
24962082Seschrock 	}
24972082Seschrock 
24982082Seschrock 	if (nspares == 1) {
24992082Seschrock 		newspares = NULL;
25002082Seschrock 	} else {
25012082Seschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
25022082Seschrock 		    KM_SLEEP);
25032082Seschrock 		for (i = 0, j = 0; i < nspares; i++) {
25042082Seschrock 			if (spares[i] != nv)
25052082Seschrock 				VERIFY(nvlist_dup(spares[i],
25062082Seschrock 				    &newspares[j++], KM_SLEEP) == 0);
25072082Seschrock 		}
25082082Seschrock 	}
25092082Seschrock 
25102082Seschrock 	VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
25112082Seschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
25122082Seschrock 	VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
25132082Seschrock 	    newspares, nspares - 1) == 0);
25142082Seschrock 	for (i = 0; i < nspares - 1; i++)
25152082Seschrock 		nvlist_free(newspares[i]);
25162082Seschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
25172082Seschrock 	spa_load_spares(spa);
25182082Seschrock 	spa->spa_sync_spares = B_TRUE;
25192082Seschrock 
25202082Seschrock out:
25212082Seschrock 	spa_config_exit(spa, FTAG);
25222082Seschrock 
25232082Seschrock 	return (ret);
2524789Sahrens }
2525789Sahrens 
2526789Sahrens /*
25274451Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
25284451Seschrock  * current spared, so we can detach it.
2529789Sahrens  */
25301544Seschrock static vdev_t *
25314451Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
2532789Sahrens {
25331544Seschrock 	vdev_t *newvd, *oldvd;
2534789Sahrens 	int c;
2535789Sahrens 
25361544Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
25374451Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
25381544Seschrock 		if (oldvd != NULL)
25391544Seschrock 			return (oldvd);
25401544Seschrock 	}
2541789Sahrens 
25424451Seschrock 	/*
25434451Seschrock 	 * Check for a completed replacement.
25444451Seschrock 	 */
2545789Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
25461544Seschrock 		oldvd = vd->vdev_child[0];
25471544Seschrock 		newvd = vd->vdev_child[1];
2548789Sahrens 
25491544Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
25501544Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
25511544Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
25521544Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
25531544Seschrock 			return (oldvd);
25541544Seschrock 		}
25551544Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
25561544Seschrock 	}
2557789Sahrens 
25584451Seschrock 	/*
25594451Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
25604451Seschrock 	 */
25614451Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
25624451Seschrock 		newvd = vd->vdev_child[0];
25634451Seschrock 		oldvd = vd->vdev_child[1];
25644451Seschrock 
25654451Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
25664451Seschrock 		if (newvd->vdev_unspare &&
25674451Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
25684451Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
25694451Seschrock 			newvd->vdev_unspare = 0;
25704451Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
25714451Seschrock 			return (oldvd);
25724451Seschrock 		}
25734451Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
25744451Seschrock 	}
25754451Seschrock 
25761544Seschrock 	return (NULL);
2577789Sahrens }
2578789Sahrens 
25791544Seschrock static void
25804451Seschrock spa_vdev_resilver_done(spa_t *spa)
2581789Sahrens {
25821544Seschrock 	vdev_t *vd;
25832082Seschrock 	vdev_t *pvd;
25841544Seschrock 	uint64_t guid;
25852082Seschrock 	uint64_t pguid = 0;
2586789Sahrens 
25871544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2588789Sahrens 
25894451Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
25901544Seschrock 		guid = vd->vdev_guid;
25912082Seschrock 		/*
25922082Seschrock 		 * If we have just finished replacing a hot spared device, then
25932082Seschrock 		 * we need to detach the parent's first child (the original hot
25942082Seschrock 		 * spare) as well.
25952082Seschrock 		 */
25962082Seschrock 		pvd = vd->vdev_parent;
25972082Seschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
25982082Seschrock 		    pvd->vdev_id == 0) {
25992082Seschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
26002082Seschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
26012082Seschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
26022082Seschrock 		}
26031544Seschrock 		spa_config_exit(spa, FTAG);
26041544Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
26051544Seschrock 			return;
26062082Seschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
26072082Seschrock 			return;
26081544Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
2609789Sahrens 	}
2610789Sahrens 
26111544Seschrock 	spa_config_exit(spa, FTAG);
2612789Sahrens }
2613789Sahrens 
2614789Sahrens /*
26151354Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
26161354Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
26171354Seschrock  */
26181354Seschrock int
26191354Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
26201354Seschrock {
26211354Seschrock 	vdev_t *rvd, *vd;
26221354Seschrock 	uint64_t txg;
26231354Seschrock 
26241354Seschrock 	rvd = spa->spa_root_vdev;
26251354Seschrock 
26261354Seschrock 	txg = spa_vdev_enter(spa);
26271354Seschrock 
26282082Seschrock 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
26292082Seschrock 		/*
26302082Seschrock 		 * Determine if this is a reference to a hot spare.  In that
26312082Seschrock 		 * case, update the path as stored in the spare list.
26322082Seschrock 		 */
26332082Seschrock 		nvlist_t **spares;
26342082Seschrock 		uint_t i, nspares;
26352082Seschrock 		if (spa->spa_sparelist != NULL) {
26362082Seschrock 			VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
26372082Seschrock 			    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
26382082Seschrock 			for (i = 0; i < nspares; i++) {
26392082Seschrock 				uint64_t theguid;
26402082Seschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
26412082Seschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
26422082Seschrock 				if (theguid == guid)
26432082Seschrock 					break;
26442082Seschrock 			}
26452082Seschrock 
26462082Seschrock 			if (i == nspares)
26472082Seschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOENT));
26482082Seschrock 
26492082Seschrock 			VERIFY(nvlist_add_string(spares[i],
26502082Seschrock 			    ZPOOL_CONFIG_PATH, newpath) == 0);
26512082Seschrock 			spa_load_spares(spa);
26522082Seschrock 			spa->spa_sync_spares = B_TRUE;
26532082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, 0));
26542082Seschrock 		} else {
26552082Seschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOENT));
26562082Seschrock 		}
26572082Seschrock 	}
26581354Seschrock 
26591585Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
26601585Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
26611585Sbonwick 
26621354Seschrock 	spa_strfree(vd->vdev_path);
26631354Seschrock 	vd->vdev_path = spa_strdup(newpath);
26641354Seschrock 
26651354Seschrock 	vdev_config_dirty(vd->vdev_top);
26661354Seschrock 
26671354Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
26681354Seschrock }
26691354Seschrock 
26701354Seschrock /*
2671789Sahrens  * ==========================================================================
2672789Sahrens  * SPA Scrubbing
2673789Sahrens  * ==========================================================================
2674789Sahrens  */
2675789Sahrens 
2676789Sahrens static void
2677789Sahrens spa_scrub_io_done(zio_t *zio)
2678789Sahrens {
2679789Sahrens 	spa_t *spa = zio->io_spa;
2680789Sahrens 
26814309Smaybee 	arc_data_buf_free(zio->io_data, zio->io_size);
2682789Sahrens 
2683789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
26841544Seschrock 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
26851775Sbillm 		vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev;
2686789Sahrens 		spa->spa_scrub_errors++;
2687789Sahrens 		mutex_enter(&vd->vdev_stat_lock);
2688789Sahrens 		vd->vdev_stat.vs_scrub_errors++;
2689789Sahrens 		mutex_exit(&vd->vdev_stat_lock);
2690789Sahrens 	}
26913697Smishra 
26923697Smishra 	if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight)
26931544Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
26943697Smishra 
26953697Smishra 	ASSERT(spa->spa_scrub_inflight >= 0);
26963697Smishra 
26971544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
2698789Sahrens }
2699789Sahrens 
2700789Sahrens static void
27011544Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags,
27021544Seschrock     zbookmark_t *zb)
2703789Sahrens {
2704789Sahrens 	size_t size = BP_GET_LSIZE(bp);
27053697Smishra 	void *data;
2706789Sahrens 
2707789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
27083697Smishra 	/*
27093697Smishra 	 * Do not give too much work to vdev(s).
27103697Smishra 	 */
27113697Smishra 	while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) {
27123697Smishra 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
27133697Smishra 	}
2714789Sahrens 	spa->spa_scrub_inflight++;
2715789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2716789Sahrens 
27174309Smaybee 	data = arc_data_buf_alloc(size);
27183697Smishra 
27191544Seschrock 	if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
27201544Seschrock 		flags |= ZIO_FLAG_SPECULATIVE;	/* intent log block */
27211544Seschrock 
27221807Sbonwick 	flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL;
27231544Seschrock 
2724789Sahrens 	zio_nowait(zio_read(NULL, spa, bp, data, size,
27251544Seschrock 	    spa_scrub_io_done, NULL, priority, flags, zb));
2726789Sahrens }
2727789Sahrens 
2728789Sahrens /* ARGSUSED */
2729789Sahrens static int
2730789Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
2731789Sahrens {
2732789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
27331775Sbillm 	vdev_t *vd = spa->spa_root_vdev;
27341775Sbillm 	dva_t *dva = bp->blk_dva;
27351775Sbillm 	int needs_resilver = B_FALSE;
27361775Sbillm 	int d;
2737789Sahrens 
27381775Sbillm 	if (bc->bc_errno) {
2739789Sahrens 		/*
2740789Sahrens 		 * We can't scrub this block, but we can continue to scrub
2741789Sahrens 		 * the rest of the pool.  Note the error and move along.
2742789Sahrens 		 */
2743789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
2744789Sahrens 		spa->spa_scrub_errors++;
2745789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
2746789Sahrens 
27471775Sbillm 		mutex_enter(&vd->vdev_stat_lock);
27481775Sbillm 		vd->vdev_stat.vs_scrub_errors++;
27491775Sbillm 		mutex_exit(&vd->vdev_stat_lock);
2750789Sahrens 
2751789Sahrens 		return (ERESTART);
2752789Sahrens 	}
2753789Sahrens 
2754789Sahrens 	ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg);
2755789Sahrens 
27561775Sbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
27571775Sbillm 		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]));
27581775Sbillm 
27591775Sbillm 		ASSERT(vd != NULL);
27601775Sbillm 
27611775Sbillm 		/*
27621775Sbillm 		 * Keep track of how much data we've examined so that
27631775Sbillm 		 * zpool(1M) status can make useful progress reports.
27641775Sbillm 		 */
27651775Sbillm 		mutex_enter(&vd->vdev_stat_lock);
27661775Sbillm 		vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]);
27671775Sbillm 		mutex_exit(&vd->vdev_stat_lock);
2768789Sahrens 
27691775Sbillm 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) {
27701775Sbillm 			if (DVA_GET_GANG(&dva[d])) {
27711775Sbillm 				/*
27721775Sbillm 				 * Gang members may be spread across multiple
27731775Sbillm 				 * vdevs, so the best we can do is look at the
27741775Sbillm 				 * pool-wide DTL.
27751775Sbillm 				 * XXX -- it would be better to change our
27761775Sbillm 				 * allocation policy to ensure that this can't
27771775Sbillm 				 * happen.
27781775Sbillm 				 */
27791775Sbillm 				vd = spa->spa_root_vdev;
27801775Sbillm 			}
27811775Sbillm 			if (vdev_dtl_contains(&vd->vdev_dtl_map,
27821775Sbillm 			    bp->blk_birth, 1))
27831775Sbillm 				needs_resilver = B_TRUE;
2784789Sahrens 		}
27851775Sbillm 	}
27861775Sbillm 
27871775Sbillm 	if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING)
2788789Sahrens 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB,
27891544Seschrock 		    ZIO_FLAG_SCRUB, &bc->bc_bookmark);
27901775Sbillm 	else if (needs_resilver)
27911775Sbillm 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER,
27921775Sbillm 		    ZIO_FLAG_RESILVER, &bc->bc_bookmark);
2793789Sahrens 
2794789Sahrens 	return (0);
2795789Sahrens }
2796789Sahrens 
2797789Sahrens static void
2798789Sahrens spa_scrub_thread(spa_t *spa)
2799789Sahrens {
2800789Sahrens 	callb_cpr_t cprinfo;
2801789Sahrens 	traverse_handle_t *th = spa->spa_scrub_th;
2802789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2803789Sahrens 	pool_scrub_type_t scrub_type = spa->spa_scrub_type;
2804789Sahrens 	int error = 0;
2805789Sahrens 	boolean_t complete;
2806789Sahrens 
2807789Sahrens 	CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG);
2808789Sahrens 
2809797Sbonwick 	/*
2810797Sbonwick 	 * If we're restarting due to a snapshot create/delete,
2811797Sbonwick 	 * wait for that to complete.
2812797Sbonwick 	 */
2813797Sbonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
2814797Sbonwick 
28151544Seschrock 	dprintf("start %s mintxg=%llu maxtxg=%llu\n",
28161544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
28171544Seschrock 	    spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg);
28181544Seschrock 
28191544Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
28201544Seschrock 	vdev_reopen(rvd);		/* purge all vdev caches */
2821789Sahrens 	vdev_config_dirty(rvd);		/* rewrite all disk labels */
2822789Sahrens 	vdev_scrub_stat_update(rvd, scrub_type, B_FALSE);
28231544Seschrock 	spa_config_exit(spa, FTAG);
2824789Sahrens 
2825789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2826789Sahrens 	spa->spa_scrub_errors = 0;
2827789Sahrens 	spa->spa_scrub_active = 1;
28281544Seschrock 	ASSERT(spa->spa_scrub_inflight == 0);
2829789Sahrens 
2830789Sahrens 	while (!spa->spa_scrub_stop) {
2831789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
28321544Seschrock 		while (spa->spa_scrub_suspended) {
2833789Sahrens 			spa->spa_scrub_active = 0;
2834789Sahrens 			cv_broadcast(&spa->spa_scrub_cv);
2835789Sahrens 			cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
2836789Sahrens 			spa->spa_scrub_active = 1;
2837789Sahrens 		}
2838789Sahrens 		CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock);
2839789Sahrens 
2840789Sahrens 		if (spa->spa_scrub_restart_txg != 0)
2841789Sahrens 			break;
2842789Sahrens 
2843789Sahrens 		mutex_exit(&spa->spa_scrub_lock);
2844789Sahrens 		error = traverse_more(th);
2845789Sahrens 		mutex_enter(&spa->spa_scrub_lock);
2846789Sahrens 		if (error != EAGAIN)
2847789Sahrens 			break;
2848789Sahrens 	}
2849789Sahrens 
2850789Sahrens 	while (spa->spa_scrub_inflight)
2851789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2852789Sahrens 
28531601Sbonwick 	spa->spa_scrub_active = 0;
28541601Sbonwick 	cv_broadcast(&spa->spa_scrub_cv);
28551601Sbonwick 
28561601Sbonwick 	mutex_exit(&spa->spa_scrub_lock);
28571601Sbonwick 
28581601Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
28591601Sbonwick 
28601601Sbonwick 	mutex_enter(&spa->spa_scrub_lock);
28611601Sbonwick 
28621601Sbonwick 	/*
28631601Sbonwick 	 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock
28641601Sbonwick 	 * AND the spa config lock to synchronize with any config changes
28651601Sbonwick 	 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
28661601Sbonwick 	 */
2867789Sahrens 	if (spa->spa_scrub_restart_txg != 0)
2868789Sahrens 		error = ERESTART;
2869789Sahrens 
28701544Seschrock 	if (spa->spa_scrub_stop)
28711544Seschrock 		error = EINTR;
28721544Seschrock 
2873789Sahrens 	/*
28741544Seschrock 	 * Even if there were uncorrectable errors, we consider the scrub
28751544Seschrock 	 * completed.  The downside is that if there is a transient error during
28761544Seschrock 	 * a resilver, we won't resilver the data properly to the target.  But
28771544Seschrock 	 * if the damage is permanent (more likely) we will resilver forever,
28781544Seschrock 	 * which isn't really acceptable.  Since there is enough information for
28791544Seschrock 	 * the user to know what has failed and why, this seems like a more
28801544Seschrock 	 * tractable approach.
2881789Sahrens 	 */
28821544Seschrock 	complete = (error == 0);
2883789Sahrens 
28841544Seschrock 	dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
28851544Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
2886789Sahrens 	    spa->spa_scrub_maxtxg, complete ? "done" : "FAILED",
2887789Sahrens 	    error, spa->spa_scrub_errors, spa->spa_scrub_stop);
2888789Sahrens 
2889789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2890789Sahrens 
2891789Sahrens 	/*
2892789Sahrens 	 * If the scrub/resilver completed, update all DTLs to reflect this.
2893789Sahrens 	 * Whether it succeeded or not, vacate all temporary scrub DTLs.
2894789Sahrens 	 */
2895789Sahrens 	vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1,
2896789Sahrens 	    complete ? spa->spa_scrub_maxtxg : 0, B_TRUE);
2897789Sahrens 	vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete);
28981544Seschrock 	spa_errlog_rotate(spa);
28991601Sbonwick 
29004451Seschrock 	if (scrub_type == POOL_SCRUB_RESILVER && complete)
29014451Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH);
29024451Seschrock 
29031544Seschrock 	spa_config_exit(spa, FTAG);
2904789Sahrens 
2905789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2906789Sahrens 
29071544Seschrock 	/*
29081544Seschrock 	 * We may have finished replacing a device.
29091544Seschrock 	 * Let the async thread assess this and handle the detach.
29101544Seschrock 	 */
29114451Seschrock 	spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
2912789Sahrens 
2913789Sahrens 	/*
2914789Sahrens 	 * If we were told to restart, our final act is to start a new scrub.
2915789Sahrens 	 */
2916789Sahrens 	if (error == ERESTART)
29171544Seschrock 		spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ?
29181544Seschrock 		    SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB);
2919789Sahrens 
29201544Seschrock 	spa->spa_scrub_type = POOL_SCRUB_NONE;
29211544Seschrock 	spa->spa_scrub_active = 0;
29221544Seschrock 	spa->spa_scrub_thread = NULL;
29231544Seschrock 	cv_broadcast(&spa->spa_scrub_cv);
2924789Sahrens 	CALLB_CPR_EXIT(&cprinfo);	/* drops &spa->spa_scrub_lock */
2925789Sahrens 	thread_exit();
2926789Sahrens }
2927789Sahrens 
2928789Sahrens void
2929789Sahrens spa_scrub_suspend(spa_t *spa)
2930789Sahrens {
2931789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
29321544Seschrock 	spa->spa_scrub_suspended++;
2933789Sahrens 	while (spa->spa_scrub_active) {
2934789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
2935789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
2936789Sahrens 	}
2937789Sahrens 	while (spa->spa_scrub_inflight)
2938789Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2939789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2940789Sahrens }
2941789Sahrens 
2942789Sahrens void
2943789Sahrens spa_scrub_resume(spa_t *spa)
2944789Sahrens {
2945789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
29461544Seschrock 	ASSERT(spa->spa_scrub_suspended != 0);
29471544Seschrock 	if (--spa->spa_scrub_suspended == 0)
2948789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
2949789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2950789Sahrens }
2951789Sahrens 
2952789Sahrens void
2953789Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg)
2954789Sahrens {
2955789Sahrens 	/*
2956789Sahrens 	 * Something happened (e.g. snapshot create/delete) that means
2957789Sahrens 	 * we must restart any in-progress scrubs.  The itinerary will
2958789Sahrens 	 * fix this properly.
2959789Sahrens 	 */
2960789Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2961789Sahrens 	spa->spa_scrub_restart_txg = txg;
2962789Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2963789Sahrens }
2964789Sahrens 
29651544Seschrock int
29661544Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force)
2967789Sahrens {
2968789Sahrens 	space_seg_t *ss;
2969789Sahrens 	uint64_t mintxg, maxtxg;
2970789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2971789Sahrens 
29724808Sek110237 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
29734808Sek110237 	ASSERT(!spa_config_held(spa, RW_WRITER));
29744808Sek110237 
2975789Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
2976789Sahrens 		return (ENOTSUP);
2977789Sahrens 
29781544Seschrock 	mutex_enter(&spa->spa_scrub_lock);
29791544Seschrock 
2980789Sahrens 	/*
2981789Sahrens 	 * If there's a scrub or resilver already in progress, stop it.
2982789Sahrens 	 */
2983789Sahrens 	while (spa->spa_scrub_thread != NULL) {
2984789Sahrens 		/*
2985789Sahrens 		 * Don't stop a resilver unless forced.
2986789Sahrens 		 */
29871544Seschrock 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) {
29881544Seschrock 			mutex_exit(&spa->spa_scrub_lock);
2989789Sahrens 			return (EBUSY);
29901544Seschrock 		}
2991789Sahrens 		spa->spa_scrub_stop = 1;
2992789Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
2993789Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
2994789Sahrens 	}
2995789Sahrens 
2996789Sahrens 	/*
2997789Sahrens 	 * Terminate the previous traverse.
2998789Sahrens 	 */
2999789Sahrens 	if (spa->spa_scrub_th != NULL) {
3000789Sahrens 		traverse_fini(spa->spa_scrub_th);
3001789Sahrens 		spa->spa_scrub_th = NULL;
3002789Sahrens 	}
3003789Sahrens 
30041544Seschrock 	if (rvd == NULL) {
30051544Seschrock 		ASSERT(spa->spa_scrub_stop == 0);
30061544Seschrock 		ASSERT(spa->spa_scrub_type == type);
30071544Seschrock 		ASSERT(spa->spa_scrub_restart_txg == 0);
30081544Seschrock 		mutex_exit(&spa->spa_scrub_lock);
30091544Seschrock 		return (0);
30101544Seschrock 	}
3011789Sahrens 
3012789Sahrens 	mintxg = TXG_INITIAL - 1;
3013789Sahrens 	maxtxg = spa_last_synced_txg(spa) + 1;
3014789Sahrens 
30151544Seschrock 	mutex_enter(&rvd->vdev_dtl_lock);
3016789Sahrens 
30171544Seschrock 	if (rvd->vdev_dtl_map.sm_space == 0) {
30181544Seschrock 		/*
30191544Seschrock 		 * The pool-wide DTL is empty.
30201732Sbonwick 		 * If this is a resilver, there's nothing to do except
30211732Sbonwick 		 * check whether any in-progress replacements have completed.
30221544Seschrock 		 */
30231732Sbonwick 		if (type == POOL_SCRUB_RESILVER) {
30241544Seschrock 			type = POOL_SCRUB_NONE;
30254451Seschrock 			spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
30261732Sbonwick 		}
30271544Seschrock 	} else {
30281544Seschrock 		/*
30291544Seschrock 		 * The pool-wide DTL is non-empty.
30301544Seschrock 		 * If this is a normal scrub, upgrade to a resilver instead.
30311544Seschrock 		 */
30321544Seschrock 		if (type == POOL_SCRUB_EVERYTHING)
30331544Seschrock 			type = POOL_SCRUB_RESILVER;
30341544Seschrock 	}
3035789Sahrens 
30361544Seschrock 	if (type == POOL_SCRUB_RESILVER) {
3037789Sahrens 		/*
3038789Sahrens 		 * Determine the resilvering boundaries.
3039789Sahrens 		 *
3040789Sahrens 		 * Note: (mintxg, maxtxg) is an open interval,
3041789Sahrens 		 * i.e. mintxg and maxtxg themselves are not included.
3042789Sahrens 		 *
3043789Sahrens 		 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
3044789Sahrens 		 * so we don't claim to resilver a txg that's still changing.
3045789Sahrens 		 */
3046789Sahrens 		ss = avl_first(&rvd->vdev_dtl_map.sm_root);
30471544Seschrock 		mintxg = ss->ss_start - 1;
3048789Sahrens 		ss = avl_last(&rvd->vdev_dtl_map.sm_root);
30491544Seschrock 		maxtxg = MIN(ss->ss_end, maxtxg);
30504451Seschrock 
30514451Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
3052789Sahrens 	}
3053789Sahrens 
30541544Seschrock 	mutex_exit(&rvd->vdev_dtl_lock);
30551544Seschrock 
30561544Seschrock 	spa->spa_scrub_stop = 0;
30571544Seschrock 	spa->spa_scrub_type = type;
30581544Seschrock 	spa->spa_scrub_restart_txg = 0;
30591544Seschrock 
30601544Seschrock 	if (type != POOL_SCRUB_NONE) {
30611544Seschrock 		spa->spa_scrub_mintxg = mintxg;
3062789Sahrens 		spa->spa_scrub_maxtxg = maxtxg;
3063789Sahrens 		spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL,
30641635Sbonwick 		    ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL,
30651635Sbonwick 		    ZIO_FLAG_CANFAIL);
3066789Sahrens 		traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg);
3067789Sahrens 		spa->spa_scrub_thread = thread_create(NULL, 0,
3068789Sahrens 		    spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri);
3069789Sahrens 	}
3070789Sahrens 
30711544Seschrock 	mutex_exit(&spa->spa_scrub_lock);
30721544Seschrock 
3073789Sahrens 	return (0);
3074789Sahrens }
3075789Sahrens 
30761544Seschrock /*
30771544Seschrock  * ==========================================================================
30781544Seschrock  * SPA async task processing
30791544Seschrock  * ==========================================================================
30801544Seschrock  */
30811544Seschrock 
30821544Seschrock static void
30834451Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3084789Sahrens {
30851544Seschrock 	vdev_t *tvd;
30861544Seschrock 	int c;
30871544Seschrock 
30884451Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
30894451Seschrock 		tvd = vd->vdev_child[c];
30904451Seschrock 		if (tvd->vdev_remove_wanted) {
30914451Seschrock 			tvd->vdev_remove_wanted = 0;
30924451Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
30934451Seschrock 			    VDEV_AUX_NONE);
30944451Seschrock 			vdev_clear(spa, tvd);
30954451Seschrock 			vdev_config_dirty(tvd->vdev_top);
30961544Seschrock 		}
30974451Seschrock 		spa_async_remove(spa, tvd);
30981544Seschrock 	}
30991544Seschrock }
31001544Seschrock 
31011544Seschrock static void
31021544Seschrock spa_async_thread(spa_t *spa)
31031544Seschrock {
31041544Seschrock 	int tasks;
31054451Seschrock 	uint64_t txg;
31061544Seschrock 
31071544Seschrock 	ASSERT(spa->spa_sync_on);
3108789Sahrens 
31091544Seschrock 	mutex_enter(&spa->spa_async_lock);
31101544Seschrock 	tasks = spa->spa_async_tasks;
31111544Seschrock 	spa->spa_async_tasks = 0;
31121544Seschrock 	mutex_exit(&spa->spa_async_lock);
31131544Seschrock 
31141544Seschrock 	/*
31151635Sbonwick 	 * See if the config needs to be updated.
31161635Sbonwick 	 */
31171635Sbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
31181635Sbonwick 		mutex_enter(&spa_namespace_lock);
31191635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
31201635Sbonwick 		mutex_exit(&spa_namespace_lock);
31211635Sbonwick 	}
31221635Sbonwick 
31231635Sbonwick 	/*
31244451Seschrock 	 * See if any devices need to be marked REMOVED.
31251544Seschrock 	 */
31264451Seschrock 	if (tasks & SPA_ASYNC_REMOVE) {
31274451Seschrock 		txg = spa_vdev_enter(spa);
31284451Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
31294451Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
31304451Seschrock 	}
31311544Seschrock 
31321544Seschrock 	/*
31331544Seschrock 	 * If any devices are done replacing, detach them.
31341544Seschrock 	 */
31354451Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
31364451Seschrock 		spa_vdev_resilver_done(spa);
3137789Sahrens 
31381544Seschrock 	/*
31394451Seschrock 	 * Kick off a scrub.  When starting a RESILVER scrub (or an EVERYTHING
31404451Seschrock 	 * scrub which can become a resilver), we need to hold
31414451Seschrock 	 * spa_namespace_lock() because the sysevent we post via
31424451Seschrock 	 * spa_event_notify() needs to get the name of the pool.
31431544Seschrock 	 */
31444451Seschrock 	if (tasks & SPA_ASYNC_SCRUB) {
31454451Seschrock 		mutex_enter(&spa_namespace_lock);
31461544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0);
31474451Seschrock 		mutex_exit(&spa_namespace_lock);
31484451Seschrock 	}
31491544Seschrock 
31501544Seschrock 	/*
31511544Seschrock 	 * Kick off a resilver.
31521544Seschrock 	 */
31534451Seschrock 	if (tasks & SPA_ASYNC_RESILVER) {
31544451Seschrock 		mutex_enter(&spa_namespace_lock);
31551544Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
31564451Seschrock 		mutex_exit(&spa_namespace_lock);
31574451Seschrock 	}
31581544Seschrock 
31591544Seschrock 	/*
31601544Seschrock 	 * Let the world know that we're done.
31611544Seschrock 	 */
31621544Seschrock 	mutex_enter(&spa->spa_async_lock);
31631544Seschrock 	spa->spa_async_thread = NULL;
31641544Seschrock 	cv_broadcast(&spa->spa_async_cv);
31651544Seschrock 	mutex_exit(&spa->spa_async_lock);
31661544Seschrock 	thread_exit();
31671544Seschrock }
31681544Seschrock 
31691544Seschrock void
31701544Seschrock spa_async_suspend(spa_t *spa)
31711544Seschrock {
31721544Seschrock 	mutex_enter(&spa->spa_async_lock);
31731544Seschrock 	spa->spa_async_suspended++;
31741544Seschrock 	while (spa->spa_async_thread != NULL)
31751544Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
31761544Seschrock 	mutex_exit(&spa->spa_async_lock);
31771544Seschrock }
31781544Seschrock 
31791544Seschrock void
31801544Seschrock spa_async_resume(spa_t *spa)
31811544Seschrock {
31821544Seschrock 	mutex_enter(&spa->spa_async_lock);
31831544Seschrock 	ASSERT(spa->spa_async_suspended != 0);
31841544Seschrock 	spa->spa_async_suspended--;
31851544Seschrock 	mutex_exit(&spa->spa_async_lock);
31861544Seschrock }
31871544Seschrock 
31881544Seschrock static void
31891544Seschrock spa_async_dispatch(spa_t *spa)
31901544Seschrock {
31911544Seschrock 	mutex_enter(&spa->spa_async_lock);
31921544Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
31931635Sbonwick 	    spa->spa_async_thread == NULL &&
31941635Sbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
31951544Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
31961544Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
31971544Seschrock 	mutex_exit(&spa->spa_async_lock);
31981544Seschrock }
31991544Seschrock 
32001544Seschrock void
32011544Seschrock spa_async_request(spa_t *spa, int task)
32021544Seschrock {
32031544Seschrock 	mutex_enter(&spa->spa_async_lock);
32041544Seschrock 	spa->spa_async_tasks |= task;
32051544Seschrock 	mutex_exit(&spa->spa_async_lock);
3206789Sahrens }
3207789Sahrens 
3208789Sahrens /*
3209789Sahrens  * ==========================================================================
3210789Sahrens  * SPA syncing routines
3211789Sahrens  * ==========================================================================
3212789Sahrens  */
3213789Sahrens 
3214789Sahrens static void
3215789Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3216789Sahrens {
3217789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3218789Sahrens 	dmu_tx_t *tx;
3219789Sahrens 	blkptr_t blk;
3220789Sahrens 	uint64_t itor = 0;
3221789Sahrens 	zio_t *zio;
3222789Sahrens 	int error;
3223789Sahrens 	uint8_t c = 1;
3224789Sahrens 
3225789Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3226789Sahrens 
3227789Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3228789Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3229789Sahrens 
3230789Sahrens 	error = zio_wait(zio);
3231789Sahrens 	ASSERT3U(error, ==, 0);
3232789Sahrens 
3233789Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3234789Sahrens 	bplist_vacate(bpl, tx);
3235789Sahrens 
3236789Sahrens 	/*
3237789Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3238789Sahrens 	 * (Usually only the first block is needed.)
3239789Sahrens 	 */
3240789Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3241789Sahrens 	dmu_tx_commit(tx);
3242789Sahrens }
3243789Sahrens 
3244789Sahrens static void
32452082Seschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
32462082Seschrock {
32472082Seschrock 	char *packed = NULL;
32482082Seschrock 	size_t nvsize = 0;
32492082Seschrock 	dmu_buf_t *db;
32502082Seschrock 
32512082Seschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
32522082Seschrock 
32532082Seschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
32542082Seschrock 
32552082Seschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
32562082Seschrock 	    KM_SLEEP) == 0);
32572082Seschrock 
32582082Seschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
32592082Seschrock 
32602082Seschrock 	kmem_free(packed, nvsize);
32612082Seschrock 
32622082Seschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
32632082Seschrock 	dmu_buf_will_dirty(db, tx);
32642082Seschrock 	*(uint64_t *)db->db_data = nvsize;
32652082Seschrock 	dmu_buf_rele(db, FTAG);
32662082Seschrock }
32672082Seschrock 
32682082Seschrock static void
32692082Seschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx)
32702082Seschrock {
32712082Seschrock 	nvlist_t *nvroot;
32722082Seschrock 	nvlist_t **spares;
32732082Seschrock 	int i;
32742082Seschrock 
32752082Seschrock 	if (!spa->spa_sync_spares)
32762082Seschrock 		return;
32772082Seschrock 
32782082Seschrock 	/*
32792082Seschrock 	 * Update the MOS nvlist describing the list of available spares.
32802082Seschrock 	 * spa_validate_spares() will have already made sure this nvlist is
32814451Seschrock 	 * valid and the vdevs are labeled appropriately.
32822082Seschrock 	 */
32832082Seschrock 	if (spa->spa_spares_object == 0) {
32842082Seschrock 		spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset,
32852082Seschrock 		    DMU_OT_PACKED_NVLIST, 1 << 14,
32862082Seschrock 		    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
32872082Seschrock 		VERIFY(zap_update(spa->spa_meta_objset,
32882082Seschrock 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES,
32892082Seschrock 		    sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0);
32902082Seschrock 	}
32912082Seschrock 
32922082Seschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
32932082Seschrock 	if (spa->spa_nspares == 0) {
32942082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
32952082Seschrock 		    NULL, 0) == 0);
32962082Seschrock 	} else {
32972082Seschrock 		spares = kmem_alloc(spa->spa_nspares * sizeof (void *),
32982082Seschrock 		    KM_SLEEP);
32992082Seschrock 		for (i = 0; i < spa->spa_nspares; i++)
33002082Seschrock 			spares[i] = vdev_config_generate(spa,
33012082Seschrock 			    spa->spa_spares[i], B_FALSE, B_TRUE);
33022082Seschrock 		VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
33032082Seschrock 		    spares, spa->spa_nspares) == 0);
33042082Seschrock 		for (i = 0; i < spa->spa_nspares; i++)
33052082Seschrock 			nvlist_free(spares[i]);
33062082Seschrock 		kmem_free(spares, spa->spa_nspares * sizeof (void *));
33072082Seschrock 	}
33082082Seschrock 
33092082Seschrock 	spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx);
33102926Sek110237 	nvlist_free(nvroot);
33112082Seschrock 
33122082Seschrock 	spa->spa_sync_spares = B_FALSE;
33132082Seschrock }
33142082Seschrock 
33152082Seschrock static void
3316789Sahrens spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3317789Sahrens {
3318789Sahrens 	nvlist_t *config;
3319789Sahrens 
3320789Sahrens 	if (list_is_empty(&spa->spa_dirty_list))
3321789Sahrens 		return;
3322789Sahrens 
3323789Sahrens 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
3324789Sahrens 
33251635Sbonwick 	if (spa->spa_config_syncing)
33261635Sbonwick 		nvlist_free(spa->spa_config_syncing);
33271635Sbonwick 	spa->spa_config_syncing = config;
3328789Sahrens 
33292082Seschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3330789Sahrens }
3331789Sahrens 
3332*5094Slling /*
3333*5094Slling  * Set zpool properties.
3334*5094Slling  */
33353912Slling static void
33364543Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
33373912Slling {
33383912Slling 	spa_t *spa = arg1;
3339*5094Slling 	objset_t *mos = spa->spa_meta_objset;
33403912Slling 	nvlist_t *nvp = arg2;
3341*5094Slling 	nvpair_t *elem;
33424451Seschrock 	uint64_t intval;
3343*5094Slling 	char *strval;
3344*5094Slling 	zpool_prop_t prop;
3345*5094Slling 	const char *propname;
3346*5094Slling 	zprop_type_t proptype;
3347*5094Slling 
3348*5094Slling 	elem = NULL;
3349*5094Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
3350*5094Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
3351*5094Slling 		case ZPOOL_PROP_VERSION:
3352*5094Slling 			/*
3353*5094Slling 			 * Only set version for non-zpool-creation cases
3354*5094Slling 			 * (set/import). spa_create() needs special care
3355*5094Slling 			 * for version setting.
3356*5094Slling 			 */
3357*5094Slling 			if (tx->tx_txg != TXG_INITIAL) {
3358*5094Slling 				VERIFY(nvpair_value_uint64(elem,
3359*5094Slling 				    &intval) == 0);
3360*5094Slling 				ASSERT(intval <= SPA_VERSION);
3361*5094Slling 				ASSERT(intval >= spa_version(spa));
3362*5094Slling 				spa->spa_uberblock.ub_version = intval;
3363*5094Slling 				vdev_config_dirty(spa->spa_root_vdev);
3364*5094Slling 			}
3365*5094Slling 			break;
3366*5094Slling 
3367*5094Slling 		case ZPOOL_PROP_ALTROOT:
3368*5094Slling 			/*
3369*5094Slling 			 * 'altroot' is a non-persistent property. It should
3370*5094Slling 			 * have been set temporarily at creation or import time.
3371*5094Slling 			 */
3372*5094Slling 			ASSERT(spa->spa_root != NULL);
3373*5094Slling 			break;
3374*5094Slling 
3375*5094Slling 		case ZPOOL_PROP_TEMPORARY:
3376*5094Slling 			/*
3377*5094Slling 			 * 'temporary' is a non-persistant property.
3378*5094Slling 			 */
3379*5094Slling 			VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3380*5094Slling 			spa->spa_temporary = intval;
33814543Smarks 			break;
3382*5094Slling 
3383*5094Slling 		default:
3384*5094Slling 			/*
3385*5094Slling 			 * Set pool property values in the poolprops mos object.
3386*5094Slling 			 */
3387*5094Slling 			mutex_enter(&spa->spa_props_lock);
3388*5094Slling 			if (spa->spa_pool_props_object == 0) {
3389*5094Slling 				objset_t *mos = spa->spa_meta_objset;
3390*5094Slling 
3391*5094Slling 				VERIFY((spa->spa_pool_props_object =
3392*5094Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
3393*5094Slling 				    DMU_OT_NONE, 0, tx)) > 0);
3394*5094Slling 
3395*5094Slling 				VERIFY(zap_update(mos,
3396*5094Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
3397*5094Slling 				    8, 1, &spa->spa_pool_props_object, tx)
3398*5094Slling 				    == 0);
3399*5094Slling 			}
3400*5094Slling 			mutex_exit(&spa->spa_props_lock);
3401*5094Slling 
3402*5094Slling 			/* normalize the property name */
3403*5094Slling 			propname = zpool_prop_to_name(prop);
3404*5094Slling 			proptype = zpool_prop_get_type(prop);
3405*5094Slling 
3406*5094Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
3407*5094Slling 				ASSERT(proptype == PROP_TYPE_STRING);
3408*5094Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
3409*5094Slling 				VERIFY(zap_update(mos,
3410*5094Slling 				    spa->spa_pool_props_object, propname,
3411*5094Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
3412*5094Slling 
3413*5094Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
3414*5094Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3415*5094Slling 
3416*5094Slling 				if (proptype == PROP_TYPE_INDEX) {
3417*5094Slling 					const char *unused;
3418*5094Slling 					VERIFY(zpool_prop_index_to_string(
3419*5094Slling 					    prop, intval, &unused) == 0);
3420*5094Slling 				}
3421*5094Slling 				VERIFY(zap_update(mos,
3422*5094Slling 				    spa->spa_pool_props_object, propname,
3423*5094Slling 				    8, 1, &intval, tx) == 0);
3424*5094Slling 			} else {
3425*5094Slling 				ASSERT(0); /* not allowed */
3426*5094Slling 			}
3427*5094Slling 
3428*5094Slling 			if (prop ==  ZPOOL_PROP_DELEGATION)
3429*5094Slling 				spa->spa_delegation = intval;
3430*5094Slling 
3431*5094Slling 			if (prop == ZPOOL_PROP_BOOTFS)
3432*5094Slling 				spa->spa_bootfs = intval;
34333912Slling 		}
3434*5094Slling 
3435*5094Slling 		/* log internal history if this is not a zpool create */
3436*5094Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
3437*5094Slling 		    tx->tx_txg != TXG_INITIAL) {
3438*5094Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
3439*5094Slling 			    spa, tx, cr, "%s %lld %s",
3440*5094Slling 			    nvpair_name(elem), intval, spa->spa_name);
3441*5094Slling 		}
34423912Slling 	}
34433912Slling }
34443912Slling 
3445789Sahrens /*
3446789Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3447789Sahrens  * part of the process, so we iterate until it converges.
3448789Sahrens  */
3449789Sahrens void
3450789Sahrens spa_sync(spa_t *spa, uint64_t txg)
3451789Sahrens {
3452789Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3453789Sahrens 	objset_t *mos = spa->spa_meta_objset;
3454789Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
34551635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3456789Sahrens 	vdev_t *vd;
3457789Sahrens 	dmu_tx_t *tx;
3458789Sahrens 	int dirty_vdevs;
3459789Sahrens 
3460789Sahrens 	/*
3461789Sahrens 	 * Lock out configuration changes.
3462789Sahrens 	 */
34631544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3464789Sahrens 
3465789Sahrens 	spa->spa_syncing_txg = txg;
3466789Sahrens 	spa->spa_sync_pass = 0;
3467789Sahrens 
34681544Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
3469789Sahrens 
34702082Seschrock 	tx = dmu_tx_create_assigned(dp, txg);
34712082Seschrock 
34722082Seschrock 	/*
34734577Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
34742082Seschrock 	 * set spa_deflate if we have no raid-z vdevs.
34752082Seschrock 	 */
34764577Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
34774577Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
34782082Seschrock 		int i;
34792082Seschrock 
34802082Seschrock 		for (i = 0; i < rvd->vdev_children; i++) {
34812082Seschrock 			vd = rvd->vdev_child[i];
34822082Seschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
34832082Seschrock 				break;
34842082Seschrock 		}
34852082Seschrock 		if (i == rvd->vdev_children) {
34862082Seschrock 			spa->spa_deflate = TRUE;
34872082Seschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
34882082Seschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
34892082Seschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
34902082Seschrock 		}
34912082Seschrock 	}
34922082Seschrock 
3493789Sahrens 	/*
3494789Sahrens 	 * If anything has changed in this txg, push the deferred frees
3495789Sahrens 	 * from the previous txg.  If not, leave them alone so that we
3496789Sahrens 	 * don't generate work on an otherwise idle system.
3497789Sahrens 	 */
3498789Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
34992329Sek110237 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
35002329Sek110237 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
3501789Sahrens 		spa_sync_deferred_frees(spa, txg);
3502789Sahrens 
3503789Sahrens 	/*
3504789Sahrens 	 * Iterate to convergence.
3505789Sahrens 	 */
3506789Sahrens 	do {
3507789Sahrens 		spa->spa_sync_pass++;
3508789Sahrens 
3509789Sahrens 		spa_sync_config_object(spa, tx);
35102082Seschrock 		spa_sync_spares(spa, tx);
35111544Seschrock 		spa_errlog_sync(spa, txg);
3512789Sahrens 		dsl_pool_sync(dp, txg);
3513789Sahrens 
3514789Sahrens 		dirty_vdevs = 0;
3515789Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
3516789Sahrens 			vdev_sync(vd, txg);
3517789Sahrens 			dirty_vdevs++;
3518789Sahrens 		}
3519789Sahrens 
3520789Sahrens 		bplist_sync(bpl, tx);
3521789Sahrens 	} while (dirty_vdevs);
3522789Sahrens 
3523789Sahrens 	bplist_close(bpl);
3524789Sahrens 
3525789Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
3526789Sahrens 
3527789Sahrens 	/*
3528789Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
3529789Sahrens 	 * to commit the transaction group.
35301635Sbonwick 	 *
35311635Sbonwick 	 * If there are any dirty vdevs, sync the uberblock to all vdevs.
35321635Sbonwick 	 * Otherwise, pick a random top-level vdev that's known to be
35331635Sbonwick 	 * visible in the config cache (see spa_vdev_add() for details).
35341635Sbonwick 	 * If the write fails, try the next vdev until we're tried them all.
3535789Sahrens 	 */
35361635Sbonwick 	if (!list_is_empty(&spa->spa_dirty_list)) {
35371635Sbonwick 		VERIFY(vdev_config_sync(rvd, txg) == 0);
35381635Sbonwick 	} else {
35391635Sbonwick 		int children = rvd->vdev_children;
35401635Sbonwick 		int c0 = spa_get_random(children);
35411635Sbonwick 		int c;
35421635Sbonwick 
35431635Sbonwick 		for (c = 0; c < children; c++) {
35441635Sbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
35451635Sbonwick 			if (vd->vdev_ms_array == 0)
35461635Sbonwick 				continue;
35471635Sbonwick 			if (vdev_config_sync(vd, txg) == 0)
35481635Sbonwick 				break;
35491635Sbonwick 		}
35501635Sbonwick 		if (c == children)
35511635Sbonwick 			VERIFY(vdev_config_sync(rvd, txg) == 0);
35521635Sbonwick 	}
35531635Sbonwick 
35542082Seschrock 	dmu_tx_commit(tx);
35552082Seschrock 
35561635Sbonwick 	/*
35571635Sbonwick 	 * Clear the dirty config list.
35581635Sbonwick 	 */
35591635Sbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
35601635Sbonwick 		vdev_config_clean(vd);
35611635Sbonwick 
35621635Sbonwick 	/*
35631635Sbonwick 	 * Now that the new config has synced transactionally,
35641635Sbonwick 	 * let it become visible to the config cache.
35651635Sbonwick 	 */
35661635Sbonwick 	if (spa->spa_config_syncing != NULL) {
35671635Sbonwick 		spa_config_set(spa, spa->spa_config_syncing);
35681635Sbonwick 		spa->spa_config_txg = txg;
35691635Sbonwick 		spa->spa_config_syncing = NULL;
35701635Sbonwick 	}
3571789Sahrens 
3572789Sahrens 	/*
3573789Sahrens 	 * Make a stable copy of the fully synced uberblock.
3574789Sahrens 	 * We use this as the root for pool traversals.
3575789Sahrens 	 */
3576789Sahrens 	spa->spa_traverse_wanted = 1;	/* tells traverse_more() to stop */
3577789Sahrens 
3578789Sahrens 	spa_scrub_suspend(spa);		/* stop scrubbing and finish I/Os */
3579789Sahrens 
3580789Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
3581789Sahrens 	spa->spa_traverse_wanted = 0;
3582789Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
3583789Sahrens 	rw_exit(&spa->spa_traverse_lock);
3584789Sahrens 
3585789Sahrens 	spa_scrub_resume(spa);		/* resume scrub with new ubsync */
3586789Sahrens 
3587789Sahrens 	/*
3588789Sahrens 	 * Clean up the ZIL records for the synced txg.
3589789Sahrens 	 */
3590789Sahrens 	dsl_pool_zil_clean(dp);
3591789Sahrens 
3592789Sahrens 	/*
3593789Sahrens 	 * Update usable space statistics.
3594789Sahrens 	 */
3595789Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
3596789Sahrens 		vdev_sync_done(vd, txg);
3597789Sahrens 
3598789Sahrens 	/*
3599789Sahrens 	 * It had better be the case that we didn't dirty anything
36002082Seschrock 	 * since vdev_config_sync().
3601789Sahrens 	 */
3602789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
3603789Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
3604789Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
3605789Sahrens 	ASSERT(bpl->bpl_queue == NULL);
3606789Sahrens 
36071544Seschrock 	spa_config_exit(spa, FTAG);
36081544Seschrock 
36091544Seschrock 	/*
36101544Seschrock 	 * If any async tasks have been requested, kick them off.
36111544Seschrock 	 */
36121544Seschrock 	spa_async_dispatch(spa);
3613789Sahrens }
3614789Sahrens 
3615789Sahrens /*
3616789Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
3617789Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
3618789Sahrens  * sync.
3619789Sahrens  */
3620789Sahrens void
3621789Sahrens spa_sync_allpools(void)
3622789Sahrens {
3623789Sahrens 	spa_t *spa = NULL;
3624789Sahrens 	mutex_enter(&spa_namespace_lock);
3625789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
3626789Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
3627789Sahrens 			continue;
3628789Sahrens 		spa_open_ref(spa, FTAG);
3629789Sahrens 		mutex_exit(&spa_namespace_lock);
3630789Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
3631789Sahrens 		mutex_enter(&spa_namespace_lock);
3632789Sahrens 		spa_close(spa, FTAG);
3633789Sahrens 	}
3634789Sahrens 	mutex_exit(&spa_namespace_lock);
3635789Sahrens }
3636789Sahrens 
3637789Sahrens /*
3638789Sahrens  * ==========================================================================
3639789Sahrens  * Miscellaneous routines
3640789Sahrens  * ==========================================================================
3641789Sahrens  */
3642789Sahrens 
3643789Sahrens /*
3644789Sahrens  * Remove all pools in the system.
3645789Sahrens  */
3646789Sahrens void
3647789Sahrens spa_evict_all(void)
3648789Sahrens {
3649789Sahrens 	spa_t *spa;
3650789Sahrens 
3651789Sahrens 	/*
3652789Sahrens 	 * Remove all cached state.  All pools should be closed now,
3653789Sahrens 	 * so every spa in the AVL tree should be unreferenced.
3654789Sahrens 	 */
3655789Sahrens 	mutex_enter(&spa_namespace_lock);
3656789Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
3657789Sahrens 		/*
36581544Seschrock 		 * Stop async tasks.  The async thread may need to detach
36591544Seschrock 		 * a device that's been replaced, which requires grabbing
36601544Seschrock 		 * spa_namespace_lock, so we must drop it here.
3661789Sahrens 		 */
3662789Sahrens 		spa_open_ref(spa, FTAG);
3663789Sahrens 		mutex_exit(&spa_namespace_lock);
36641544Seschrock 		spa_async_suspend(spa);
36654808Sek110237 		mutex_enter(&spa_namespace_lock);
3666789Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
3667789Sahrens 		spa_close(spa, FTAG);
3668789Sahrens 
3669789Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3670789Sahrens 			spa_unload(spa);
3671789Sahrens 			spa_deactivate(spa);
3672789Sahrens 		}
3673789Sahrens 		spa_remove(spa);
3674789Sahrens 	}
3675789Sahrens 	mutex_exit(&spa_namespace_lock);
3676789Sahrens }
36771544Seschrock 
36781544Seschrock vdev_t *
36791544Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid)
36801544Seschrock {
36811544Seschrock 	return (vdev_lookup_by_guid(spa->spa_root_vdev, guid));
36821544Seschrock }
36831760Seschrock 
36841760Seschrock void
3685*5094Slling spa_upgrade(spa_t *spa, uint64_t version)
36861760Seschrock {
36871760Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
36881760Seschrock 
36891760Seschrock 	/*
36901760Seschrock 	 * This should only be called for a non-faulted pool, and since a
36911760Seschrock 	 * future version would result in an unopenable pool, this shouldn't be
36921760Seschrock 	 * possible.
36931760Seschrock 	 */
36944577Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
3695*5094Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
3696*5094Slling 
3697*5094Slling 	spa->spa_uberblock.ub_version = version;
36981760Seschrock 	vdev_config_dirty(spa->spa_root_vdev);
36991760Seschrock 
37001760Seschrock 	spa_config_exit(spa, FTAG);
37012082Seschrock 
37022082Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
37031760Seschrock }
37042082Seschrock 
37052082Seschrock boolean_t
37062082Seschrock spa_has_spare(spa_t *spa, uint64_t guid)
37072082Seschrock {
37082082Seschrock 	int i;
37093377Seschrock 	uint64_t spareguid;
37102082Seschrock 
37112082Seschrock 	for (i = 0; i < spa->spa_nspares; i++)
37122082Seschrock 		if (spa->spa_spares[i]->vdev_guid == guid)
37132082Seschrock 			return (B_TRUE);
37142082Seschrock 
37153377Seschrock 	for (i = 0; i < spa->spa_pending_nspares; i++) {
37163377Seschrock 		if (nvlist_lookup_uint64(spa->spa_pending_spares[i],
37173377Seschrock 		    ZPOOL_CONFIG_GUID, &spareguid) == 0 &&
37183377Seschrock 		    spareguid == guid)
37193377Seschrock 			return (B_TRUE);
37203377Seschrock 	}
37213377Seschrock 
37222082Seschrock 	return (B_FALSE);
37232082Seschrock }
37243912Slling 
37254451Seschrock /*
37264451Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
37274451Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
37284451Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
37294451Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
37304451Seschrock  * or zdb as real changes.
37314451Seschrock  */
37324451Seschrock void
37334451Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
37344451Seschrock {
37354451Seschrock #ifdef _KERNEL
37364451Seschrock 	sysevent_t		*ev;
37374451Seschrock 	sysevent_attr_list_t	*attr = NULL;
37384451Seschrock 	sysevent_value_t	value;
37394451Seschrock 	sysevent_id_t		eid;
37404451Seschrock 
37414451Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
37424451Seschrock 	    SE_SLEEP);
37434451Seschrock 
37444451Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
37454451Seschrock 	value.value.sv_string = spa_name(spa);
37464451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
37474451Seschrock 		goto done;
37484451Seschrock 
37494451Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
37504451Seschrock 	value.value.sv_uint64 = spa_guid(spa);
37514451Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
37524451Seschrock 		goto done;
37534451Seschrock 
37544451Seschrock 	if (vd) {
37554451Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
37564451Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
37574451Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
37584451Seschrock 		    SE_SLEEP) != 0)
37594451Seschrock 			goto done;
37604451Seschrock 
37614451Seschrock 		if (vd->vdev_path) {
37624451Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
37634451Seschrock 			value.value.sv_string = vd->vdev_path;
37644451Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
37654451Seschrock 			    &value, SE_SLEEP) != 0)
37664451Seschrock 				goto done;
37674451Seschrock 		}
37684451Seschrock 	}
37694451Seschrock 
37704451Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
37714451Seschrock 
37724451Seschrock done:
37734451Seschrock 	if (attr)
37744451Seschrock 		sysevent_free_attr(attr);
37754451Seschrock 	sysevent_free(ev);
37764451Seschrock #endif
37774451Seschrock }
3778