xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa.c (revision 0a4e9518a44f226be6d39383330b5b1792d2f184)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
2339c23413Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * This file contains all the routines used when modifying on-disk SPA state.
31fa9e4066Sahrens  * This includes opening, importing, destroying, exporting a pool, and syncing a
32fa9e4066Sahrens  * pool.
33fa9e4066Sahrens  */
34fa9e4066Sahrens 
35fa9e4066Sahrens #include <sys/zfs_context.h>
36ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
37fa9e4066Sahrens #include <sys/spa_impl.h>
38fa9e4066Sahrens #include <sys/zio.h>
39fa9e4066Sahrens #include <sys/zio_checksum.h>
40fa9e4066Sahrens #include <sys/zio_compress.h>
41fa9e4066Sahrens #include <sys/dmu.h>
42fa9e4066Sahrens #include <sys/dmu_tx.h>
43fa9e4066Sahrens #include <sys/zap.h>
44fa9e4066Sahrens #include <sys/zil.h>
45fa9e4066Sahrens #include <sys/vdev_impl.h>
46fa9e4066Sahrens #include <sys/metaslab.h>
47fa9e4066Sahrens #include <sys/uberblock_impl.h>
48fa9e4066Sahrens #include <sys/txg.h>
49fa9e4066Sahrens #include <sys/avl.h>
50fa9e4066Sahrens #include <sys/dmu_traverse.h>
51b1b8ab34Slling #include <sys/dmu_objset.h>
52fa9e4066Sahrens #include <sys/unique.h>
53fa9e4066Sahrens #include <sys/dsl_pool.h>
54b1b8ab34Slling #include <sys/dsl_dataset.h>
55fa9e4066Sahrens #include <sys/dsl_dir.h>
56fa9e4066Sahrens #include <sys/dsl_prop.h>
57b1b8ab34Slling #include <sys/dsl_synctask.h>
58fa9e4066Sahrens #include <sys/fs/zfs.h>
59fa9e4066Sahrens #include <sys/callb.h>
6095173954Sek #include <sys/systeminfo.h>
6195173954Sek #include <sys/sunddi.h>
62fa9e4066Sahrens 
63990b4856Slling #include "zfs_prop.h"
64990b4856Slling 
65416e0cd8Sek int zio_taskq_threads = 8;
66416e0cd8Sek 
67990b4856Slling static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
68990b4856Slling 
69990b4856Slling /*
70990b4856Slling  * ==========================================================================
71990b4856Slling  * SPA properties routines
72990b4856Slling  * ==========================================================================
73990b4856Slling  */
74990b4856Slling 
75990b4856Slling /*
76990b4856Slling  * Add a (source=src, propname=propval) list to an nvlist.
77990b4856Slling  */
78990b4856Slling static int
79990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
80990b4856Slling     uint64_t intval, zprop_source_t src)
81990b4856Slling {
82990b4856Slling 	const char *propname = zpool_prop_to_name(prop);
83990b4856Slling 	nvlist_t *propval;
84990b4856Slling 	int err = 0;
85990b4856Slling 
86990b4856Slling 	if (err = nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP))
87990b4856Slling 		return (err);
88990b4856Slling 
89990b4856Slling 	if (err = nvlist_add_uint64(propval, ZPROP_SOURCE, src))
90990b4856Slling 		goto out;
91990b4856Slling 
92990b4856Slling 	if (strval != NULL) {
93990b4856Slling 		if (err = nvlist_add_string(propval, ZPROP_VALUE, strval))
94990b4856Slling 			goto out;
95990b4856Slling 	} else {
96990b4856Slling 		if (err = nvlist_add_uint64(propval, ZPROP_VALUE, intval))
97990b4856Slling 			goto out;
98990b4856Slling 	}
99990b4856Slling 
100990b4856Slling 	err = nvlist_add_nvlist(nvl, propname, propval);
101990b4856Slling out:
102990b4856Slling 	nvlist_free(propval);
103990b4856Slling 	return (err);
104990b4856Slling }
105990b4856Slling 
106990b4856Slling /*
107990b4856Slling  * Get property values from the spa configuration.
108990b4856Slling  */
109990b4856Slling static int
110990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
111990b4856Slling {
112990b4856Slling 	uint64_t size = spa_get_space(spa);
113990b4856Slling 	uint64_t used = spa_get_alloc(spa);
114990b4856Slling 	uint64_t cap, version;
115990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
116990b4856Slling 	int err;
117990b4856Slling 
118990b4856Slling 	/*
119990b4856Slling 	 * readonly properties
120990b4856Slling 	 */
121990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name,
122990b4856Slling 	    0, src))
123990b4856Slling 		return (err);
124990b4856Slling 
125990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src))
126990b4856Slling 		return (err);
127990b4856Slling 
128990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src))
129990b4856Slling 		return (err);
130990b4856Slling 
131990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
132990b4856Slling 	    size - used, src))
133990b4856Slling 		return (err);
134990b4856Slling 
135990b4856Slling 	cap = (size == 0) ? 0 : (used * 100 / size);
136990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src))
137990b4856Slling 		return (err);
138990b4856Slling 
139990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL,
140990b4856Slling 	    spa_guid(spa), src))
141990b4856Slling 		return (err);
142990b4856Slling 
143990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
144990b4856Slling 	    spa->spa_root_vdev->vdev_state, src))
145990b4856Slling 		return (err);
146990b4856Slling 
147990b4856Slling 	/*
148990b4856Slling 	 * settable properties that are not stored in the pool property object.
149990b4856Slling 	 */
150990b4856Slling 	version = spa_version(spa);
151990b4856Slling 	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
152990b4856Slling 		src = ZPROP_SRC_DEFAULT;
153990b4856Slling 	else
154990b4856Slling 		src = ZPROP_SRC_LOCAL;
155990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
156990b4856Slling 	    version, src))
157990b4856Slling 		return (err);
158990b4856Slling 
159990b4856Slling 	if (spa->spa_root != NULL) {
160990b4856Slling 		src = ZPROP_SRC_LOCAL;
161990b4856Slling 		if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT,
162990b4856Slling 		    spa->spa_root, 0, src))
163990b4856Slling 			return (err);
164990b4856Slling 	}
165990b4856Slling 
166990b4856Slling 	if (spa->spa_temporary ==
167990b4856Slling 	    zpool_prop_default_numeric(ZPOOL_PROP_TEMPORARY))
168990b4856Slling 		src = ZPROP_SRC_DEFAULT;
169990b4856Slling 	else
170990b4856Slling 		src = ZPROP_SRC_LOCAL;
171990b4856Slling 	if (err = spa_prop_add_list(*nvp, ZPOOL_PROP_TEMPORARY, NULL,
172990b4856Slling 	    spa->spa_temporary, src))
173990b4856Slling 		return (err);
174990b4856Slling 
175990b4856Slling 	return (0);
176990b4856Slling }
177990b4856Slling 
178990b4856Slling /*
179990b4856Slling  * Get zpool property values.
180990b4856Slling  */
181990b4856Slling int
182990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
183990b4856Slling {
184990b4856Slling 	zap_cursor_t zc;
185990b4856Slling 	zap_attribute_t za;
186990b4856Slling 	objset_t *mos = spa->spa_meta_objset;
187990b4856Slling 	int err;
188990b4856Slling 
189990b4856Slling 	if (err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP))
190990b4856Slling 		return (err);
191990b4856Slling 
192990b4856Slling 	/*
193990b4856Slling 	 * Get properties from the spa config.
194990b4856Slling 	 */
195990b4856Slling 	if (err = spa_prop_get_config(spa, nvp))
196990b4856Slling 		goto out;
197990b4856Slling 
198990b4856Slling 	mutex_enter(&spa->spa_props_lock);
199990b4856Slling 	/* If no pool property object, no more prop to get. */
200990b4856Slling 	if (spa->spa_pool_props_object == 0) {
201990b4856Slling 		mutex_exit(&spa->spa_props_lock);
202990b4856Slling 		return (0);
203990b4856Slling 	}
204990b4856Slling 
205990b4856Slling 	/*
206990b4856Slling 	 * Get properties from the MOS pool property object.
207990b4856Slling 	 */
208990b4856Slling 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
209990b4856Slling 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
210990b4856Slling 	    zap_cursor_advance(&zc)) {
211990b4856Slling 		uint64_t intval = 0;
212990b4856Slling 		char *strval = NULL;
213990b4856Slling 		zprop_source_t src = ZPROP_SRC_DEFAULT;
214990b4856Slling 		zpool_prop_t prop;
215990b4856Slling 
216990b4856Slling 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
217990b4856Slling 			continue;
218990b4856Slling 
219990b4856Slling 		switch (za.za_integer_length) {
220990b4856Slling 		case 8:
221990b4856Slling 			/* integer property */
222990b4856Slling 			if (za.za_first_integer !=
223990b4856Slling 			    zpool_prop_default_numeric(prop))
224990b4856Slling 				src = ZPROP_SRC_LOCAL;
225990b4856Slling 
226990b4856Slling 			if (prop == ZPOOL_PROP_BOOTFS) {
227990b4856Slling 				dsl_pool_t *dp;
228990b4856Slling 				dsl_dataset_t *ds = NULL;
229990b4856Slling 
230990b4856Slling 				dp = spa_get_dsl(spa);
231990b4856Slling 				rw_enter(&dp->dp_config_rwlock, RW_READER);
232990b4856Slling 				if (err = dsl_dataset_open_obj(dp,
233990b4856Slling 				    za.za_first_integer, NULL, DS_MODE_NONE,
234990b4856Slling 				    FTAG, &ds)) {
235990b4856Slling 					rw_exit(&dp->dp_config_rwlock);
236990b4856Slling 					break;
237990b4856Slling 				}
238990b4856Slling 
239990b4856Slling 				strval = kmem_alloc(
240990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
241990b4856Slling 				    KM_SLEEP);
242990b4856Slling 				dsl_dataset_name(ds, strval);
243990b4856Slling 				dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
244990b4856Slling 				rw_exit(&dp->dp_config_rwlock);
245990b4856Slling 			} else {
246990b4856Slling 				strval = NULL;
247990b4856Slling 				intval = za.za_first_integer;
248990b4856Slling 			}
249990b4856Slling 
250990b4856Slling 			err = spa_prop_add_list(*nvp, prop, strval,
251990b4856Slling 			    intval, src);
252990b4856Slling 
253990b4856Slling 			if (strval != NULL)
254990b4856Slling 				kmem_free(strval,
255990b4856Slling 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
256990b4856Slling 
257990b4856Slling 			break;
258990b4856Slling 
259990b4856Slling 		case 1:
260990b4856Slling 			/* string property */
261990b4856Slling 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
262990b4856Slling 			err = zap_lookup(mos, spa->spa_pool_props_object,
263990b4856Slling 			    za.za_name, 1, za.za_num_integers, strval);
264990b4856Slling 			if (err) {
265990b4856Slling 				kmem_free(strval, za.za_num_integers);
266990b4856Slling 				break;
267990b4856Slling 			}
268990b4856Slling 			err = spa_prop_add_list(*nvp, prop, strval, 0, src);
269990b4856Slling 			kmem_free(strval, za.za_num_integers);
270990b4856Slling 			break;
271990b4856Slling 
272990b4856Slling 		default:
273990b4856Slling 			break;
274990b4856Slling 		}
275990b4856Slling 	}
276990b4856Slling 	zap_cursor_fini(&zc);
277990b4856Slling 	mutex_exit(&spa->spa_props_lock);
278990b4856Slling out:
279990b4856Slling 	if (err && err != ENOENT) {
280990b4856Slling 		nvlist_free(*nvp);
281990b4856Slling 		return (err);
282990b4856Slling 	}
283990b4856Slling 
284990b4856Slling 	return (0);
285990b4856Slling }
286990b4856Slling 
287990b4856Slling /*
288990b4856Slling  * Validate the given pool properties nvlist and modify the list
289990b4856Slling  * for the property values to be set.
290990b4856Slling  */
291990b4856Slling static int
292990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
293990b4856Slling {
294990b4856Slling 	nvpair_t *elem;
295990b4856Slling 	int error = 0, reset_bootfs = 0;
296990b4856Slling 	uint64_t objnum;
297990b4856Slling 
298990b4856Slling 	elem = NULL;
299990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
300990b4856Slling 		zpool_prop_t prop;
301990b4856Slling 		char *propname, *strval;
302990b4856Slling 		uint64_t intval;
303990b4856Slling 		vdev_t *rvdev;
304990b4856Slling 		char *vdev_type;
305990b4856Slling 		objset_t *os;
306990b4856Slling 
307990b4856Slling 		propname = nvpair_name(elem);
308990b4856Slling 
309990b4856Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
310990b4856Slling 			return (EINVAL);
311990b4856Slling 
312990b4856Slling 		switch (prop) {
313990b4856Slling 		case ZPOOL_PROP_VERSION:
314990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
315990b4856Slling 			if (!error &&
316990b4856Slling 			    (intval < spa_version(spa) || intval > SPA_VERSION))
317990b4856Slling 				error = EINVAL;
318990b4856Slling 			break;
319990b4856Slling 
320990b4856Slling 		case ZPOOL_PROP_DELEGATION:
321990b4856Slling 		case ZPOOL_PROP_AUTOREPLACE:
322990b4856Slling 			error = nvpair_value_uint64(elem, &intval);
323990b4856Slling 			if (!error && intval > 1)
324990b4856Slling 				error = EINVAL;
325990b4856Slling 			break;
326990b4856Slling 
327990b4856Slling 		case ZPOOL_PROP_BOOTFS:
328990b4856Slling 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
329990b4856Slling 				error = ENOTSUP;
330990b4856Slling 				break;
331990b4856Slling 			}
332990b4856Slling 
333990b4856Slling 			/*
334990b4856Slling 			 * A bootable filesystem can not be on a RAIDZ pool
335990b4856Slling 			 * nor a striped pool with more than 1 device.
336990b4856Slling 			 */
337990b4856Slling 			rvdev = spa->spa_root_vdev;
338990b4856Slling 			vdev_type =
339990b4856Slling 			    rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
340990b4856Slling 			if (rvdev->vdev_children > 1 ||
341990b4856Slling 			    strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
342990b4856Slling 			    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
343990b4856Slling 				error = ENOTSUP;
344990b4856Slling 				break;
345990b4856Slling 			}
346990b4856Slling 
347990b4856Slling 			reset_bootfs = 1;
348990b4856Slling 
349990b4856Slling 			error = nvpair_value_string(elem, &strval);
350990b4856Slling 
351990b4856Slling 			if (!error) {
352990b4856Slling 				if (strval == NULL || strval[0] == '\0') {
353990b4856Slling 					objnum = zpool_prop_default_numeric(
354990b4856Slling 					    ZPOOL_PROP_BOOTFS);
355990b4856Slling 					break;
356990b4856Slling 				}
357990b4856Slling 
358990b4856Slling 				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
359990b4856Slling 				    DS_MODE_STANDARD | DS_MODE_READONLY, &os))
360990b4856Slling 					break;
361990b4856Slling 				objnum = dmu_objset_id(os);
362990b4856Slling 				dmu_objset_close(os);
363990b4856Slling 			}
364990b4856Slling 			break;
365*0a4e9518Sgw 		case ZPOOL_PROP_FAILUREMODE:
366*0a4e9518Sgw 			error = nvpair_value_uint64(elem, &intval);
367*0a4e9518Sgw 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
368*0a4e9518Sgw 			    intval > ZIO_FAILURE_MODE_PANIC))
369*0a4e9518Sgw 				error = EINVAL;
370*0a4e9518Sgw 
371*0a4e9518Sgw 			/*
372*0a4e9518Sgw 			 * This is a special case which only occurs when
373*0a4e9518Sgw 			 * the pool has completely failed. This allows
374*0a4e9518Sgw 			 * the user to change the in-core failmode property
375*0a4e9518Sgw 			 * without syncing it out to disk (I/Os might
376*0a4e9518Sgw 			 * currently be blocked). We do this by returning
377*0a4e9518Sgw 			 * EIO to the caller (spa_prop_set) to trick it
378*0a4e9518Sgw 			 * into thinking we encountered a property validation
379*0a4e9518Sgw 			 * error.
380*0a4e9518Sgw 			 */
381*0a4e9518Sgw 			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
382*0a4e9518Sgw 				spa->spa_failmode = intval;
383*0a4e9518Sgw 				error = EIO;
384*0a4e9518Sgw 			}
385*0a4e9518Sgw 			break;
386990b4856Slling 		}
387990b4856Slling 
388990b4856Slling 		if (error)
389990b4856Slling 			break;
390990b4856Slling 	}
391990b4856Slling 
392990b4856Slling 	if (!error && reset_bootfs) {
393990b4856Slling 		error = nvlist_remove(props,
394990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
395990b4856Slling 
396990b4856Slling 		if (!error) {
397990b4856Slling 			error = nvlist_add_uint64(props,
398990b4856Slling 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
399990b4856Slling 		}
400990b4856Slling 	}
401990b4856Slling 
402990b4856Slling 	return (error);
403990b4856Slling }
404990b4856Slling 
405990b4856Slling int
406990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
407990b4856Slling {
408990b4856Slling 	int error;
409990b4856Slling 
410990b4856Slling 	if ((error = spa_prop_validate(spa, nvp)) != 0)
411990b4856Slling 		return (error);
412990b4856Slling 
413990b4856Slling 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
414990b4856Slling 	    spa, nvp, 3));
415990b4856Slling }
416990b4856Slling 
417990b4856Slling /*
418990b4856Slling  * If the bootfs property value is dsobj, clear it.
419990b4856Slling  */
420990b4856Slling void
421990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
422990b4856Slling {
423990b4856Slling 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
424990b4856Slling 		VERIFY(zap_remove(spa->spa_meta_objset,
425990b4856Slling 		    spa->spa_pool_props_object,
426990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
427990b4856Slling 		spa->spa_bootfs = 0;
428990b4856Slling 	}
429990b4856Slling }
430990b4856Slling 
431fa9e4066Sahrens /*
432fa9e4066Sahrens  * ==========================================================================
433fa9e4066Sahrens  * SPA state manipulation (open/create/destroy/import/export)
434fa9e4066Sahrens  * ==========================================================================
435fa9e4066Sahrens  */
436fa9e4066Sahrens 
437ea8dc4b6Seschrock static int
438ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b)
439ea8dc4b6Seschrock {
440ea8dc4b6Seschrock 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
441ea8dc4b6Seschrock 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
442ea8dc4b6Seschrock 	int ret;
443ea8dc4b6Seschrock 
444ea8dc4b6Seschrock 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
445ea8dc4b6Seschrock 	    sizeof (zbookmark_t));
446ea8dc4b6Seschrock 
447ea8dc4b6Seschrock 	if (ret < 0)
448ea8dc4b6Seschrock 		return (-1);
449ea8dc4b6Seschrock 	else if (ret > 0)
450ea8dc4b6Seschrock 		return (1);
451ea8dc4b6Seschrock 	else
452ea8dc4b6Seschrock 		return (0);
453ea8dc4b6Seschrock }
454ea8dc4b6Seschrock 
455ea8dc4b6Seschrock /*
456ea8dc4b6Seschrock  * Utility function which retrieves copies of the current logs and
457ea8dc4b6Seschrock  * re-initializes them in the process.
458ea8dc4b6Seschrock  */
459ea8dc4b6Seschrock void
460ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
461ea8dc4b6Seschrock {
462ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
463ea8dc4b6Seschrock 
464ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
465ea8dc4b6Seschrock 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
466ea8dc4b6Seschrock 
467ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
468ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
469ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
470ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
471ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
472ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
473ea8dc4b6Seschrock }
474ea8dc4b6Seschrock 
475fa9e4066Sahrens /*
476fa9e4066Sahrens  * Activate an uninitialized pool.
477fa9e4066Sahrens  */
478fa9e4066Sahrens static void
479fa9e4066Sahrens spa_activate(spa_t *spa)
480fa9e4066Sahrens {
481fa9e4066Sahrens 	int t;
482fa9e4066Sahrens 
483fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
484fa9e4066Sahrens 
485fa9e4066Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
486fa9e4066Sahrens 
487fa9e4066Sahrens 	spa->spa_normal_class = metaslab_class_create();
4888654d025Sperrin 	spa->spa_log_class = metaslab_class_create();
489fa9e4066Sahrens 
490fa9e4066Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
491fa9e4066Sahrens 		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
492416e0cd8Sek 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
493fa9e4066Sahrens 		    TASKQ_PREPOPULATE);
494fa9e4066Sahrens 		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
495416e0cd8Sek 		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
496fa9e4066Sahrens 		    TASKQ_PREPOPULATE);
497fa9e4066Sahrens 	}
498fa9e4066Sahrens 
499fa9e4066Sahrens 	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
500fa9e4066Sahrens 	    offsetof(vdev_t, vdev_dirty_node));
501*0a4e9518Sgw 	list_create(&spa->spa_zio_list, sizeof (zio_t),
502*0a4e9518Sgw 	    offsetof(zio_t, zio_link_node));
503fa9e4066Sahrens 
504fa9e4066Sahrens 	txg_list_create(&spa->spa_vdev_txg_list,
505fa9e4066Sahrens 	    offsetof(struct vdev, vdev_txg_node));
506ea8dc4b6Seschrock 
507ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_scrub,
508ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
509ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
510ea8dc4b6Seschrock 	avl_create(&spa->spa_errlist_last,
511ea8dc4b6Seschrock 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
512ea8dc4b6Seschrock 	    offsetof(spa_error_entry_t, se_avl));
513fa9e4066Sahrens }
514fa9e4066Sahrens 
515fa9e4066Sahrens /*
516fa9e4066Sahrens  * Opposite of spa_activate().
517fa9e4066Sahrens  */
518fa9e4066Sahrens static void
519fa9e4066Sahrens spa_deactivate(spa_t *spa)
520fa9e4066Sahrens {
521fa9e4066Sahrens 	int t;
522fa9e4066Sahrens 
523fa9e4066Sahrens 	ASSERT(spa->spa_sync_on == B_FALSE);
524fa9e4066Sahrens 	ASSERT(spa->spa_dsl_pool == NULL);
525fa9e4066Sahrens 	ASSERT(spa->spa_root_vdev == NULL);
526fa9e4066Sahrens 
527fa9e4066Sahrens 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
528fa9e4066Sahrens 
529fa9e4066Sahrens 	txg_list_destroy(&spa->spa_vdev_txg_list);
530fa9e4066Sahrens 
531fa9e4066Sahrens 	list_destroy(&spa->spa_dirty_list);
532*0a4e9518Sgw 	list_destroy(&spa->spa_zio_list);
533fa9e4066Sahrens 
534fa9e4066Sahrens 	for (t = 0; t < ZIO_TYPES; t++) {
535fa9e4066Sahrens 		taskq_destroy(spa->spa_zio_issue_taskq[t]);
536fa9e4066Sahrens 		taskq_destroy(spa->spa_zio_intr_taskq[t]);
537fa9e4066Sahrens 		spa->spa_zio_issue_taskq[t] = NULL;
538fa9e4066Sahrens 		spa->spa_zio_intr_taskq[t] = NULL;
539fa9e4066Sahrens 	}
540fa9e4066Sahrens 
541fa9e4066Sahrens 	metaslab_class_destroy(spa->spa_normal_class);
542fa9e4066Sahrens 	spa->spa_normal_class = NULL;
543fa9e4066Sahrens 
5448654d025Sperrin 	metaslab_class_destroy(spa->spa_log_class);
5458654d025Sperrin 	spa->spa_log_class = NULL;
5468654d025Sperrin 
547ea8dc4b6Seschrock 	/*
548ea8dc4b6Seschrock 	 * If this was part of an import or the open otherwise failed, we may
549ea8dc4b6Seschrock 	 * still have errors left in the queues.  Empty them just in case.
550ea8dc4b6Seschrock 	 */
551ea8dc4b6Seschrock 	spa_errlog_drain(spa);
552ea8dc4b6Seschrock 
553ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_scrub);
554ea8dc4b6Seschrock 	avl_destroy(&spa->spa_errlist_last);
555ea8dc4b6Seschrock 
556fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
557fa9e4066Sahrens }
558fa9e4066Sahrens 
559fa9e4066Sahrens /*
560fa9e4066Sahrens  * Verify a pool configuration, and construct the vdev tree appropriately.  This
561fa9e4066Sahrens  * will create all the necessary vdevs in the appropriate layout, with each vdev
562fa9e4066Sahrens  * in the CLOSED state.  This will prep the pool before open/creation/import.
563fa9e4066Sahrens  * All vdev validation is done by the vdev_alloc() routine.
564fa9e4066Sahrens  */
56599653d4eSeschrock static int
56699653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
56799653d4eSeschrock     uint_t id, int atype)
568fa9e4066Sahrens {
569fa9e4066Sahrens 	nvlist_t **child;
570fa9e4066Sahrens 	uint_t c, children;
57199653d4eSeschrock 	int error;
572fa9e4066Sahrens 
57399653d4eSeschrock 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
57499653d4eSeschrock 		return (error);
575fa9e4066Sahrens 
57699653d4eSeschrock 	if ((*vdp)->vdev_ops->vdev_op_leaf)
57799653d4eSeschrock 		return (0);
578fa9e4066Sahrens 
579fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
580fa9e4066Sahrens 	    &child, &children) != 0) {
58199653d4eSeschrock 		vdev_free(*vdp);
58299653d4eSeschrock 		*vdp = NULL;
58399653d4eSeschrock 		return (EINVAL);
584fa9e4066Sahrens 	}
585fa9e4066Sahrens 
586fa9e4066Sahrens 	for (c = 0; c < children; c++) {
58799653d4eSeschrock 		vdev_t *vd;
58899653d4eSeschrock 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
58999653d4eSeschrock 		    atype)) != 0) {
59099653d4eSeschrock 			vdev_free(*vdp);
59199653d4eSeschrock 			*vdp = NULL;
59299653d4eSeschrock 			return (error);
593fa9e4066Sahrens 		}
594fa9e4066Sahrens 	}
595fa9e4066Sahrens 
59699653d4eSeschrock 	ASSERT(*vdp != NULL);
59799653d4eSeschrock 
59899653d4eSeschrock 	return (0);
599fa9e4066Sahrens }
600fa9e4066Sahrens 
601fa9e4066Sahrens /*
602fa9e4066Sahrens  * Opposite of spa_load().
603fa9e4066Sahrens  */
604fa9e4066Sahrens static void
605fa9e4066Sahrens spa_unload(spa_t *spa)
606fa9e4066Sahrens {
60799653d4eSeschrock 	int i;
60899653d4eSeschrock 
609ea8dc4b6Seschrock 	/*
610ea8dc4b6Seschrock 	 * Stop async tasks.
611ea8dc4b6Seschrock 	 */
612ea8dc4b6Seschrock 	spa_async_suspend(spa);
613ea8dc4b6Seschrock 
614fa9e4066Sahrens 	/*
615fa9e4066Sahrens 	 * Stop syncing.
616fa9e4066Sahrens 	 */
617fa9e4066Sahrens 	if (spa->spa_sync_on) {
618fa9e4066Sahrens 		txg_sync_stop(spa->spa_dsl_pool);
619fa9e4066Sahrens 		spa->spa_sync_on = B_FALSE;
620fa9e4066Sahrens 	}
621fa9e4066Sahrens 
622fa9e4066Sahrens 	/*
623fa9e4066Sahrens 	 * Wait for any outstanding prefetch I/O to complete.
624fa9e4066Sahrens 	 */
625ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
626ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
627fa9e4066Sahrens 
628fa9e4066Sahrens 	/*
629fa9e4066Sahrens 	 * Close the dsl pool.
630fa9e4066Sahrens 	 */
631fa9e4066Sahrens 	if (spa->spa_dsl_pool) {
632fa9e4066Sahrens 		dsl_pool_close(spa->spa_dsl_pool);
633fa9e4066Sahrens 		spa->spa_dsl_pool = NULL;
634fa9e4066Sahrens 	}
635fa9e4066Sahrens 
636fa9e4066Sahrens 	/*
637fa9e4066Sahrens 	 * Close all vdevs.
638fa9e4066Sahrens 	 */
6390e34b6a7Sbonwick 	if (spa->spa_root_vdev)
640fa9e4066Sahrens 		vdev_free(spa->spa_root_vdev);
6410e34b6a7Sbonwick 	ASSERT(spa->spa_root_vdev == NULL);
642ea8dc4b6Seschrock 
64399653d4eSeschrock 	for (i = 0; i < spa->spa_nspares; i++)
64499653d4eSeschrock 		vdev_free(spa->spa_spares[i]);
64599653d4eSeschrock 	if (spa->spa_spares) {
64699653d4eSeschrock 		kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *));
64799653d4eSeschrock 		spa->spa_spares = NULL;
64899653d4eSeschrock 	}
64999653d4eSeschrock 	if (spa->spa_sparelist) {
65099653d4eSeschrock 		nvlist_free(spa->spa_sparelist);
65199653d4eSeschrock 		spa->spa_sparelist = NULL;
65299653d4eSeschrock 	}
65399653d4eSeschrock 
654ea8dc4b6Seschrock 	spa->spa_async_suspended = 0;
655fa9e4066Sahrens }
656fa9e4066Sahrens 
65799653d4eSeschrock /*
65899653d4eSeschrock  * Load (or re-load) the current list of vdevs describing the active spares for
65999653d4eSeschrock  * this pool.  When this is called, we have some form of basic information in
66099653d4eSeschrock  * 'spa_sparelist'.  We parse this into vdevs, try to open them, and then
66199653d4eSeschrock  * re-generate a more complete list including status information.
66299653d4eSeschrock  */
66399653d4eSeschrock static void
66499653d4eSeschrock spa_load_spares(spa_t *spa)
66599653d4eSeschrock {
66699653d4eSeschrock 	nvlist_t **spares;
66799653d4eSeschrock 	uint_t nspares;
66899653d4eSeschrock 	int i;
66939c23413Seschrock 	vdev_t *vd, *tvd;
67099653d4eSeschrock 
67199653d4eSeschrock 	/*
67299653d4eSeschrock 	 * First, close and free any existing spare vdevs.
67399653d4eSeschrock 	 */
67499653d4eSeschrock 	for (i = 0; i < spa->spa_nspares; i++) {
67539c23413Seschrock 		vd = spa->spa_spares[i];
67639c23413Seschrock 
67739c23413Seschrock 		/* Undo the call to spa_activate() below */
67839c23413Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL &&
67939c23413Seschrock 		    tvd->vdev_isspare)
68039c23413Seschrock 			spa_spare_remove(tvd);
68139c23413Seschrock 		vdev_close(vd);
68239c23413Seschrock 		vdev_free(vd);
68399653d4eSeschrock 	}
68439c23413Seschrock 
68599653d4eSeschrock 	if (spa->spa_spares)
68699653d4eSeschrock 		kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *));
68799653d4eSeschrock 
68899653d4eSeschrock 	if (spa->spa_sparelist == NULL)
68999653d4eSeschrock 		nspares = 0;
69099653d4eSeschrock 	else
69199653d4eSeschrock 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
69299653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
69399653d4eSeschrock 
69499653d4eSeschrock 	spa->spa_nspares = (int)nspares;
69599653d4eSeschrock 	spa->spa_spares = NULL;
69699653d4eSeschrock 
69799653d4eSeschrock 	if (nspares == 0)
69899653d4eSeschrock 		return;
69999653d4eSeschrock 
70099653d4eSeschrock 	/*
70199653d4eSeschrock 	 * Construct the array of vdevs, opening them to get status in the
70239c23413Seschrock 	 * process.   For each spare, there is potentially two different vdev_t
70339c23413Seschrock 	 * structures associated with it: one in the list of spares (used only
70439c23413Seschrock 	 * for basic validation purposes) and one in the active vdev
70539c23413Seschrock 	 * configuration (if it's spared in).  During this phase we open and
70639c23413Seschrock 	 * validate each vdev on the spare list.  If the vdev also exists in the
70739c23413Seschrock 	 * active configuration, then we also mark this vdev as an active spare.
70899653d4eSeschrock 	 */
70999653d4eSeschrock 	spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP);
71099653d4eSeschrock 	for (i = 0; i < spa->spa_nspares; i++) {
71199653d4eSeschrock 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
71299653d4eSeschrock 		    VDEV_ALLOC_SPARE) == 0);
71399653d4eSeschrock 		ASSERT(vd != NULL);
71499653d4eSeschrock 
71599653d4eSeschrock 		spa->spa_spares[i] = vd;
71699653d4eSeschrock 
71739c23413Seschrock 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) {
71839c23413Seschrock 			if (!tvd->vdev_isspare)
71939c23413Seschrock 				spa_spare_add(tvd);
72039c23413Seschrock 
72139c23413Seschrock 			/*
72239c23413Seschrock 			 * We only mark the spare active if we were successfully
72339c23413Seschrock 			 * able to load the vdev.  Otherwise, importing a pool
72439c23413Seschrock 			 * with a bad active spare would result in strange
72539c23413Seschrock 			 * behavior, because multiple pool would think the spare
72639c23413Seschrock 			 * is actively in use.
72739c23413Seschrock 			 *
72839c23413Seschrock 			 * There is a vulnerability here to an equally bizarre
72939c23413Seschrock 			 * circumstance, where a dead active spare is later
73039c23413Seschrock 			 * brought back to life (onlined or otherwise).  Given
73139c23413Seschrock 			 * the rarity of this scenario, and the extra complexity
73239c23413Seschrock 			 * it adds, we ignore the possibility.
73339c23413Seschrock 			 */
73439c23413Seschrock 			if (!vdev_is_dead(tvd))
73539c23413Seschrock 				spa_spare_activate(tvd);
73639c23413Seschrock 		}
73739c23413Seschrock 
73899653d4eSeschrock 		if (vdev_open(vd) != 0)
73999653d4eSeschrock 			continue;
74099653d4eSeschrock 
74199653d4eSeschrock 		vd->vdev_top = vd;
74299653d4eSeschrock 		(void) vdev_validate_spare(vd);
74399653d4eSeschrock 	}
74499653d4eSeschrock 
74599653d4eSeschrock 	/*
74699653d4eSeschrock 	 * Recompute the stashed list of spares, with status information
74799653d4eSeschrock 	 * this time.
74899653d4eSeschrock 	 */
74999653d4eSeschrock 	VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
75099653d4eSeschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
75199653d4eSeschrock 
75299653d4eSeschrock 	spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP);
75399653d4eSeschrock 	for (i = 0; i < spa->spa_nspares; i++)
75499653d4eSeschrock 		spares[i] = vdev_config_generate(spa, spa->spa_spares[i],
75599653d4eSeschrock 		    B_TRUE, B_TRUE);
75699653d4eSeschrock 	VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
75799653d4eSeschrock 	    spares, spa->spa_nspares) == 0);
75899653d4eSeschrock 	for (i = 0; i < spa->spa_nspares; i++)
75999653d4eSeschrock 		nvlist_free(spares[i]);
76099653d4eSeschrock 	kmem_free(spares, spa->spa_nspares * sizeof (void *));
76199653d4eSeschrock }
76299653d4eSeschrock 
76399653d4eSeschrock static int
76499653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
76599653d4eSeschrock {
76699653d4eSeschrock 	dmu_buf_t *db;
76799653d4eSeschrock 	char *packed = NULL;
76899653d4eSeschrock 	size_t nvsize = 0;
76999653d4eSeschrock 	int error;
77099653d4eSeschrock 	*value = NULL;
77199653d4eSeschrock 
77299653d4eSeschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
77399653d4eSeschrock 	nvsize = *(uint64_t *)db->db_data;
77499653d4eSeschrock 	dmu_buf_rele(db, FTAG);
77599653d4eSeschrock 
77699653d4eSeschrock 	packed = kmem_alloc(nvsize, KM_SLEEP);
77799653d4eSeschrock 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
77899653d4eSeschrock 	if (error == 0)
77999653d4eSeschrock 		error = nvlist_unpack(packed, nvsize, value, 0);
78099653d4eSeschrock 	kmem_free(packed, nvsize);
78199653d4eSeschrock 
78299653d4eSeschrock 	return (error);
78399653d4eSeschrock }
78499653d4eSeschrock 
7853d7072f8Seschrock /*
7863d7072f8Seschrock  * Checks to see if the given vdev could not be opened, in which case we post a
7873d7072f8Seschrock  * sysevent to notify the autoreplace code that the device has been removed.
7883d7072f8Seschrock  */
7893d7072f8Seschrock static void
7903d7072f8Seschrock spa_check_removed(vdev_t *vd)
7913d7072f8Seschrock {
7923d7072f8Seschrock 	int c;
7933d7072f8Seschrock 
7943d7072f8Seschrock 	for (c = 0; c < vd->vdev_children; c++)
7953d7072f8Seschrock 		spa_check_removed(vd->vdev_child[c]);
7963d7072f8Seschrock 
7973d7072f8Seschrock 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
7983d7072f8Seschrock 		zfs_post_autoreplace(vd->vdev_spa, vd);
7993d7072f8Seschrock 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
8003d7072f8Seschrock 	}
8013d7072f8Seschrock }
8023d7072f8Seschrock 
803fa9e4066Sahrens /*
804fa9e4066Sahrens  * Load an existing storage pool, using the pool's builtin spa_config as a
805ea8dc4b6Seschrock  * source of configuration information.
806fa9e4066Sahrens  */
807fa9e4066Sahrens static int
808ea8dc4b6Seschrock spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
809fa9e4066Sahrens {
810fa9e4066Sahrens 	int error = 0;
811fa9e4066Sahrens 	nvlist_t *nvroot = NULL;
812fa9e4066Sahrens 	vdev_t *rvd;
813fa9e4066Sahrens 	uberblock_t *ub = &spa->spa_uberblock;
8140373e76bSbonwick 	uint64_t config_cache_txg = spa->spa_config_txg;
815fa9e4066Sahrens 	uint64_t pool_guid;
81699653d4eSeschrock 	uint64_t version;
817fa9e4066Sahrens 	zio_t *zio;
8183d7072f8Seschrock 	uint64_t autoreplace = 0;
819fa9e4066Sahrens 
820ea8dc4b6Seschrock 	spa->spa_load_state = state;
8210373e76bSbonwick 
822fa9e4066Sahrens 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
823a9926bf0Sbonwick 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
824ea8dc4b6Seschrock 		error = EINVAL;
825ea8dc4b6Seschrock 		goto out;
826ea8dc4b6Seschrock 	}
827fa9e4066Sahrens 
82899653d4eSeschrock 	/*
82999653d4eSeschrock 	 * Versioning wasn't explicitly added to the label until later, so if
83099653d4eSeschrock 	 * it's not present treat it as the initial version.
83199653d4eSeschrock 	 */
83299653d4eSeschrock 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
833e7437265Sahrens 		version = SPA_VERSION_INITIAL;
83499653d4eSeschrock 
835a9926bf0Sbonwick 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
836a9926bf0Sbonwick 	    &spa->spa_config_txg);
837a9926bf0Sbonwick 
8380373e76bSbonwick 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
839ea8dc4b6Seschrock 	    spa_guid_exists(pool_guid, 0)) {
840ea8dc4b6Seschrock 		error = EEXIST;
841ea8dc4b6Seschrock 		goto out;
842ea8dc4b6Seschrock 	}
843fa9e4066Sahrens 
844b5989ec7Seschrock 	spa->spa_load_guid = pool_guid;
845b5989ec7Seschrock 
846fa9e4066Sahrens 	/*
84799653d4eSeschrock 	 * Parse the configuration into a vdev tree.  We explicitly set the
84899653d4eSeschrock 	 * value that will be returned by spa_version() since parsing the
84999653d4eSeschrock 	 * configuration requires knowing the version number.
850fa9e4066Sahrens 	 */
851ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
85299653d4eSeschrock 	spa->spa_ubsync.ub_version = version;
85399653d4eSeschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
854ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
855fa9e4066Sahrens 
85699653d4eSeschrock 	if (error != 0)
857ea8dc4b6Seschrock 		goto out;
858fa9e4066Sahrens 
8590e34b6a7Sbonwick 	ASSERT(spa->spa_root_vdev == rvd);
860fa9e4066Sahrens 	ASSERT(spa_guid(spa) == pool_guid);
861fa9e4066Sahrens 
862fa9e4066Sahrens 	/*
863fa9e4066Sahrens 	 * Try to open all vdevs, loading each label in the process.
864fa9e4066Sahrens 	 */
8650bf246f5Smc 	error = vdev_open(rvd);
8660bf246f5Smc 	if (error != 0)
867ea8dc4b6Seschrock 		goto out;
868fa9e4066Sahrens 
869560e6e96Seschrock 	/*
870560e6e96Seschrock 	 * Validate the labels for all leaf vdevs.  We need to grab the config
871560e6e96Seschrock 	 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD
872560e6e96Seschrock 	 * flag.
873560e6e96Seschrock 	 */
874560e6e96Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
875560e6e96Seschrock 	error = vdev_validate(rvd);
876560e6e96Seschrock 	spa_config_exit(spa, FTAG);
877560e6e96Seschrock 
8780bf246f5Smc 	if (error != 0)
879560e6e96Seschrock 		goto out;
880560e6e96Seschrock 
881560e6e96Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
882560e6e96Seschrock 		error = ENXIO;
883560e6e96Seschrock 		goto out;
884560e6e96Seschrock 	}
885560e6e96Seschrock 
886fa9e4066Sahrens 	/*
887fa9e4066Sahrens 	 * Find the best uberblock.
888fa9e4066Sahrens 	 */
889fa9e4066Sahrens 	bzero(ub, sizeof (uberblock_t));
890fa9e4066Sahrens 
891fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL,
892fa9e4066Sahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
893fa9e4066Sahrens 	vdev_uberblock_load(zio, rvd, ub);
894fa9e4066Sahrens 	error = zio_wait(zio);
895fa9e4066Sahrens 
896fa9e4066Sahrens 	/*
897fa9e4066Sahrens 	 * If we weren't able to find a single valid uberblock, return failure.
898fa9e4066Sahrens 	 */
899fa9e4066Sahrens 	if (ub->ub_txg == 0) {
900eaca9bbdSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
901eaca9bbdSeschrock 		    VDEV_AUX_CORRUPT_DATA);
902ea8dc4b6Seschrock 		error = ENXIO;
903ea8dc4b6Seschrock 		goto out;
904ea8dc4b6Seschrock 	}
905ea8dc4b6Seschrock 
906ea8dc4b6Seschrock 	/*
907ea8dc4b6Seschrock 	 * If the pool is newer than the code, we can't open it.
908ea8dc4b6Seschrock 	 */
909e7437265Sahrens 	if (ub->ub_version > SPA_VERSION) {
910eaca9bbdSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
911eaca9bbdSeschrock 		    VDEV_AUX_VERSION_NEWER);
912ea8dc4b6Seschrock 		error = ENOTSUP;
913ea8dc4b6Seschrock 		goto out;
914fa9e4066Sahrens 	}
915fa9e4066Sahrens 
916fa9e4066Sahrens 	/*
917fa9e4066Sahrens 	 * If the vdev guid sum doesn't match the uberblock, we have an
918fa9e4066Sahrens 	 * incomplete configuration.
919fa9e4066Sahrens 	 */
920ecc2d604Sbonwick 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
921ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
922ea8dc4b6Seschrock 		    VDEV_AUX_BAD_GUID_SUM);
923ea8dc4b6Seschrock 		error = ENXIO;
924ea8dc4b6Seschrock 		goto out;
925fa9e4066Sahrens 	}
926fa9e4066Sahrens 
927fa9e4066Sahrens 	/*
928fa9e4066Sahrens 	 * Initialize internal SPA structures.
929fa9e4066Sahrens 	 */
930fa9e4066Sahrens 	spa->spa_state = POOL_STATE_ACTIVE;
931fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
932fa9e4066Sahrens 	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
933ea8dc4b6Seschrock 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
934ea8dc4b6Seschrock 	if (error) {
935ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
936ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
937ea8dc4b6Seschrock 		goto out;
938ea8dc4b6Seschrock 	}
939fa9e4066Sahrens 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
940fa9e4066Sahrens 
941ea8dc4b6Seschrock 	if (zap_lookup(spa->spa_meta_objset,
942fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
943ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
944ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
945ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
946ea8dc4b6Seschrock 		error = EIO;
947ea8dc4b6Seschrock 		goto out;
948ea8dc4b6Seschrock 	}
949fa9e4066Sahrens 
950fa9e4066Sahrens 	if (!mosconfig) {
95199653d4eSeschrock 		nvlist_t *newconfig;
95295173954Sek 		uint64_t hostid;
953fa9e4066Sahrens 
95499653d4eSeschrock 		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
955ea8dc4b6Seschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
956ea8dc4b6Seschrock 			    VDEV_AUX_CORRUPT_DATA);
957ea8dc4b6Seschrock 			error = EIO;
958ea8dc4b6Seschrock 			goto out;
959ea8dc4b6Seschrock 		}
960fa9e4066Sahrens 
96195173954Sek 		if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID,
96295173954Sek 		    &hostid) == 0) {
96395173954Sek 			char *hostname;
96495173954Sek 			unsigned long myhostid = 0;
96595173954Sek 
96695173954Sek 			VERIFY(nvlist_lookup_string(newconfig,
96795173954Sek 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
96895173954Sek 
96995173954Sek 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
97017194a52Slling 			if (hostid != 0 && myhostid != 0 &&
97117194a52Slling 			    (unsigned long)hostid != myhostid) {
97295173954Sek 				cmn_err(CE_WARN, "pool '%s' could not be "
97395173954Sek 				    "loaded as it was last accessed by "
97495173954Sek 				    "another system (host: %s hostid: 0x%lx).  "
97595173954Sek 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
97695173954Sek 				    spa->spa_name, hostname,
97795173954Sek 				    (unsigned long)hostid);
97895173954Sek 				error = EBADF;
97995173954Sek 				goto out;
98095173954Sek 			}
98195173954Sek 		}
98295173954Sek 
983fa9e4066Sahrens 		spa_config_set(spa, newconfig);
984fa9e4066Sahrens 		spa_unload(spa);
985fa9e4066Sahrens 		spa_deactivate(spa);
986fa9e4066Sahrens 		spa_activate(spa);
987fa9e4066Sahrens 
988ea8dc4b6Seschrock 		return (spa_load(spa, newconfig, state, B_TRUE));
989fa9e4066Sahrens 	}
990fa9e4066Sahrens 
991ea8dc4b6Seschrock 	if (zap_lookup(spa->spa_meta_objset,
992fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
993ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
994ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
995ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
996ea8dc4b6Seschrock 		error = EIO;
997ea8dc4b6Seschrock 		goto out;
998ea8dc4b6Seschrock 	}
999fa9e4066Sahrens 
100099653d4eSeschrock 	/*
100199653d4eSeschrock 	 * Load the bit that tells us to use the new accounting function
100299653d4eSeschrock 	 * (raid-z deflation).  If we have an older pool, this will not
100399653d4eSeschrock 	 * be present.
100499653d4eSeschrock 	 */
100599653d4eSeschrock 	error = zap_lookup(spa->spa_meta_objset,
100699653d4eSeschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
100799653d4eSeschrock 	    sizeof (uint64_t), 1, &spa->spa_deflate);
100899653d4eSeschrock 	if (error != 0 && error != ENOENT) {
100999653d4eSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
101099653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
101199653d4eSeschrock 		error = EIO;
101299653d4eSeschrock 		goto out;
101399653d4eSeschrock 	}
101499653d4eSeschrock 
1015fa9e4066Sahrens 	/*
1016ea8dc4b6Seschrock 	 * Load the persistent error log.  If we have an older pool, this will
1017ea8dc4b6Seschrock 	 * not be present.
1018fa9e4066Sahrens 	 */
1019ea8dc4b6Seschrock 	error = zap_lookup(spa->spa_meta_objset,
1020ea8dc4b6Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
1021ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
1022d80c45e0Sbonwick 	if (error != 0 && error != ENOENT) {
1023ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1024ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1025ea8dc4b6Seschrock 		error = EIO;
1026ea8dc4b6Seschrock 		goto out;
1027ea8dc4b6Seschrock 	}
1028ea8dc4b6Seschrock 
1029ea8dc4b6Seschrock 	error = zap_lookup(spa->spa_meta_objset,
1030ea8dc4b6Seschrock 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
1031ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
1032ea8dc4b6Seschrock 	if (error != 0 && error != ENOENT) {
1033ea8dc4b6Seschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1034ea8dc4b6Seschrock 		    VDEV_AUX_CORRUPT_DATA);
1035ea8dc4b6Seschrock 		error = EIO;
1036ea8dc4b6Seschrock 		goto out;
1037ea8dc4b6Seschrock 	}
1038ea8dc4b6Seschrock 
103906eeb2adSek 	/*
104006eeb2adSek 	 * Load the history object.  If we have an older pool, this
104106eeb2adSek 	 * will not be present.
104206eeb2adSek 	 */
104306eeb2adSek 	error = zap_lookup(spa->spa_meta_objset,
104406eeb2adSek 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
104506eeb2adSek 	    sizeof (uint64_t), 1, &spa->spa_history);
104606eeb2adSek 	if (error != 0 && error != ENOENT) {
104706eeb2adSek 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
104806eeb2adSek 		    VDEV_AUX_CORRUPT_DATA);
104906eeb2adSek 		error = EIO;
105006eeb2adSek 		goto out;
105106eeb2adSek 	}
105206eeb2adSek 
105399653d4eSeschrock 	/*
105499653d4eSeschrock 	 * Load any hot spares for this pool.
105599653d4eSeschrock 	 */
105699653d4eSeschrock 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
105799653d4eSeschrock 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object);
105899653d4eSeschrock 	if (error != 0 && error != ENOENT) {
105999653d4eSeschrock 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
106099653d4eSeschrock 		    VDEV_AUX_CORRUPT_DATA);
106199653d4eSeschrock 		error = EIO;
106299653d4eSeschrock 		goto out;
106399653d4eSeschrock 	}
106499653d4eSeschrock 	if (error == 0) {
1065e7437265Sahrens 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
106699653d4eSeschrock 		if (load_nvlist(spa, spa->spa_spares_object,
106799653d4eSeschrock 		    &spa->spa_sparelist) != 0) {
106899653d4eSeschrock 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
106999653d4eSeschrock 			    VDEV_AUX_CORRUPT_DATA);
107099653d4eSeschrock 			error = EIO;
107199653d4eSeschrock 			goto out;
107299653d4eSeschrock 		}
107399653d4eSeschrock 
107499653d4eSeschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
107599653d4eSeschrock 		spa_load_spares(spa);
107699653d4eSeschrock 		spa_config_exit(spa, FTAG);
107799653d4eSeschrock 	}
107899653d4eSeschrock 
1079990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1080ecd6cf80Smarks 
1081b1b8ab34Slling 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1082b1b8ab34Slling 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
1083b1b8ab34Slling 
1084b1b8ab34Slling 	if (error && error != ENOENT) {
1085b1b8ab34Slling 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1086b1b8ab34Slling 		    VDEV_AUX_CORRUPT_DATA);
1087b1b8ab34Slling 		error = EIO;
1088b1b8ab34Slling 		goto out;
1089b1b8ab34Slling 	}
1090b1b8ab34Slling 
1091b1b8ab34Slling 	if (error == 0) {
1092b1b8ab34Slling 		(void) zap_lookup(spa->spa_meta_objset,
1093b1b8ab34Slling 		    spa->spa_pool_props_object,
10943d7072f8Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1095b1b8ab34Slling 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
10963d7072f8Seschrock 		(void) zap_lookup(spa->spa_meta_objset,
10973d7072f8Seschrock 		    spa->spa_pool_props_object,
10983d7072f8Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
10993d7072f8Seschrock 		    sizeof (uint64_t), 1, &autoreplace);
1100ecd6cf80Smarks 		(void) zap_lookup(spa->spa_meta_objset,
1101ecd6cf80Smarks 		    spa->spa_pool_props_object,
1102ecd6cf80Smarks 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
1103ecd6cf80Smarks 		    sizeof (uint64_t), 1, &spa->spa_delegation);
1104*0a4e9518Sgw 		(void) zap_lookup(spa->spa_meta_objset,
1105*0a4e9518Sgw 		    spa->spa_pool_props_object,
1106*0a4e9518Sgw 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
1107*0a4e9518Sgw 		    sizeof (uint64_t), 1, &spa->spa_failmode);
1108b1b8ab34Slling 	}
1109b1b8ab34Slling 
11103d7072f8Seschrock 	/*
11113d7072f8Seschrock 	 * If the 'autoreplace' property is set, then post a resource notifying
11123d7072f8Seschrock 	 * the ZFS DE that it should not issue any faults for unopenable
11133d7072f8Seschrock 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
11143d7072f8Seschrock 	 * unopenable vdevs so that the normal autoreplace handler can take
11153d7072f8Seschrock 	 * over.
11163d7072f8Seschrock 	 */
11173d7072f8Seschrock 	if (autoreplace)
11183d7072f8Seschrock 		spa_check_removed(spa->spa_root_vdev);
11193d7072f8Seschrock 
1120ea8dc4b6Seschrock 	/*
1121560e6e96Seschrock 	 * Load the vdev state for all toplevel vdevs.
1122ea8dc4b6Seschrock 	 */
1123560e6e96Seschrock 	vdev_load(rvd);
11240373e76bSbonwick 
1125fa9e4066Sahrens 	/*
1126fa9e4066Sahrens 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1127fa9e4066Sahrens 	 */
1128ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
1129fa9e4066Sahrens 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
1130ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	/*
1133fa9e4066Sahrens 	 * Check the state of the root vdev.  If it can't be opened, it
1134fa9e4066Sahrens 	 * indicates one or more toplevel vdevs are faulted.
1135fa9e4066Sahrens 	 */
1136ea8dc4b6Seschrock 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1137ea8dc4b6Seschrock 		error = ENXIO;
1138ea8dc4b6Seschrock 		goto out;
1139ea8dc4b6Seschrock 	}
1140fa9e4066Sahrens 
1141ea8dc4b6Seschrock 	if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) {
11425dabedeeSbonwick 		dmu_tx_t *tx;
11430373e76bSbonwick 		int need_update = B_FALSE;
11440373e76bSbonwick 		int c;
11455dabedeeSbonwick 
11460373e76bSbonwick 		/*
11470373e76bSbonwick 		 * Claim log blocks that haven't been committed yet.
11480373e76bSbonwick 		 * This must all happen in a single txg.
11490373e76bSbonwick 		 */
11505dabedeeSbonwick 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1151fa9e4066Sahrens 		    spa_first_txg(spa));
11520b69c2f0Sahrens 		(void) dmu_objset_find(spa->spa_name,
11530b69c2f0Sahrens 		    zil_claim, tx, DS_FIND_CHILDREN);
1154fa9e4066Sahrens 		dmu_tx_commit(tx);
1155fa9e4066Sahrens 
1156fa9e4066Sahrens 		spa->spa_sync_on = B_TRUE;
1157fa9e4066Sahrens 		txg_sync_start(spa->spa_dsl_pool);
1158fa9e4066Sahrens 
1159fa9e4066Sahrens 		/*
1160fa9e4066Sahrens 		 * Wait for all claims to sync.
1161fa9e4066Sahrens 		 */
1162fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
11630e34b6a7Sbonwick 
11640e34b6a7Sbonwick 		/*
11650373e76bSbonwick 		 * If the config cache is stale, or we have uninitialized
11660373e76bSbonwick 		 * metaslabs (see spa_vdev_add()), then update the config.
11670e34b6a7Sbonwick 		 */
11680373e76bSbonwick 		if (config_cache_txg != spa->spa_config_txg ||
11690373e76bSbonwick 		    state == SPA_LOAD_IMPORT)
11700373e76bSbonwick 			need_update = B_TRUE;
11710373e76bSbonwick 
11720373e76bSbonwick 		for (c = 0; c < rvd->vdev_children; c++)
11730373e76bSbonwick 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
11740373e76bSbonwick 				need_update = B_TRUE;
11750e34b6a7Sbonwick 
11760e34b6a7Sbonwick 		/*
11770373e76bSbonwick 		 * Update the config cache asychronously in case we're the
11780373e76bSbonwick 		 * root pool, in which case the config cache isn't writable yet.
11790e34b6a7Sbonwick 		 */
11800373e76bSbonwick 		if (need_update)
11810373e76bSbonwick 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1182fa9e4066Sahrens 	}
1183fa9e4066Sahrens 
1184ea8dc4b6Seschrock 	error = 0;
1185ea8dc4b6Seschrock out:
118699653d4eSeschrock 	if (error && error != EBADF)
1187ea8dc4b6Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0);
1188ea8dc4b6Seschrock 	spa->spa_load_state = SPA_LOAD_NONE;
1189ea8dc4b6Seschrock 	spa->spa_ena = 0;
1190ea8dc4b6Seschrock 
1191ea8dc4b6Seschrock 	return (error);
1192fa9e4066Sahrens }
1193fa9e4066Sahrens 
1194fa9e4066Sahrens /*
1195fa9e4066Sahrens  * Pool Open/Import
1196fa9e4066Sahrens  *
1197fa9e4066Sahrens  * The import case is identical to an open except that the configuration is sent
1198fa9e4066Sahrens  * down from userland, instead of grabbed from the configuration cache.  For the
1199fa9e4066Sahrens  * case of an open, the pool configuration will exist in the
12003d7072f8Seschrock  * POOL_STATE_UNINITIALIZED state.
1201fa9e4066Sahrens  *
1202fa9e4066Sahrens  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1203fa9e4066Sahrens  * the same time open the pool, without having to keep around the spa_t in some
1204fa9e4066Sahrens  * ambiguous state.
1205fa9e4066Sahrens  */
1206fa9e4066Sahrens static int
1207fa9e4066Sahrens spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1208fa9e4066Sahrens {
1209fa9e4066Sahrens 	spa_t *spa;
1210fa9e4066Sahrens 	int error;
1211fa9e4066Sahrens 	int loaded = B_FALSE;
1212fa9e4066Sahrens 	int locked = B_FALSE;
1213fa9e4066Sahrens 
1214fa9e4066Sahrens 	*spapp = NULL;
1215fa9e4066Sahrens 
1216fa9e4066Sahrens 	/*
1217fa9e4066Sahrens 	 * As disgusting as this is, we need to support recursive calls to this
1218fa9e4066Sahrens 	 * function because dsl_dir_open() is called during spa_load(), and ends
1219fa9e4066Sahrens 	 * up calling spa_open() again.  The real fix is to figure out how to
1220fa9e4066Sahrens 	 * avoid dsl_dir_open() calling this in the first place.
1221fa9e4066Sahrens 	 */
1222fa9e4066Sahrens 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1223fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
1224fa9e4066Sahrens 		locked = B_TRUE;
1225fa9e4066Sahrens 	}
1226fa9e4066Sahrens 
1227fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1228fa9e4066Sahrens 		if (locked)
1229fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
1230fa9e4066Sahrens 		return (ENOENT);
1231fa9e4066Sahrens 	}
1232fa9e4066Sahrens 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1233fa9e4066Sahrens 
1234fa9e4066Sahrens 		spa_activate(spa);
1235fa9e4066Sahrens 
12360373e76bSbonwick 		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1237fa9e4066Sahrens 
1238fa9e4066Sahrens 		if (error == EBADF) {
1239fa9e4066Sahrens 			/*
1240560e6e96Seschrock 			 * If vdev_validate() returns failure (indicated by
1241560e6e96Seschrock 			 * EBADF), it indicates that one of the vdevs indicates
1242560e6e96Seschrock 			 * that the pool has been exported or destroyed.  If
1243560e6e96Seschrock 			 * this is the case, the config cache is out of sync and
1244560e6e96Seschrock 			 * we should remove the pool from the namespace.
1245fa9e4066Sahrens 			 */
124699653d4eSeschrock 			zfs_post_ok(spa, NULL);
1247fa9e4066Sahrens 			spa_unload(spa);
1248fa9e4066Sahrens 			spa_deactivate(spa);
1249fa9e4066Sahrens 			spa_remove(spa);
1250fa9e4066Sahrens 			spa_config_sync();
1251fa9e4066Sahrens 			if (locked)
1252fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
1253fa9e4066Sahrens 			return (ENOENT);
1254ea8dc4b6Seschrock 		}
1255ea8dc4b6Seschrock 
1256ea8dc4b6Seschrock 		if (error) {
1257fa9e4066Sahrens 			/*
1258fa9e4066Sahrens 			 * We can't open the pool, but we still have useful
1259fa9e4066Sahrens 			 * information: the state of each vdev after the
1260fa9e4066Sahrens 			 * attempted vdev_open().  Return this to the user.
1261fa9e4066Sahrens 			 */
12620373e76bSbonwick 			if (config != NULL && spa->spa_root_vdev != NULL) {
12630373e76bSbonwick 				spa_config_enter(spa, RW_READER, FTAG);
1264fa9e4066Sahrens 				*config = spa_config_generate(spa, NULL, -1ULL,
1265fa9e4066Sahrens 				    B_TRUE);
12660373e76bSbonwick 				spa_config_exit(spa, FTAG);
12670373e76bSbonwick 			}
1268fa9e4066Sahrens 			spa_unload(spa);
1269fa9e4066Sahrens 			spa_deactivate(spa);
1270ea8dc4b6Seschrock 			spa->spa_last_open_failed = B_TRUE;
1271fa9e4066Sahrens 			if (locked)
1272fa9e4066Sahrens 				mutex_exit(&spa_namespace_lock);
1273fa9e4066Sahrens 			*spapp = NULL;
1274fa9e4066Sahrens 			return (error);
1275ea8dc4b6Seschrock 		} else {
1276ea8dc4b6Seschrock 			zfs_post_ok(spa, NULL);
1277ea8dc4b6Seschrock 			spa->spa_last_open_failed = B_FALSE;
1278fa9e4066Sahrens 		}
1279fa9e4066Sahrens 
1280fa9e4066Sahrens 		loaded = B_TRUE;
1281fa9e4066Sahrens 	}
1282fa9e4066Sahrens 
1283fa9e4066Sahrens 	spa_open_ref(spa, tag);
12843d7072f8Seschrock 
12853d7072f8Seschrock 	/*
12863d7072f8Seschrock 	 * If we just loaded the pool, resilver anything that's out of date.
12873d7072f8Seschrock 	 */
12883d7072f8Seschrock 	if (loaded && (spa_mode & FWRITE))
12893d7072f8Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
12903d7072f8Seschrock 
1291fa9e4066Sahrens 	if (locked)
1292fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1293fa9e4066Sahrens 
1294fa9e4066Sahrens 	*spapp = spa;
1295fa9e4066Sahrens 
1296fa9e4066Sahrens 	if (config != NULL) {
1297ea8dc4b6Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
1298fa9e4066Sahrens 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1299ea8dc4b6Seschrock 		spa_config_exit(spa, FTAG);
1300fa9e4066Sahrens 	}
1301fa9e4066Sahrens 
1302fa9e4066Sahrens 	return (0);
1303fa9e4066Sahrens }
1304fa9e4066Sahrens 
1305fa9e4066Sahrens int
1306fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
1307fa9e4066Sahrens {
1308fa9e4066Sahrens 	return (spa_open_common(name, spapp, tag, NULL));
1309fa9e4066Sahrens }
1310fa9e4066Sahrens 
1311ea8dc4b6Seschrock /*
1312ea8dc4b6Seschrock  * Lookup the given spa_t, incrementing the inject count in the process,
1313ea8dc4b6Seschrock  * preventing it from being exported or destroyed.
1314ea8dc4b6Seschrock  */
1315ea8dc4b6Seschrock spa_t *
1316ea8dc4b6Seschrock spa_inject_addref(char *name)
1317ea8dc4b6Seschrock {
1318ea8dc4b6Seschrock 	spa_t *spa;
1319ea8dc4b6Seschrock 
1320ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
1321ea8dc4b6Seschrock 	if ((spa = spa_lookup(name)) == NULL) {
1322ea8dc4b6Seschrock 		mutex_exit(&spa_namespace_lock);
1323ea8dc4b6Seschrock 		return (NULL);
1324ea8dc4b6Seschrock 	}
1325ea8dc4b6Seschrock 	spa->spa_inject_ref++;
1326ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1327ea8dc4b6Seschrock 
1328ea8dc4b6Seschrock 	return (spa);
1329ea8dc4b6Seschrock }
1330ea8dc4b6Seschrock 
1331ea8dc4b6Seschrock void
1332ea8dc4b6Seschrock spa_inject_delref(spa_t *spa)
1333ea8dc4b6Seschrock {
1334ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
1335ea8dc4b6Seschrock 	spa->spa_inject_ref--;
1336ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1337ea8dc4b6Seschrock }
1338ea8dc4b6Seschrock 
133999653d4eSeschrock static void
134099653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config)
134199653d4eSeschrock {
134299653d4eSeschrock 	nvlist_t **spares;
134399653d4eSeschrock 	uint_t i, nspares;
134499653d4eSeschrock 	nvlist_t *nvroot;
134599653d4eSeschrock 	uint64_t guid;
134699653d4eSeschrock 	vdev_stat_t *vs;
134799653d4eSeschrock 	uint_t vsc;
134839c23413Seschrock 	uint64_t pool;
134999653d4eSeschrock 
135099653d4eSeschrock 	if (spa->spa_nspares == 0)
135199653d4eSeschrock 		return;
135299653d4eSeschrock 
135399653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist(config,
135499653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
135599653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
135699653d4eSeschrock 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
135799653d4eSeschrock 	if (nspares != 0) {
135899653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(nvroot,
135999653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
136099653d4eSeschrock 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
136199653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
136299653d4eSeschrock 
136399653d4eSeschrock 		/*
136499653d4eSeschrock 		 * Go through and find any spares which have since been
136599653d4eSeschrock 		 * repurposed as an active spare.  If this is the case, update
136699653d4eSeschrock 		 * their status appropriately.
136799653d4eSeschrock 		 */
136899653d4eSeschrock 		for (i = 0; i < nspares; i++) {
136999653d4eSeschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
137099653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &guid) == 0);
137139c23413Seschrock 			if (spa_spare_exists(guid, &pool) && pool != 0ULL) {
137299653d4eSeschrock 				VERIFY(nvlist_lookup_uint64_array(
137399653d4eSeschrock 				    spares[i], ZPOOL_CONFIG_STATS,
137499653d4eSeschrock 				    (uint64_t **)&vs, &vsc) == 0);
137599653d4eSeschrock 				vs->vs_state = VDEV_STATE_CANT_OPEN;
137699653d4eSeschrock 				vs->vs_aux = VDEV_AUX_SPARED;
137799653d4eSeschrock 			}
137899653d4eSeschrock 		}
137999653d4eSeschrock 	}
138099653d4eSeschrock }
138199653d4eSeschrock 
1382fa9e4066Sahrens int
1383ea8dc4b6Seschrock spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1384fa9e4066Sahrens {
1385fa9e4066Sahrens 	int error;
1386fa9e4066Sahrens 	spa_t *spa;
1387fa9e4066Sahrens 
1388fa9e4066Sahrens 	*config = NULL;
1389fa9e4066Sahrens 	error = spa_open_common(name, &spa, FTAG, config);
1390fa9e4066Sahrens 
139199653d4eSeschrock 	if (spa && *config != NULL) {
1392ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
1393ea8dc4b6Seschrock 		    spa_get_errlog_size(spa)) == 0);
1394ea8dc4b6Seschrock 
139599653d4eSeschrock 		spa_add_spares(spa, *config);
139699653d4eSeschrock 	}
139799653d4eSeschrock 
1398ea8dc4b6Seschrock 	/*
1399ea8dc4b6Seschrock 	 * We want to get the alternate root even for faulted pools, so we cheat
1400ea8dc4b6Seschrock 	 * and call spa_lookup() directly.
1401ea8dc4b6Seschrock 	 */
1402ea8dc4b6Seschrock 	if (altroot) {
1403ea8dc4b6Seschrock 		if (spa == NULL) {
1404ea8dc4b6Seschrock 			mutex_enter(&spa_namespace_lock);
1405ea8dc4b6Seschrock 			spa = spa_lookup(name);
1406ea8dc4b6Seschrock 			if (spa)
1407ea8dc4b6Seschrock 				spa_altroot(spa, altroot, buflen);
1408ea8dc4b6Seschrock 			else
1409ea8dc4b6Seschrock 				altroot[0] = '\0';
1410ea8dc4b6Seschrock 			spa = NULL;
1411ea8dc4b6Seschrock 			mutex_exit(&spa_namespace_lock);
1412ea8dc4b6Seschrock 		} else {
1413ea8dc4b6Seschrock 			spa_altroot(spa, altroot, buflen);
1414ea8dc4b6Seschrock 		}
1415ea8dc4b6Seschrock 	}
1416ea8dc4b6Seschrock 
1417fa9e4066Sahrens 	if (spa != NULL)
1418fa9e4066Sahrens 		spa_close(spa, FTAG);
1419fa9e4066Sahrens 
1420fa9e4066Sahrens 	return (error);
1421fa9e4066Sahrens }
1422fa9e4066Sahrens 
142399653d4eSeschrock /*
142499653d4eSeschrock  * Validate that the 'spares' array is well formed.  We must have an array of
142539c23413Seschrock  * nvlists, each which describes a valid leaf vdev.  If this is an import (mode
142639c23413Seschrock  * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long
142739c23413Seschrock  * as they are well-formed.
142899653d4eSeschrock  */
142999653d4eSeschrock static int
143099653d4eSeschrock spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
143199653d4eSeschrock {
143299653d4eSeschrock 	nvlist_t **spares;
143399653d4eSeschrock 	uint_t i, nspares;
143499653d4eSeschrock 	vdev_t *vd;
143599653d4eSeschrock 	int error;
143699653d4eSeschrock 
143799653d4eSeschrock 	/*
143899653d4eSeschrock 	 * It's acceptable to have no spares specified.
143999653d4eSeschrock 	 */
144099653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
144199653d4eSeschrock 	    &spares, &nspares) != 0)
144299653d4eSeschrock 		return (0);
144399653d4eSeschrock 
144499653d4eSeschrock 	if (nspares == 0)
144599653d4eSeschrock 		return (EINVAL);
144699653d4eSeschrock 
144799653d4eSeschrock 	/*
144899653d4eSeschrock 	 * Make sure the pool is formatted with a version that supports hot
144999653d4eSeschrock 	 * spares.
145099653d4eSeschrock 	 */
1451e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_SPARES)
145299653d4eSeschrock 		return (ENOTSUP);
145399653d4eSeschrock 
145439c23413Seschrock 	/*
145539c23413Seschrock 	 * Set the pending spare list so we correctly handle device in-use
145639c23413Seschrock 	 * checking.
145739c23413Seschrock 	 */
145839c23413Seschrock 	spa->spa_pending_spares = spares;
145939c23413Seschrock 	spa->spa_pending_nspares = nspares;
146039c23413Seschrock 
146199653d4eSeschrock 	for (i = 0; i < nspares; i++) {
146299653d4eSeschrock 		if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0,
146399653d4eSeschrock 		    mode)) != 0)
146439c23413Seschrock 			goto out;
146599653d4eSeschrock 
146699653d4eSeschrock 		if (!vd->vdev_ops->vdev_op_leaf) {
146799653d4eSeschrock 			vdev_free(vd);
146839c23413Seschrock 			error = EINVAL;
146939c23413Seschrock 			goto out;
147099653d4eSeschrock 		}
147199653d4eSeschrock 
147299653d4eSeschrock 		vd->vdev_top = vd;
147399653d4eSeschrock 
147439c23413Seschrock 		if ((error = vdev_open(vd)) == 0 &&
147539c23413Seschrock 		    (error = vdev_label_init(vd, crtxg,
147639c23413Seschrock 		    VDEV_LABEL_SPARE)) == 0) {
147739c23413Seschrock 			VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID,
147839c23413Seschrock 			    vd->vdev_guid) == 0);
147939c23413Seschrock 		}
148099653d4eSeschrock 
148199653d4eSeschrock 		vdev_free(vd);
148239c23413Seschrock 
148339c23413Seschrock 		if (error && mode != VDEV_ALLOC_SPARE)
148439c23413Seschrock 			goto out;
148539c23413Seschrock 		else
148639c23413Seschrock 			error = 0;
148799653d4eSeschrock 	}
148899653d4eSeschrock 
148939c23413Seschrock out:
149039c23413Seschrock 	spa->spa_pending_spares = NULL;
149139c23413Seschrock 	spa->spa_pending_nspares = 0;
149239c23413Seschrock 	return (error);
149399653d4eSeschrock }
149499653d4eSeschrock 
1495fa9e4066Sahrens /*
1496fa9e4066Sahrens  * Pool Creation
1497fa9e4066Sahrens  */
1498fa9e4066Sahrens int
1499990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
1500228975ccSek     const char *history_str)
1501fa9e4066Sahrens {
1502fa9e4066Sahrens 	spa_t *spa;
1503990b4856Slling 	char *altroot = NULL;
15040373e76bSbonwick 	vdev_t *rvd;
1505fa9e4066Sahrens 	dsl_pool_t *dp;
1506fa9e4066Sahrens 	dmu_tx_t *tx;
150799653d4eSeschrock 	int c, error = 0;
1508fa9e4066Sahrens 	uint64_t txg = TXG_INITIAL;
150999653d4eSeschrock 	nvlist_t **spares;
151099653d4eSeschrock 	uint_t nspares;
1511990b4856Slling 	uint64_t version;
1512fa9e4066Sahrens 
1513fa9e4066Sahrens 	/*
1514fa9e4066Sahrens 	 * If this pool already exists, return failure.
1515fa9e4066Sahrens 	 */
1516fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1517fa9e4066Sahrens 	if (spa_lookup(pool) != NULL) {
1518fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1519fa9e4066Sahrens 		return (EEXIST);
1520fa9e4066Sahrens 	}
1521fa9e4066Sahrens 
1522fa9e4066Sahrens 	/*
1523fa9e4066Sahrens 	 * Allocate a new spa_t structure.
1524fa9e4066Sahrens 	 */
1525990b4856Slling 	(void) nvlist_lookup_string(props,
1526990b4856Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
15270373e76bSbonwick 	spa = spa_add(pool, altroot);
1528fa9e4066Sahrens 	spa_activate(spa);
1529fa9e4066Sahrens 
1530fa9e4066Sahrens 	spa->spa_uberblock.ub_txg = txg - 1;
1531990b4856Slling 
1532990b4856Slling 	if (props && (error = spa_prop_validate(spa, props))) {
1533990b4856Slling 		spa_unload(spa);
1534990b4856Slling 		spa_deactivate(spa);
1535990b4856Slling 		spa_remove(spa);
1536990b4856Slling 		return (error);
1537990b4856Slling 	}
1538990b4856Slling 
1539990b4856Slling 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
1540990b4856Slling 	    &version) != 0)
1541990b4856Slling 		version = SPA_VERSION;
1542990b4856Slling 	ASSERT(version <= SPA_VERSION);
1543990b4856Slling 	spa->spa_uberblock.ub_version = version;
1544fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
1545fa9e4066Sahrens 
15460373e76bSbonwick 	/*
15470373e76bSbonwick 	 * Create the root vdev.
15480373e76bSbonwick 	 */
15490373e76bSbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
15500373e76bSbonwick 
155199653d4eSeschrock 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
15520373e76bSbonwick 
155399653d4eSeschrock 	ASSERT(error != 0 || rvd != NULL);
155499653d4eSeschrock 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
15550373e76bSbonwick 
155699653d4eSeschrock 	if (error == 0 && rvd->vdev_children == 0)
15570373e76bSbonwick 		error = EINVAL;
155899653d4eSeschrock 
155999653d4eSeschrock 	if (error == 0 &&
156099653d4eSeschrock 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
156199653d4eSeschrock 	    (error = spa_validate_spares(spa, nvroot, txg,
156299653d4eSeschrock 	    VDEV_ALLOC_ADD)) == 0) {
156399653d4eSeschrock 		for (c = 0; c < rvd->vdev_children; c++)
156499653d4eSeschrock 			vdev_init(rvd->vdev_child[c], txg);
156599653d4eSeschrock 		vdev_config_dirty(rvd);
15660373e76bSbonwick 	}
15670373e76bSbonwick 
15680373e76bSbonwick 	spa_config_exit(spa, FTAG);
1569fa9e4066Sahrens 
157099653d4eSeschrock 	if (error != 0) {
1571fa9e4066Sahrens 		spa_unload(spa);
1572fa9e4066Sahrens 		spa_deactivate(spa);
1573fa9e4066Sahrens 		spa_remove(spa);
1574fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1575fa9e4066Sahrens 		return (error);
1576fa9e4066Sahrens 	}
1577fa9e4066Sahrens 
157899653d4eSeschrock 	/*
157999653d4eSeschrock 	 * Get the list of spares, if specified.
158099653d4eSeschrock 	 */
158199653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
158299653d4eSeschrock 	    &spares, &nspares) == 0) {
158399653d4eSeschrock 		VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME,
158499653d4eSeschrock 		    KM_SLEEP) == 0);
158599653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
158699653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
158799653d4eSeschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
158899653d4eSeschrock 		spa_load_spares(spa);
158999653d4eSeschrock 		spa_config_exit(spa, FTAG);
159099653d4eSeschrock 		spa->spa_sync_spares = B_TRUE;
159199653d4eSeschrock 	}
159299653d4eSeschrock 
1593fa9e4066Sahrens 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg);
1594fa9e4066Sahrens 	spa->spa_meta_objset = dp->dp_meta_objset;
1595fa9e4066Sahrens 
1596fa9e4066Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
1597fa9e4066Sahrens 
1598fa9e4066Sahrens 	/*
1599fa9e4066Sahrens 	 * Create the pool config object.
1600fa9e4066Sahrens 	 */
1601fa9e4066Sahrens 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
1602fa9e4066Sahrens 	    DMU_OT_PACKED_NVLIST, 1 << 14,
1603fa9e4066Sahrens 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
1604fa9e4066Sahrens 
1605ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
1606fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
1607ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
1608ea8dc4b6Seschrock 		cmn_err(CE_PANIC, "failed to add pool config");
1609ea8dc4b6Seschrock 	}
1610fa9e4066Sahrens 
1611990b4856Slling 	/* Newly created pools with the right version are always deflated. */
1612990b4856Slling 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
1613990b4856Slling 		spa->spa_deflate = TRUE;
1614990b4856Slling 		if (zap_add(spa->spa_meta_objset,
1615990b4856Slling 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
1616990b4856Slling 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
1617990b4856Slling 			cmn_err(CE_PANIC, "failed to add deflate");
1618990b4856Slling 		}
161999653d4eSeschrock 	}
162099653d4eSeschrock 
1621fa9e4066Sahrens 	/*
1622fa9e4066Sahrens 	 * Create the deferred-free bplist object.  Turn off compression
1623fa9e4066Sahrens 	 * because sync-to-convergence takes longer if the blocksize
1624fa9e4066Sahrens 	 * keeps changing.
1625fa9e4066Sahrens 	 */
1626fa9e4066Sahrens 	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
1627fa9e4066Sahrens 	    1 << 14, tx);
1628fa9e4066Sahrens 	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
1629fa9e4066Sahrens 	    ZIO_COMPRESS_OFF, tx);
1630fa9e4066Sahrens 
1631ea8dc4b6Seschrock 	if (zap_add(spa->spa_meta_objset,
1632fa9e4066Sahrens 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
1633ea8dc4b6Seschrock 	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
1634ea8dc4b6Seschrock 		cmn_err(CE_PANIC, "failed to add bplist");
1635ea8dc4b6Seschrock 	}
1636fa9e4066Sahrens 
163706eeb2adSek 	/*
163806eeb2adSek 	 * Create the pool's history object.
163906eeb2adSek 	 */
1640990b4856Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
1641990b4856Slling 		spa_history_create_obj(spa, tx);
1642990b4856Slling 
1643990b4856Slling 	/*
1644990b4856Slling 	 * Set pool properties.
1645990b4856Slling 	 */
1646990b4856Slling 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
1647990b4856Slling 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1648990b4856Slling 	spa->spa_temporary = zpool_prop_default_numeric(ZPOOL_PROP_TEMPORARY);
1649*0a4e9518Sgw 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
1650990b4856Slling 	if (props)
1651990b4856Slling 		spa_sync_props(spa, props, CRED(), tx);
165206eeb2adSek 
1653fa9e4066Sahrens 	dmu_tx_commit(tx);
1654fa9e4066Sahrens 
1655fa9e4066Sahrens 	spa->spa_sync_on = B_TRUE;
1656fa9e4066Sahrens 	txg_sync_start(spa->spa_dsl_pool);
1657fa9e4066Sahrens 
1658fa9e4066Sahrens 	/*
1659fa9e4066Sahrens 	 * We explicitly wait for the first transaction to complete so that our
1660fa9e4066Sahrens 	 * bean counters are appropriately updated.
1661fa9e4066Sahrens 	 */
1662fa9e4066Sahrens 	txg_wait_synced(spa->spa_dsl_pool, txg);
1663fa9e4066Sahrens 
1664fa9e4066Sahrens 	spa_config_sync();
1665fa9e4066Sahrens 
1666990b4856Slling 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
1667228975ccSek 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
1668228975ccSek 
1669fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1670fa9e4066Sahrens 
1671fa9e4066Sahrens 	return (0);
1672fa9e4066Sahrens }
1673fa9e4066Sahrens 
1674fa9e4066Sahrens /*
1675fa9e4066Sahrens  * Import the given pool into the system.  We set up the necessary spa_t and
1676fa9e4066Sahrens  * then call spa_load() to do the dirty work.
1677fa9e4066Sahrens  */
1678fa9e4066Sahrens int
1679990b4856Slling spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
1680fa9e4066Sahrens {
1681fa9e4066Sahrens 	spa_t *spa;
1682990b4856Slling 	char *altroot = NULL;
1683fa9e4066Sahrens 	int error;
168499653d4eSeschrock 	nvlist_t *nvroot;
168599653d4eSeschrock 	nvlist_t **spares;
168699653d4eSeschrock 	uint_t nspares;
1687fa9e4066Sahrens 
1688fa9e4066Sahrens 	/*
1689fa9e4066Sahrens 	 * If a pool with this name exists, return failure.
1690fa9e4066Sahrens 	 */
1691fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1692fa9e4066Sahrens 	if (spa_lookup(pool) != NULL) {
1693fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1694fa9e4066Sahrens 		return (EEXIST);
1695fa9e4066Sahrens 	}
1696fa9e4066Sahrens 
1697fa9e4066Sahrens 	/*
16980373e76bSbonwick 	 * Create and initialize the spa structure.
1699fa9e4066Sahrens 	 */
1700990b4856Slling 	(void) nvlist_lookup_string(props,
1701990b4856Slling 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
17020373e76bSbonwick 	spa = spa_add(pool, altroot);
1703fa9e4066Sahrens 	spa_activate(spa);
1704fa9e4066Sahrens 
17055dabedeeSbonwick 	/*
17060373e76bSbonwick 	 * Pass off the heavy lifting to spa_load().
1707ecc2d604Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
1708ecc2d604Sbonwick 	 * is actually the one to trust when doing an import.
17095dabedeeSbonwick 	 */
1710ecc2d604Sbonwick 	error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE);
1711fa9e4066Sahrens 
171299653d4eSeschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
171399653d4eSeschrock 	/*
171499653d4eSeschrock 	 * Toss any existing sparelist, as it doesn't have any validity anymore,
171599653d4eSeschrock 	 * and conflicts with spa_has_spare().
171699653d4eSeschrock 	 */
171799653d4eSeschrock 	if (spa->spa_sparelist) {
171899653d4eSeschrock 		nvlist_free(spa->spa_sparelist);
171999653d4eSeschrock 		spa->spa_sparelist = NULL;
172099653d4eSeschrock 		spa_load_spares(spa);
172199653d4eSeschrock 	}
172299653d4eSeschrock 
172399653d4eSeschrock 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
172499653d4eSeschrock 	    &nvroot) == 0);
1725990b4856Slling 	if (error == 0) {
172699653d4eSeschrock 		error = spa_validate_spares(spa, nvroot, -1ULL,
172799653d4eSeschrock 		    VDEV_ALLOC_SPARE);
1728990b4856Slling 	}
172999653d4eSeschrock 	spa_config_exit(spa, FTAG);
173099653d4eSeschrock 
1731990b4856Slling 	if (error != 0 || (props && (error = spa_prop_set(spa, props)))) {
1732fa9e4066Sahrens 		spa_unload(spa);
1733fa9e4066Sahrens 		spa_deactivate(spa);
1734fa9e4066Sahrens 		spa_remove(spa);
1735fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1736fa9e4066Sahrens 		return (error);
1737fa9e4066Sahrens 	}
1738fa9e4066Sahrens 
173999653d4eSeschrock 	/*
174099653d4eSeschrock 	 * Override any spares as specified by the user, as these may have
174199653d4eSeschrock 	 * correct device names/devids, etc.
174299653d4eSeschrock 	 */
174399653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
174499653d4eSeschrock 	    &spares, &nspares) == 0) {
174599653d4eSeschrock 		if (spa->spa_sparelist)
174699653d4eSeschrock 			VERIFY(nvlist_remove(spa->spa_sparelist,
174799653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
174899653d4eSeschrock 		else
174999653d4eSeschrock 			VERIFY(nvlist_alloc(&spa->spa_sparelist,
175099653d4eSeschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
175199653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
175299653d4eSeschrock 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
175399653d4eSeschrock 		spa_config_enter(spa, RW_WRITER, FTAG);
175499653d4eSeschrock 		spa_load_spares(spa);
175599653d4eSeschrock 		spa_config_exit(spa, FTAG);
175699653d4eSeschrock 		spa->spa_sync_spares = B_TRUE;
175799653d4eSeschrock 	}
175899653d4eSeschrock 
17590373e76bSbonwick 	/*
17600373e76bSbonwick 	 * Update the config cache to include the newly-imported pool.
17610373e76bSbonwick 	 */
1762de6628f0Sck 	if (spa_mode & FWRITE)
1763de6628f0Sck 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
17640373e76bSbonwick 
1765fa9e4066Sahrens 	/*
1766fa9e4066Sahrens 	 * Resilver anything that's out of date.
1767fa9e4066Sahrens 	 */
1768fa9e4066Sahrens 	if (spa_mode & FWRITE)
1769fa9e4066Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1770fa9e4066Sahrens 
17713d7072f8Seschrock 	mutex_exit(&spa_namespace_lock);
17723d7072f8Seschrock 
1773fa9e4066Sahrens 	return (0);
1774fa9e4066Sahrens }
1775fa9e4066Sahrens 
1776fa9e4066Sahrens /*
1777fa9e4066Sahrens  * This (illegal) pool name is used when temporarily importing a spa_t in order
1778fa9e4066Sahrens  * to get the vdev stats associated with the imported devices.
1779fa9e4066Sahrens  */
1780fa9e4066Sahrens #define	TRYIMPORT_NAME	"$import"
1781fa9e4066Sahrens 
1782fa9e4066Sahrens nvlist_t *
1783fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig)
1784fa9e4066Sahrens {
1785fa9e4066Sahrens 	nvlist_t *config = NULL;
1786fa9e4066Sahrens 	char *poolname;
1787fa9e4066Sahrens 	spa_t *spa;
1788fa9e4066Sahrens 	uint64_t state;
1789fa9e4066Sahrens 
1790fa9e4066Sahrens 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
1791fa9e4066Sahrens 		return (NULL);
1792fa9e4066Sahrens 
1793fa9e4066Sahrens 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
1794fa9e4066Sahrens 		return (NULL);
1795fa9e4066Sahrens 
1796fa9e4066Sahrens 	/*
17970373e76bSbonwick 	 * Create and initialize the spa structure.
1798fa9e4066Sahrens 	 */
17990373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
18000373e76bSbonwick 	spa = spa_add(TRYIMPORT_NAME, NULL);
1801fa9e4066Sahrens 	spa_activate(spa);
1802fa9e4066Sahrens 
1803fa9e4066Sahrens 	/*
18040373e76bSbonwick 	 * Pass off the heavy lifting to spa_load().
1805ecc2d604Sbonwick 	 * Pass TRUE for mosconfig because the user-supplied config
1806ecc2d604Sbonwick 	 * is actually the one to trust when doing an import.
1807fa9e4066Sahrens 	 */
1808ecc2d604Sbonwick 	(void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
1809fa9e4066Sahrens 
1810fa9e4066Sahrens 	/*
1811fa9e4066Sahrens 	 * If 'tryconfig' was at least parsable, return the current config.
1812fa9e4066Sahrens 	 */
1813fa9e4066Sahrens 	if (spa->spa_root_vdev != NULL) {
18140373e76bSbonwick 		spa_config_enter(spa, RW_READER, FTAG);
1815fa9e4066Sahrens 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
18160373e76bSbonwick 		spa_config_exit(spa, FTAG);
1817fa9e4066Sahrens 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
1818fa9e4066Sahrens 		    poolname) == 0);
1819fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1820fa9e4066Sahrens 		    state) == 0);
182195173954Sek 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
182295173954Sek 		    spa->spa_uberblock.ub_timestamp) == 0);
182399653d4eSeschrock 
182499653d4eSeschrock 		/*
182599653d4eSeschrock 		 * Add the list of hot spares.
182699653d4eSeschrock 		 */
182799653d4eSeschrock 		spa_add_spares(spa, config);
1828fa9e4066Sahrens 	}
1829fa9e4066Sahrens 
1830fa9e4066Sahrens 	spa_unload(spa);
1831fa9e4066Sahrens 	spa_deactivate(spa);
1832fa9e4066Sahrens 	spa_remove(spa);
1833fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1834fa9e4066Sahrens 
1835fa9e4066Sahrens 	return (config);
1836fa9e4066Sahrens }
1837fa9e4066Sahrens 
1838fa9e4066Sahrens /*
1839fa9e4066Sahrens  * Pool export/destroy
1840fa9e4066Sahrens  *
1841fa9e4066Sahrens  * The act of destroying or exporting a pool is very simple.  We make sure there
1842fa9e4066Sahrens  * is no more pending I/O and any references to the pool are gone.  Then, we
1843fa9e4066Sahrens  * update the pool state and sync all the labels to disk, removing the
1844fa9e4066Sahrens  * configuration from the cache afterwards.
1845fa9e4066Sahrens  */
1846fa9e4066Sahrens static int
184744cd46caSbillm spa_export_common(char *pool, int new_state, nvlist_t **oldconfig)
1848fa9e4066Sahrens {
1849fa9e4066Sahrens 	spa_t *spa;
1850fa9e4066Sahrens 
185144cd46caSbillm 	if (oldconfig)
185244cd46caSbillm 		*oldconfig = NULL;
185344cd46caSbillm 
1854fa9e4066Sahrens 	if (!(spa_mode & FWRITE))
1855fa9e4066Sahrens 		return (EROFS);
1856fa9e4066Sahrens 
1857fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1858fa9e4066Sahrens 	if ((spa = spa_lookup(pool)) == NULL) {
1859fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1860fa9e4066Sahrens 		return (ENOENT);
1861fa9e4066Sahrens 	}
1862fa9e4066Sahrens 
1863ea8dc4b6Seschrock 	/*
1864ea8dc4b6Seschrock 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
1865ea8dc4b6Seschrock 	 * reacquire the namespace lock, and see if we can export.
1866ea8dc4b6Seschrock 	 */
1867ea8dc4b6Seschrock 	spa_open_ref(spa, FTAG);
1868ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1869ea8dc4b6Seschrock 	spa_async_suspend(spa);
1870ea8dc4b6Seschrock 	mutex_enter(&spa_namespace_lock);
1871ea8dc4b6Seschrock 	spa_close(spa, FTAG);
1872ea8dc4b6Seschrock 
1873fa9e4066Sahrens 	/*
1874fa9e4066Sahrens 	 * The pool will be in core if it's openable,
1875fa9e4066Sahrens 	 * in which case we can modify its state.
1876fa9e4066Sahrens 	 */
1877fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
1878fa9e4066Sahrens 		/*
1879fa9e4066Sahrens 		 * Objsets may be open only because they're dirty, so we
1880fa9e4066Sahrens 		 * have to force it to sync before checking spa_refcnt.
1881fa9e4066Sahrens 		 */
1882fa9e4066Sahrens 		spa_scrub_suspend(spa);
1883fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, 0);
1884fa9e4066Sahrens 
1885ea8dc4b6Seschrock 		/*
1886ea8dc4b6Seschrock 		 * A pool cannot be exported or destroyed if there are active
1887ea8dc4b6Seschrock 		 * references.  If we are resetting a pool, allow references by
1888ea8dc4b6Seschrock 		 * fault injection handlers.
1889ea8dc4b6Seschrock 		 */
1890ea8dc4b6Seschrock 		if (!spa_refcount_zero(spa) ||
1891ea8dc4b6Seschrock 		    (spa->spa_inject_ref != 0 &&
1892ea8dc4b6Seschrock 		    new_state != POOL_STATE_UNINITIALIZED)) {
1893fa9e4066Sahrens 			spa_scrub_resume(spa);
1894ea8dc4b6Seschrock 			spa_async_resume(spa);
1895fa9e4066Sahrens 			mutex_exit(&spa_namespace_lock);
1896fa9e4066Sahrens 			return (EBUSY);
1897fa9e4066Sahrens 		}
1898fa9e4066Sahrens 
1899fa9e4066Sahrens 		spa_scrub_resume(spa);
1900fa9e4066Sahrens 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
1901fa9e4066Sahrens 
1902fa9e4066Sahrens 		/*
1903fa9e4066Sahrens 		 * We want this to be reflected on every label,
1904fa9e4066Sahrens 		 * so mark them all dirty.  spa_unload() will do the
1905fa9e4066Sahrens 		 * final sync that pushes these changes out.
1906fa9e4066Sahrens 		 */
1907ea8dc4b6Seschrock 		if (new_state != POOL_STATE_UNINITIALIZED) {
19085dabedeeSbonwick 			spa_config_enter(spa, RW_WRITER, FTAG);
1909ea8dc4b6Seschrock 			spa->spa_state = new_state;
19100373e76bSbonwick 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
1911ea8dc4b6Seschrock 			vdev_config_dirty(spa->spa_root_vdev);
19125dabedeeSbonwick 			spa_config_exit(spa, FTAG);
1913ea8dc4b6Seschrock 		}
1914fa9e4066Sahrens 	}
1915fa9e4066Sahrens 
19163d7072f8Seschrock 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
19173d7072f8Seschrock 
1918fa9e4066Sahrens 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
1919fa9e4066Sahrens 		spa_unload(spa);
1920fa9e4066Sahrens 		spa_deactivate(spa);
1921fa9e4066Sahrens 	}
1922fa9e4066Sahrens 
192344cd46caSbillm 	if (oldconfig && spa->spa_config)
192444cd46caSbillm 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
192544cd46caSbillm 
1926ea8dc4b6Seschrock 	if (new_state != POOL_STATE_UNINITIALIZED) {
1927ea8dc4b6Seschrock 		spa_remove(spa);
1928ea8dc4b6Seschrock 		spa_config_sync();
1929ea8dc4b6Seschrock 	}
1930fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1931fa9e4066Sahrens 
1932fa9e4066Sahrens 	return (0);
1933fa9e4066Sahrens }
1934fa9e4066Sahrens 
1935fa9e4066Sahrens /*
1936fa9e4066Sahrens  * Destroy a storage pool.
1937fa9e4066Sahrens  */
1938fa9e4066Sahrens int
1939fa9e4066Sahrens spa_destroy(char *pool)
1940fa9e4066Sahrens {
194144cd46caSbillm 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL));
1942fa9e4066Sahrens }
1943fa9e4066Sahrens 
1944fa9e4066Sahrens /*
1945fa9e4066Sahrens  * Export a storage pool.
1946fa9e4066Sahrens  */
1947fa9e4066Sahrens int
194844cd46caSbillm spa_export(char *pool, nvlist_t **oldconfig)
1949fa9e4066Sahrens {
195044cd46caSbillm 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig));
1951fa9e4066Sahrens }
1952fa9e4066Sahrens 
1953ea8dc4b6Seschrock /*
1954ea8dc4b6Seschrock  * Similar to spa_export(), this unloads the spa_t without actually removing it
1955ea8dc4b6Seschrock  * from the namespace in any way.
1956ea8dc4b6Seschrock  */
1957ea8dc4b6Seschrock int
1958ea8dc4b6Seschrock spa_reset(char *pool)
1959ea8dc4b6Seschrock {
196044cd46caSbillm 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL));
1961ea8dc4b6Seschrock }
1962ea8dc4b6Seschrock 
1963ea8dc4b6Seschrock 
1964fa9e4066Sahrens /*
1965fa9e4066Sahrens  * ==========================================================================
1966fa9e4066Sahrens  * Device manipulation
1967fa9e4066Sahrens  * ==========================================================================
1968fa9e4066Sahrens  */
1969fa9e4066Sahrens 
1970fa9e4066Sahrens /*
19718654d025Sperrin  * Add a device to a storage pool.
1972fa9e4066Sahrens  */
1973fa9e4066Sahrens int
1974fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
1975fa9e4066Sahrens {
1976fa9e4066Sahrens 	uint64_t txg;
19770373e76bSbonwick 	int c, error;
1978fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
19790e34b6a7Sbonwick 	vdev_t *vd, *tvd;
198099653d4eSeschrock 	nvlist_t **spares;
198199653d4eSeschrock 	uint_t i, nspares;
1982fa9e4066Sahrens 
1983fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
1984fa9e4066Sahrens 
198599653d4eSeschrock 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
198699653d4eSeschrock 	    VDEV_ALLOC_ADD)) != 0)
198799653d4eSeschrock 		return (spa_vdev_exit(spa, NULL, txg, error));
1988fa9e4066Sahrens 
198939c23413Seschrock 	spa->spa_pending_vdev = vd;
199099653d4eSeschrock 
199199653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
199299653d4eSeschrock 	    &spares, &nspares) != 0)
199399653d4eSeschrock 		nspares = 0;
199499653d4eSeschrock 
199539c23413Seschrock 	if (vd->vdev_children == 0 && nspares == 0) {
199639c23413Seschrock 		spa->spa_pending_vdev = NULL;
1997fa9e4066Sahrens 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
199839c23413Seschrock 	}
1999fa9e4066Sahrens 
200099653d4eSeschrock 	if (vd->vdev_children != 0) {
200139c23413Seschrock 		if ((error = vdev_create(vd, txg, B_FALSE)) != 0) {
200239c23413Seschrock 			spa->spa_pending_vdev = NULL;
200399653d4eSeschrock 			return (spa_vdev_exit(spa, vd, txg, error));
200499653d4eSeschrock 		}
200599653d4eSeschrock 	}
200699653d4eSeschrock 
200739c23413Seschrock 	/*
200839c23413Seschrock 	 * We must validate the spares after checking the children.  Otherwise,
200939c23413Seschrock 	 * vdev_inuse() will blindly overwrite the spare.
201039c23413Seschrock 	 */
201139c23413Seschrock 	if ((error = spa_validate_spares(spa, nvroot, txg,
201239c23413Seschrock 	    VDEV_ALLOC_ADD)) != 0) {
201339c23413Seschrock 		spa->spa_pending_vdev = NULL;
201439c23413Seschrock 		return (spa_vdev_exit(spa, vd, txg, error));
201539c23413Seschrock 	}
201639c23413Seschrock 
201739c23413Seschrock 	spa->spa_pending_vdev = NULL;
201839c23413Seschrock 
201939c23413Seschrock 	/*
202039c23413Seschrock 	 * Transfer each new top-level vdev from vd to rvd.
202139c23413Seschrock 	 */
202239c23413Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
202339c23413Seschrock 		tvd = vd->vdev_child[c];
202439c23413Seschrock 		vdev_remove_child(vd, tvd);
202539c23413Seschrock 		tvd->vdev_id = rvd->vdev_children;
202639c23413Seschrock 		vdev_add_child(rvd, tvd);
202739c23413Seschrock 		vdev_config_dirty(tvd);
202839c23413Seschrock 	}
202939c23413Seschrock 
203099653d4eSeschrock 	if (nspares != 0) {
203199653d4eSeschrock 		if (spa->spa_sparelist != NULL) {
203299653d4eSeschrock 			nvlist_t **oldspares;
203399653d4eSeschrock 			uint_t oldnspares;
203499653d4eSeschrock 			nvlist_t **newspares;
203599653d4eSeschrock 
203699653d4eSeschrock 			VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
203799653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0);
203899653d4eSeschrock 
203999653d4eSeschrock 			newspares = kmem_alloc(sizeof (void *) *
204099653d4eSeschrock 			    (nspares + oldnspares), KM_SLEEP);
204199653d4eSeschrock 			for (i = 0; i < oldnspares; i++)
204299653d4eSeschrock 				VERIFY(nvlist_dup(oldspares[i],
204399653d4eSeschrock 				    &newspares[i], KM_SLEEP) == 0);
204499653d4eSeschrock 			for (i = 0; i < nspares; i++)
204599653d4eSeschrock 				VERIFY(nvlist_dup(spares[i],
204699653d4eSeschrock 				    &newspares[i + oldnspares],
204799653d4eSeschrock 				    KM_SLEEP) == 0);
204899653d4eSeschrock 
204999653d4eSeschrock 			VERIFY(nvlist_remove(spa->spa_sparelist,
205099653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
205199653d4eSeschrock 
205299653d4eSeschrock 			VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
205399653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, newspares,
205499653d4eSeschrock 			    nspares + oldnspares) == 0);
205599653d4eSeschrock 			for (i = 0; i < oldnspares + nspares; i++)
205699653d4eSeschrock 				nvlist_free(newspares[i]);
205799653d4eSeschrock 			kmem_free(newspares, (oldnspares + nspares) *
205899653d4eSeschrock 			    sizeof (void *));
205999653d4eSeschrock 		} else {
206099653d4eSeschrock 			VERIFY(nvlist_alloc(&spa->spa_sparelist,
206199653d4eSeschrock 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
206299653d4eSeschrock 			VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist,
206399653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
206499653d4eSeschrock 		}
206599653d4eSeschrock 
206699653d4eSeschrock 		spa_load_spares(spa);
206799653d4eSeschrock 		spa->spa_sync_spares = B_TRUE;
2068fa9e4066Sahrens 	}
2069fa9e4066Sahrens 
2070fa9e4066Sahrens 	/*
20710e34b6a7Sbonwick 	 * We have to be careful when adding new vdevs to an existing pool.
20720e34b6a7Sbonwick 	 * If other threads start allocating from these vdevs before we
20730e34b6a7Sbonwick 	 * sync the config cache, and we lose power, then upon reboot we may
20740e34b6a7Sbonwick 	 * fail to open the pool because there are DVAs that the config cache
20750e34b6a7Sbonwick 	 * can't translate.  Therefore, we first add the vdevs without
20760e34b6a7Sbonwick 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
20770373e76bSbonwick 	 * and then let spa_config_update() initialize the new metaslabs.
20780e34b6a7Sbonwick 	 *
20790e34b6a7Sbonwick 	 * spa_load() checks for added-but-not-initialized vdevs, so that
20800e34b6a7Sbonwick 	 * if we lose power at any point in this sequence, the remaining
20810e34b6a7Sbonwick 	 * steps will be completed the next time we load the pool.
20820e34b6a7Sbonwick 	 */
20830373e76bSbonwick 	(void) spa_vdev_exit(spa, vd, txg, 0);
20840e34b6a7Sbonwick 
20850373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
20860373e76bSbonwick 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
20870373e76bSbonwick 	mutex_exit(&spa_namespace_lock);
2088fa9e4066Sahrens 
20890373e76bSbonwick 	return (0);
2090fa9e4066Sahrens }
2091fa9e4066Sahrens 
2092fa9e4066Sahrens /*
2093fa9e4066Sahrens  * Attach a device to a mirror.  The arguments are the path to any device
2094fa9e4066Sahrens  * in the mirror, and the nvroot for the new device.  If the path specifies
2095fa9e4066Sahrens  * a device that is not mirrored, we automatically insert the mirror vdev.
2096fa9e4066Sahrens  *
2097fa9e4066Sahrens  * If 'replacing' is specified, the new device is intended to replace the
2098fa9e4066Sahrens  * existing device; in this case the two devices are made into their own
20993d7072f8Seschrock  * mirror using the 'replacing' vdev, which is functionally identical to
2100fa9e4066Sahrens  * the mirror vdev (it actually reuses all the same ops) but has a few
2101fa9e4066Sahrens  * extra rules: you can't attach to it after it's been created, and upon
2102fa9e4066Sahrens  * completion of resilvering, the first disk (the one being replaced)
2103fa9e4066Sahrens  * is automatically detached.
2104fa9e4066Sahrens  */
2105fa9e4066Sahrens int
2106ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2107fa9e4066Sahrens {
2108fa9e4066Sahrens 	uint64_t txg, open_txg;
2109fa9e4066Sahrens 	int error;
2110fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2111fa9e4066Sahrens 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
211299653d4eSeschrock 	vdev_ops_t *pvops;
21138654d025Sperrin 	int is_log;
2114fa9e4066Sahrens 
2115fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
2116fa9e4066Sahrens 
2117ea8dc4b6Seschrock 	oldvd = vdev_lookup_by_guid(rvd, guid);
2118fa9e4066Sahrens 
2119fa9e4066Sahrens 	if (oldvd == NULL)
2120fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2121fa9e4066Sahrens 
21220e34b6a7Sbonwick 	if (!oldvd->vdev_ops->vdev_op_leaf)
21230e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
21240e34b6a7Sbonwick 
2125fa9e4066Sahrens 	pvd = oldvd->vdev_parent;
2126fa9e4066Sahrens 
212799653d4eSeschrock 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
21283d7072f8Seschrock 	    VDEV_ALLOC_ADD)) != 0)
21293d7072f8Seschrock 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
21303d7072f8Seschrock 
21313d7072f8Seschrock 	if (newrootvd->vdev_children != 1)
2132fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2133fa9e4066Sahrens 
2134fa9e4066Sahrens 	newvd = newrootvd->vdev_child[0];
2135fa9e4066Sahrens 
2136fa9e4066Sahrens 	if (!newvd->vdev_ops->vdev_op_leaf)
2137fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2138fa9e4066Sahrens 
213999653d4eSeschrock 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2140fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, error));
2141fa9e4066Sahrens 
21428654d025Sperrin 	/*
21438654d025Sperrin 	 * Spares can't replace logs
21448654d025Sperrin 	 */
21458654d025Sperrin 	is_log = oldvd->vdev_islog;
21468654d025Sperrin 	if (is_log && newvd->vdev_isspare)
21478654d025Sperrin 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
21488654d025Sperrin 
214999653d4eSeschrock 	if (!replacing) {
215099653d4eSeschrock 		/*
215199653d4eSeschrock 		 * For attach, the only allowable parent is a mirror or the root
215299653d4eSeschrock 		 * vdev.
215399653d4eSeschrock 		 */
215499653d4eSeschrock 		if (pvd->vdev_ops != &vdev_mirror_ops &&
215599653d4eSeschrock 		    pvd->vdev_ops != &vdev_root_ops)
215699653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
215799653d4eSeschrock 
215899653d4eSeschrock 		pvops = &vdev_mirror_ops;
215999653d4eSeschrock 	} else {
216099653d4eSeschrock 		/*
216199653d4eSeschrock 		 * Active hot spares can only be replaced by inactive hot
216299653d4eSeschrock 		 * spares.
216399653d4eSeschrock 		 */
216499653d4eSeschrock 		if (pvd->vdev_ops == &vdev_spare_ops &&
216599653d4eSeschrock 		    pvd->vdev_child[1] == oldvd &&
216699653d4eSeschrock 		    !spa_has_spare(spa, newvd->vdev_guid))
216799653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
216899653d4eSeschrock 
216999653d4eSeschrock 		/*
217099653d4eSeschrock 		 * If the source is a hot spare, and the parent isn't already a
217199653d4eSeschrock 		 * spare, then we want to create a new hot spare.  Otherwise, we
217239c23413Seschrock 		 * want to create a replacing vdev.  The user is not allowed to
217339c23413Seschrock 		 * attach to a spared vdev child unless the 'isspare' state is
217439c23413Seschrock 		 * the same (spare replaces spare, non-spare replaces
217539c23413Seschrock 		 * non-spare).
217699653d4eSeschrock 		 */
217799653d4eSeschrock 		if (pvd->vdev_ops == &vdev_replacing_ops)
217899653d4eSeschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
217939c23413Seschrock 		else if (pvd->vdev_ops == &vdev_spare_ops &&
218039c23413Seschrock 		    newvd->vdev_isspare != oldvd->vdev_isspare)
218139c23413Seschrock 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
218299653d4eSeschrock 		else if (pvd->vdev_ops != &vdev_spare_ops &&
218399653d4eSeschrock 		    newvd->vdev_isspare)
218499653d4eSeschrock 			pvops = &vdev_spare_ops;
218599653d4eSeschrock 		else
218699653d4eSeschrock 			pvops = &vdev_replacing_ops;
218799653d4eSeschrock 	}
218899653d4eSeschrock 
21892a79c5feSlling 	/*
21902a79c5feSlling 	 * Compare the new device size with the replaceable/attachable
21912a79c5feSlling 	 * device size.
21922a79c5feSlling 	 */
21932a79c5feSlling 	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2194fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2195fa9e4066Sahrens 
2196ecc2d604Sbonwick 	/*
2197ecc2d604Sbonwick 	 * The new device cannot have a higher alignment requirement
2198ecc2d604Sbonwick 	 * than the top-level vdev.
2199ecc2d604Sbonwick 	 */
2200ecc2d604Sbonwick 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2201fa9e4066Sahrens 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2202fa9e4066Sahrens 
2203fa9e4066Sahrens 	/*
2204fa9e4066Sahrens 	 * If this is an in-place replacement, update oldvd's path and devid
2205fa9e4066Sahrens 	 * to make it distinguishable from newvd, and unopenable from now on.
2206fa9e4066Sahrens 	 */
2207fa9e4066Sahrens 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2208fa9e4066Sahrens 		spa_strfree(oldvd->vdev_path);
2209fa9e4066Sahrens 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2210fa9e4066Sahrens 		    KM_SLEEP);
2211fa9e4066Sahrens 		(void) sprintf(oldvd->vdev_path, "%s/%s",
2212fa9e4066Sahrens 		    newvd->vdev_path, "old");
2213fa9e4066Sahrens 		if (oldvd->vdev_devid != NULL) {
2214fa9e4066Sahrens 			spa_strfree(oldvd->vdev_devid);
2215fa9e4066Sahrens 			oldvd->vdev_devid = NULL;
2216fa9e4066Sahrens 		}
2217fa9e4066Sahrens 	}
2218fa9e4066Sahrens 
2219fa9e4066Sahrens 	/*
222099653d4eSeschrock 	 * If the parent is not a mirror, or if we're replacing, insert the new
222199653d4eSeschrock 	 * mirror/replacing/spare vdev above oldvd.
2222fa9e4066Sahrens 	 */
2223fa9e4066Sahrens 	if (pvd->vdev_ops != pvops)
2224fa9e4066Sahrens 		pvd = vdev_add_parent(oldvd, pvops);
2225fa9e4066Sahrens 
2226fa9e4066Sahrens 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
2227fa9e4066Sahrens 	ASSERT(pvd->vdev_ops == pvops);
2228fa9e4066Sahrens 	ASSERT(oldvd->vdev_parent == pvd);
2229fa9e4066Sahrens 
2230fa9e4066Sahrens 	/*
2231fa9e4066Sahrens 	 * Extract the new device from its root and add it to pvd.
2232fa9e4066Sahrens 	 */
2233fa9e4066Sahrens 	vdev_remove_child(newrootvd, newvd);
2234fa9e4066Sahrens 	newvd->vdev_id = pvd->vdev_children;
2235fa9e4066Sahrens 	vdev_add_child(pvd, newvd);
2236fa9e4066Sahrens 
2237ea8dc4b6Seschrock 	/*
2238ea8dc4b6Seschrock 	 * If newvd is smaller than oldvd, but larger than its rsize,
2239ea8dc4b6Seschrock 	 * the addition of newvd may have decreased our parent's asize.
2240ea8dc4b6Seschrock 	 */
2241ea8dc4b6Seschrock 	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
2242ea8dc4b6Seschrock 
2243fa9e4066Sahrens 	tvd = newvd->vdev_top;
2244fa9e4066Sahrens 	ASSERT(pvd->vdev_top == tvd);
2245fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2246fa9e4066Sahrens 
2247fa9e4066Sahrens 	vdev_config_dirty(tvd);
2248fa9e4066Sahrens 
2249fa9e4066Sahrens 	/*
2250fa9e4066Sahrens 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
2251fa9e4066Sahrens 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2252fa9e4066Sahrens 	 */
2253fa9e4066Sahrens 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
2254fa9e4066Sahrens 
2255fa9e4066Sahrens 	mutex_enter(&newvd->vdev_dtl_lock);
2256fa9e4066Sahrens 	space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL,
2257fa9e4066Sahrens 	    open_txg - TXG_INITIAL + 1);
2258fa9e4066Sahrens 	mutex_exit(&newvd->vdev_dtl_lock);
2259fa9e4066Sahrens 
226039c23413Seschrock 	if (newvd->vdev_isspare)
226139c23413Seschrock 		spa_spare_activate(newvd);
2262ea8dc4b6Seschrock 
2263fa9e4066Sahrens 	/*
2264fa9e4066Sahrens 	 * Mark newvd's DTL dirty in this txg.
2265fa9e4066Sahrens 	 */
2266ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
2267fa9e4066Sahrens 
2268fa9e4066Sahrens 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2269fa9e4066Sahrens 
2270fa9e4066Sahrens 	/*
22713d7072f8Seschrock 	 * Kick off a resilver to update newvd.  We need to grab the namespace
22723d7072f8Seschrock 	 * lock because spa_scrub() needs to post a sysevent with the pool name.
2273fa9e4066Sahrens 	 */
22743d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
2275fa9e4066Sahrens 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
22763d7072f8Seschrock 	mutex_exit(&spa_namespace_lock);
2277fa9e4066Sahrens 
2278fa9e4066Sahrens 	return (0);
2279fa9e4066Sahrens }
2280fa9e4066Sahrens 
2281fa9e4066Sahrens /*
2282fa9e4066Sahrens  * Detach a device from a mirror or replacing vdev.
2283fa9e4066Sahrens  * If 'replace_done' is specified, only detach if the parent
2284fa9e4066Sahrens  * is a replacing vdev.
2285fa9e4066Sahrens  */
2286fa9e4066Sahrens int
2287ea8dc4b6Seschrock spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done)
2288fa9e4066Sahrens {
2289fa9e4066Sahrens 	uint64_t txg;
2290fa9e4066Sahrens 	int c, t, error;
2291fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2292fa9e4066Sahrens 	vdev_t *vd, *pvd, *cvd, *tvd;
229399653d4eSeschrock 	boolean_t unspare = B_FALSE;
229499653d4eSeschrock 	uint64_t unspare_guid;
2295fa9e4066Sahrens 
2296fa9e4066Sahrens 	txg = spa_vdev_enter(spa);
2297fa9e4066Sahrens 
2298ea8dc4b6Seschrock 	vd = vdev_lookup_by_guid(rvd, guid);
2299fa9e4066Sahrens 
2300fa9e4066Sahrens 	if (vd == NULL)
2301fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2302fa9e4066Sahrens 
23030e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
23040e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
23050e34b6a7Sbonwick 
2306fa9e4066Sahrens 	pvd = vd->vdev_parent;
2307fa9e4066Sahrens 
2308fa9e4066Sahrens 	/*
2309fa9e4066Sahrens 	 * If replace_done is specified, only remove this device if it's
231099653d4eSeschrock 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
231199653d4eSeschrock 	 * disk can be removed.
231299653d4eSeschrock 	 */
231399653d4eSeschrock 	if (replace_done) {
231499653d4eSeschrock 		if (pvd->vdev_ops == &vdev_replacing_ops) {
231599653d4eSeschrock 			if (vd->vdev_id != 0)
231699653d4eSeschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
231799653d4eSeschrock 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
231899653d4eSeschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
231999653d4eSeschrock 		}
232099653d4eSeschrock 	}
232199653d4eSeschrock 
232299653d4eSeschrock 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
2323e7437265Sahrens 	    spa_version(spa) >= SPA_VERSION_SPARES);
2324fa9e4066Sahrens 
2325fa9e4066Sahrens 	/*
232699653d4eSeschrock 	 * Only mirror, replacing, and spare vdevs support detach.
2327fa9e4066Sahrens 	 */
2328fa9e4066Sahrens 	if (pvd->vdev_ops != &vdev_replacing_ops &&
232999653d4eSeschrock 	    pvd->vdev_ops != &vdev_mirror_ops &&
233099653d4eSeschrock 	    pvd->vdev_ops != &vdev_spare_ops)
2331fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2332fa9e4066Sahrens 
2333fa9e4066Sahrens 	/*
2334fa9e4066Sahrens 	 * If there's only one replica, you can't detach it.
2335fa9e4066Sahrens 	 */
2336fa9e4066Sahrens 	if (pvd->vdev_children <= 1)
2337fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2338fa9e4066Sahrens 
2339fa9e4066Sahrens 	/*
2340fa9e4066Sahrens 	 * If all siblings have non-empty DTLs, this device may have the only
2341fa9e4066Sahrens 	 * valid copy of the data, which means we cannot safely detach it.
2342fa9e4066Sahrens 	 *
2343fa9e4066Sahrens 	 * XXX -- as in the vdev_offline() case, we really want a more
2344fa9e4066Sahrens 	 * precise DTL check.
2345fa9e4066Sahrens 	 */
2346fa9e4066Sahrens 	for (c = 0; c < pvd->vdev_children; c++) {
2347fa9e4066Sahrens 		uint64_t dirty;
2348fa9e4066Sahrens 
2349fa9e4066Sahrens 		cvd = pvd->vdev_child[c];
2350fa9e4066Sahrens 		if (cvd == vd)
2351fa9e4066Sahrens 			continue;
2352fa9e4066Sahrens 		if (vdev_is_dead(cvd))
2353fa9e4066Sahrens 			continue;
2354fa9e4066Sahrens 		mutex_enter(&cvd->vdev_dtl_lock);
2355fa9e4066Sahrens 		dirty = cvd->vdev_dtl_map.sm_space |
2356fa9e4066Sahrens 		    cvd->vdev_dtl_scrub.sm_space;
2357fa9e4066Sahrens 		mutex_exit(&cvd->vdev_dtl_lock);
2358fa9e4066Sahrens 		if (!dirty)
2359fa9e4066Sahrens 			break;
2360fa9e4066Sahrens 	}
236199653d4eSeschrock 
236299653d4eSeschrock 	/*
236399653d4eSeschrock 	 * If we are a replacing or spare vdev, then we can always detach the
236499653d4eSeschrock 	 * latter child, as that is how one cancels the operation.
236599653d4eSeschrock 	 */
236699653d4eSeschrock 	if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) &&
236799653d4eSeschrock 	    c == pvd->vdev_children)
2368fa9e4066Sahrens 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
2369fa9e4066Sahrens 
237099653d4eSeschrock 	/*
237199653d4eSeschrock 	 * If we are detaching the original disk from a spare, then it implies
237299653d4eSeschrock 	 * that the spare should become a real disk, and be removed from the
237399653d4eSeschrock 	 * active spare list for the pool.
237499653d4eSeschrock 	 */
237599653d4eSeschrock 	if (pvd->vdev_ops == &vdev_spare_ops &&
237699653d4eSeschrock 	    vd->vdev_id == 0)
237799653d4eSeschrock 		unspare = B_TRUE;
237899653d4eSeschrock 
2379fa9e4066Sahrens 	/*
2380fa9e4066Sahrens 	 * Erase the disk labels so the disk can be used for other things.
2381fa9e4066Sahrens 	 * This must be done after all other error cases are handled,
2382fa9e4066Sahrens 	 * but before we disembowel vd (so we can still do I/O to it).
2383fa9e4066Sahrens 	 * But if we can't do it, don't treat the error as fatal --
2384fa9e4066Sahrens 	 * it may be that the unwritability of the disk is the reason
2385fa9e4066Sahrens 	 * it's being detached!
2386fa9e4066Sahrens 	 */
238739c23413Seschrock 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
2388fa9e4066Sahrens 
2389fa9e4066Sahrens 	/*
2390fa9e4066Sahrens 	 * Remove vd from its parent and compact the parent's children.
2391fa9e4066Sahrens 	 */
2392fa9e4066Sahrens 	vdev_remove_child(pvd, vd);
2393fa9e4066Sahrens 	vdev_compact_children(pvd);
2394fa9e4066Sahrens 
2395fa9e4066Sahrens 	/*
2396fa9e4066Sahrens 	 * Remember one of the remaining children so we can get tvd below.
2397fa9e4066Sahrens 	 */
2398fa9e4066Sahrens 	cvd = pvd->vdev_child[0];
2399fa9e4066Sahrens 
240099653d4eSeschrock 	/*
240199653d4eSeschrock 	 * If we need to remove the remaining child from the list of hot spares,
240299653d4eSeschrock 	 * do it now, marking the vdev as no longer a spare in the process.  We
240399653d4eSeschrock 	 * must do this before vdev_remove_parent(), because that can change the
240499653d4eSeschrock 	 * GUID if it creates a new toplevel GUID.
240599653d4eSeschrock 	 */
240699653d4eSeschrock 	if (unspare) {
240799653d4eSeschrock 		ASSERT(cvd->vdev_isspare);
240839c23413Seschrock 		spa_spare_remove(cvd);
240999653d4eSeschrock 		unspare_guid = cvd->vdev_guid;
241099653d4eSeschrock 	}
241199653d4eSeschrock 
2412fa9e4066Sahrens 	/*
2413fa9e4066Sahrens 	 * If the parent mirror/replacing vdev only has one child,
2414fa9e4066Sahrens 	 * the parent is no longer needed.  Remove it from the tree.
2415fa9e4066Sahrens 	 */
2416fa9e4066Sahrens 	if (pvd->vdev_children == 1)
2417fa9e4066Sahrens 		vdev_remove_parent(cvd);
2418fa9e4066Sahrens 
2419fa9e4066Sahrens 	/*
2420fa9e4066Sahrens 	 * We don't set tvd until now because the parent we just removed
2421fa9e4066Sahrens 	 * may have been the previous top-level vdev.
2422fa9e4066Sahrens 	 */
2423fa9e4066Sahrens 	tvd = cvd->vdev_top;
2424fa9e4066Sahrens 	ASSERT(tvd->vdev_parent == rvd);
2425fa9e4066Sahrens 
2426fa9e4066Sahrens 	/*
242739c23413Seschrock 	 * Reevaluate the parent vdev state.
2428fa9e4066Sahrens 	 */
24293d7072f8Seschrock 	vdev_propagate_state(cvd);
2430fa9e4066Sahrens 
2431fa9e4066Sahrens 	/*
243239c23413Seschrock 	 * If the device we just detached was smaller than the others, it may be
243339c23413Seschrock 	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
243439c23413Seschrock 	 * can't fail because the existing metaslabs are already in core, so
243539c23413Seschrock 	 * there's nothing to read from disk.
2436fa9e4066Sahrens 	 */
2437ecc2d604Sbonwick 	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
2438fa9e4066Sahrens 
2439fa9e4066Sahrens 	vdev_config_dirty(tvd);
2440fa9e4066Sahrens 
2441fa9e4066Sahrens 	/*
244239c23413Seschrock 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
244339c23413Seschrock 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
244439c23413Seschrock 	 * But first make sure we're not on any *other* txg's DTL list, to
244539c23413Seschrock 	 * prevent vd from being accessed after it's freed.
2446fa9e4066Sahrens 	 */
2447fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++)
2448fa9e4066Sahrens 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
2449ecc2d604Sbonwick 	vd->vdev_detached = B_TRUE;
2450ecc2d604Sbonwick 	vdev_dirty(tvd, VDD_DTL, vd, txg);
2451fa9e4066Sahrens 
24523d7072f8Seschrock 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
24533d7072f8Seschrock 
245499653d4eSeschrock 	error = spa_vdev_exit(spa, vd, txg, 0);
245599653d4eSeschrock 
245699653d4eSeschrock 	/*
245739c23413Seschrock 	 * If this was the removal of the original device in a hot spare vdev,
245839c23413Seschrock 	 * then we want to go through and remove the device from the hot spare
245939c23413Seschrock 	 * list of every other pool.
246099653d4eSeschrock 	 */
246199653d4eSeschrock 	if (unspare) {
246299653d4eSeschrock 		spa = NULL;
246399653d4eSeschrock 		mutex_enter(&spa_namespace_lock);
246499653d4eSeschrock 		while ((spa = spa_next(spa)) != NULL) {
246599653d4eSeschrock 			if (spa->spa_state != POOL_STATE_ACTIVE)
246699653d4eSeschrock 				continue;
246799653d4eSeschrock 
246899653d4eSeschrock 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
246999653d4eSeschrock 		}
247099653d4eSeschrock 		mutex_exit(&spa_namespace_lock);
247199653d4eSeschrock 	}
247299653d4eSeschrock 
247399653d4eSeschrock 	return (error);
247499653d4eSeschrock }
247599653d4eSeschrock 
247699653d4eSeschrock /*
247799653d4eSeschrock  * Remove a device from the pool.  Currently, this supports removing only hot
247899653d4eSeschrock  * spares.
247999653d4eSeschrock  */
248099653d4eSeschrock int
248199653d4eSeschrock spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
248299653d4eSeschrock {
248399653d4eSeschrock 	vdev_t *vd;
248499653d4eSeschrock 	nvlist_t **spares, *nv, **newspares;
248599653d4eSeschrock 	uint_t i, j, nspares;
248699653d4eSeschrock 	int ret = 0;
248799653d4eSeschrock 
248899653d4eSeschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
248999653d4eSeschrock 
249099653d4eSeschrock 	vd = spa_lookup_by_guid(spa, guid);
249199653d4eSeschrock 
249299653d4eSeschrock 	nv = NULL;
249399653d4eSeschrock 	if (spa->spa_spares != NULL &&
249499653d4eSeschrock 	    nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
249599653d4eSeschrock 	    &spares, &nspares) == 0) {
249699653d4eSeschrock 		for (i = 0; i < nspares; i++) {
249799653d4eSeschrock 			uint64_t theguid;
249899653d4eSeschrock 
249999653d4eSeschrock 			VERIFY(nvlist_lookup_uint64(spares[i],
250099653d4eSeschrock 			    ZPOOL_CONFIG_GUID, &theguid) == 0);
250199653d4eSeschrock 			if (theguid == guid) {
250299653d4eSeschrock 				nv = spares[i];
250399653d4eSeschrock 				break;
250499653d4eSeschrock 			}
250599653d4eSeschrock 		}
250699653d4eSeschrock 	}
250799653d4eSeschrock 
250899653d4eSeschrock 	/*
250999653d4eSeschrock 	 * We only support removing a hot spare, and only if it's not currently
251099653d4eSeschrock 	 * in use in this pool.
251199653d4eSeschrock 	 */
251299653d4eSeschrock 	if (nv == NULL && vd == NULL) {
251399653d4eSeschrock 		ret = ENOENT;
251499653d4eSeschrock 		goto out;
251599653d4eSeschrock 	}
251699653d4eSeschrock 
251799653d4eSeschrock 	if (nv == NULL && vd != NULL) {
251899653d4eSeschrock 		ret = ENOTSUP;
251999653d4eSeschrock 		goto out;
252099653d4eSeschrock 	}
252199653d4eSeschrock 
252299653d4eSeschrock 	if (!unspare && nv != NULL && vd != NULL) {
252399653d4eSeschrock 		ret = EBUSY;
252499653d4eSeschrock 		goto out;
252599653d4eSeschrock 	}
252699653d4eSeschrock 
252799653d4eSeschrock 	if (nspares == 1) {
252899653d4eSeschrock 		newspares = NULL;
252999653d4eSeschrock 	} else {
253099653d4eSeschrock 		newspares = kmem_alloc((nspares - 1) * sizeof (void *),
253199653d4eSeschrock 		    KM_SLEEP);
253299653d4eSeschrock 		for (i = 0, j = 0; i < nspares; i++) {
253399653d4eSeschrock 			if (spares[i] != nv)
253499653d4eSeschrock 				VERIFY(nvlist_dup(spares[i],
253599653d4eSeschrock 				    &newspares[j++], KM_SLEEP) == 0);
253699653d4eSeschrock 		}
253799653d4eSeschrock 	}
253899653d4eSeschrock 
253999653d4eSeschrock 	VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
254099653d4eSeschrock 	    DATA_TYPE_NVLIST_ARRAY) == 0);
254199653d4eSeschrock 	VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES,
254299653d4eSeschrock 	    newspares, nspares - 1) == 0);
254399653d4eSeschrock 	for (i = 0; i < nspares - 1; i++)
254499653d4eSeschrock 		nvlist_free(newspares[i]);
254599653d4eSeschrock 	kmem_free(newspares, (nspares - 1) * sizeof (void *));
254699653d4eSeschrock 	spa_load_spares(spa);
254799653d4eSeschrock 	spa->spa_sync_spares = B_TRUE;
254899653d4eSeschrock 
254999653d4eSeschrock out:
255099653d4eSeschrock 	spa_config_exit(spa, FTAG);
255199653d4eSeschrock 
255299653d4eSeschrock 	return (ret);
2553fa9e4066Sahrens }
2554fa9e4066Sahrens 
2555fa9e4066Sahrens /*
25563d7072f8Seschrock  * Find any device that's done replacing, or a vdev marked 'unspare' that's
25573d7072f8Seschrock  * current spared, so we can detach it.
2558fa9e4066Sahrens  */
2559ea8dc4b6Seschrock static vdev_t *
25603d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
2561fa9e4066Sahrens {
2562ea8dc4b6Seschrock 	vdev_t *newvd, *oldvd;
2563fa9e4066Sahrens 	int c;
2564fa9e4066Sahrens 
2565ea8dc4b6Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
25663d7072f8Seschrock 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
2567ea8dc4b6Seschrock 		if (oldvd != NULL)
2568ea8dc4b6Seschrock 			return (oldvd);
2569ea8dc4b6Seschrock 	}
2570fa9e4066Sahrens 
25713d7072f8Seschrock 	/*
25723d7072f8Seschrock 	 * Check for a completed replacement.
25733d7072f8Seschrock 	 */
2574fa9e4066Sahrens 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
2575ea8dc4b6Seschrock 		oldvd = vd->vdev_child[0];
2576ea8dc4b6Seschrock 		newvd = vd->vdev_child[1];
2577ea8dc4b6Seschrock 
2578ea8dc4b6Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
2579ea8dc4b6Seschrock 		if (newvd->vdev_dtl_map.sm_space == 0 &&
2580ea8dc4b6Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
2581ea8dc4b6Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
2582ea8dc4b6Seschrock 			return (oldvd);
2583fa9e4066Sahrens 		}
2584ea8dc4b6Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
2585fa9e4066Sahrens 	}
2586ea8dc4b6Seschrock 
25873d7072f8Seschrock 	/*
25883d7072f8Seschrock 	 * Check for a completed resilver with the 'unspare' flag set.
25893d7072f8Seschrock 	 */
25903d7072f8Seschrock 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
25913d7072f8Seschrock 		newvd = vd->vdev_child[0];
25923d7072f8Seschrock 		oldvd = vd->vdev_child[1];
25933d7072f8Seschrock 
25943d7072f8Seschrock 		mutex_enter(&newvd->vdev_dtl_lock);
25953d7072f8Seschrock 		if (newvd->vdev_unspare &&
25963d7072f8Seschrock 		    newvd->vdev_dtl_map.sm_space == 0 &&
25973d7072f8Seschrock 		    newvd->vdev_dtl_scrub.sm_space == 0) {
25983d7072f8Seschrock 			newvd->vdev_unspare = 0;
25993d7072f8Seschrock 			mutex_exit(&newvd->vdev_dtl_lock);
26003d7072f8Seschrock 			return (oldvd);
26013d7072f8Seschrock 		}
26023d7072f8Seschrock 		mutex_exit(&newvd->vdev_dtl_lock);
26033d7072f8Seschrock 	}
26043d7072f8Seschrock 
2605ea8dc4b6Seschrock 	return (NULL);
2606fa9e4066Sahrens }
2607fa9e4066Sahrens 
2608ea8dc4b6Seschrock static void
26093d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa)
2610fa9e4066Sahrens {
2611ea8dc4b6Seschrock 	vdev_t *vd;
261299653d4eSeschrock 	vdev_t *pvd;
2613ea8dc4b6Seschrock 	uint64_t guid;
261499653d4eSeschrock 	uint64_t pguid = 0;
2615ea8dc4b6Seschrock 
2616ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2617ea8dc4b6Seschrock 
26183d7072f8Seschrock 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
2619ea8dc4b6Seschrock 		guid = vd->vdev_guid;
262099653d4eSeschrock 		/*
262199653d4eSeschrock 		 * If we have just finished replacing a hot spared device, then
262299653d4eSeschrock 		 * we need to detach the parent's first child (the original hot
262399653d4eSeschrock 		 * spare) as well.
262499653d4eSeschrock 		 */
262599653d4eSeschrock 		pvd = vd->vdev_parent;
262699653d4eSeschrock 		if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
262799653d4eSeschrock 		    pvd->vdev_id == 0) {
262899653d4eSeschrock 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
262999653d4eSeschrock 			ASSERT(pvd->vdev_parent->vdev_children == 2);
263099653d4eSeschrock 			pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid;
263199653d4eSeschrock 		}
2632ea8dc4b6Seschrock 		spa_config_exit(spa, FTAG);
2633ea8dc4b6Seschrock 		if (spa_vdev_detach(spa, guid, B_TRUE) != 0)
2634ea8dc4b6Seschrock 			return;
263599653d4eSeschrock 		if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0)
263699653d4eSeschrock 			return;
2637ea8dc4b6Seschrock 		spa_config_enter(spa, RW_READER, FTAG);
2638fa9e4066Sahrens 	}
2639fa9e4066Sahrens 
2640ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
2641fa9e4066Sahrens }
2642fa9e4066Sahrens 
2643c67d9675Seschrock /*
2644c67d9675Seschrock  * Update the stored path for this vdev.  Dirty the vdev configuration, relying
2645c67d9675Seschrock  * on spa_vdev_enter/exit() to synchronize the labels and cache.
2646c67d9675Seschrock  */
2647c67d9675Seschrock int
2648c67d9675Seschrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
2649c67d9675Seschrock {
2650c67d9675Seschrock 	vdev_t *rvd, *vd;
2651c67d9675Seschrock 	uint64_t txg;
2652c67d9675Seschrock 
2653c67d9675Seschrock 	rvd = spa->spa_root_vdev;
2654c67d9675Seschrock 
2655c67d9675Seschrock 	txg = spa_vdev_enter(spa);
2656c67d9675Seschrock 
265799653d4eSeschrock 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
265899653d4eSeschrock 		/*
265999653d4eSeschrock 		 * Determine if this is a reference to a hot spare.  In that
266099653d4eSeschrock 		 * case, update the path as stored in the spare list.
266199653d4eSeschrock 		 */
266299653d4eSeschrock 		nvlist_t **spares;
266399653d4eSeschrock 		uint_t i, nspares;
266499653d4eSeschrock 		if (spa->spa_sparelist != NULL) {
266599653d4eSeschrock 			VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist,
266699653d4eSeschrock 			    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
266799653d4eSeschrock 			for (i = 0; i < nspares; i++) {
266899653d4eSeschrock 				uint64_t theguid;
266999653d4eSeschrock 				VERIFY(nvlist_lookup_uint64(spares[i],
267099653d4eSeschrock 				    ZPOOL_CONFIG_GUID, &theguid) == 0);
267199653d4eSeschrock 				if (theguid == guid)
267299653d4eSeschrock 					break;
267399653d4eSeschrock 			}
267499653d4eSeschrock 
267599653d4eSeschrock 			if (i == nspares)
267699653d4eSeschrock 				return (spa_vdev_exit(spa, NULL, txg, ENOENT));
267799653d4eSeschrock 
267899653d4eSeschrock 			VERIFY(nvlist_add_string(spares[i],
267999653d4eSeschrock 			    ZPOOL_CONFIG_PATH, newpath) == 0);
268099653d4eSeschrock 			spa_load_spares(spa);
268199653d4eSeschrock 			spa->spa_sync_spares = B_TRUE;
268299653d4eSeschrock 			return (spa_vdev_exit(spa, NULL, txg, 0));
268399653d4eSeschrock 		} else {
268499653d4eSeschrock 			return (spa_vdev_exit(spa, NULL, txg, ENOENT));
268599653d4eSeschrock 		}
268699653d4eSeschrock 	}
2687c67d9675Seschrock 
26880e34b6a7Sbonwick 	if (!vd->vdev_ops->vdev_op_leaf)
26890e34b6a7Sbonwick 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
26900e34b6a7Sbonwick 
2691c67d9675Seschrock 	spa_strfree(vd->vdev_path);
2692c67d9675Seschrock 	vd->vdev_path = spa_strdup(newpath);
2693c67d9675Seschrock 
2694c67d9675Seschrock 	vdev_config_dirty(vd->vdev_top);
2695c67d9675Seschrock 
2696c67d9675Seschrock 	return (spa_vdev_exit(spa, NULL, txg, 0));
2697c67d9675Seschrock }
2698c67d9675Seschrock 
2699fa9e4066Sahrens /*
2700fa9e4066Sahrens  * ==========================================================================
2701fa9e4066Sahrens  * SPA Scrubbing
2702fa9e4066Sahrens  * ==========================================================================
2703fa9e4066Sahrens  */
2704fa9e4066Sahrens 
2705fa9e4066Sahrens static void
2706fa9e4066Sahrens spa_scrub_io_done(zio_t *zio)
2707fa9e4066Sahrens {
2708fa9e4066Sahrens 	spa_t *spa = zio->io_spa;
2709fa9e4066Sahrens 
27100e8c6158Smaybee 	arc_data_buf_free(zio->io_data, zio->io_size);
2711fa9e4066Sahrens 
2712fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2713ea8dc4b6Seschrock 	if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
271444cd46caSbillm 		vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev;
2715ea8dc4b6Seschrock 		spa->spa_scrub_errors++;
2716fa9e4066Sahrens 		mutex_enter(&vd->vdev_stat_lock);
2717fa9e4066Sahrens 		vd->vdev_stat.vs_scrub_errors++;
2718fa9e4066Sahrens 		mutex_exit(&vd->vdev_stat_lock);
2719fa9e4066Sahrens 	}
272005b2b3b8Smishra 
272105b2b3b8Smishra 	if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight)
2722ea8dc4b6Seschrock 		cv_broadcast(&spa->spa_scrub_io_cv);
272305b2b3b8Smishra 
272405b2b3b8Smishra 	ASSERT(spa->spa_scrub_inflight >= 0);
272505b2b3b8Smishra 
2726ea8dc4b6Seschrock 	mutex_exit(&spa->spa_scrub_lock);
2727fa9e4066Sahrens }
2728fa9e4066Sahrens 
2729fa9e4066Sahrens static void
2730ea8dc4b6Seschrock spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags,
2731ea8dc4b6Seschrock     zbookmark_t *zb)
2732fa9e4066Sahrens {
2733fa9e4066Sahrens 	size_t size = BP_GET_LSIZE(bp);
273405b2b3b8Smishra 	void *data;
2735fa9e4066Sahrens 
2736fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
273705b2b3b8Smishra 	/*
273805b2b3b8Smishra 	 * Do not give too much work to vdev(s).
273905b2b3b8Smishra 	 */
274005b2b3b8Smishra 	while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) {
274105b2b3b8Smishra 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
274205b2b3b8Smishra 	}
2743fa9e4066Sahrens 	spa->spa_scrub_inflight++;
2744fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2745fa9e4066Sahrens 
27460e8c6158Smaybee 	data = arc_data_buf_alloc(size);
274705b2b3b8Smishra 
2748ea8dc4b6Seschrock 	if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
2749ea8dc4b6Seschrock 		flags |= ZIO_FLAG_SPECULATIVE;	/* intent log block */
2750ea8dc4b6Seschrock 
2751d80c45e0Sbonwick 	flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL;
2752ea8dc4b6Seschrock 
2753fa9e4066Sahrens 	zio_nowait(zio_read(NULL, spa, bp, data, size,
2754ea8dc4b6Seschrock 	    spa_scrub_io_done, NULL, priority, flags, zb));
2755fa9e4066Sahrens }
2756fa9e4066Sahrens 
2757fa9e4066Sahrens /* ARGSUSED */
2758fa9e4066Sahrens static int
2759fa9e4066Sahrens spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
2760fa9e4066Sahrens {
2761fa9e4066Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
276244cd46caSbillm 	vdev_t *vd = spa->spa_root_vdev;
276344cd46caSbillm 	dva_t *dva = bp->blk_dva;
276444cd46caSbillm 	int needs_resilver = B_FALSE;
276544cd46caSbillm 	int d;
2766fa9e4066Sahrens 
276744cd46caSbillm 	if (bc->bc_errno) {
2768fa9e4066Sahrens 		/*
2769fa9e4066Sahrens 		 * We can't scrub this block, but we can continue to scrub
2770fa9e4066Sahrens 		 * the rest of the pool.  Note the error and move along.
2771fa9e4066Sahrens 		 */
2772fa9e4066Sahrens 		mutex_enter(&spa->spa_scrub_lock);
2773fa9e4066Sahrens 		spa->spa_scrub_errors++;
2774fa9e4066Sahrens 		mutex_exit(&spa->spa_scrub_lock);
2775fa9e4066Sahrens 
277644cd46caSbillm 		mutex_enter(&vd->vdev_stat_lock);
277744cd46caSbillm 		vd->vdev_stat.vs_scrub_errors++;
277844cd46caSbillm 		mutex_exit(&vd->vdev_stat_lock);
2779fa9e4066Sahrens 
2780fa9e4066Sahrens 		return (ERESTART);
2781fa9e4066Sahrens 	}
2782fa9e4066Sahrens 
2783fa9e4066Sahrens 	ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg);
2784fa9e4066Sahrens 
278544cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
278644cd46caSbillm 		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]));
2787fa9e4066Sahrens 
278844cd46caSbillm 		ASSERT(vd != NULL);
278944cd46caSbillm 
279044cd46caSbillm 		/*
279144cd46caSbillm 		 * Keep track of how much data we've examined so that
279244cd46caSbillm 		 * zpool(1M) status can make useful progress reports.
279344cd46caSbillm 		 */
279444cd46caSbillm 		mutex_enter(&vd->vdev_stat_lock);
279544cd46caSbillm 		vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]);
279644cd46caSbillm 		mutex_exit(&vd->vdev_stat_lock);
279744cd46caSbillm 
279844cd46caSbillm 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) {
279944cd46caSbillm 			if (DVA_GET_GANG(&dva[d])) {
280044cd46caSbillm 				/*
280144cd46caSbillm 				 * Gang members may be spread across multiple
280244cd46caSbillm 				 * vdevs, so the best we can do is look at the
280344cd46caSbillm 				 * pool-wide DTL.
280444cd46caSbillm 				 * XXX -- it would be better to change our
280544cd46caSbillm 				 * allocation policy to ensure that this can't
280644cd46caSbillm 				 * happen.
280744cd46caSbillm 				 */
280844cd46caSbillm 				vd = spa->spa_root_vdev;
280944cd46caSbillm 			}
281044cd46caSbillm 			if (vdev_dtl_contains(&vd->vdev_dtl_map,
281144cd46caSbillm 			    bp->blk_birth, 1))
281244cd46caSbillm 				needs_resilver = B_TRUE;
2813fa9e4066Sahrens 		}
281444cd46caSbillm 	}
281544cd46caSbillm 
281644cd46caSbillm 	if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING)
2817fa9e4066Sahrens 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB,
2818ea8dc4b6Seschrock 		    ZIO_FLAG_SCRUB, &bc->bc_bookmark);
281944cd46caSbillm 	else if (needs_resilver)
282044cd46caSbillm 		spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER,
282144cd46caSbillm 		    ZIO_FLAG_RESILVER, &bc->bc_bookmark);
2822fa9e4066Sahrens 
2823fa9e4066Sahrens 	return (0);
2824fa9e4066Sahrens }
2825fa9e4066Sahrens 
2826fa9e4066Sahrens static void
2827fa9e4066Sahrens spa_scrub_thread(spa_t *spa)
2828fa9e4066Sahrens {
2829fa9e4066Sahrens 	callb_cpr_t cprinfo;
2830fa9e4066Sahrens 	traverse_handle_t *th = spa->spa_scrub_th;
2831fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2832fa9e4066Sahrens 	pool_scrub_type_t scrub_type = spa->spa_scrub_type;
2833fa9e4066Sahrens 	int error = 0;
2834fa9e4066Sahrens 	boolean_t complete;
2835fa9e4066Sahrens 
2836fa9e4066Sahrens 	CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG);
2837fa9e4066Sahrens 
2838f0aa80d4Sbonwick 	/*
2839f0aa80d4Sbonwick 	 * If we're restarting due to a snapshot create/delete,
2840f0aa80d4Sbonwick 	 * wait for that to complete.
2841f0aa80d4Sbonwick 	 */
2842f0aa80d4Sbonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
2843f0aa80d4Sbonwick 
2844ea8dc4b6Seschrock 	dprintf("start %s mintxg=%llu maxtxg=%llu\n",
2845ea8dc4b6Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
2846ea8dc4b6Seschrock 	    spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg);
2847ea8dc4b6Seschrock 
2848ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
2849ea8dc4b6Seschrock 	vdev_reopen(rvd);		/* purge all vdev caches */
2850fa9e4066Sahrens 	vdev_config_dirty(rvd);		/* rewrite all disk labels */
2851fa9e4066Sahrens 	vdev_scrub_stat_update(rvd, scrub_type, B_FALSE);
2852ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
2853fa9e4066Sahrens 
2854fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2855fa9e4066Sahrens 	spa->spa_scrub_errors = 0;
2856fa9e4066Sahrens 	spa->spa_scrub_active = 1;
2857ea8dc4b6Seschrock 	ASSERT(spa->spa_scrub_inflight == 0);
2858fa9e4066Sahrens 
2859fa9e4066Sahrens 	while (!spa->spa_scrub_stop) {
2860fa9e4066Sahrens 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2861ea8dc4b6Seschrock 		while (spa->spa_scrub_suspended) {
2862fa9e4066Sahrens 			spa->spa_scrub_active = 0;
2863fa9e4066Sahrens 			cv_broadcast(&spa->spa_scrub_cv);
2864fa9e4066Sahrens 			cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
2865fa9e4066Sahrens 			spa->spa_scrub_active = 1;
2866fa9e4066Sahrens 		}
2867fa9e4066Sahrens 		CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock);
2868fa9e4066Sahrens 
2869fa9e4066Sahrens 		if (spa->spa_scrub_restart_txg != 0)
2870fa9e4066Sahrens 			break;
2871fa9e4066Sahrens 
2872fa9e4066Sahrens 		mutex_exit(&spa->spa_scrub_lock);
2873fa9e4066Sahrens 		error = traverse_more(th);
2874fa9e4066Sahrens 		mutex_enter(&spa->spa_scrub_lock);
2875fa9e4066Sahrens 		if (error != EAGAIN)
2876fa9e4066Sahrens 			break;
2877fa9e4066Sahrens 	}
2878fa9e4066Sahrens 
2879fa9e4066Sahrens 	while (spa->spa_scrub_inflight)
2880fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2881fa9e4066Sahrens 
28825dabedeeSbonwick 	spa->spa_scrub_active = 0;
28835dabedeeSbonwick 	cv_broadcast(&spa->spa_scrub_cv);
28845dabedeeSbonwick 
28855dabedeeSbonwick 	mutex_exit(&spa->spa_scrub_lock);
28865dabedeeSbonwick 
28875dabedeeSbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
28885dabedeeSbonwick 
28895dabedeeSbonwick 	mutex_enter(&spa->spa_scrub_lock);
28905dabedeeSbonwick 
28915dabedeeSbonwick 	/*
28925dabedeeSbonwick 	 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock
28935dabedeeSbonwick 	 * AND the spa config lock to synchronize with any config changes
28945dabedeeSbonwick 	 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
28955dabedeeSbonwick 	 */
2896fa9e4066Sahrens 	if (spa->spa_scrub_restart_txg != 0)
2897fa9e4066Sahrens 		error = ERESTART;
2898fa9e4066Sahrens 
2899ea8dc4b6Seschrock 	if (spa->spa_scrub_stop)
2900ea8dc4b6Seschrock 		error = EINTR;
2901ea8dc4b6Seschrock 
2902fa9e4066Sahrens 	/*
2903ea8dc4b6Seschrock 	 * Even if there were uncorrectable errors, we consider the scrub
2904ea8dc4b6Seschrock 	 * completed.  The downside is that if there is a transient error during
2905ea8dc4b6Seschrock 	 * a resilver, we won't resilver the data properly to the target.  But
2906ea8dc4b6Seschrock 	 * if the damage is permanent (more likely) we will resilver forever,
2907ea8dc4b6Seschrock 	 * which isn't really acceptable.  Since there is enough information for
2908ea8dc4b6Seschrock 	 * the user to know what has failed and why, this seems like a more
2909ea8dc4b6Seschrock 	 * tractable approach.
2910fa9e4066Sahrens 	 */
2911ea8dc4b6Seschrock 	complete = (error == 0);
2912fa9e4066Sahrens 
2913ea8dc4b6Seschrock 	dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
2914ea8dc4b6Seschrock 	    scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub",
2915fa9e4066Sahrens 	    spa->spa_scrub_maxtxg, complete ? "done" : "FAILED",
2916fa9e4066Sahrens 	    error, spa->spa_scrub_errors, spa->spa_scrub_stop);
2917fa9e4066Sahrens 
2918fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2919fa9e4066Sahrens 
2920fa9e4066Sahrens 	/*
2921fa9e4066Sahrens 	 * If the scrub/resilver completed, update all DTLs to reflect this.
2922fa9e4066Sahrens 	 * Whether it succeeded or not, vacate all temporary scrub DTLs.
2923fa9e4066Sahrens 	 */
2924fa9e4066Sahrens 	vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1,
2925fa9e4066Sahrens 	    complete ? spa->spa_scrub_maxtxg : 0, B_TRUE);
2926fa9e4066Sahrens 	vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete);
2927ea8dc4b6Seschrock 	spa_errlog_rotate(spa);
29285dabedeeSbonwick 
29293d7072f8Seschrock 	if (scrub_type == POOL_SCRUB_RESILVER && complete)
29303d7072f8Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH);
29313d7072f8Seschrock 
2932ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
2933fa9e4066Sahrens 
2934fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2935fa9e4066Sahrens 
2936ea8dc4b6Seschrock 	/*
2937ea8dc4b6Seschrock 	 * We may have finished replacing a device.
2938ea8dc4b6Seschrock 	 * Let the async thread assess this and handle the detach.
2939ea8dc4b6Seschrock 	 */
29403d7072f8Seschrock 	spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
2941fa9e4066Sahrens 
2942fa9e4066Sahrens 	/*
2943fa9e4066Sahrens 	 * If we were told to restart, our final act is to start a new scrub.
2944fa9e4066Sahrens 	 */
2945fa9e4066Sahrens 	if (error == ERESTART)
2946ea8dc4b6Seschrock 		spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ?
2947ea8dc4b6Seschrock 		    SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB);
2948fa9e4066Sahrens 
2949ea8dc4b6Seschrock 	spa->spa_scrub_type = POOL_SCRUB_NONE;
2950ea8dc4b6Seschrock 	spa->spa_scrub_active = 0;
2951ea8dc4b6Seschrock 	spa->spa_scrub_thread = NULL;
2952ea8dc4b6Seschrock 	cv_broadcast(&spa->spa_scrub_cv);
2953fa9e4066Sahrens 	CALLB_CPR_EXIT(&cprinfo);	/* drops &spa->spa_scrub_lock */
2954fa9e4066Sahrens 	thread_exit();
2955fa9e4066Sahrens }
2956fa9e4066Sahrens 
2957fa9e4066Sahrens void
2958fa9e4066Sahrens spa_scrub_suspend(spa_t *spa)
2959fa9e4066Sahrens {
2960fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2961ea8dc4b6Seschrock 	spa->spa_scrub_suspended++;
2962fa9e4066Sahrens 	while (spa->spa_scrub_active) {
2963fa9e4066Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
2964fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
2965fa9e4066Sahrens 	}
2966fa9e4066Sahrens 	while (spa->spa_scrub_inflight)
2967fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2968fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2969fa9e4066Sahrens }
2970fa9e4066Sahrens 
2971fa9e4066Sahrens void
2972fa9e4066Sahrens spa_scrub_resume(spa_t *spa)
2973fa9e4066Sahrens {
2974fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2975ea8dc4b6Seschrock 	ASSERT(spa->spa_scrub_suspended != 0);
2976ea8dc4b6Seschrock 	if (--spa->spa_scrub_suspended == 0)
2977fa9e4066Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
2978fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2979fa9e4066Sahrens }
2980fa9e4066Sahrens 
2981fa9e4066Sahrens void
2982fa9e4066Sahrens spa_scrub_restart(spa_t *spa, uint64_t txg)
2983fa9e4066Sahrens {
2984fa9e4066Sahrens 	/*
2985fa9e4066Sahrens 	 * Something happened (e.g. snapshot create/delete) that means
2986fa9e4066Sahrens 	 * we must restart any in-progress scrubs.  The itinerary will
2987fa9e4066Sahrens 	 * fix this properly.
2988fa9e4066Sahrens 	 */
2989fa9e4066Sahrens 	mutex_enter(&spa->spa_scrub_lock);
2990fa9e4066Sahrens 	spa->spa_scrub_restart_txg = txg;
2991fa9e4066Sahrens 	mutex_exit(&spa->spa_scrub_lock);
2992fa9e4066Sahrens }
2993fa9e4066Sahrens 
2994ea8dc4b6Seschrock int
2995ea8dc4b6Seschrock spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force)
2996fa9e4066Sahrens {
2997fa9e4066Sahrens 	space_seg_t *ss;
2998fa9e4066Sahrens 	uint64_t mintxg, maxtxg;
2999fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3000fa9e4066Sahrens 
3001bb8b5132Sek 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3002bb8b5132Sek 	ASSERT(!spa_config_held(spa, RW_WRITER));
3003bb8b5132Sek 
3004fa9e4066Sahrens 	if ((uint_t)type >= POOL_SCRUB_TYPES)
3005fa9e4066Sahrens 		return (ENOTSUP);
3006fa9e4066Sahrens 
3007ea8dc4b6Seschrock 	mutex_enter(&spa->spa_scrub_lock);
3008ea8dc4b6Seschrock 
3009fa9e4066Sahrens 	/*
3010fa9e4066Sahrens 	 * If there's a scrub or resilver already in progress, stop it.
3011fa9e4066Sahrens 	 */
3012fa9e4066Sahrens 	while (spa->spa_scrub_thread != NULL) {
3013fa9e4066Sahrens 		/*
3014fa9e4066Sahrens 		 * Don't stop a resilver unless forced.
3015fa9e4066Sahrens 		 */
3016ea8dc4b6Seschrock 		if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) {
3017ea8dc4b6Seschrock 			mutex_exit(&spa->spa_scrub_lock);
3018fa9e4066Sahrens 			return (EBUSY);
3019ea8dc4b6Seschrock 		}
3020fa9e4066Sahrens 		spa->spa_scrub_stop = 1;
3021fa9e4066Sahrens 		cv_broadcast(&spa->spa_scrub_cv);
3022fa9e4066Sahrens 		cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock);
3023fa9e4066Sahrens 	}
3024fa9e4066Sahrens 
3025fa9e4066Sahrens 	/*
3026fa9e4066Sahrens 	 * Terminate the previous traverse.
3027fa9e4066Sahrens 	 */
3028fa9e4066Sahrens 	if (spa->spa_scrub_th != NULL) {
3029fa9e4066Sahrens 		traverse_fini(spa->spa_scrub_th);
3030fa9e4066Sahrens 		spa->spa_scrub_th = NULL;
3031fa9e4066Sahrens 	}
3032fa9e4066Sahrens 
3033ea8dc4b6Seschrock 	if (rvd == NULL) {
3034ea8dc4b6Seschrock 		ASSERT(spa->spa_scrub_stop == 0);
3035ea8dc4b6Seschrock 		ASSERT(spa->spa_scrub_type == type);
3036ea8dc4b6Seschrock 		ASSERT(spa->spa_scrub_restart_txg == 0);
3037ea8dc4b6Seschrock 		mutex_exit(&spa->spa_scrub_lock);
3038ea8dc4b6Seschrock 		return (0);
3039ea8dc4b6Seschrock 	}
3040fa9e4066Sahrens 
3041fa9e4066Sahrens 	mintxg = TXG_INITIAL - 1;
3042fa9e4066Sahrens 	maxtxg = spa_last_synced_txg(spa) + 1;
3043fa9e4066Sahrens 
3044ea8dc4b6Seschrock 	mutex_enter(&rvd->vdev_dtl_lock);
3045fa9e4066Sahrens 
3046ea8dc4b6Seschrock 	if (rvd->vdev_dtl_map.sm_space == 0) {
3047ea8dc4b6Seschrock 		/*
3048ea8dc4b6Seschrock 		 * The pool-wide DTL is empty.
3049ecc2d604Sbonwick 		 * If this is a resilver, there's nothing to do except
3050ecc2d604Sbonwick 		 * check whether any in-progress replacements have completed.
3051ea8dc4b6Seschrock 		 */
3052ecc2d604Sbonwick 		if (type == POOL_SCRUB_RESILVER) {
3053ea8dc4b6Seschrock 			type = POOL_SCRUB_NONE;
30543d7072f8Seschrock 			spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3055ecc2d604Sbonwick 		}
3056ea8dc4b6Seschrock 	} else {
3057ea8dc4b6Seschrock 		/*
3058ea8dc4b6Seschrock 		 * The pool-wide DTL is non-empty.
3059ea8dc4b6Seschrock 		 * If this is a normal scrub, upgrade to a resilver instead.
3060ea8dc4b6Seschrock 		 */
3061ea8dc4b6Seschrock 		if (type == POOL_SCRUB_EVERYTHING)
3062ea8dc4b6Seschrock 			type = POOL_SCRUB_RESILVER;
3063ea8dc4b6Seschrock 	}
3064fa9e4066Sahrens 
3065ea8dc4b6Seschrock 	if (type == POOL_SCRUB_RESILVER) {
3066fa9e4066Sahrens 		/*
3067fa9e4066Sahrens 		 * Determine the resilvering boundaries.
3068fa9e4066Sahrens 		 *
3069fa9e4066Sahrens 		 * Note: (mintxg, maxtxg) is an open interval,
3070fa9e4066Sahrens 		 * i.e. mintxg and maxtxg themselves are not included.
3071fa9e4066Sahrens 		 *
3072fa9e4066Sahrens 		 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
3073fa9e4066Sahrens 		 * so we don't claim to resilver a txg that's still changing.
3074fa9e4066Sahrens 		 */
3075fa9e4066Sahrens 		ss = avl_first(&rvd->vdev_dtl_map.sm_root);
3076ea8dc4b6Seschrock 		mintxg = ss->ss_start - 1;
3077fa9e4066Sahrens 		ss = avl_last(&rvd->vdev_dtl_map.sm_root);
3078ea8dc4b6Seschrock 		maxtxg = MIN(ss->ss_end, maxtxg);
30793d7072f8Seschrock 
30803d7072f8Seschrock 		spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
3081fa9e4066Sahrens 	}
3082fa9e4066Sahrens 
3083ea8dc4b6Seschrock 	mutex_exit(&rvd->vdev_dtl_lock);
3084ea8dc4b6Seschrock 
3085ea8dc4b6Seschrock 	spa->spa_scrub_stop = 0;
3086ea8dc4b6Seschrock 	spa->spa_scrub_type = type;
3087ea8dc4b6Seschrock 	spa->spa_scrub_restart_txg = 0;
3088ea8dc4b6Seschrock 
3089ea8dc4b6Seschrock 	if (type != POOL_SCRUB_NONE) {
3090ea8dc4b6Seschrock 		spa->spa_scrub_mintxg = mintxg;
3091fa9e4066Sahrens 		spa->spa_scrub_maxtxg = maxtxg;
3092fa9e4066Sahrens 		spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL,
30930373e76bSbonwick 		    ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL,
30940373e76bSbonwick 		    ZIO_FLAG_CANFAIL);
3095fa9e4066Sahrens 		traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg);
3096fa9e4066Sahrens 		spa->spa_scrub_thread = thread_create(NULL, 0,
3097fa9e4066Sahrens 		    spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri);
3098fa9e4066Sahrens 	}
3099fa9e4066Sahrens 
3100ea8dc4b6Seschrock 	mutex_exit(&spa->spa_scrub_lock);
3101ea8dc4b6Seschrock 
3102fa9e4066Sahrens 	return (0);
3103fa9e4066Sahrens }
3104fa9e4066Sahrens 
3105ea8dc4b6Seschrock /*
3106ea8dc4b6Seschrock  * ==========================================================================
3107ea8dc4b6Seschrock  * SPA async task processing
3108ea8dc4b6Seschrock  * ==========================================================================
3109ea8dc4b6Seschrock  */
3110ea8dc4b6Seschrock 
3111ea8dc4b6Seschrock static void
31123d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
3113fa9e4066Sahrens {
3114ea8dc4b6Seschrock 	vdev_t *tvd;
3115ea8dc4b6Seschrock 	int c;
3116fa9e4066Sahrens 
31173d7072f8Seschrock 	for (c = 0; c < vd->vdev_children; c++) {
31183d7072f8Seschrock 		tvd = vd->vdev_child[c];
31193d7072f8Seschrock 		if (tvd->vdev_remove_wanted) {
31203d7072f8Seschrock 			tvd->vdev_remove_wanted = 0;
31213d7072f8Seschrock 			vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED,
31223d7072f8Seschrock 			    VDEV_AUX_NONE);
3123*0a4e9518Sgw 			vdev_clear(spa, tvd, B_TRUE);
31243d7072f8Seschrock 			vdev_config_dirty(tvd->vdev_top);
3125ea8dc4b6Seschrock 		}
31263d7072f8Seschrock 		spa_async_remove(spa, tvd);
3127ea8dc4b6Seschrock 	}
3128ea8dc4b6Seschrock }
3129fa9e4066Sahrens 
3130ea8dc4b6Seschrock static void
3131ea8dc4b6Seschrock spa_async_thread(spa_t *spa)
3132ea8dc4b6Seschrock {
3133ea8dc4b6Seschrock 	int tasks;
31343d7072f8Seschrock 	uint64_t txg;
3135ea8dc4b6Seschrock 
3136ea8dc4b6Seschrock 	ASSERT(spa->spa_sync_on);
3137ea8dc4b6Seschrock 
3138ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3139ea8dc4b6Seschrock 	tasks = spa->spa_async_tasks;
3140ea8dc4b6Seschrock 	spa->spa_async_tasks = 0;
3141ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3142ea8dc4b6Seschrock 
31430373e76bSbonwick 	/*
31440373e76bSbonwick 	 * See if the config needs to be updated.
31450373e76bSbonwick 	 */
31460373e76bSbonwick 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
31470373e76bSbonwick 		mutex_enter(&spa_namespace_lock);
31480373e76bSbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
31490373e76bSbonwick 		mutex_exit(&spa_namespace_lock);
31500373e76bSbonwick 	}
31510373e76bSbonwick 
3152ea8dc4b6Seschrock 	/*
31533d7072f8Seschrock 	 * See if any devices need to be marked REMOVED.
3154*0a4e9518Sgw 	 *
3155*0a4e9518Sgw 	 * XXX - We avoid doing this when we are in
3156*0a4e9518Sgw 	 * I/O failure state since spa_vdev_enter() grabs
3157*0a4e9518Sgw 	 * the namespace lock and would not be able to obtain
3158*0a4e9518Sgw 	 * the writer config lock.
3159ea8dc4b6Seschrock 	 */
3160*0a4e9518Sgw 	if (tasks & SPA_ASYNC_REMOVE &&
3161*0a4e9518Sgw 	    spa_state(spa) != POOL_STATE_IO_FAILURE) {
31623d7072f8Seschrock 		txg = spa_vdev_enter(spa);
31633d7072f8Seschrock 		spa_async_remove(spa, spa->spa_root_vdev);
31643d7072f8Seschrock 		(void) spa_vdev_exit(spa, NULL, txg, 0);
31653d7072f8Seschrock 	}
3166ea8dc4b6Seschrock 
3167ea8dc4b6Seschrock 	/*
3168ea8dc4b6Seschrock 	 * If any devices are done replacing, detach them.
3169ea8dc4b6Seschrock 	 */
31703d7072f8Seschrock 	if (tasks & SPA_ASYNC_RESILVER_DONE)
31713d7072f8Seschrock 		spa_vdev_resilver_done(spa);
3172fa9e4066Sahrens 
3173ea8dc4b6Seschrock 	/*
31743d7072f8Seschrock 	 * Kick off a scrub.  When starting a RESILVER scrub (or an EVERYTHING
31753d7072f8Seschrock 	 * scrub which can become a resilver), we need to hold
31763d7072f8Seschrock 	 * spa_namespace_lock() because the sysevent we post via
31773d7072f8Seschrock 	 * spa_event_notify() needs to get the name of the pool.
3178ea8dc4b6Seschrock 	 */
31793d7072f8Seschrock 	if (tasks & SPA_ASYNC_SCRUB) {
31803d7072f8Seschrock 		mutex_enter(&spa_namespace_lock);
3181ea8dc4b6Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0);
31823d7072f8Seschrock 		mutex_exit(&spa_namespace_lock);
31833d7072f8Seschrock 	}
3184ea8dc4b6Seschrock 
3185ea8dc4b6Seschrock 	/*
3186ea8dc4b6Seschrock 	 * Kick off a resilver.
3187ea8dc4b6Seschrock 	 */
31883d7072f8Seschrock 	if (tasks & SPA_ASYNC_RESILVER) {
31893d7072f8Seschrock 		mutex_enter(&spa_namespace_lock);
3190ea8dc4b6Seschrock 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
31913d7072f8Seschrock 		mutex_exit(&spa_namespace_lock);
31923d7072f8Seschrock 	}
3193ea8dc4b6Seschrock 
3194ea8dc4b6Seschrock 	/*
3195ea8dc4b6Seschrock 	 * Let the world know that we're done.
3196ea8dc4b6Seschrock 	 */
3197ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3198ea8dc4b6Seschrock 	spa->spa_async_thread = NULL;
3199ea8dc4b6Seschrock 	cv_broadcast(&spa->spa_async_cv);
3200ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3201ea8dc4b6Seschrock 	thread_exit();
3202ea8dc4b6Seschrock }
3203ea8dc4b6Seschrock 
3204ea8dc4b6Seschrock void
3205ea8dc4b6Seschrock spa_async_suspend(spa_t *spa)
3206ea8dc4b6Seschrock {
3207ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3208ea8dc4b6Seschrock 	spa->spa_async_suspended++;
3209ea8dc4b6Seschrock 	while (spa->spa_async_thread != NULL)
3210ea8dc4b6Seschrock 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
3211ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3212ea8dc4b6Seschrock }
3213ea8dc4b6Seschrock 
3214ea8dc4b6Seschrock void
3215ea8dc4b6Seschrock spa_async_resume(spa_t *spa)
3216ea8dc4b6Seschrock {
3217ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3218ea8dc4b6Seschrock 	ASSERT(spa->spa_async_suspended != 0);
3219ea8dc4b6Seschrock 	spa->spa_async_suspended--;
3220ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3221ea8dc4b6Seschrock }
3222ea8dc4b6Seschrock 
3223ea8dc4b6Seschrock static void
3224ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa)
3225ea8dc4b6Seschrock {
3226ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3227ea8dc4b6Seschrock 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
32280373e76bSbonwick 	    spa->spa_async_thread == NULL &&
32290373e76bSbonwick 	    rootdir != NULL && !vn_is_readonly(rootdir))
3230ea8dc4b6Seschrock 		spa->spa_async_thread = thread_create(NULL, 0,
3231ea8dc4b6Seschrock 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
3232ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3233ea8dc4b6Seschrock }
3234ea8dc4b6Seschrock 
3235ea8dc4b6Seschrock void
3236ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task)
3237ea8dc4b6Seschrock {
3238ea8dc4b6Seschrock 	mutex_enter(&spa->spa_async_lock);
3239ea8dc4b6Seschrock 	spa->spa_async_tasks |= task;
3240ea8dc4b6Seschrock 	mutex_exit(&spa->spa_async_lock);
3241fa9e4066Sahrens }
3242fa9e4066Sahrens 
3243fa9e4066Sahrens /*
3244fa9e4066Sahrens  * ==========================================================================
3245fa9e4066Sahrens  * SPA syncing routines
3246fa9e4066Sahrens  * ==========================================================================
3247fa9e4066Sahrens  */
3248fa9e4066Sahrens 
3249fa9e4066Sahrens static void
3250fa9e4066Sahrens spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3251fa9e4066Sahrens {
3252fa9e4066Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
3253fa9e4066Sahrens 	dmu_tx_t *tx;
3254fa9e4066Sahrens 	blkptr_t blk;
3255fa9e4066Sahrens 	uint64_t itor = 0;
3256fa9e4066Sahrens 	zio_t *zio;
3257fa9e4066Sahrens 	int error;
3258fa9e4066Sahrens 	uint8_t c = 1;
3259fa9e4066Sahrens 
3260fa9e4066Sahrens 	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD);
3261fa9e4066Sahrens 
3262fa9e4066Sahrens 	while (bplist_iterate(bpl, &itor, &blk) == 0)
3263fa9e4066Sahrens 		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL));
3264fa9e4066Sahrens 
3265fa9e4066Sahrens 	error = zio_wait(zio);
3266fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
3267fa9e4066Sahrens 
3268fa9e4066Sahrens 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3269fa9e4066Sahrens 	bplist_vacate(bpl, tx);
3270fa9e4066Sahrens 
3271fa9e4066Sahrens 	/*
3272fa9e4066Sahrens 	 * Pre-dirty the first block so we sync to convergence faster.
3273fa9e4066Sahrens 	 * (Usually only the first block is needed.)
3274fa9e4066Sahrens 	 */
3275fa9e4066Sahrens 	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3276fa9e4066Sahrens 	dmu_tx_commit(tx);
3277fa9e4066Sahrens }
3278fa9e4066Sahrens 
3279fa9e4066Sahrens static void
328099653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
3281fa9e4066Sahrens {
3282fa9e4066Sahrens 	char *packed = NULL;
3283fa9e4066Sahrens 	size_t nvsize = 0;
3284fa9e4066Sahrens 	dmu_buf_t *db;
3285fa9e4066Sahrens 
328699653d4eSeschrock 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
3287fa9e4066Sahrens 
3288fa9e4066Sahrens 	packed = kmem_alloc(nvsize, KM_SLEEP);
3289fa9e4066Sahrens 
329099653d4eSeschrock 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
3291ea8dc4b6Seschrock 	    KM_SLEEP) == 0);
3292fa9e4066Sahrens 
329399653d4eSeschrock 	dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx);
3294fa9e4066Sahrens 
3295fa9e4066Sahrens 	kmem_free(packed, nvsize);
3296fa9e4066Sahrens 
329799653d4eSeschrock 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
3298fa9e4066Sahrens 	dmu_buf_will_dirty(db, tx);
3299fa9e4066Sahrens 	*(uint64_t *)db->db_data = nvsize;
3300ea8dc4b6Seschrock 	dmu_buf_rele(db, FTAG);
3301fa9e4066Sahrens }
3302fa9e4066Sahrens 
330399653d4eSeschrock static void
330499653d4eSeschrock spa_sync_spares(spa_t *spa, dmu_tx_t *tx)
330599653d4eSeschrock {
330699653d4eSeschrock 	nvlist_t *nvroot;
330799653d4eSeschrock 	nvlist_t **spares;
330899653d4eSeschrock 	int i;
330999653d4eSeschrock 
331099653d4eSeschrock 	if (!spa->spa_sync_spares)
331199653d4eSeschrock 		return;
331299653d4eSeschrock 
331399653d4eSeschrock 	/*
331499653d4eSeschrock 	 * Update the MOS nvlist describing the list of available spares.
331599653d4eSeschrock 	 * spa_validate_spares() will have already made sure this nvlist is
33163d7072f8Seschrock 	 * valid and the vdevs are labeled appropriately.
331799653d4eSeschrock 	 */
331899653d4eSeschrock 	if (spa->spa_spares_object == 0) {
331999653d4eSeschrock 		spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset,
332099653d4eSeschrock 		    DMU_OT_PACKED_NVLIST, 1 << 14,
332199653d4eSeschrock 		    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
332299653d4eSeschrock 		VERIFY(zap_update(spa->spa_meta_objset,
332399653d4eSeschrock 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES,
332499653d4eSeschrock 		    sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0);
332599653d4eSeschrock 	}
332699653d4eSeschrock 
332799653d4eSeschrock 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
332899653d4eSeschrock 	if (spa->spa_nspares == 0) {
332999653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
333099653d4eSeschrock 		    NULL, 0) == 0);
333199653d4eSeschrock 	} else {
333299653d4eSeschrock 		spares = kmem_alloc(spa->spa_nspares * sizeof (void *),
333399653d4eSeschrock 		    KM_SLEEP);
333499653d4eSeschrock 		for (i = 0; i < spa->spa_nspares; i++)
333599653d4eSeschrock 			spares[i] = vdev_config_generate(spa,
333699653d4eSeschrock 			    spa->spa_spares[i], B_FALSE, B_TRUE);
333799653d4eSeschrock 		VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
333899653d4eSeschrock 		    spares, spa->spa_nspares) == 0);
333999653d4eSeschrock 		for (i = 0; i < spa->spa_nspares; i++)
334099653d4eSeschrock 			nvlist_free(spares[i]);
334199653d4eSeschrock 		kmem_free(spares, spa->spa_nspares * sizeof (void *));
334299653d4eSeschrock 	}
334399653d4eSeschrock 
334499653d4eSeschrock 	spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx);
334506eeb2adSek 	nvlist_free(nvroot);
334699653d4eSeschrock 
334799653d4eSeschrock 	spa->spa_sync_spares = B_FALSE;
334899653d4eSeschrock }
334999653d4eSeschrock 
335099653d4eSeschrock static void
335199653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
335299653d4eSeschrock {
335399653d4eSeschrock 	nvlist_t *config;
335499653d4eSeschrock 
335599653d4eSeschrock 	if (list_is_empty(&spa->spa_dirty_list))
335699653d4eSeschrock 		return;
335799653d4eSeschrock 
335899653d4eSeschrock 	config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE);
335999653d4eSeschrock 
336099653d4eSeschrock 	if (spa->spa_config_syncing)
336199653d4eSeschrock 		nvlist_free(spa->spa_config_syncing);
336299653d4eSeschrock 	spa->spa_config_syncing = config;
336399653d4eSeschrock 
336499653d4eSeschrock 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
336599653d4eSeschrock }
336699653d4eSeschrock 
3367990b4856Slling /*
3368990b4856Slling  * Set zpool properties.
3369990b4856Slling  */
3370b1b8ab34Slling static void
3371ecd6cf80Smarks spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
3372b1b8ab34Slling {
3373b1b8ab34Slling 	spa_t *spa = arg1;
3374b1b8ab34Slling 	objset_t *mos = spa->spa_meta_objset;
3375990b4856Slling 	nvlist_t *nvp = arg2;
3376990b4856Slling 	nvpair_t *elem;
33773d7072f8Seschrock 	uint64_t intval;
3378990b4856Slling 	char *strval;
3379990b4856Slling 	zpool_prop_t prop;
3380990b4856Slling 	const char *propname;
3381990b4856Slling 	zprop_type_t proptype;
3382b1b8ab34Slling 
3383990b4856Slling 	elem = NULL;
3384990b4856Slling 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
3385990b4856Slling 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
3386990b4856Slling 		case ZPOOL_PROP_VERSION:
3387990b4856Slling 			/*
3388990b4856Slling 			 * Only set version for non-zpool-creation cases
3389990b4856Slling 			 * (set/import). spa_create() needs special care
3390990b4856Slling 			 * for version setting.
3391990b4856Slling 			 */
3392990b4856Slling 			if (tx->tx_txg != TXG_INITIAL) {
3393990b4856Slling 				VERIFY(nvpair_value_uint64(elem,
3394990b4856Slling 				    &intval) == 0);
3395990b4856Slling 				ASSERT(intval <= SPA_VERSION);
3396990b4856Slling 				ASSERT(intval >= spa_version(spa));
3397990b4856Slling 				spa->spa_uberblock.ub_version = intval;
3398990b4856Slling 				vdev_config_dirty(spa->spa_root_vdev);
3399990b4856Slling 			}
3400ecd6cf80Smarks 			break;
3401990b4856Slling 
3402990b4856Slling 		case ZPOOL_PROP_ALTROOT:
3403990b4856Slling 			/*
3404990b4856Slling 			 * 'altroot' is a non-persistent property. It should
3405990b4856Slling 			 * have been set temporarily at creation or import time.
3406990b4856Slling 			 */
3407990b4856Slling 			ASSERT(spa->spa_root != NULL);
3408b1b8ab34Slling 			break;
34093d7072f8Seschrock 
3410990b4856Slling 		case ZPOOL_PROP_TEMPORARY:
3411990b4856Slling 			/*
3412990b4856Slling 			 * 'temporary' is a non-persistant property.
3413990b4856Slling 			 */
3414990b4856Slling 			VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3415990b4856Slling 			spa->spa_temporary = intval;
34163d7072f8Seschrock 			break;
3417990b4856Slling 		default:
3418990b4856Slling 			/*
3419990b4856Slling 			 * Set pool property values in the poolprops mos object.
3420990b4856Slling 			 */
3421990b4856Slling 			mutex_enter(&spa->spa_props_lock);
3422990b4856Slling 			if (spa->spa_pool_props_object == 0) {
3423990b4856Slling 				objset_t *mos = spa->spa_meta_objset;
3424990b4856Slling 
3425990b4856Slling 				VERIFY((spa->spa_pool_props_object =
3426990b4856Slling 				    zap_create(mos, DMU_OT_POOL_PROPS,
3427990b4856Slling 				    DMU_OT_NONE, 0, tx)) > 0);
3428990b4856Slling 
3429990b4856Slling 				VERIFY(zap_update(mos,
3430990b4856Slling 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
3431990b4856Slling 				    8, 1, &spa->spa_pool_props_object, tx)
3432990b4856Slling 				    == 0);
3433990b4856Slling 			}
3434990b4856Slling 			mutex_exit(&spa->spa_props_lock);
3435990b4856Slling 
3436990b4856Slling 			/* normalize the property name */
3437990b4856Slling 			propname = zpool_prop_to_name(prop);
3438990b4856Slling 			proptype = zpool_prop_get_type(prop);
3439990b4856Slling 
3440990b4856Slling 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
3441990b4856Slling 				ASSERT(proptype == PROP_TYPE_STRING);
3442990b4856Slling 				VERIFY(nvpair_value_string(elem, &strval) == 0);
3443990b4856Slling 				VERIFY(zap_update(mos,
3444990b4856Slling 				    spa->spa_pool_props_object, propname,
3445990b4856Slling 				    1, strlen(strval) + 1, strval, tx) == 0);
3446990b4856Slling 
3447990b4856Slling 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
3448990b4856Slling 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3449990b4856Slling 
3450990b4856Slling 				if (proptype == PROP_TYPE_INDEX) {
3451990b4856Slling 					const char *unused;
3452990b4856Slling 					VERIFY(zpool_prop_index_to_string(
3453990b4856Slling 					    prop, intval, &unused) == 0);
3454990b4856Slling 				}
3455990b4856Slling 				VERIFY(zap_update(mos,
3456990b4856Slling 				    spa->spa_pool_props_object, propname,
3457990b4856Slling 				    8, 1, &intval, tx) == 0);
3458990b4856Slling 			} else {
3459990b4856Slling 				ASSERT(0); /* not allowed */
3460990b4856Slling 			}
3461990b4856Slling 
3462*0a4e9518Sgw 			switch (prop) {
3463*0a4e9518Sgw 			case ZPOOL_PROP_DELEGATION:
3464990b4856Slling 				spa->spa_delegation = intval;
3465*0a4e9518Sgw 				break;
3466*0a4e9518Sgw 			case ZPOOL_PROP_BOOTFS:
3467990b4856Slling 				spa->spa_bootfs = intval;
3468*0a4e9518Sgw 				break;
3469*0a4e9518Sgw 			case ZPOOL_PROP_FAILUREMODE:
3470*0a4e9518Sgw 				spa->spa_failmode = intval;
3471*0a4e9518Sgw 				break;
3472*0a4e9518Sgw 			default:
3473*0a4e9518Sgw 				break;
3474*0a4e9518Sgw 			}
3475990b4856Slling 		}
3476990b4856Slling 
3477990b4856Slling 		/* log internal history if this is not a zpool create */
3478990b4856Slling 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
3479990b4856Slling 		    tx->tx_txg != TXG_INITIAL) {
3480990b4856Slling 			spa_history_internal_log(LOG_POOL_PROPSET,
3481990b4856Slling 			    spa, tx, cr, "%s %lld %s",
3482990b4856Slling 			    nvpair_name(elem), intval, spa->spa_name);
3483b1b8ab34Slling 		}
3484b1b8ab34Slling 	}
3485b1b8ab34Slling }
3486b1b8ab34Slling 
3487fa9e4066Sahrens /*
3488fa9e4066Sahrens  * Sync the specified transaction group.  New blocks may be dirtied as
3489fa9e4066Sahrens  * part of the process, so we iterate until it converges.
3490fa9e4066Sahrens  */
3491fa9e4066Sahrens void
3492fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg)
3493fa9e4066Sahrens {
3494fa9e4066Sahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
3495fa9e4066Sahrens 	objset_t *mos = spa->spa_meta_objset;
3496fa9e4066Sahrens 	bplist_t *bpl = &spa->spa_sync_bplist;
34970373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3498fa9e4066Sahrens 	vdev_t *vd;
3499fa9e4066Sahrens 	dmu_tx_t *tx;
3500fa9e4066Sahrens 	int dirty_vdevs;
3501fa9e4066Sahrens 
3502fa9e4066Sahrens 	/*
3503fa9e4066Sahrens 	 * Lock out configuration changes.
3504fa9e4066Sahrens 	 */
3505ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
3506fa9e4066Sahrens 
3507fa9e4066Sahrens 	spa->spa_syncing_txg = txg;
3508fa9e4066Sahrens 	spa->spa_sync_pass = 0;
3509fa9e4066Sahrens 
3510ea8dc4b6Seschrock 	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
3511fa9e4066Sahrens 
351299653d4eSeschrock 	tx = dmu_tx_create_assigned(dp, txg);
351399653d4eSeschrock 
351499653d4eSeschrock 	/*
3515e7437265Sahrens 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
351699653d4eSeschrock 	 * set spa_deflate if we have no raid-z vdevs.
351799653d4eSeschrock 	 */
3518e7437265Sahrens 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
3519e7437265Sahrens 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
352099653d4eSeschrock 		int i;
352199653d4eSeschrock 
352299653d4eSeschrock 		for (i = 0; i < rvd->vdev_children; i++) {
352399653d4eSeschrock 			vd = rvd->vdev_child[i];
352499653d4eSeschrock 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
352599653d4eSeschrock 				break;
352699653d4eSeschrock 		}
352799653d4eSeschrock 		if (i == rvd->vdev_children) {
352899653d4eSeschrock 			spa->spa_deflate = TRUE;
352999653d4eSeschrock 			VERIFY(0 == zap_add(spa->spa_meta_objset,
353099653d4eSeschrock 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
353199653d4eSeschrock 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
353299653d4eSeschrock 		}
353399653d4eSeschrock 	}
353499653d4eSeschrock 
3535fa9e4066Sahrens 	/*
3536fa9e4066Sahrens 	 * If anything has changed in this txg, push the deferred frees
3537fa9e4066Sahrens 	 * from the previous txg.  If not, leave them alone so that we
3538fa9e4066Sahrens 	 * don't generate work on an otherwise idle system.
3539fa9e4066Sahrens 	 */
3540fa9e4066Sahrens 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
35411615a317Sek 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
35421615a317Sek 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
3543fa9e4066Sahrens 		spa_sync_deferred_frees(spa, txg);
3544fa9e4066Sahrens 
3545fa9e4066Sahrens 	/*
3546fa9e4066Sahrens 	 * Iterate to convergence.
3547fa9e4066Sahrens 	 */
3548fa9e4066Sahrens 	do {
3549fa9e4066Sahrens 		spa->spa_sync_pass++;
3550fa9e4066Sahrens 
3551fa9e4066Sahrens 		spa_sync_config_object(spa, tx);
355299653d4eSeschrock 		spa_sync_spares(spa, tx);
3553ea8dc4b6Seschrock 		spa_errlog_sync(spa, txg);
3554fa9e4066Sahrens 		dsl_pool_sync(dp, txg);
3555fa9e4066Sahrens 
3556fa9e4066Sahrens 		dirty_vdevs = 0;
3557fa9e4066Sahrens 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
3558fa9e4066Sahrens 			vdev_sync(vd, txg);
3559fa9e4066Sahrens 			dirty_vdevs++;
3560fa9e4066Sahrens 		}
3561fa9e4066Sahrens 
3562fa9e4066Sahrens 		bplist_sync(bpl, tx);
3563fa9e4066Sahrens 	} while (dirty_vdevs);
3564fa9e4066Sahrens 
3565fa9e4066Sahrens 	bplist_close(bpl);
3566fa9e4066Sahrens 
3567fa9e4066Sahrens 	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
3568fa9e4066Sahrens 
3569fa9e4066Sahrens 	/*
3570fa9e4066Sahrens 	 * Rewrite the vdev configuration (which includes the uberblock)
3571fa9e4066Sahrens 	 * to commit the transaction group.
35720373e76bSbonwick 	 *
35730373e76bSbonwick 	 * If there are any dirty vdevs, sync the uberblock to all vdevs.
35740373e76bSbonwick 	 * Otherwise, pick a random top-level vdev that's known to be
35750373e76bSbonwick 	 * visible in the config cache (see spa_vdev_add() for details).
35760373e76bSbonwick 	 * If the write fails, try the next vdev until we're tried them all.
35770373e76bSbonwick 	 */
35780373e76bSbonwick 	if (!list_is_empty(&spa->spa_dirty_list)) {
35790373e76bSbonwick 		VERIFY(vdev_config_sync(rvd, txg) == 0);
35800373e76bSbonwick 	} else {
35810373e76bSbonwick 		int children = rvd->vdev_children;
35820373e76bSbonwick 		int c0 = spa_get_random(children);
35830373e76bSbonwick 		int c;
35840373e76bSbonwick 
35850373e76bSbonwick 		for (c = 0; c < children; c++) {
35860373e76bSbonwick 			vd = rvd->vdev_child[(c0 + c) % children];
35870373e76bSbonwick 			if (vd->vdev_ms_array == 0)
35880373e76bSbonwick 				continue;
35890373e76bSbonwick 			if (vdev_config_sync(vd, txg) == 0)
35900373e76bSbonwick 				break;
35910373e76bSbonwick 		}
35920373e76bSbonwick 		if (c == children)
35930373e76bSbonwick 			VERIFY(vdev_config_sync(rvd, txg) == 0);
35940373e76bSbonwick 	}
35950373e76bSbonwick 
359699653d4eSeschrock 	dmu_tx_commit(tx);
359799653d4eSeschrock 
35980373e76bSbonwick 	/*
35990373e76bSbonwick 	 * Clear the dirty config list.
3600fa9e4066Sahrens 	 */
36010373e76bSbonwick 	while ((vd = list_head(&spa->spa_dirty_list)) != NULL)
36020373e76bSbonwick 		vdev_config_clean(vd);
36030373e76bSbonwick 
36040373e76bSbonwick 	/*
36050373e76bSbonwick 	 * Now that the new config has synced transactionally,
36060373e76bSbonwick 	 * let it become visible to the config cache.
36070373e76bSbonwick 	 */
36080373e76bSbonwick 	if (spa->spa_config_syncing != NULL) {
36090373e76bSbonwick 		spa_config_set(spa, spa->spa_config_syncing);
36100373e76bSbonwick 		spa->spa_config_txg = txg;
36110373e76bSbonwick 		spa->spa_config_syncing = NULL;
36120373e76bSbonwick 	}
3613fa9e4066Sahrens 
3614fa9e4066Sahrens 	/*
3615fa9e4066Sahrens 	 * Make a stable copy of the fully synced uberblock.
3616fa9e4066Sahrens 	 * We use this as the root for pool traversals.
3617fa9e4066Sahrens 	 */
3618fa9e4066Sahrens 	spa->spa_traverse_wanted = 1;	/* tells traverse_more() to stop */
3619fa9e4066Sahrens 
3620fa9e4066Sahrens 	spa_scrub_suspend(spa);		/* stop scrubbing and finish I/Os */
3621fa9e4066Sahrens 
3622fa9e4066Sahrens 	rw_enter(&spa->spa_traverse_lock, RW_WRITER);
3623fa9e4066Sahrens 	spa->spa_traverse_wanted = 0;
3624fa9e4066Sahrens 	spa->spa_ubsync = spa->spa_uberblock;
3625fa9e4066Sahrens 	rw_exit(&spa->spa_traverse_lock);
3626fa9e4066Sahrens 
3627fa9e4066Sahrens 	spa_scrub_resume(spa);		/* resume scrub with new ubsync */
3628fa9e4066Sahrens 
3629fa9e4066Sahrens 	/*
3630fa9e4066Sahrens 	 * Clean up the ZIL records for the synced txg.
3631fa9e4066Sahrens 	 */
3632fa9e4066Sahrens 	dsl_pool_zil_clean(dp);
3633fa9e4066Sahrens 
3634fa9e4066Sahrens 	/*
3635fa9e4066Sahrens 	 * Update usable space statistics.
3636fa9e4066Sahrens 	 */
3637fa9e4066Sahrens 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
3638fa9e4066Sahrens 		vdev_sync_done(vd, txg);
3639fa9e4066Sahrens 
3640fa9e4066Sahrens 	/*
3641fa9e4066Sahrens 	 * It had better be the case that we didn't dirty anything
364299653d4eSeschrock 	 * since vdev_config_sync().
3643fa9e4066Sahrens 	 */
3644fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
3645fa9e4066Sahrens 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
3646fa9e4066Sahrens 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
3647fa9e4066Sahrens 	ASSERT(bpl->bpl_queue == NULL);
3648fa9e4066Sahrens 
3649ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
3650ea8dc4b6Seschrock 
3651ea8dc4b6Seschrock 	/*
3652ea8dc4b6Seschrock 	 * If any async tasks have been requested, kick them off.
3653ea8dc4b6Seschrock 	 */
3654ea8dc4b6Seschrock 	spa_async_dispatch(spa);
3655fa9e4066Sahrens }
3656fa9e4066Sahrens 
3657fa9e4066Sahrens /*
3658fa9e4066Sahrens  * Sync all pools.  We don't want to hold the namespace lock across these
3659fa9e4066Sahrens  * operations, so we take a reference on the spa_t and drop the lock during the
3660fa9e4066Sahrens  * sync.
3661fa9e4066Sahrens  */
3662fa9e4066Sahrens void
3663fa9e4066Sahrens spa_sync_allpools(void)
3664fa9e4066Sahrens {
3665fa9e4066Sahrens 	spa_t *spa = NULL;
3666fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
3667fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
3668fa9e4066Sahrens 		if (spa_state(spa) != POOL_STATE_ACTIVE)
3669fa9e4066Sahrens 			continue;
3670fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
3671fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
3672fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
3673fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
3674fa9e4066Sahrens 		spa_close(spa, FTAG);
3675fa9e4066Sahrens 	}
3676fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
3677fa9e4066Sahrens }
3678fa9e4066Sahrens 
3679fa9e4066Sahrens /*
3680fa9e4066Sahrens  * ==========================================================================
3681fa9e4066Sahrens  * Miscellaneous routines
3682fa9e4066Sahrens  * ==========================================================================
3683fa9e4066Sahrens  */
3684fa9e4066Sahrens 
3685fa9e4066Sahrens /*
3686fa9e4066Sahrens  * Remove all pools in the system.
3687fa9e4066Sahrens  */
3688fa9e4066Sahrens void
3689fa9e4066Sahrens spa_evict_all(void)
3690fa9e4066Sahrens {
3691fa9e4066Sahrens 	spa_t *spa;
3692fa9e4066Sahrens 
3693fa9e4066Sahrens 	/*
3694fa9e4066Sahrens 	 * Remove all cached state.  All pools should be closed now,
3695fa9e4066Sahrens 	 * so every spa in the AVL tree should be unreferenced.
3696fa9e4066Sahrens 	 */
3697fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
3698fa9e4066Sahrens 	while ((spa = spa_next(NULL)) != NULL) {
3699fa9e4066Sahrens 		/*
3700ea8dc4b6Seschrock 		 * Stop async tasks.  The async thread may need to detach
3701ea8dc4b6Seschrock 		 * a device that's been replaced, which requires grabbing
3702ea8dc4b6Seschrock 		 * spa_namespace_lock, so we must drop it here.
3703fa9e4066Sahrens 		 */
3704fa9e4066Sahrens 		spa_open_ref(spa, FTAG);
3705fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
3706ea8dc4b6Seschrock 		spa_async_suspend(spa);
3707fa9e4066Sahrens 		mutex_enter(&spa_namespace_lock);
3708bb8b5132Sek 		VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0);
3709fa9e4066Sahrens 		spa_close(spa, FTAG);
3710fa9e4066Sahrens 
3711fa9e4066Sahrens 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3712fa9e4066Sahrens 			spa_unload(spa);
3713fa9e4066Sahrens 			spa_deactivate(spa);
3714fa9e4066Sahrens 		}
3715fa9e4066Sahrens 		spa_remove(spa);
3716fa9e4066Sahrens 	}
3717fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
3718fa9e4066Sahrens }
3719ea8dc4b6Seschrock 
3720ea8dc4b6Seschrock vdev_t *
3721ea8dc4b6Seschrock spa_lookup_by_guid(spa_t *spa, uint64_t guid)
3722ea8dc4b6Seschrock {
3723ea8dc4b6Seschrock 	return (vdev_lookup_by_guid(spa->spa_root_vdev, guid));
3724ea8dc4b6Seschrock }
3725eaca9bbdSeschrock 
3726eaca9bbdSeschrock void
3727990b4856Slling spa_upgrade(spa_t *spa, uint64_t version)
3728eaca9bbdSeschrock {
3729eaca9bbdSeschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
3730eaca9bbdSeschrock 
3731eaca9bbdSeschrock 	/*
3732eaca9bbdSeschrock 	 * This should only be called for a non-faulted pool, and since a
3733eaca9bbdSeschrock 	 * future version would result in an unopenable pool, this shouldn't be
3734eaca9bbdSeschrock 	 * possible.
3735eaca9bbdSeschrock 	 */
3736e7437265Sahrens 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
3737990b4856Slling 	ASSERT(version >= spa->spa_uberblock.ub_version);
3738eaca9bbdSeschrock 
3739990b4856Slling 	spa->spa_uberblock.ub_version = version;
3740eaca9bbdSeschrock 	vdev_config_dirty(spa->spa_root_vdev);
3741eaca9bbdSeschrock 
3742eaca9bbdSeschrock 	spa_config_exit(spa, FTAG);
374399653d4eSeschrock 
374499653d4eSeschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
374599653d4eSeschrock }
374699653d4eSeschrock 
374799653d4eSeschrock boolean_t
374899653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid)
374999653d4eSeschrock {
375099653d4eSeschrock 	int i;
375139c23413Seschrock 	uint64_t spareguid;
375299653d4eSeschrock 
375399653d4eSeschrock 	for (i = 0; i < spa->spa_nspares; i++)
375499653d4eSeschrock 		if (spa->spa_spares[i]->vdev_guid == guid)
375599653d4eSeschrock 			return (B_TRUE);
375699653d4eSeschrock 
375739c23413Seschrock 	for (i = 0; i < spa->spa_pending_nspares; i++) {
375839c23413Seschrock 		if (nvlist_lookup_uint64(spa->spa_pending_spares[i],
375939c23413Seschrock 		    ZPOOL_CONFIG_GUID, &spareguid) == 0 &&
376039c23413Seschrock 		    spareguid == guid)
376139c23413Seschrock 			return (B_TRUE);
376239c23413Seschrock 	}
376339c23413Seschrock 
376499653d4eSeschrock 	return (B_FALSE);
3765eaca9bbdSeschrock }
3766b1b8ab34Slling 
37673d7072f8Seschrock /*
37683d7072f8Seschrock  * Post a sysevent corresponding to the given event.  The 'name' must be one of
37693d7072f8Seschrock  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
37703d7072f8Seschrock  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
37713d7072f8Seschrock  * in the userland libzpool, as we don't want consumers to misinterpret ztest
37723d7072f8Seschrock  * or zdb as real changes.
37733d7072f8Seschrock  */
37743d7072f8Seschrock void
37753d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
37763d7072f8Seschrock {
37773d7072f8Seschrock #ifdef _KERNEL
37783d7072f8Seschrock 	sysevent_t		*ev;
37793d7072f8Seschrock 	sysevent_attr_list_t	*attr = NULL;
37803d7072f8Seschrock 	sysevent_value_t	value;
37813d7072f8Seschrock 	sysevent_id_t		eid;
37823d7072f8Seschrock 
37833d7072f8Seschrock 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
37843d7072f8Seschrock 	    SE_SLEEP);
37853d7072f8Seschrock 
37863d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_STRING;
37873d7072f8Seschrock 	value.value.sv_string = spa_name(spa);
37883d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
37893d7072f8Seschrock 		goto done;
37903d7072f8Seschrock 
37913d7072f8Seschrock 	value.value_type = SE_DATA_TYPE_UINT64;
37923d7072f8Seschrock 	value.value.sv_uint64 = spa_guid(spa);
37933d7072f8Seschrock 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
37943d7072f8Seschrock 		goto done;
37953d7072f8Seschrock 
37963d7072f8Seschrock 	if (vd) {
37973d7072f8Seschrock 		value.value_type = SE_DATA_TYPE_UINT64;
37983d7072f8Seschrock 		value.value.sv_uint64 = vd->vdev_guid;
37993d7072f8Seschrock 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
38003d7072f8Seschrock 		    SE_SLEEP) != 0)
38013d7072f8Seschrock 			goto done;
38023d7072f8Seschrock 
38033d7072f8Seschrock 		if (vd->vdev_path) {
38043d7072f8Seschrock 			value.value_type = SE_DATA_TYPE_STRING;
38053d7072f8Seschrock 			value.value.sv_string = vd->vdev_path;
38063d7072f8Seschrock 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
38073d7072f8Seschrock 			    &value, SE_SLEEP) != 0)
38083d7072f8Seschrock 				goto done;
38093d7072f8Seschrock 		}
38103d7072f8Seschrock 	}
38113d7072f8Seschrock 
38123d7072f8Seschrock 	(void) log_sysevent(ev, SE_SLEEP, &eid);
38133d7072f8Seschrock 
38143d7072f8Seschrock done:
38153d7072f8Seschrock 	if (attr)
38163d7072f8Seschrock 		sysevent_free_attr(attr);
38173d7072f8Seschrock 	sysevent_free(ev);
38183d7072f8Seschrock #endif
38193d7072f8Seschrock }
3820