xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa.c (revision b3388e4fc5f5c24c8a39fbe132a00b02dae5b717)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This file contains all the routines used when modifying on-disk SPA state.
29  * This includes opening, importing, destroying, exporting a pool, and syncing a
30  * pool.
31  */
32 
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa_impl.h>
36 #include <sys/zio.h>
37 #include <sys/zio_checksum.h>
38 #include <sys/dmu.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/zap.h>
41 #include <sys/zil.h>
42 #include <sys/ddt.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/metaslab.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/uberblock_impl.h>
47 #include <sys/txg.h>
48 #include <sys/avl.h>
49 #include <sys/dmu_traverse.h>
50 #include <sys/dmu_objset.h>
51 #include <sys/unique.h>
52 #include <sys/dsl_pool.h>
53 #include <sys/dsl_dataset.h>
54 #include <sys/dsl_dir.h>
55 #include <sys/dsl_prop.h>
56 #include <sys/dsl_synctask.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/arc.h>
59 #include <sys/callb.h>
60 #include <sys/systeminfo.h>
61 #include <sys/spa_boot.h>
62 #include <sys/zfs_ioctl.h>
63 
64 #ifdef	_KERNEL
65 #include <sys/zone.h>
66 #include <sys/bootprops.h>
67 #endif	/* _KERNEL */
68 
69 #include "zfs_prop.h"
70 #include "zfs_comutil.h"
71 
72 enum zti_modes {
73 	zti_mode_fixed,			/* value is # of threads (min 1) */
74 	zti_mode_online_percent,	/* value is % of online CPUs */
75 	zti_mode_tune,			/* fill from zio_taskq_tune_* */
76 	zti_nmodes
77 };
78 
79 #define	ZTI_THREAD_FIX(n)	{ zti_mode_fixed, (n) }
80 #define	ZTI_THREAD_PCT(n)	{ zti_mode_online_percent, (n) }
81 #define	ZTI_THREAD_TUNE		{ zti_mode_tune, 0 }
82 
83 #define	ZTI_THREAD_ONE		ZTI_THREAD_FIX(1)
84 
85 typedef struct zio_taskq_info {
86 	const char *zti_name;
87 	struct {
88 		enum zti_modes zti_mode;
89 		uint_t zti_value;
90 	} zti_nthreads[ZIO_TASKQ_TYPES];
91 } zio_taskq_info_t;
92 
93 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
94 				"issue",		"intr"
95 };
96 
97 const zio_taskq_info_t zio_taskqs[ZIO_TYPES] = {
98 	/*			ISSUE			INTR		*/
99 	{ "spa_zio_null",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
100 	{ "spa_zio_read",	{ ZTI_THREAD_FIX(8),	ZTI_THREAD_TUNE } },
101 	{ "spa_zio_write",	{ ZTI_THREAD_TUNE,	ZTI_THREAD_FIX(8) } },
102 	{ "spa_zio_free",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
103 	{ "spa_zio_claim",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
104 	{ "spa_zio_ioctl",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
105 };
106 
107 enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent;
108 uint_t zio_taskq_tune_value = 80;	/* #threads = 80% of # online CPUs */
109 
110 static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
111 static boolean_t spa_has_active_shared_spare(spa_t *spa);
112 
113 /*
114  * ==========================================================================
115  * SPA properties routines
116  * ==========================================================================
117  */
118 
119 /*
120  * Add a (source=src, propname=propval) list to an nvlist.
121  */
122 static void
123 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
124     uint64_t intval, zprop_source_t src)
125 {
126 	const char *propname = zpool_prop_to_name(prop);
127 	nvlist_t *propval;
128 
129 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
130 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
131 
132 	if (strval != NULL)
133 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
134 	else
135 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
136 
137 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
138 	nvlist_free(propval);
139 }
140 
141 /*
142  * Get property values from the spa configuration.
143  */
144 static void
145 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
146 {
147 	uint64_t size;
148 	uint64_t alloc;
149 	uint64_t cap, version;
150 	zprop_source_t src = ZPROP_SRC_NONE;
151 	spa_config_dirent_t *dp;
152 
153 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
154 
155 	if (spa->spa_root_vdev != NULL) {
156 		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
157 		size = metaslab_class_get_space(spa_normal_class(spa));
158 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
159 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
160 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
161 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
162 		    size - alloc, src);
163 
164 		cap = (size == 0) ? 0 : (alloc * 100 / size);
165 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
166 
167 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
168 		    ddt_get_pool_dedup_ratio(spa), src);
169 
170 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
171 		    spa->spa_root_vdev->vdev_state, src);
172 
173 		version = spa_version(spa);
174 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
175 			src = ZPROP_SRC_DEFAULT;
176 		else
177 			src = ZPROP_SRC_LOCAL;
178 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
179 	}
180 
181 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
182 
183 	if (spa->spa_root != NULL)
184 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
185 		    0, ZPROP_SRC_LOCAL);
186 
187 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
188 		if (dp->scd_path == NULL) {
189 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
190 			    "none", 0, ZPROP_SRC_LOCAL);
191 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
192 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
193 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
194 		}
195 	}
196 }
197 
198 /*
199  * Get zpool property values.
200  */
201 int
202 spa_prop_get(spa_t *spa, nvlist_t **nvp)
203 {
204 	objset_t *mos = spa->spa_meta_objset;
205 	zap_cursor_t zc;
206 	zap_attribute_t za;
207 	int err;
208 
209 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
210 
211 	mutex_enter(&spa->spa_props_lock);
212 
213 	/*
214 	 * Get properties from the spa config.
215 	 */
216 	spa_prop_get_config(spa, nvp);
217 
218 	/* If no pool property object, no more prop to get. */
219 	if (spa->spa_pool_props_object == 0) {
220 		mutex_exit(&spa->spa_props_lock);
221 		return (0);
222 	}
223 
224 	/*
225 	 * Get properties from the MOS pool property object.
226 	 */
227 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
228 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
229 	    zap_cursor_advance(&zc)) {
230 		uint64_t intval = 0;
231 		char *strval = NULL;
232 		zprop_source_t src = ZPROP_SRC_DEFAULT;
233 		zpool_prop_t prop;
234 
235 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
236 			continue;
237 
238 		switch (za.za_integer_length) {
239 		case 8:
240 			/* integer property */
241 			if (za.za_first_integer !=
242 			    zpool_prop_default_numeric(prop))
243 				src = ZPROP_SRC_LOCAL;
244 
245 			if (prop == ZPOOL_PROP_BOOTFS) {
246 				dsl_pool_t *dp;
247 				dsl_dataset_t *ds = NULL;
248 
249 				dp = spa_get_dsl(spa);
250 				rw_enter(&dp->dp_config_rwlock, RW_READER);
251 				if (err = dsl_dataset_hold_obj(dp,
252 				    za.za_first_integer, FTAG, &ds)) {
253 					rw_exit(&dp->dp_config_rwlock);
254 					break;
255 				}
256 
257 				strval = kmem_alloc(
258 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
259 				    KM_SLEEP);
260 				dsl_dataset_name(ds, strval);
261 				dsl_dataset_rele(ds, FTAG);
262 				rw_exit(&dp->dp_config_rwlock);
263 			} else {
264 				strval = NULL;
265 				intval = za.za_first_integer;
266 			}
267 
268 			spa_prop_add_list(*nvp, prop, strval, intval, src);
269 
270 			if (strval != NULL)
271 				kmem_free(strval,
272 				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
273 
274 			break;
275 
276 		case 1:
277 			/* string property */
278 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
279 			err = zap_lookup(mos, spa->spa_pool_props_object,
280 			    za.za_name, 1, za.za_num_integers, strval);
281 			if (err) {
282 				kmem_free(strval, za.za_num_integers);
283 				break;
284 			}
285 			spa_prop_add_list(*nvp, prop, strval, 0, src);
286 			kmem_free(strval, za.za_num_integers);
287 			break;
288 
289 		default:
290 			break;
291 		}
292 	}
293 	zap_cursor_fini(&zc);
294 	mutex_exit(&spa->spa_props_lock);
295 out:
296 	if (err && err != ENOENT) {
297 		nvlist_free(*nvp);
298 		*nvp = NULL;
299 		return (err);
300 	}
301 
302 	return (0);
303 }
304 
305 /*
306  * Validate the given pool properties nvlist and modify the list
307  * for the property values to be set.
308  */
309 static int
310 spa_prop_validate(spa_t *spa, nvlist_t *props)
311 {
312 	nvpair_t *elem;
313 	int error = 0, reset_bootfs = 0;
314 	uint64_t objnum;
315 
316 	elem = NULL;
317 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
318 		zpool_prop_t prop;
319 		char *propname, *strval;
320 		uint64_t intval;
321 		objset_t *os;
322 		char *slash;
323 
324 		propname = nvpair_name(elem);
325 
326 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
327 			return (EINVAL);
328 
329 		switch (prop) {
330 		case ZPOOL_PROP_VERSION:
331 			error = nvpair_value_uint64(elem, &intval);
332 			if (!error &&
333 			    (intval < spa_version(spa) || intval > SPA_VERSION))
334 				error = EINVAL;
335 			break;
336 
337 		case ZPOOL_PROP_DELEGATION:
338 		case ZPOOL_PROP_AUTOREPLACE:
339 		case ZPOOL_PROP_LISTSNAPS:
340 		case ZPOOL_PROP_AUTOEXPAND:
341 			error = nvpair_value_uint64(elem, &intval);
342 			if (!error && intval > 1)
343 				error = EINVAL;
344 			break;
345 
346 		case ZPOOL_PROP_BOOTFS:
347 			/*
348 			 * If the pool version is less than SPA_VERSION_BOOTFS,
349 			 * or the pool is still being created (version == 0),
350 			 * the bootfs property cannot be set.
351 			 */
352 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
353 				error = ENOTSUP;
354 				break;
355 			}
356 
357 			/*
358 			 * Make sure the vdev config is bootable
359 			 */
360 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
361 				error = ENOTSUP;
362 				break;
363 			}
364 
365 			reset_bootfs = 1;
366 
367 			error = nvpair_value_string(elem, &strval);
368 
369 			if (!error) {
370 				uint64_t compress;
371 
372 				if (strval == NULL || strval[0] == '\0') {
373 					objnum = zpool_prop_default_numeric(
374 					    ZPOOL_PROP_BOOTFS);
375 					break;
376 				}
377 
378 				if (error = dmu_objset_hold(strval, FTAG, &os))
379 					break;
380 
381 				/* Must be ZPL and not gzip compressed. */
382 
383 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
384 					error = ENOTSUP;
385 				} else if ((error = dsl_prop_get_integer(strval,
386 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
387 				    &compress, NULL)) == 0 &&
388 				    !BOOTFS_COMPRESS_VALID(compress)) {
389 					error = ENOTSUP;
390 				} else {
391 					objnum = dmu_objset_id(os);
392 				}
393 				dmu_objset_rele(os, FTAG);
394 			}
395 			break;
396 
397 		case ZPOOL_PROP_FAILUREMODE:
398 			error = nvpair_value_uint64(elem, &intval);
399 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
400 			    intval > ZIO_FAILURE_MODE_PANIC))
401 				error = EINVAL;
402 
403 			/*
404 			 * This is a special case which only occurs when
405 			 * the pool has completely failed. This allows
406 			 * the user to change the in-core failmode property
407 			 * without syncing it out to disk (I/Os might
408 			 * currently be blocked). We do this by returning
409 			 * EIO to the caller (spa_prop_set) to trick it
410 			 * into thinking we encountered a property validation
411 			 * error.
412 			 */
413 			if (!error && spa_suspended(spa)) {
414 				spa->spa_failmode = intval;
415 				error = EIO;
416 			}
417 			break;
418 
419 		case ZPOOL_PROP_CACHEFILE:
420 			if ((error = nvpair_value_string(elem, &strval)) != 0)
421 				break;
422 
423 			if (strval[0] == '\0')
424 				break;
425 
426 			if (strcmp(strval, "none") == 0)
427 				break;
428 
429 			if (strval[0] != '/') {
430 				error = EINVAL;
431 				break;
432 			}
433 
434 			slash = strrchr(strval, '/');
435 			ASSERT(slash != NULL);
436 
437 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
438 			    strcmp(slash, "/..") == 0)
439 				error = EINVAL;
440 			break;
441 
442 		case ZPOOL_PROP_DEDUPDITTO:
443 			if (spa_version(spa) < SPA_VERSION_DEDUP)
444 				error = ENOTSUP;
445 			else
446 				error = nvpair_value_uint64(elem, &intval);
447 			if (error == 0 &&
448 			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
449 				error = EINVAL;
450 			break;
451 		}
452 
453 		if (error)
454 			break;
455 	}
456 
457 	if (!error && reset_bootfs) {
458 		error = nvlist_remove(props,
459 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
460 
461 		if (!error) {
462 			error = nvlist_add_uint64(props,
463 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
464 		}
465 	}
466 
467 	return (error);
468 }
469 
470 void
471 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
472 {
473 	char *cachefile;
474 	spa_config_dirent_t *dp;
475 
476 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
477 	    &cachefile) != 0)
478 		return;
479 
480 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
481 	    KM_SLEEP);
482 
483 	if (cachefile[0] == '\0')
484 		dp->scd_path = spa_strdup(spa_config_path);
485 	else if (strcmp(cachefile, "none") == 0)
486 		dp->scd_path = NULL;
487 	else
488 		dp->scd_path = spa_strdup(cachefile);
489 
490 	list_insert_head(&spa->spa_config_list, dp);
491 	if (need_sync)
492 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
493 }
494 
495 int
496 spa_prop_set(spa_t *spa, nvlist_t *nvp)
497 {
498 	int error;
499 	nvpair_t *elem;
500 	boolean_t need_sync = B_FALSE;
501 	zpool_prop_t prop;
502 
503 	if ((error = spa_prop_validate(spa, nvp)) != 0)
504 		return (error);
505 
506 	elem = NULL;
507 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
508 		if ((prop = zpool_name_to_prop(
509 		    nvpair_name(elem))) == ZPROP_INVAL)
510 			return (EINVAL);
511 
512 		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
513 			continue;
514 
515 		need_sync = B_TRUE;
516 		break;
517 	}
518 
519 	if (need_sync)
520 		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
521 		    spa, nvp, 3));
522 	else
523 		return (0);
524 }
525 
526 /*
527  * If the bootfs property value is dsobj, clear it.
528  */
529 void
530 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
531 {
532 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
533 		VERIFY(zap_remove(spa->spa_meta_objset,
534 		    spa->spa_pool_props_object,
535 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
536 		spa->spa_bootfs = 0;
537 	}
538 }
539 
540 /*
541  * ==========================================================================
542  * SPA state manipulation (open/create/destroy/import/export)
543  * ==========================================================================
544  */
545 
546 static int
547 spa_error_entry_compare(const void *a, const void *b)
548 {
549 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
550 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
551 	int ret;
552 
553 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
554 	    sizeof (zbookmark_t));
555 
556 	if (ret < 0)
557 		return (-1);
558 	else if (ret > 0)
559 		return (1);
560 	else
561 		return (0);
562 }
563 
564 /*
565  * Utility function which retrieves copies of the current logs and
566  * re-initializes them in the process.
567  */
568 void
569 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
570 {
571 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
572 
573 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
574 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
575 
576 	avl_create(&spa->spa_errlist_scrub,
577 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
578 	    offsetof(spa_error_entry_t, se_avl));
579 	avl_create(&spa->spa_errlist_last,
580 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
581 	    offsetof(spa_error_entry_t, se_avl));
582 }
583 
584 /*
585  * Activate an uninitialized pool.
586  */
587 static void
588 spa_activate(spa_t *spa, int mode)
589 {
590 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
591 
592 	spa->spa_state = POOL_STATE_ACTIVE;
593 	spa->spa_mode = mode;
594 
595 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
596 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
597 
598 	for (int t = 0; t < ZIO_TYPES; t++) {
599 		const zio_taskq_info_t *ztip = &zio_taskqs[t];
600 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
601 			enum zti_modes mode = ztip->zti_nthreads[q].zti_mode;
602 			uint_t value = ztip->zti_nthreads[q].zti_value;
603 			char name[32];
604 
605 			(void) snprintf(name, sizeof (name),
606 			    "%s_%s", ztip->zti_name, zio_taskq_types[q]);
607 
608 			if (mode == zti_mode_tune) {
609 				mode = zio_taskq_tune_mode;
610 				value = zio_taskq_tune_value;
611 				if (mode == zti_mode_tune)
612 					mode = zti_mode_online_percent;
613 			}
614 
615 			switch (mode) {
616 			case zti_mode_fixed:
617 				ASSERT3U(value, >=, 1);
618 				value = MAX(value, 1);
619 
620 				spa->spa_zio_taskq[t][q] = taskq_create(name,
621 				    value, maxclsyspri, 50, INT_MAX,
622 				    TASKQ_PREPOPULATE);
623 				break;
624 
625 			case zti_mode_online_percent:
626 				spa->spa_zio_taskq[t][q] = taskq_create(name,
627 				    value, maxclsyspri, 50, INT_MAX,
628 				    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
629 				break;
630 
631 			case zti_mode_tune:
632 			default:
633 				panic("unrecognized mode for "
634 				    "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) "
635 				    "in spa_activate()",
636 				    t, q, mode, value);
637 				break;
638 			}
639 		}
640 	}
641 
642 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
643 	    offsetof(vdev_t, vdev_config_dirty_node));
644 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
645 	    offsetof(vdev_t, vdev_state_dirty_node));
646 
647 	txg_list_create(&spa->spa_vdev_txg_list,
648 	    offsetof(struct vdev, vdev_txg_node));
649 
650 	avl_create(&spa->spa_errlist_scrub,
651 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
652 	    offsetof(spa_error_entry_t, se_avl));
653 	avl_create(&spa->spa_errlist_last,
654 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
655 	    offsetof(spa_error_entry_t, se_avl));
656 }
657 
658 /*
659  * Opposite of spa_activate().
660  */
661 static void
662 spa_deactivate(spa_t *spa)
663 {
664 	ASSERT(spa->spa_sync_on == B_FALSE);
665 	ASSERT(spa->spa_dsl_pool == NULL);
666 	ASSERT(spa->spa_root_vdev == NULL);
667 	ASSERT(spa->spa_async_zio_root == NULL);
668 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
669 
670 	txg_list_destroy(&spa->spa_vdev_txg_list);
671 
672 	list_destroy(&spa->spa_config_dirty_list);
673 	list_destroy(&spa->spa_state_dirty_list);
674 
675 	for (int t = 0; t < ZIO_TYPES; t++) {
676 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
677 			taskq_destroy(spa->spa_zio_taskq[t][q]);
678 			spa->spa_zio_taskq[t][q] = NULL;
679 		}
680 	}
681 
682 	metaslab_class_destroy(spa->spa_normal_class);
683 	spa->spa_normal_class = NULL;
684 
685 	metaslab_class_destroy(spa->spa_log_class);
686 	spa->spa_log_class = NULL;
687 
688 	/*
689 	 * If this was part of an import or the open otherwise failed, we may
690 	 * still have errors left in the queues.  Empty them just in case.
691 	 */
692 	spa_errlog_drain(spa);
693 
694 	avl_destroy(&spa->spa_errlist_scrub);
695 	avl_destroy(&spa->spa_errlist_last);
696 
697 	spa->spa_state = POOL_STATE_UNINITIALIZED;
698 }
699 
700 /*
701  * Verify a pool configuration, and construct the vdev tree appropriately.  This
702  * will create all the necessary vdevs in the appropriate layout, with each vdev
703  * in the CLOSED state.  This will prep the pool before open/creation/import.
704  * All vdev validation is done by the vdev_alloc() routine.
705  */
706 static int
707 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
708     uint_t id, int atype)
709 {
710 	nvlist_t **child;
711 	uint_t children;
712 	int error;
713 
714 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
715 		return (error);
716 
717 	if ((*vdp)->vdev_ops->vdev_op_leaf)
718 		return (0);
719 
720 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
721 	    &child, &children);
722 
723 	if (error == ENOENT)
724 		return (0);
725 
726 	if (error) {
727 		vdev_free(*vdp);
728 		*vdp = NULL;
729 		return (EINVAL);
730 	}
731 
732 	for (int c = 0; c < children; c++) {
733 		vdev_t *vd;
734 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
735 		    atype)) != 0) {
736 			vdev_free(*vdp);
737 			*vdp = NULL;
738 			return (error);
739 		}
740 	}
741 
742 	ASSERT(*vdp != NULL);
743 
744 	return (0);
745 }
746 
747 /*
748  * Opposite of spa_load().
749  */
750 static void
751 spa_unload(spa_t *spa)
752 {
753 	int i;
754 
755 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
756 
757 	/*
758 	 * Stop async tasks.
759 	 */
760 	spa_async_suspend(spa);
761 
762 	/*
763 	 * Stop syncing.
764 	 */
765 	if (spa->spa_sync_on) {
766 		txg_sync_stop(spa->spa_dsl_pool);
767 		spa->spa_sync_on = B_FALSE;
768 	}
769 
770 	/*
771 	 * Wait for any outstanding async I/O to complete.
772 	 */
773 	if (spa->spa_async_zio_root != NULL) {
774 		(void) zio_wait(spa->spa_async_zio_root);
775 		spa->spa_async_zio_root = NULL;
776 	}
777 
778 	/*
779 	 * Close the dsl pool.
780 	 */
781 	if (spa->spa_dsl_pool) {
782 		dsl_pool_close(spa->spa_dsl_pool);
783 		spa->spa_dsl_pool = NULL;
784 	}
785 
786 	ddt_unload(spa);
787 
788 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
789 
790 	/*
791 	 * Drop and purge level 2 cache
792 	 */
793 	spa_l2cache_drop(spa);
794 
795 	/*
796 	 * Close all vdevs.
797 	 */
798 	if (spa->spa_root_vdev)
799 		vdev_free(spa->spa_root_vdev);
800 	ASSERT(spa->spa_root_vdev == NULL);
801 
802 	for (i = 0; i < spa->spa_spares.sav_count; i++)
803 		vdev_free(spa->spa_spares.sav_vdevs[i]);
804 	if (spa->spa_spares.sav_vdevs) {
805 		kmem_free(spa->spa_spares.sav_vdevs,
806 		    spa->spa_spares.sav_count * sizeof (void *));
807 		spa->spa_spares.sav_vdevs = NULL;
808 	}
809 	if (spa->spa_spares.sav_config) {
810 		nvlist_free(spa->spa_spares.sav_config);
811 		spa->spa_spares.sav_config = NULL;
812 	}
813 	spa->spa_spares.sav_count = 0;
814 
815 	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
816 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
817 	if (spa->spa_l2cache.sav_vdevs) {
818 		kmem_free(spa->spa_l2cache.sav_vdevs,
819 		    spa->spa_l2cache.sav_count * sizeof (void *));
820 		spa->spa_l2cache.sav_vdevs = NULL;
821 	}
822 	if (spa->spa_l2cache.sav_config) {
823 		nvlist_free(spa->spa_l2cache.sav_config);
824 		spa->spa_l2cache.sav_config = NULL;
825 	}
826 	spa->spa_l2cache.sav_count = 0;
827 
828 	spa->spa_async_suspended = 0;
829 
830 	spa_config_exit(spa, SCL_ALL, FTAG);
831 }
832 
833 /*
834  * Load (or re-load) the current list of vdevs describing the active spares for
835  * this pool.  When this is called, we have some form of basic information in
836  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
837  * then re-generate a more complete list including status information.
838  */
839 static void
840 spa_load_spares(spa_t *spa)
841 {
842 	nvlist_t **spares;
843 	uint_t nspares;
844 	int i;
845 	vdev_t *vd, *tvd;
846 
847 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
848 
849 	/*
850 	 * First, close and free any existing spare vdevs.
851 	 */
852 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
853 		vd = spa->spa_spares.sav_vdevs[i];
854 
855 		/* Undo the call to spa_activate() below */
856 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
857 		    B_FALSE)) != NULL && tvd->vdev_isspare)
858 			spa_spare_remove(tvd);
859 		vdev_close(vd);
860 		vdev_free(vd);
861 	}
862 
863 	if (spa->spa_spares.sav_vdevs)
864 		kmem_free(spa->spa_spares.sav_vdevs,
865 		    spa->spa_spares.sav_count * sizeof (void *));
866 
867 	if (spa->spa_spares.sav_config == NULL)
868 		nspares = 0;
869 	else
870 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
871 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
872 
873 	spa->spa_spares.sav_count = (int)nspares;
874 	spa->spa_spares.sav_vdevs = NULL;
875 
876 	if (nspares == 0)
877 		return;
878 
879 	/*
880 	 * Construct the array of vdevs, opening them to get status in the
881 	 * process.   For each spare, there is potentially two different vdev_t
882 	 * structures associated with it: one in the list of spares (used only
883 	 * for basic validation purposes) and one in the active vdev
884 	 * configuration (if it's spared in).  During this phase we open and
885 	 * validate each vdev on the spare list.  If the vdev also exists in the
886 	 * active configuration, then we also mark this vdev as an active spare.
887 	 */
888 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
889 	    KM_SLEEP);
890 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
891 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
892 		    VDEV_ALLOC_SPARE) == 0);
893 		ASSERT(vd != NULL);
894 
895 		spa->spa_spares.sav_vdevs[i] = vd;
896 
897 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
898 		    B_FALSE)) != NULL) {
899 			if (!tvd->vdev_isspare)
900 				spa_spare_add(tvd);
901 
902 			/*
903 			 * We only mark the spare active if we were successfully
904 			 * able to load the vdev.  Otherwise, importing a pool
905 			 * with a bad active spare would result in strange
906 			 * behavior, because multiple pool would think the spare
907 			 * is actively in use.
908 			 *
909 			 * There is a vulnerability here to an equally bizarre
910 			 * circumstance, where a dead active spare is later
911 			 * brought back to life (onlined or otherwise).  Given
912 			 * the rarity of this scenario, and the extra complexity
913 			 * it adds, we ignore the possibility.
914 			 */
915 			if (!vdev_is_dead(tvd))
916 				spa_spare_activate(tvd);
917 		}
918 
919 		vd->vdev_top = vd;
920 		vd->vdev_aux = &spa->spa_spares;
921 
922 		if (vdev_open(vd) != 0)
923 			continue;
924 
925 		if (vdev_validate_aux(vd) == 0)
926 			spa_spare_add(vd);
927 	}
928 
929 	/*
930 	 * Recompute the stashed list of spares, with status information
931 	 * this time.
932 	 */
933 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
934 	    DATA_TYPE_NVLIST_ARRAY) == 0);
935 
936 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
937 	    KM_SLEEP);
938 	for (i = 0; i < spa->spa_spares.sav_count; i++)
939 		spares[i] = vdev_config_generate(spa,
940 		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
941 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
942 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
943 	for (i = 0; i < spa->spa_spares.sav_count; i++)
944 		nvlist_free(spares[i]);
945 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
946 }
947 
948 /*
949  * Load (or re-load) the current list of vdevs describing the active l2cache for
950  * this pool.  When this is called, we have some form of basic information in
951  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
952  * then re-generate a more complete list including status information.
953  * Devices which are already active have their details maintained, and are
954  * not re-opened.
955  */
956 static void
957 spa_load_l2cache(spa_t *spa)
958 {
959 	nvlist_t **l2cache;
960 	uint_t nl2cache;
961 	int i, j, oldnvdevs;
962 	uint64_t guid;
963 	vdev_t *vd, **oldvdevs, **newvdevs;
964 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
965 
966 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
967 
968 	if (sav->sav_config != NULL) {
969 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
970 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
971 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
972 	} else {
973 		nl2cache = 0;
974 	}
975 
976 	oldvdevs = sav->sav_vdevs;
977 	oldnvdevs = sav->sav_count;
978 	sav->sav_vdevs = NULL;
979 	sav->sav_count = 0;
980 
981 	/*
982 	 * Process new nvlist of vdevs.
983 	 */
984 	for (i = 0; i < nl2cache; i++) {
985 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
986 		    &guid) == 0);
987 
988 		newvdevs[i] = NULL;
989 		for (j = 0; j < oldnvdevs; j++) {
990 			vd = oldvdevs[j];
991 			if (vd != NULL && guid == vd->vdev_guid) {
992 				/*
993 				 * Retain previous vdev for add/remove ops.
994 				 */
995 				newvdevs[i] = vd;
996 				oldvdevs[j] = NULL;
997 				break;
998 			}
999 		}
1000 
1001 		if (newvdevs[i] == NULL) {
1002 			/*
1003 			 * Create new vdev
1004 			 */
1005 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1006 			    VDEV_ALLOC_L2CACHE) == 0);
1007 			ASSERT(vd != NULL);
1008 			newvdevs[i] = vd;
1009 
1010 			/*
1011 			 * Commit this vdev as an l2cache device,
1012 			 * even if it fails to open.
1013 			 */
1014 			spa_l2cache_add(vd);
1015 
1016 			vd->vdev_top = vd;
1017 			vd->vdev_aux = sav;
1018 
1019 			spa_l2cache_activate(vd);
1020 
1021 			if (vdev_open(vd) != 0)
1022 				continue;
1023 
1024 			(void) vdev_validate_aux(vd);
1025 
1026 			if (!vdev_is_dead(vd))
1027 				l2arc_add_vdev(spa, vd);
1028 		}
1029 	}
1030 
1031 	/*
1032 	 * Purge vdevs that were dropped
1033 	 */
1034 	for (i = 0; i < oldnvdevs; i++) {
1035 		uint64_t pool;
1036 
1037 		vd = oldvdevs[i];
1038 		if (vd != NULL) {
1039 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1040 			    pool != 0ULL && l2arc_vdev_present(vd))
1041 				l2arc_remove_vdev(vd);
1042 			(void) vdev_close(vd);
1043 			spa_l2cache_remove(vd);
1044 		}
1045 	}
1046 
1047 	if (oldvdevs)
1048 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1049 
1050 	if (sav->sav_config == NULL)
1051 		goto out;
1052 
1053 	sav->sav_vdevs = newvdevs;
1054 	sav->sav_count = (int)nl2cache;
1055 
1056 	/*
1057 	 * Recompute the stashed list of l2cache devices, with status
1058 	 * information this time.
1059 	 */
1060 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1061 	    DATA_TYPE_NVLIST_ARRAY) == 0);
1062 
1063 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1064 	for (i = 0; i < sav->sav_count; i++)
1065 		l2cache[i] = vdev_config_generate(spa,
1066 		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
1067 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1068 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1069 out:
1070 	for (i = 0; i < sav->sav_count; i++)
1071 		nvlist_free(l2cache[i]);
1072 	if (sav->sav_count)
1073 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
1074 }
1075 
1076 static int
1077 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1078 {
1079 	dmu_buf_t *db;
1080 	char *packed = NULL;
1081 	size_t nvsize = 0;
1082 	int error;
1083 	*value = NULL;
1084 
1085 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1086 	nvsize = *(uint64_t *)db->db_data;
1087 	dmu_buf_rele(db, FTAG);
1088 
1089 	packed = kmem_alloc(nvsize, KM_SLEEP);
1090 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1091 	    DMU_READ_PREFETCH);
1092 	if (error == 0)
1093 		error = nvlist_unpack(packed, nvsize, value, 0);
1094 	kmem_free(packed, nvsize);
1095 
1096 	return (error);
1097 }
1098 
1099 /*
1100  * Checks to see if the given vdev could not be opened, in which case we post a
1101  * sysevent to notify the autoreplace code that the device has been removed.
1102  */
1103 static void
1104 spa_check_removed(vdev_t *vd)
1105 {
1106 	for (int c = 0; c < vd->vdev_children; c++)
1107 		spa_check_removed(vd->vdev_child[c]);
1108 
1109 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1110 		zfs_post_autoreplace(vd->vdev_spa, vd);
1111 		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1112 	}
1113 }
1114 
1115 /*
1116  * Load the slog device state from the config object since it's possible
1117  * that the label does not contain the most up-to-date information.
1118  */
1119 void
1120 spa_load_log_state(spa_t *spa, nvlist_t *nv)
1121 {
1122 	vdev_t *ovd, *rvd = spa->spa_root_vdev;
1123 
1124 	/*
1125 	 * Load the original root vdev tree from the passed config.
1126 	 */
1127 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1128 	VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1129 
1130 	for (int c = 0; c < rvd->vdev_children; c++) {
1131 		vdev_t *cvd = rvd->vdev_child[c];
1132 		if (cvd->vdev_islog)
1133 			vdev_load_log_state(cvd, ovd->vdev_child[c]);
1134 	}
1135 	vdev_free(ovd);
1136 	spa_config_exit(spa, SCL_ALL, FTAG);
1137 }
1138 
1139 /*
1140  * Check for missing log devices
1141  */
1142 int
1143 spa_check_logs(spa_t *spa)
1144 {
1145 	switch (spa->spa_log_state) {
1146 	case SPA_LOG_MISSING:
1147 		/* need to recheck in case slog has been restored */
1148 	case SPA_LOG_UNKNOWN:
1149 		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1150 		    DS_FIND_CHILDREN)) {
1151 			spa->spa_log_state = SPA_LOG_MISSING;
1152 			return (1);
1153 		}
1154 		break;
1155 	}
1156 	return (0);
1157 }
1158 
1159 static void
1160 spa_aux_check_removed(spa_aux_vdev_t *sav)
1161 {
1162 	for (int i = 0; i < sav->sav_count; i++)
1163 		spa_check_removed(sav->sav_vdevs[i]);
1164 }
1165 
1166 void
1167 spa_claim_notify(zio_t *zio)
1168 {
1169 	spa_t *spa = zio->io_spa;
1170 
1171 	if (zio->io_error)
1172 		return;
1173 
1174 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
1175 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1176 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1177 	mutex_exit(&spa->spa_props_lock);
1178 }
1179 
1180 typedef struct spa_load_error {
1181 	uint64_t	sle_metadata_count;
1182 	uint64_t	sle_data_count;
1183 } spa_load_error_t;
1184 
1185 static void
1186 spa_load_verify_done(zio_t *zio)
1187 {
1188 	blkptr_t *bp = zio->io_bp;
1189 	spa_load_error_t *sle = zio->io_private;
1190 	dmu_object_type_t type = BP_GET_TYPE(bp);
1191 	int error = zio->io_error;
1192 
1193 	if (error) {
1194 		if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1195 		    type != DMU_OT_INTENT_LOG)
1196 			atomic_add_64(&sle->sle_metadata_count, 1);
1197 		else
1198 			atomic_add_64(&sle->sle_data_count, 1);
1199 	}
1200 	zio_data_buf_free(zio->io_data, zio->io_size);
1201 }
1202 
1203 /*ARGSUSED*/
1204 static int
1205 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1206     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1207 {
1208 	if (bp != NULL) {
1209 		zio_t *rio = arg;
1210 		size_t size = BP_GET_PSIZE(bp);
1211 		void *data = zio_data_buf_alloc(size);
1212 
1213 		zio_nowait(zio_read(rio, spa, bp, data, size,
1214 		    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1215 		    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1216 		    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1217 	}
1218 	return (0);
1219 }
1220 
1221 static int
1222 spa_load_verify(spa_t *spa)
1223 {
1224 	zio_t *rio;
1225 	spa_load_error_t sle = { 0 };
1226 	zpool_rewind_policy_t policy;
1227 	boolean_t verify_ok = B_FALSE;
1228 	int error;
1229 
1230 	rio = zio_root(spa, NULL, &sle,
1231 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1232 
1233 	error = traverse_pool(spa, spa_load_verify_cb, rio,
1234 	    spa->spa_verify_min_txg);
1235 
1236 	(void) zio_wait(rio);
1237 
1238 	zpool_get_rewind_policy(spa->spa_config, &policy);
1239 
1240 	spa->spa_load_meta_errors = sle.sle_metadata_count;
1241 	spa->spa_load_data_errors = sle.sle_data_count;
1242 
1243 	if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta &&
1244 	    sle.sle_data_count <= policy.zrp_maxdata) {
1245 		verify_ok = B_TRUE;
1246 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1247 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1248 	} else {
1249 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1250 	}
1251 
1252 	if (error) {
1253 		if (error != ENXIO && error != EIO)
1254 			error = EIO;
1255 		return (error);
1256 	}
1257 
1258 	return (verify_ok ? 0 : EIO);
1259 }
1260 
1261 /*
1262  * Load an existing storage pool, using the pool's builtin spa_config as a
1263  * source of configuration information.
1264  */
1265 static int
1266 spa_load(spa_t *spa, spa_load_state_t state, int mosconfig)
1267 {
1268 	int error = 0;
1269 	nvlist_t *nvconfig, *nvroot = NULL;
1270 	vdev_t *rvd;
1271 	uberblock_t *ub = &spa->spa_uberblock;
1272 	uint64_t config_cache_txg = spa->spa_config_txg;
1273 	uint64_t pool_guid;
1274 	uint64_t version;
1275 	uint64_t autoreplace = 0;
1276 	int orig_mode = spa->spa_mode;
1277 	char *ereport = FM_EREPORT_ZFS_POOL;
1278 	nvlist_t *config = spa->spa_config;
1279 
1280 	/*
1281 	 * If this is an untrusted config, access the pool in read-only mode.
1282 	 * This prevents things like resilvering recently removed devices.
1283 	 */
1284 	if (!mosconfig)
1285 		spa->spa_mode = FREAD;
1286 
1287 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1288 
1289 	spa->spa_load_state = state;
1290 
1291 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1292 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
1293 		error = EINVAL;
1294 		goto out;
1295 	}
1296 
1297 	/*
1298 	 * Versioning wasn't explicitly added to the label until later, so if
1299 	 * it's not present treat it as the initial version.
1300 	 */
1301 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
1302 		version = SPA_VERSION_INITIAL;
1303 
1304 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1305 	    &spa->spa_config_txg);
1306 
1307 	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1308 	    spa_guid_exists(pool_guid, 0)) {
1309 		error = EEXIST;
1310 		goto out;
1311 	}
1312 
1313 	spa->spa_load_guid = pool_guid;
1314 
1315 	/*
1316 	 * Create "The Godfather" zio to hold all async IOs
1317 	 */
1318 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1319 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1320 
1321 	/*
1322 	 * Parse the configuration into a vdev tree.  We explicitly set the
1323 	 * value that will be returned by spa_version() since parsing the
1324 	 * configuration requires knowing the version number.
1325 	 */
1326 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1327 	spa->spa_ubsync.ub_version = version;
1328 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
1329 	spa_config_exit(spa, SCL_ALL, FTAG);
1330 
1331 	if (error != 0)
1332 		goto out;
1333 
1334 	ASSERT(spa->spa_root_vdev == rvd);
1335 	ASSERT(spa_guid(spa) == pool_guid);
1336 
1337 	/*
1338 	 * Try to open all vdevs, loading each label in the process.
1339 	 */
1340 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1341 	error = vdev_open(rvd);
1342 	spa_config_exit(spa, SCL_ALL, FTAG);
1343 	if (error != 0)
1344 		goto out;
1345 
1346 	/*
1347 	 * We need to validate the vdev labels against the configuration that
1348 	 * we have in hand, which is dependent on the setting of mosconfig. If
1349 	 * mosconfig is true then we're validating the vdev labels based on
1350 	 * that config. Otherwise, we're validating against the cached config
1351 	 * (zpool.cache) that was read when we loaded the zfs module, and then
1352 	 * later we will recursively call spa_load() and validate against
1353 	 * the vdev config.
1354 	 */
1355 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1356 	error = vdev_validate(rvd);
1357 	spa_config_exit(spa, SCL_ALL, FTAG);
1358 	if (error != 0)
1359 		goto out;
1360 
1361 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1362 		error = ENXIO;
1363 		goto out;
1364 	}
1365 
1366 	/*
1367 	 * Find the best uberblock.
1368 	 */
1369 	vdev_uberblock_load(NULL, rvd, ub);
1370 
1371 	/*
1372 	 * If we weren't able to find a single valid uberblock, return failure.
1373 	 */
1374 	if (ub->ub_txg == 0) {
1375 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1376 		    VDEV_AUX_CORRUPT_DATA);
1377 		error = ENXIO;
1378 		goto out;
1379 	}
1380 
1381 	/*
1382 	 * If the pool is newer than the code, we can't open it.
1383 	 */
1384 	if (ub->ub_version > SPA_VERSION) {
1385 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1386 		    VDEV_AUX_VERSION_NEWER);
1387 		error = ENOTSUP;
1388 		goto out;
1389 	}
1390 
1391 	/*
1392 	 * If the vdev guid sum doesn't match the uberblock, we have an
1393 	 * incomplete configuration.
1394 	 */
1395 	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
1396 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1397 		    VDEV_AUX_BAD_GUID_SUM);
1398 		error = ENXIO;
1399 		goto out;
1400 	}
1401 
1402 	/*
1403 	 * Initialize internal SPA structures.
1404 	 */
1405 	spa->spa_state = POOL_STATE_ACTIVE;
1406 	spa->spa_ubsync = spa->spa_uberblock;
1407 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
1408 	    TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE;
1409 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
1410 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
1411 	spa->spa_claim_max_txg = spa->spa_first_txg;
1412 
1413 	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1414 	if (error) {
1415 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1416 		    VDEV_AUX_CORRUPT_DATA);
1417 		error = EIO;
1418 		goto out;
1419 	}
1420 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1421 
1422 	if (zap_lookup(spa->spa_meta_objset,
1423 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
1424 	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
1425 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1426 		    VDEV_AUX_CORRUPT_DATA);
1427 		error = EIO;
1428 		goto out;
1429 	}
1430 
1431 	if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) {
1432 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1433 		    VDEV_AUX_CORRUPT_DATA);
1434 		error = EIO;
1435 		goto out;
1436 	}
1437 
1438 	if (!mosconfig) {
1439 		uint64_t hostid;
1440 
1441 		if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
1442 		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
1443 			char *hostname;
1444 			unsigned long myhostid = 0;
1445 
1446 			VERIFY(nvlist_lookup_string(nvconfig,
1447 			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1448 
1449 #ifdef	_KERNEL
1450 			myhostid = zone_get_hostid(NULL);
1451 #else	/* _KERNEL */
1452 			/*
1453 			 * We're emulating the system's hostid in userland, so
1454 			 * we can't use zone_get_hostid().
1455 			 */
1456 			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
1457 #endif	/* _KERNEL */
1458 			if (hostid != 0 && myhostid != 0 &&
1459 			    hostid != myhostid) {
1460 				cmn_err(CE_WARN, "pool '%s' could not be "
1461 				    "loaded as it was last accessed by "
1462 				    "another system (host: %s hostid: 0x%lx). "
1463 				    "See: http://www.sun.com/msg/ZFS-8000-EY",
1464 				    spa_name(spa), hostname,
1465 				    (unsigned long)hostid);
1466 				error = EBADF;
1467 				goto out;
1468 			}
1469 		}
1470 
1471 		spa_config_set(spa, nvconfig);
1472 		spa_unload(spa);
1473 		spa_deactivate(spa);
1474 		spa_activate(spa, orig_mode);
1475 
1476 		return (spa_load(spa, state, B_TRUE));
1477 	}
1478 
1479 	if (zap_lookup(spa->spa_meta_objset,
1480 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
1481 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj) != 0) {
1482 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1483 		    VDEV_AUX_CORRUPT_DATA);
1484 		error = EIO;
1485 		goto out;
1486 	}
1487 
1488 	/*
1489 	 * Load the bit that tells us to use the new accounting function
1490 	 * (raid-z deflation).  If we have an older pool, this will not
1491 	 * be present.
1492 	 */
1493 	error = zap_lookup(spa->spa_meta_objset,
1494 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
1495 	    sizeof (uint64_t), 1, &spa->spa_deflate);
1496 	if (error != 0 && error != ENOENT) {
1497 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1498 		    VDEV_AUX_CORRUPT_DATA);
1499 		error = EIO;
1500 		goto out;
1501 	}
1502 
1503 	/*
1504 	 * Load the persistent error log.  If we have an older pool, this will
1505 	 * not be present.
1506 	 */
1507 	error = zap_lookup(spa->spa_meta_objset,
1508 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
1509 	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
1510 	if (error != 0 && error != ENOENT) {
1511 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1512 		    VDEV_AUX_CORRUPT_DATA);
1513 		error = EIO;
1514 		goto out;
1515 	}
1516 
1517 	error = zap_lookup(spa->spa_meta_objset,
1518 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
1519 	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
1520 	if (error != 0 && error != ENOENT) {
1521 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1522 		    VDEV_AUX_CORRUPT_DATA);
1523 		error = EIO;
1524 		goto out;
1525 	}
1526 
1527 	/*
1528 	 * Load the history object.  If we have an older pool, this
1529 	 * will not be present.
1530 	 */
1531 	error = zap_lookup(spa->spa_meta_objset,
1532 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
1533 	    sizeof (uint64_t), 1, &spa->spa_history);
1534 	if (error != 0 && error != ENOENT) {
1535 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1536 		    VDEV_AUX_CORRUPT_DATA);
1537 		error = EIO;
1538 		goto out;
1539 	}
1540 
1541 	/*
1542 	 * Load any hot spares for this pool.
1543 	 */
1544 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1545 	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
1546 	if (error != 0 && error != ENOENT) {
1547 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1548 		    VDEV_AUX_CORRUPT_DATA);
1549 		error = EIO;
1550 		goto out;
1551 	}
1552 	if (error == 0) {
1553 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
1554 		if (load_nvlist(spa, spa->spa_spares.sav_object,
1555 		    &spa->spa_spares.sav_config) != 0) {
1556 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1557 			    VDEV_AUX_CORRUPT_DATA);
1558 			error = EIO;
1559 			goto out;
1560 		}
1561 
1562 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1563 		spa_load_spares(spa);
1564 		spa_config_exit(spa, SCL_ALL, FTAG);
1565 	}
1566 
1567 	/*
1568 	 * Load any level 2 ARC devices for this pool.
1569 	 */
1570 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1571 	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
1572 	    &spa->spa_l2cache.sav_object);
1573 	if (error != 0 && error != ENOENT) {
1574 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1575 		    VDEV_AUX_CORRUPT_DATA);
1576 		error = EIO;
1577 		goto out;
1578 	}
1579 	if (error == 0) {
1580 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
1581 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
1582 		    &spa->spa_l2cache.sav_config) != 0) {
1583 			vdev_set_state(rvd, B_TRUE,
1584 			    VDEV_STATE_CANT_OPEN,
1585 			    VDEV_AUX_CORRUPT_DATA);
1586 			error = EIO;
1587 			goto out;
1588 		}
1589 
1590 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1591 		spa_load_l2cache(spa);
1592 		spa_config_exit(spa, SCL_ALL, FTAG);
1593 	}
1594 
1595 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1596 
1597 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1598 	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
1599 
1600 	if (error && error != ENOENT) {
1601 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1602 		    VDEV_AUX_CORRUPT_DATA);
1603 		error = EIO;
1604 		goto out;
1605 	}
1606 
1607 	if (error == 0) {
1608 		(void) zap_lookup(spa->spa_meta_objset,
1609 		    spa->spa_pool_props_object,
1610 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1611 		    sizeof (uint64_t), 1, &spa->spa_bootfs);
1612 		(void) zap_lookup(spa->spa_meta_objset,
1613 		    spa->spa_pool_props_object,
1614 		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
1615 		    sizeof (uint64_t), 1, &autoreplace);
1616 		spa->spa_autoreplace = (autoreplace != 0);
1617 		(void) zap_lookup(spa->spa_meta_objset,
1618 		    spa->spa_pool_props_object,
1619 		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
1620 		    sizeof (uint64_t), 1, &spa->spa_delegation);
1621 		(void) zap_lookup(spa->spa_meta_objset,
1622 		    spa->spa_pool_props_object,
1623 		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
1624 		    sizeof (uint64_t), 1, &spa->spa_failmode);
1625 		(void) zap_lookup(spa->spa_meta_objset,
1626 		    spa->spa_pool_props_object,
1627 		    zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND),
1628 		    sizeof (uint64_t), 1, &spa->spa_autoexpand);
1629 		(void) zap_lookup(spa->spa_meta_objset,
1630 		    spa->spa_pool_props_object,
1631 		    zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO),
1632 		    sizeof (uint64_t), 1, &spa->spa_dedup_ditto);
1633 	}
1634 
1635 	/*
1636 	 * If the 'autoreplace' property is set, then post a resource notifying
1637 	 * the ZFS DE that it should not issue any faults for unopenable
1638 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
1639 	 * unopenable vdevs so that the normal autoreplace handler can take
1640 	 * over.
1641 	 */
1642 	if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
1643 		spa_check_removed(spa->spa_root_vdev);
1644 		/*
1645 		 * For the import case, this is done in spa_import(), because
1646 		 * at this point we're using the spare definitions from
1647 		 * the MOS config, not necessarily from the userland config.
1648 		 */
1649 		if (state != SPA_LOAD_IMPORT) {
1650 			spa_aux_check_removed(&spa->spa_spares);
1651 			spa_aux_check_removed(&spa->spa_l2cache);
1652 		}
1653 	}
1654 
1655 	/*
1656 	 * Load the vdev state for all toplevel vdevs.
1657 	 */
1658 	vdev_load(rvd);
1659 
1660 	/*
1661 	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1662 	 */
1663 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1664 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
1665 	spa_config_exit(spa, SCL_ALL, FTAG);
1666 
1667 	/*
1668 	 * Check the state of the root vdev.  If it can't be opened, it
1669 	 * indicates one or more toplevel vdevs are faulted.
1670 	 */
1671 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1672 		error = ENXIO;
1673 		goto out;
1674 	}
1675 
1676 	/*
1677 	 * Load the DDTs (dedup tables).
1678 	 */
1679 	error = ddt_load(spa);
1680 	if (error != 0) {
1681 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1682 		    VDEV_AUX_CORRUPT_DATA);
1683 		error = EIO;
1684 		goto out;
1685 	}
1686 
1687 	spa_update_dspace(spa);
1688 
1689 	if (state != SPA_LOAD_TRYIMPORT) {
1690 		error = spa_load_verify(spa);
1691 		if (error) {
1692 			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1693 			    VDEV_AUX_CORRUPT_DATA);
1694 			goto out;
1695 		}
1696 	}
1697 
1698 	/*
1699 	 * Load the intent log state and check log integrity.
1700 	 */
1701 	VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE,
1702 	    &nvroot) == 0);
1703 	spa_load_log_state(spa, nvroot);
1704 	nvlist_free(nvconfig);
1705 
1706 	if (spa_check_logs(spa)) {
1707 		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1708 		    VDEV_AUX_BAD_LOG);
1709 		error = ENXIO;
1710 		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
1711 		goto out;
1712 	}
1713 
1714 	if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
1715 	    spa->spa_load_max_txg == UINT64_MAX)) {
1716 		dmu_tx_t *tx;
1717 		int need_update = B_FALSE;
1718 
1719 		ASSERT(state != SPA_LOAD_TRYIMPORT);
1720 
1721 		/*
1722 		 * Claim log blocks that haven't been committed yet.
1723 		 * This must all happen in a single txg.
1724 		 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
1725 		 * invoked from zil_claim_log_block()'s i/o done callback.
1726 		 * Price of rollback is that we abandon the log.
1727 		 */
1728 		spa->spa_claiming = B_TRUE;
1729 
1730 		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1731 		    spa_first_txg(spa));
1732 		(void) dmu_objset_find(spa_name(spa),
1733 		    zil_claim, tx, DS_FIND_CHILDREN);
1734 		dmu_tx_commit(tx);
1735 
1736 		spa->spa_claiming = B_FALSE;
1737 
1738 		spa->spa_log_state = SPA_LOG_GOOD;
1739 		spa->spa_sync_on = B_TRUE;
1740 		txg_sync_start(spa->spa_dsl_pool);
1741 
1742 		/*
1743 		 * Wait for all claims to sync.  We sync up to the highest
1744 		 * claimed log block birth time so that claimed log blocks
1745 		 * don't appear to be from the future.  spa_claim_max_txg
1746 		 * will have been set for us by either zil_check_log_chain()
1747 		 * (invoked from spa_check_logs()) or zil_claim() above.
1748 		 */
1749 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
1750 
1751 		/*
1752 		 * If the config cache is stale, or we have uninitialized
1753 		 * metaslabs (see spa_vdev_add()), then update the config.
1754 		 *
1755 		 * If spa_load_verbatim is true, trust the current
1756 		 * in-core spa_config and update the disk labels.
1757 		 */
1758 		if (config_cache_txg != spa->spa_config_txg ||
1759 		    state == SPA_LOAD_IMPORT || spa->spa_load_verbatim ||
1760 		    state == SPA_LOAD_RECOVER)
1761 			need_update = B_TRUE;
1762 
1763 		for (int c = 0; c < rvd->vdev_children; c++)
1764 			if (rvd->vdev_child[c]->vdev_ms_array == 0)
1765 				need_update = B_TRUE;
1766 
1767 		/*
1768 		 * Update the config cache asychronously in case we're the
1769 		 * root pool, in which case the config cache isn't writable yet.
1770 		 */
1771 		if (need_update)
1772 			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1773 
1774 		/*
1775 		 * Check all DTLs to see if anything needs resilvering.
1776 		 */
1777 		if (vdev_resilver_needed(rvd, NULL, NULL))
1778 			spa_async_request(spa, SPA_ASYNC_RESILVER);
1779 
1780 		/*
1781 		 * Delete any inconsistent datasets.
1782 		 */
1783 		(void) dmu_objset_find(spa_name(spa),
1784 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
1785 
1786 		/*
1787 		 * Clean up any stale temporary dataset userrefs.
1788 		 */
1789 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
1790 	}
1791 
1792 	error = 0;
1793 out:
1794 
1795 	spa->spa_minref = refcount_count(&spa->spa_refcount);
1796 	if (error && error != EBADF)
1797 		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1798 	spa->spa_load_state = SPA_LOAD_NONE;
1799 	spa->spa_ena = 0;
1800 
1801 	return (error);
1802 }
1803 
1804 static int
1805 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
1806 {
1807 	spa_unload(spa);
1808 	spa_deactivate(spa);
1809 
1810 	spa->spa_load_max_txg--;
1811 
1812 	spa_activate(spa, spa_mode_global);
1813 	spa_async_suspend(spa);
1814 
1815 	return (spa_load(spa, state, mosconfig));
1816 }
1817 
1818 static int
1819 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
1820     uint64_t max_request, boolean_t extreme)
1821 {
1822 	nvlist_t *config = NULL;
1823 	int load_error, rewind_error;
1824 	uint64_t safe_rollback_txg;
1825 	uint64_t min_txg;
1826 
1827 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
1828 		spa->spa_load_max_txg = spa->spa_load_txg;
1829 		spa->spa_log_state = SPA_LOG_CLEAR;
1830 	} else {
1831 		spa->spa_load_max_txg = max_request;
1832 	}
1833 
1834 	load_error = rewind_error = spa_load(spa, state, mosconfig);
1835 	if (load_error == 0)
1836 		return (0);
1837 
1838 	if (spa->spa_root_vdev != NULL)
1839 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1840 
1841 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
1842 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
1843 
1844 	/* specific txg requested */
1845 	if (spa->spa_load_max_txg != UINT64_MAX && !extreme) {
1846 		nvlist_free(config);
1847 		return (load_error);
1848 	}
1849 
1850 	/* Price of rolling back is discarding txgs, including log */
1851 	if (state == SPA_LOAD_RECOVER)
1852 		spa->spa_log_state = SPA_LOG_CLEAR;
1853 
1854 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1855 	safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE;
1856 
1857 	min_txg = extreme ? TXG_INITIAL : safe_rollback_txg;
1858 	while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) {
1859 		if (spa->spa_load_max_txg < safe_rollback_txg)
1860 			spa->spa_extreme_rewind = B_TRUE;
1861 		rewind_error = spa_load_retry(spa, state, mosconfig);
1862 	}
1863 
1864 	if (config)
1865 		spa_rewind_data_to_nvlist(spa, config);
1866 
1867 	spa->spa_extreme_rewind = B_FALSE;
1868 	spa->spa_load_max_txg = UINT64_MAX;
1869 
1870 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
1871 		spa_config_set(spa, config);
1872 
1873 	return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
1874 }
1875 
1876 /*
1877  * Pool Open/Import
1878  *
1879  * The import case is identical to an open except that the configuration is sent
1880  * down from userland, instead of grabbed from the configuration cache.  For the
1881  * case of an open, the pool configuration will exist in the
1882  * POOL_STATE_UNINITIALIZED state.
1883  *
1884  * The stats information (gen/count/ustats) is used to gather vdev statistics at
1885  * the same time open the pool, without having to keep around the spa_t in some
1886  * ambiguous state.
1887  */
1888 static int
1889 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
1890     nvlist_t **config)
1891 {
1892 	spa_t *spa;
1893 	boolean_t norewind;
1894 	boolean_t extreme;
1895 	zpool_rewind_policy_t policy;
1896 	spa_load_state_t state = SPA_LOAD_OPEN;
1897 	int error;
1898 	int locked = B_FALSE;
1899 
1900 	*spapp = NULL;
1901 
1902 	zpool_get_rewind_policy(nvpolicy, &policy);
1903 	if (policy.zrp_request & ZPOOL_DO_REWIND)
1904 		state = SPA_LOAD_RECOVER;
1905 	norewind = (policy.zrp_request == ZPOOL_NO_REWIND);
1906 	extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0);
1907 
1908 	/*
1909 	 * As disgusting as this is, we need to support recursive calls to this
1910 	 * function because dsl_dir_open() is called during spa_load(), and ends
1911 	 * up calling spa_open() again.  The real fix is to figure out how to
1912 	 * avoid dsl_dir_open() calling this in the first place.
1913 	 */
1914 	if (mutex_owner(&spa_namespace_lock) != curthread) {
1915 		mutex_enter(&spa_namespace_lock);
1916 		locked = B_TRUE;
1917 	}
1918 
1919 	if ((spa = spa_lookup(pool)) == NULL) {
1920 		if (locked)
1921 			mutex_exit(&spa_namespace_lock);
1922 		return (ENOENT);
1923 	}
1924 
1925 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1926 
1927 		spa_activate(spa, spa_mode_global);
1928 
1929 		if (spa->spa_last_open_failed && norewind) {
1930 			if (config != NULL && spa->spa_config)
1931 				VERIFY(nvlist_dup(spa->spa_config,
1932 				    config, KM_SLEEP) == 0);
1933 			spa_deactivate(spa);
1934 			if (locked)
1935 				mutex_exit(&spa_namespace_lock);
1936 			return (spa->spa_last_open_failed);
1937 		}
1938 
1939 		if (state != SPA_LOAD_RECOVER)
1940 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
1941 
1942 		error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
1943 		    extreme);
1944 
1945 		if (error == EBADF) {
1946 			/*
1947 			 * If vdev_validate() returns failure (indicated by
1948 			 * EBADF), it indicates that one of the vdevs indicates
1949 			 * that the pool has been exported or destroyed.  If
1950 			 * this is the case, the config cache is out of sync and
1951 			 * we should remove the pool from the namespace.
1952 			 */
1953 			spa_unload(spa);
1954 			spa_deactivate(spa);
1955 			spa_config_sync(spa, B_TRUE, B_TRUE);
1956 			spa_remove(spa);
1957 			if (locked)
1958 				mutex_exit(&spa_namespace_lock);
1959 			return (ENOENT);
1960 		}
1961 
1962 		if (error) {
1963 			/*
1964 			 * We can't open the pool, but we still have useful
1965 			 * information: the state of each vdev after the
1966 			 * attempted vdev_open().  Return this to the user.
1967 			 */
1968 			if (config != NULL && spa->spa_config)
1969 				VERIFY(nvlist_dup(spa->spa_config, config,
1970 				    KM_SLEEP) == 0);
1971 			spa_unload(spa);
1972 			spa_deactivate(spa);
1973 			spa->spa_last_open_failed = error;
1974 			if (locked)
1975 				mutex_exit(&spa_namespace_lock);
1976 			*spapp = NULL;
1977 			return (error);
1978 		}
1979 
1980 	}
1981 
1982 	spa_open_ref(spa, tag);
1983 
1984 
1985 	if (config != NULL)
1986 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1987 
1988 	if (locked) {
1989 		spa->spa_last_open_failed = 0;
1990 		spa->spa_last_ubsync_txg = 0;
1991 		spa->spa_load_txg = 0;
1992 		mutex_exit(&spa_namespace_lock);
1993 	}
1994 
1995 	*spapp = spa;
1996 
1997 	return (0);
1998 }
1999 
2000 int
2001 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2002     nvlist_t **config)
2003 {
2004 	return (spa_open_common(name, spapp, tag, policy, config));
2005 }
2006 
2007 int
2008 spa_open(const char *name, spa_t **spapp, void *tag)
2009 {
2010 	return (spa_open_common(name, spapp, tag, NULL, NULL));
2011 }
2012 
2013 /*
2014  * Lookup the given spa_t, incrementing the inject count in the process,
2015  * preventing it from being exported or destroyed.
2016  */
2017 spa_t *
2018 spa_inject_addref(char *name)
2019 {
2020 	spa_t *spa;
2021 
2022 	mutex_enter(&spa_namespace_lock);
2023 	if ((spa = spa_lookup(name)) == NULL) {
2024 		mutex_exit(&spa_namespace_lock);
2025 		return (NULL);
2026 	}
2027 	spa->spa_inject_ref++;
2028 	mutex_exit(&spa_namespace_lock);
2029 
2030 	return (spa);
2031 }
2032 
2033 void
2034 spa_inject_delref(spa_t *spa)
2035 {
2036 	mutex_enter(&spa_namespace_lock);
2037 	spa->spa_inject_ref--;
2038 	mutex_exit(&spa_namespace_lock);
2039 }
2040 
2041 /*
2042  * Add spares device information to the nvlist.
2043  */
2044 static void
2045 spa_add_spares(spa_t *spa, nvlist_t *config)
2046 {
2047 	nvlist_t **spares;
2048 	uint_t i, nspares;
2049 	nvlist_t *nvroot;
2050 	uint64_t guid;
2051 	vdev_stat_t *vs;
2052 	uint_t vsc;
2053 	uint64_t pool;
2054 
2055 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2056 
2057 	if (spa->spa_spares.sav_count == 0)
2058 		return;
2059 
2060 	VERIFY(nvlist_lookup_nvlist(config,
2061 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2062 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2063 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2064 	if (nspares != 0) {
2065 		VERIFY(nvlist_add_nvlist_array(nvroot,
2066 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2067 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
2068 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2069 
2070 		/*
2071 		 * Go through and find any spares which have since been
2072 		 * repurposed as an active spare.  If this is the case, update
2073 		 * their status appropriately.
2074 		 */
2075 		for (i = 0; i < nspares; i++) {
2076 			VERIFY(nvlist_lookup_uint64(spares[i],
2077 			    ZPOOL_CONFIG_GUID, &guid) == 0);
2078 			if (spa_spare_exists(guid, &pool, NULL) &&
2079 			    pool != 0ULL) {
2080 				VERIFY(nvlist_lookup_uint64_array(
2081 				    spares[i], ZPOOL_CONFIG_STATS,
2082 				    (uint64_t **)&vs, &vsc) == 0);
2083 				vs->vs_state = VDEV_STATE_CANT_OPEN;
2084 				vs->vs_aux = VDEV_AUX_SPARED;
2085 			}
2086 		}
2087 	}
2088 }
2089 
2090 /*
2091  * Add l2cache device information to the nvlist, including vdev stats.
2092  */
2093 static void
2094 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2095 {
2096 	nvlist_t **l2cache;
2097 	uint_t i, j, nl2cache;
2098 	nvlist_t *nvroot;
2099 	uint64_t guid;
2100 	vdev_t *vd;
2101 	vdev_stat_t *vs;
2102 	uint_t vsc;
2103 
2104 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2105 
2106 	if (spa->spa_l2cache.sav_count == 0)
2107 		return;
2108 
2109 	VERIFY(nvlist_lookup_nvlist(config,
2110 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2111 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2112 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2113 	if (nl2cache != 0) {
2114 		VERIFY(nvlist_add_nvlist_array(nvroot,
2115 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2116 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
2117 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2118 
2119 		/*
2120 		 * Update level 2 cache device stats.
2121 		 */
2122 
2123 		for (i = 0; i < nl2cache; i++) {
2124 			VERIFY(nvlist_lookup_uint64(l2cache[i],
2125 			    ZPOOL_CONFIG_GUID, &guid) == 0);
2126 
2127 			vd = NULL;
2128 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2129 				if (guid ==
2130 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2131 					vd = spa->spa_l2cache.sav_vdevs[j];
2132 					break;
2133 				}
2134 			}
2135 			ASSERT(vd != NULL);
2136 
2137 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2138 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
2139 			vdev_get_stats(vd, vs);
2140 		}
2141 	}
2142 }
2143 
2144 int
2145 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2146 {
2147 	int error;
2148 	spa_t *spa;
2149 
2150 	*config = NULL;
2151 	error = spa_open_common(name, &spa, FTAG, NULL, config);
2152 
2153 	if (spa != NULL) {
2154 		/*
2155 		 * This still leaves a window of inconsistency where the spares
2156 		 * or l2cache devices could change and the config would be
2157 		 * self-inconsistent.
2158 		 */
2159 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2160 
2161 		if (*config != NULL) {
2162 			VERIFY(nvlist_add_uint64(*config,
2163 			    ZPOOL_CONFIG_ERRCOUNT,
2164 			    spa_get_errlog_size(spa)) == 0);
2165 
2166 			if (spa_suspended(spa))
2167 				VERIFY(nvlist_add_uint64(*config,
2168 				    ZPOOL_CONFIG_SUSPENDED,
2169 				    spa->spa_failmode) == 0);
2170 
2171 			spa_add_spares(spa, *config);
2172 			spa_add_l2cache(spa, *config);
2173 		}
2174 	}
2175 
2176 	/*
2177 	 * We want to get the alternate root even for faulted pools, so we cheat
2178 	 * and call spa_lookup() directly.
2179 	 */
2180 	if (altroot) {
2181 		if (spa == NULL) {
2182 			mutex_enter(&spa_namespace_lock);
2183 			spa = spa_lookup(name);
2184 			if (spa)
2185 				spa_altroot(spa, altroot, buflen);
2186 			else
2187 				altroot[0] = '\0';
2188 			spa = NULL;
2189 			mutex_exit(&spa_namespace_lock);
2190 		} else {
2191 			spa_altroot(spa, altroot, buflen);
2192 		}
2193 	}
2194 
2195 	if (spa != NULL) {
2196 		spa_config_exit(spa, SCL_CONFIG, FTAG);
2197 		spa_close(spa, FTAG);
2198 	}
2199 
2200 	return (error);
2201 }
2202 
2203 /*
2204  * Validate that the auxiliary device array is well formed.  We must have an
2205  * array of nvlists, each which describes a valid leaf vdev.  If this is an
2206  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2207  * specified, as long as they are well-formed.
2208  */
2209 static int
2210 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2211     spa_aux_vdev_t *sav, const char *config, uint64_t version,
2212     vdev_labeltype_t label)
2213 {
2214 	nvlist_t **dev;
2215 	uint_t i, ndev;
2216 	vdev_t *vd;
2217 	int error;
2218 
2219 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2220 
2221 	/*
2222 	 * It's acceptable to have no devs specified.
2223 	 */
2224 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2225 		return (0);
2226 
2227 	if (ndev == 0)
2228 		return (EINVAL);
2229 
2230 	/*
2231 	 * Make sure the pool is formatted with a version that supports this
2232 	 * device type.
2233 	 */
2234 	if (spa_version(spa) < version)
2235 		return (ENOTSUP);
2236 
2237 	/*
2238 	 * Set the pending device list so we correctly handle device in-use
2239 	 * checking.
2240 	 */
2241 	sav->sav_pending = dev;
2242 	sav->sav_npending = ndev;
2243 
2244 	for (i = 0; i < ndev; i++) {
2245 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2246 		    mode)) != 0)
2247 			goto out;
2248 
2249 		if (!vd->vdev_ops->vdev_op_leaf) {
2250 			vdev_free(vd);
2251 			error = EINVAL;
2252 			goto out;
2253 		}
2254 
2255 		/*
2256 		 * The L2ARC currently only supports disk devices in
2257 		 * kernel context.  For user-level testing, we allow it.
2258 		 */
2259 #ifdef _KERNEL
2260 		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2261 		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2262 			error = ENOTBLK;
2263 			goto out;
2264 		}
2265 #endif
2266 		vd->vdev_top = vd;
2267 
2268 		if ((error = vdev_open(vd)) == 0 &&
2269 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
2270 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2271 			    vd->vdev_guid) == 0);
2272 		}
2273 
2274 		vdev_free(vd);
2275 
2276 		if (error &&
2277 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2278 			goto out;
2279 		else
2280 			error = 0;
2281 	}
2282 
2283 out:
2284 	sav->sav_pending = NULL;
2285 	sav->sav_npending = 0;
2286 	return (error);
2287 }
2288 
2289 static int
2290 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2291 {
2292 	int error;
2293 
2294 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2295 
2296 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2297 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2298 	    VDEV_LABEL_SPARE)) != 0) {
2299 		return (error);
2300 	}
2301 
2302 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2303 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2304 	    VDEV_LABEL_L2CACHE));
2305 }
2306 
2307 static void
2308 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2309     const char *config)
2310 {
2311 	int i;
2312 
2313 	if (sav->sav_config != NULL) {
2314 		nvlist_t **olddevs;
2315 		uint_t oldndevs;
2316 		nvlist_t **newdevs;
2317 
2318 		/*
2319 		 * Generate new dev list by concatentating with the
2320 		 * current dev list.
2321 		 */
2322 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2323 		    &olddevs, &oldndevs) == 0);
2324 
2325 		newdevs = kmem_alloc(sizeof (void *) *
2326 		    (ndevs + oldndevs), KM_SLEEP);
2327 		for (i = 0; i < oldndevs; i++)
2328 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2329 			    KM_SLEEP) == 0);
2330 		for (i = 0; i < ndevs; i++)
2331 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2332 			    KM_SLEEP) == 0);
2333 
2334 		VERIFY(nvlist_remove(sav->sav_config, config,
2335 		    DATA_TYPE_NVLIST_ARRAY) == 0);
2336 
2337 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2338 		    config, newdevs, ndevs + oldndevs) == 0);
2339 		for (i = 0; i < oldndevs + ndevs; i++)
2340 			nvlist_free(newdevs[i]);
2341 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2342 	} else {
2343 		/*
2344 		 * Generate a new dev list.
2345 		 */
2346 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2347 		    KM_SLEEP) == 0);
2348 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2349 		    devs, ndevs) == 0);
2350 	}
2351 }
2352 
2353 /*
2354  * Stop and drop level 2 ARC devices
2355  */
2356 void
2357 spa_l2cache_drop(spa_t *spa)
2358 {
2359 	vdev_t *vd;
2360 	int i;
2361 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
2362 
2363 	for (i = 0; i < sav->sav_count; i++) {
2364 		uint64_t pool;
2365 
2366 		vd = sav->sav_vdevs[i];
2367 		ASSERT(vd != NULL);
2368 
2369 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2370 		    pool != 0ULL && l2arc_vdev_present(vd))
2371 			l2arc_remove_vdev(vd);
2372 		if (vd->vdev_isl2cache)
2373 			spa_l2cache_remove(vd);
2374 		vdev_clear_stats(vd);
2375 		(void) vdev_close(vd);
2376 	}
2377 }
2378 
2379 /*
2380  * Pool Creation
2381  */
2382 int
2383 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2384     const char *history_str, nvlist_t *zplprops)
2385 {
2386 	spa_t *spa;
2387 	char *altroot = NULL;
2388 	vdev_t *rvd;
2389 	dsl_pool_t *dp;
2390 	dmu_tx_t *tx;
2391 	int error = 0;
2392 	uint64_t txg = TXG_INITIAL;
2393 	nvlist_t **spares, **l2cache;
2394 	uint_t nspares, nl2cache;
2395 	uint64_t version;
2396 
2397 	/*
2398 	 * If this pool already exists, return failure.
2399 	 */
2400 	mutex_enter(&spa_namespace_lock);
2401 	if (spa_lookup(pool) != NULL) {
2402 		mutex_exit(&spa_namespace_lock);
2403 		return (EEXIST);
2404 	}
2405 
2406 	/*
2407 	 * Allocate a new spa_t structure.
2408 	 */
2409 	(void) nvlist_lookup_string(props,
2410 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2411 	spa = spa_add(pool, NULL, altroot);
2412 	spa_activate(spa, spa_mode_global);
2413 
2414 	if (props && (error = spa_prop_validate(spa, props))) {
2415 		spa_deactivate(spa);
2416 		spa_remove(spa);
2417 		mutex_exit(&spa_namespace_lock);
2418 		return (error);
2419 	}
2420 
2421 	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2422 	    &version) != 0)
2423 		version = SPA_VERSION;
2424 	ASSERT(version <= SPA_VERSION);
2425 
2426 	spa->spa_first_txg = txg;
2427 	spa->spa_uberblock.ub_txg = txg - 1;
2428 	spa->spa_uberblock.ub_version = version;
2429 	spa->spa_ubsync = spa->spa_uberblock;
2430 
2431 	/*
2432 	 * Create "The Godfather" zio to hold all async IOs
2433 	 */
2434 	spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2435 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2436 
2437 	/*
2438 	 * Create the root vdev.
2439 	 */
2440 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2441 
2442 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
2443 
2444 	ASSERT(error != 0 || rvd != NULL);
2445 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
2446 
2447 	if (error == 0 && !zfs_allocatable_devs(nvroot))
2448 		error = EINVAL;
2449 
2450 	if (error == 0 &&
2451 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
2452 	    (error = spa_validate_aux(spa, nvroot, txg,
2453 	    VDEV_ALLOC_ADD)) == 0) {
2454 		for (int c = 0; c < rvd->vdev_children; c++) {
2455 			vdev_metaslab_set_size(rvd->vdev_child[c]);
2456 			vdev_expand(rvd->vdev_child[c], txg);
2457 		}
2458 	}
2459 
2460 	spa_config_exit(spa, SCL_ALL, FTAG);
2461 
2462 	if (error != 0) {
2463 		spa_unload(spa);
2464 		spa_deactivate(spa);
2465 		spa_remove(spa);
2466 		mutex_exit(&spa_namespace_lock);
2467 		return (error);
2468 	}
2469 
2470 	/*
2471 	 * Get the list of spares, if specified.
2472 	 */
2473 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2474 	    &spares, &nspares) == 0) {
2475 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
2476 		    KM_SLEEP) == 0);
2477 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2478 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2479 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2480 		spa_load_spares(spa);
2481 		spa_config_exit(spa, SCL_ALL, FTAG);
2482 		spa->spa_spares.sav_sync = B_TRUE;
2483 	}
2484 
2485 	/*
2486 	 * Get the list of level 2 cache devices, if specified.
2487 	 */
2488 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2489 	    &l2cache, &nl2cache) == 0) {
2490 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2491 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2492 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2493 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2494 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2495 		spa_load_l2cache(spa);
2496 		spa_config_exit(spa, SCL_ALL, FTAG);
2497 		spa->spa_l2cache.sav_sync = B_TRUE;
2498 	}
2499 
2500 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2501 	spa->spa_meta_objset = dp->dp_meta_objset;
2502 
2503 	/*
2504 	 * Create DDTs (dedup tables).
2505 	 */
2506 	ddt_create(spa);
2507 
2508 	spa_update_dspace(spa);
2509 
2510 	tx = dmu_tx_create_assigned(dp, txg);
2511 
2512 	/*
2513 	 * Create the pool config object.
2514 	 */
2515 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
2516 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2517 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2518 
2519 	if (zap_add(spa->spa_meta_objset,
2520 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
2521 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
2522 		cmn_err(CE_PANIC, "failed to add pool config");
2523 	}
2524 
2525 	/* Newly created pools with the right version are always deflated. */
2526 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
2527 		spa->spa_deflate = TRUE;
2528 		if (zap_add(spa->spa_meta_objset,
2529 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
2530 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
2531 			cmn_err(CE_PANIC, "failed to add deflate");
2532 		}
2533 	}
2534 
2535 	/*
2536 	 * Create the deferred-free bplist object.  Turn off compression
2537 	 * because sync-to-convergence takes longer if the blocksize
2538 	 * keeps changing.
2539 	 */
2540 	spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset,
2541 	    1 << 14, tx);
2542 	dmu_object_set_compress(spa->spa_meta_objset,
2543 	    spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx);
2544 
2545 	if (zap_add(spa->spa_meta_objset,
2546 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
2547 	    sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) {
2548 		cmn_err(CE_PANIC, "failed to add bplist");
2549 	}
2550 
2551 	/*
2552 	 * Create the pool's history object.
2553 	 */
2554 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
2555 		spa_history_create_obj(spa, tx);
2556 
2557 	/*
2558 	 * Set pool properties.
2559 	 */
2560 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
2561 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2562 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
2563 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
2564 
2565 	if (props != NULL) {
2566 		spa_configfile_set(spa, props, B_FALSE);
2567 		spa_sync_props(spa, props, CRED(), tx);
2568 	}
2569 
2570 	dmu_tx_commit(tx);
2571 
2572 	spa->spa_sync_on = B_TRUE;
2573 	txg_sync_start(spa->spa_dsl_pool);
2574 
2575 	/*
2576 	 * We explicitly wait for the first transaction to complete so that our
2577 	 * bean counters are appropriately updated.
2578 	 */
2579 	txg_wait_synced(spa->spa_dsl_pool, txg);
2580 
2581 	spa_config_sync(spa, B_FALSE, B_TRUE);
2582 
2583 	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
2584 		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
2585 	spa_history_log_version(spa, LOG_POOL_CREATE);
2586 
2587 	spa->spa_minref = refcount_count(&spa->spa_refcount);
2588 
2589 	mutex_exit(&spa_namespace_lock);
2590 
2591 	return (0);
2592 }
2593 
2594 #ifdef _KERNEL
2595 /*
2596  * Get the root pool information from the root disk, then import the root pool
2597  * during the system boot up time.
2598  */
2599 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
2600 
2601 static nvlist_t *
2602 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
2603 {
2604 	nvlist_t *config;
2605 	nvlist_t *nvtop, *nvroot;
2606 	uint64_t pgid;
2607 
2608 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
2609 		return (NULL);
2610 
2611 	/*
2612 	 * Add this top-level vdev to the child array.
2613 	 */
2614 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2615 	    &nvtop) == 0);
2616 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2617 	    &pgid) == 0);
2618 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
2619 
2620 	/*
2621 	 * Put this pool's top-level vdevs into a root vdev.
2622 	 */
2623 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2624 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
2625 	    VDEV_TYPE_ROOT) == 0);
2626 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
2627 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
2628 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2629 	    &nvtop, 1) == 0);
2630 
2631 	/*
2632 	 * Replace the existing vdev_tree with the new root vdev in
2633 	 * this pool's configuration (remove the old, add the new).
2634 	 */
2635 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
2636 	nvlist_free(nvroot);
2637 	return (config);
2638 }
2639 
2640 /*
2641  * Walk the vdev tree and see if we can find a device with "better"
2642  * configuration. A configuration is "better" if the label on that
2643  * device has a more recent txg.
2644  */
2645 static void
2646 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
2647 {
2648 	for (int c = 0; c < vd->vdev_children; c++)
2649 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
2650 
2651 	if (vd->vdev_ops->vdev_op_leaf) {
2652 		nvlist_t *label;
2653 		uint64_t label_txg;
2654 
2655 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
2656 		    &label) != 0)
2657 			return;
2658 
2659 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
2660 		    &label_txg) == 0);
2661 
2662 		/*
2663 		 * Do we have a better boot device?
2664 		 */
2665 		if (label_txg > *txg) {
2666 			*txg = label_txg;
2667 			*avd = vd;
2668 		}
2669 		nvlist_free(label);
2670 	}
2671 }
2672 
2673 /*
2674  * Import a root pool.
2675  *
2676  * For x86. devpath_list will consist of devid and/or physpath name of
2677  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
2678  * The GRUB "findroot" command will return the vdev we should boot.
2679  *
2680  * For Sparc, devpath_list consists the physpath name of the booting device
2681  * no matter the rootpool is a single device pool or a mirrored pool.
2682  * e.g.
2683  *	"/pci@1f,0/ide@d/disk@0,0:a"
2684  */
2685 int
2686 spa_import_rootpool(char *devpath, char *devid)
2687 {
2688 	spa_t *spa;
2689 	vdev_t *rvd, *bvd, *avd = NULL;
2690 	nvlist_t *config, *nvtop;
2691 	uint64_t guid, txg;
2692 	char *pname;
2693 	int error;
2694 
2695 	/*
2696 	 * Read the label from the boot device and generate a configuration.
2697 	 */
2698 	config = spa_generate_rootconf(devpath, devid, &guid);
2699 #if defined(_OBP) && defined(_KERNEL)
2700 	if (config == NULL) {
2701 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
2702 			/* iscsi boot */
2703 			get_iscsi_bootpath_phy(devpath);
2704 			config = spa_generate_rootconf(devpath, devid, &guid);
2705 		}
2706 	}
2707 #endif
2708 	if (config == NULL) {
2709 		cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
2710 		    devpath);
2711 		return (EIO);
2712 	}
2713 
2714 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2715 	    &pname) == 0);
2716 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
2717 
2718 	mutex_enter(&spa_namespace_lock);
2719 	if ((spa = spa_lookup(pname)) != NULL) {
2720 		/*
2721 		 * Remove the existing root pool from the namespace so that we
2722 		 * can replace it with the correct config we just read in.
2723 		 */
2724 		spa_remove(spa);
2725 	}
2726 
2727 	spa = spa_add(pname, config, NULL);
2728 	spa->spa_is_root = B_TRUE;
2729 	spa->spa_load_verbatim = B_TRUE;
2730 
2731 	/*
2732 	 * Build up a vdev tree based on the boot device's label config.
2733 	 */
2734 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2735 	    &nvtop) == 0);
2736 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2737 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
2738 	    VDEV_ALLOC_ROOTPOOL);
2739 	spa_config_exit(spa, SCL_ALL, FTAG);
2740 	if (error) {
2741 		mutex_exit(&spa_namespace_lock);
2742 		nvlist_free(config);
2743 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
2744 		    pname);
2745 		return (error);
2746 	}
2747 
2748 	/*
2749 	 * Get the boot vdev.
2750 	 */
2751 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
2752 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
2753 		    (u_longlong_t)guid);
2754 		error = ENOENT;
2755 		goto out;
2756 	}
2757 
2758 	/*
2759 	 * Determine if there is a better boot device.
2760 	 */
2761 	avd = bvd;
2762 	spa_alt_rootvdev(rvd, &avd, &txg);
2763 	if (avd != bvd) {
2764 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
2765 		    "try booting from '%s'", avd->vdev_path);
2766 		error = EINVAL;
2767 		goto out;
2768 	}
2769 
2770 	/*
2771 	 * If the boot device is part of a spare vdev then ensure that
2772 	 * we're booting off the active spare.
2773 	 */
2774 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2775 	    !bvd->vdev_isspare) {
2776 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
2777 		    "try booting from '%s'",
2778 		    bvd->vdev_parent->vdev_child[1]->vdev_path);
2779 		error = EINVAL;
2780 		goto out;
2781 	}
2782 
2783 	error = 0;
2784 	spa_history_log_version(spa, LOG_POOL_IMPORT);
2785 out:
2786 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2787 	vdev_free(rvd);
2788 	spa_config_exit(spa, SCL_ALL, FTAG);
2789 	mutex_exit(&spa_namespace_lock);
2790 
2791 	nvlist_free(config);
2792 	return (error);
2793 }
2794 
2795 #endif
2796 
2797 /*
2798  * Take a pool and insert it into the namespace as if it had been loaded at
2799  * boot.
2800  */
2801 int
2802 spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props)
2803 {
2804 	spa_t *spa;
2805 	zpool_rewind_policy_t policy;
2806 	char *altroot = NULL;
2807 
2808 	mutex_enter(&spa_namespace_lock);
2809 	if (spa_lookup(pool) != NULL) {
2810 		mutex_exit(&spa_namespace_lock);
2811 		return (EEXIST);
2812 	}
2813 
2814 	(void) nvlist_lookup_string(props,
2815 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2816 	spa = spa_add(pool, config, altroot);
2817 
2818 	zpool_get_rewind_policy(config, &policy);
2819 	spa->spa_load_max_txg = policy.zrp_txg;
2820 
2821 	spa->spa_load_verbatim = B_TRUE;
2822 
2823 	if (props != NULL)
2824 		spa_configfile_set(spa, props, B_FALSE);
2825 
2826 	spa_config_sync(spa, B_FALSE, B_TRUE);
2827 
2828 	mutex_exit(&spa_namespace_lock);
2829 	spa_history_log_version(spa, LOG_POOL_IMPORT);
2830 
2831 	return (0);
2832 }
2833 
2834 /*
2835  * Import a non-root pool into the system.
2836  */
2837 int
2838 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
2839 {
2840 	spa_t *spa;
2841 	char *altroot = NULL;
2842 	spa_load_state_t state = SPA_LOAD_IMPORT;
2843 	zpool_rewind_policy_t policy;
2844 	int error;
2845 	nvlist_t *nvroot;
2846 	nvlist_t **spares, **l2cache;
2847 	uint_t nspares, nl2cache;
2848 
2849 	/*
2850 	 * If a pool with this name exists, return failure.
2851 	 */
2852 	mutex_enter(&spa_namespace_lock);
2853 	if ((spa = spa_lookup(pool)) != NULL) {
2854 		mutex_exit(&spa_namespace_lock);
2855 		return (EEXIST);
2856 	}
2857 
2858 	zpool_get_rewind_policy(config, &policy);
2859 	if (policy.zrp_request & ZPOOL_DO_REWIND)
2860 		state = SPA_LOAD_RECOVER;
2861 
2862 	/*
2863 	 * Create and initialize the spa structure.
2864 	 */
2865 	(void) nvlist_lookup_string(props,
2866 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2867 	spa = spa_add(pool, config, altroot);
2868 	spa_activate(spa, spa_mode_global);
2869 
2870 	/*
2871 	 * Don't start async tasks until we know everything is healthy.
2872 	 */
2873 	spa_async_suspend(spa);
2874 
2875 	/*
2876 	 * Pass off the heavy lifting to spa_load().  Pass TRUE for mosconfig
2877 	 * because the user-supplied config is actually the one to trust when
2878 	 * doing an import.
2879 	 */
2880 	if (state != SPA_LOAD_RECOVER)
2881 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2882 	error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
2883 	    ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0));
2884 
2885 	/*
2886 	 * Propagate anything learned about failing or best txgs
2887 	 * back to caller
2888 	 */
2889 	spa_rewind_data_to_nvlist(spa, config);
2890 
2891 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2892 	/*
2893 	 * Toss any existing sparelist, as it doesn't have any validity
2894 	 * anymore, and conflicts with spa_has_spare().
2895 	 */
2896 	if (spa->spa_spares.sav_config) {
2897 		nvlist_free(spa->spa_spares.sav_config);
2898 		spa->spa_spares.sav_config = NULL;
2899 		spa_load_spares(spa);
2900 	}
2901 	if (spa->spa_l2cache.sav_config) {
2902 		nvlist_free(spa->spa_l2cache.sav_config);
2903 		spa->spa_l2cache.sav_config = NULL;
2904 		spa_load_l2cache(spa);
2905 	}
2906 
2907 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2908 	    &nvroot) == 0);
2909 	if (error == 0)
2910 		error = spa_validate_aux(spa, nvroot, -1ULL,
2911 		    VDEV_ALLOC_SPARE);
2912 	if (error == 0)
2913 		error = spa_validate_aux(spa, nvroot, -1ULL,
2914 		    VDEV_ALLOC_L2CACHE);
2915 	spa_config_exit(spa, SCL_ALL, FTAG);
2916 
2917 	if (props != NULL)
2918 		spa_configfile_set(spa, props, B_FALSE);
2919 
2920 	if (error != 0 || (props && spa_writeable(spa) &&
2921 	    (error = spa_prop_set(spa, props)))) {
2922 		spa_unload(spa);
2923 		spa_deactivate(spa);
2924 		spa_remove(spa);
2925 		mutex_exit(&spa_namespace_lock);
2926 		return (error);
2927 	}
2928 
2929 	spa_async_resume(spa);
2930 
2931 	/*
2932 	 * Override any spares and level 2 cache devices as specified by
2933 	 * the user, as these may have correct device names/devids, etc.
2934 	 */
2935 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2936 	    &spares, &nspares) == 0) {
2937 		if (spa->spa_spares.sav_config)
2938 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
2939 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
2940 		else
2941 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
2942 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2943 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2944 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2945 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2946 		spa_load_spares(spa);
2947 		spa_config_exit(spa, SCL_ALL, FTAG);
2948 		spa->spa_spares.sav_sync = B_TRUE;
2949 	}
2950 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2951 	    &l2cache, &nl2cache) == 0) {
2952 		if (spa->spa_l2cache.sav_config)
2953 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
2954 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
2955 		else
2956 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2957 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2958 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2959 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2960 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2961 		spa_load_l2cache(spa);
2962 		spa_config_exit(spa, SCL_ALL, FTAG);
2963 		spa->spa_l2cache.sav_sync = B_TRUE;
2964 	}
2965 
2966 	/*
2967 	 * Check for any removed devices.
2968 	 */
2969 	if (spa->spa_autoreplace) {
2970 		spa_aux_check_removed(&spa->spa_spares);
2971 		spa_aux_check_removed(&spa->spa_l2cache);
2972 	}
2973 
2974 	if (spa_writeable(spa)) {
2975 		/*
2976 		 * Update the config cache to include the newly-imported pool.
2977 		 */
2978 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
2979 	}
2980 
2981 	/*
2982 	 * It's possible that the pool was expanded while it was exported.
2983 	 * We kick off an async task to handle this for us.
2984 	 */
2985 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
2986 
2987 	mutex_exit(&spa_namespace_lock);
2988 	spa_history_log_version(spa, LOG_POOL_IMPORT);
2989 
2990 	return (0);
2991 }
2992 
2993 
2994 /*
2995  * This (illegal) pool name is used when temporarily importing a spa_t in order
2996  * to get the vdev stats associated with the imported devices.
2997  */
2998 #define	TRYIMPORT_NAME	"$import"
2999 
3000 nvlist_t *
3001 spa_tryimport(nvlist_t *tryconfig)
3002 {
3003 	nvlist_t *config = NULL;
3004 	char *poolname;
3005 	spa_t *spa;
3006 	uint64_t state;
3007 	int error;
3008 
3009 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3010 		return (NULL);
3011 
3012 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3013 		return (NULL);
3014 
3015 	/*
3016 	 * Create and initialize the spa structure.
3017 	 */
3018 	mutex_enter(&spa_namespace_lock);
3019 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3020 	spa_activate(spa, FREAD);
3021 
3022 	/*
3023 	 * Pass off the heavy lifting to spa_load().
3024 	 * Pass TRUE for mosconfig because the user-supplied config
3025 	 * is actually the one to trust when doing an import.
3026 	 */
3027 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, B_TRUE);
3028 
3029 	/*
3030 	 * If 'tryconfig' was at least parsable, return the current config.
3031 	 */
3032 	if (spa->spa_root_vdev != NULL) {
3033 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3034 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3035 		    poolname) == 0);
3036 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3037 		    state) == 0);
3038 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3039 		    spa->spa_uberblock.ub_timestamp) == 0);
3040 
3041 		/*
3042 		 * If the bootfs property exists on this pool then we
3043 		 * copy it out so that external consumers can tell which
3044 		 * pools are bootable.
3045 		 */
3046 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
3047 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3048 
3049 			/*
3050 			 * We have to play games with the name since the
3051 			 * pool was opened as TRYIMPORT_NAME.
3052 			 */
3053 			if (dsl_dsobj_to_dsname(spa_name(spa),
3054 			    spa->spa_bootfs, tmpname) == 0) {
3055 				char *cp;
3056 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3057 
3058 				cp = strchr(tmpname, '/');
3059 				if (cp == NULL) {
3060 					(void) strlcpy(dsname, tmpname,
3061 					    MAXPATHLEN);
3062 				} else {
3063 					(void) snprintf(dsname, MAXPATHLEN,
3064 					    "%s/%s", poolname, ++cp);
3065 				}
3066 				VERIFY(nvlist_add_string(config,
3067 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3068 				kmem_free(dsname, MAXPATHLEN);
3069 			}
3070 			kmem_free(tmpname, MAXPATHLEN);
3071 		}
3072 
3073 		/*
3074 		 * Add the list of hot spares and level 2 cache devices.
3075 		 */
3076 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3077 		spa_add_spares(spa, config);
3078 		spa_add_l2cache(spa, config);
3079 		spa_config_exit(spa, SCL_CONFIG, FTAG);
3080 	}
3081 
3082 	spa_unload(spa);
3083 	spa_deactivate(spa);
3084 	spa_remove(spa);
3085 	mutex_exit(&spa_namespace_lock);
3086 
3087 	return (config);
3088 }
3089 
3090 /*
3091  * Pool export/destroy
3092  *
3093  * The act of destroying or exporting a pool is very simple.  We make sure there
3094  * is no more pending I/O and any references to the pool are gone.  Then, we
3095  * update the pool state and sync all the labels to disk, removing the
3096  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3097  * we don't sync the labels or remove the configuration cache.
3098  */
3099 static int
3100 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3101     boolean_t force, boolean_t hardforce)
3102 {
3103 	spa_t *spa;
3104 
3105 	if (oldconfig)
3106 		*oldconfig = NULL;
3107 
3108 	if (!(spa_mode_global & FWRITE))
3109 		return (EROFS);
3110 
3111 	mutex_enter(&spa_namespace_lock);
3112 	if ((spa = spa_lookup(pool)) == NULL) {
3113 		mutex_exit(&spa_namespace_lock);
3114 		return (ENOENT);
3115 	}
3116 
3117 	/*
3118 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3119 	 * reacquire the namespace lock, and see if we can export.
3120 	 */
3121 	spa_open_ref(spa, FTAG);
3122 	mutex_exit(&spa_namespace_lock);
3123 	spa_async_suspend(spa);
3124 	mutex_enter(&spa_namespace_lock);
3125 	spa_close(spa, FTAG);
3126 
3127 	/*
3128 	 * The pool will be in core if it's openable,
3129 	 * in which case we can modify its state.
3130 	 */
3131 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3132 		/*
3133 		 * Objsets may be open only because they're dirty, so we
3134 		 * have to force it to sync before checking spa_refcnt.
3135 		 */
3136 		txg_wait_synced(spa->spa_dsl_pool, 0);
3137 
3138 		/*
3139 		 * A pool cannot be exported or destroyed if there are active
3140 		 * references.  If we are resetting a pool, allow references by
3141 		 * fault injection handlers.
3142 		 */
3143 		if (!spa_refcount_zero(spa) ||
3144 		    (spa->spa_inject_ref != 0 &&
3145 		    new_state != POOL_STATE_UNINITIALIZED)) {
3146 			spa_async_resume(spa);
3147 			mutex_exit(&spa_namespace_lock);
3148 			return (EBUSY);
3149 		}
3150 
3151 		/*
3152 		 * A pool cannot be exported if it has an active shared spare.
3153 		 * This is to prevent other pools stealing the active spare
3154 		 * from an exported pool. At user's own will, such pool can
3155 		 * be forcedly exported.
3156 		 */
3157 		if (!force && new_state == POOL_STATE_EXPORTED &&
3158 		    spa_has_active_shared_spare(spa)) {
3159 			spa_async_resume(spa);
3160 			mutex_exit(&spa_namespace_lock);
3161 			return (EXDEV);
3162 		}
3163 
3164 		/*
3165 		 * We want this to be reflected on every label,
3166 		 * so mark them all dirty.  spa_unload() will do the
3167 		 * final sync that pushes these changes out.
3168 		 */
3169 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3170 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3171 			spa->spa_state = new_state;
3172 			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
3173 			vdev_config_dirty(spa->spa_root_vdev);
3174 			spa_config_exit(spa, SCL_ALL, FTAG);
3175 		}
3176 	}
3177 
3178 	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
3179 
3180 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3181 		spa_unload(spa);
3182 		spa_deactivate(spa);
3183 	}
3184 
3185 	if (oldconfig && spa->spa_config)
3186 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3187 
3188 	if (new_state != POOL_STATE_UNINITIALIZED) {
3189 		if (!hardforce)
3190 			spa_config_sync(spa, B_TRUE, B_TRUE);
3191 		spa_remove(spa);
3192 	}
3193 	mutex_exit(&spa_namespace_lock);
3194 
3195 	return (0);
3196 }
3197 
3198 /*
3199  * Destroy a storage pool.
3200  */
3201 int
3202 spa_destroy(char *pool)
3203 {
3204 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3205 	    B_FALSE, B_FALSE));
3206 }
3207 
3208 /*
3209  * Export a storage pool.
3210  */
3211 int
3212 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3213     boolean_t hardforce)
3214 {
3215 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3216 	    force, hardforce));
3217 }
3218 
3219 /*
3220  * Similar to spa_export(), this unloads the spa_t without actually removing it
3221  * from the namespace in any way.
3222  */
3223 int
3224 spa_reset(char *pool)
3225 {
3226 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3227 	    B_FALSE, B_FALSE));
3228 }
3229 
3230 /*
3231  * ==========================================================================
3232  * Device manipulation
3233  * ==========================================================================
3234  */
3235 
3236 /*
3237  * Add a device to a storage pool.
3238  */
3239 int
3240 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3241 {
3242 	uint64_t txg, id;
3243 	int error;
3244 	vdev_t *rvd = spa->spa_root_vdev;
3245 	vdev_t *vd, *tvd;
3246 	nvlist_t **spares, **l2cache;
3247 	uint_t nspares, nl2cache;
3248 
3249 	txg = spa_vdev_enter(spa);
3250 
3251 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
3252 	    VDEV_ALLOC_ADD)) != 0)
3253 		return (spa_vdev_exit(spa, NULL, txg, error));
3254 
3255 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
3256 
3257 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
3258 	    &nspares) != 0)
3259 		nspares = 0;
3260 
3261 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
3262 	    &nl2cache) != 0)
3263 		nl2cache = 0;
3264 
3265 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
3266 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
3267 
3268 	if (vd->vdev_children != 0 &&
3269 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
3270 		return (spa_vdev_exit(spa, vd, txg, error));
3271 
3272 	/*
3273 	 * We must validate the spares and l2cache devices after checking the
3274 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
3275 	 */
3276 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
3277 		return (spa_vdev_exit(spa, vd, txg, error));
3278 
3279 	/*
3280 	 * Transfer each new top-level vdev from vd to rvd.
3281 	 */
3282 	for (int c = 0; c < vd->vdev_children; c++) {
3283 
3284 		/*
3285 		 * Set the vdev id to the first hole, if one exists.
3286 		 */
3287 		for (id = 0; id < rvd->vdev_children; id++) {
3288 			if (rvd->vdev_child[id]->vdev_ishole) {
3289 				vdev_free(rvd->vdev_child[id]);
3290 				break;
3291 			}
3292 		}
3293 		tvd = vd->vdev_child[c];
3294 		vdev_remove_child(vd, tvd);
3295 		tvd->vdev_id = id;
3296 		vdev_add_child(rvd, tvd);
3297 		vdev_config_dirty(tvd);
3298 	}
3299 
3300 	if (nspares != 0) {
3301 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
3302 		    ZPOOL_CONFIG_SPARES);
3303 		spa_load_spares(spa);
3304 		spa->spa_spares.sav_sync = B_TRUE;
3305 	}
3306 
3307 	if (nl2cache != 0) {
3308 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
3309 		    ZPOOL_CONFIG_L2CACHE);
3310 		spa_load_l2cache(spa);
3311 		spa->spa_l2cache.sav_sync = B_TRUE;
3312 	}
3313 
3314 	/*
3315 	 * We have to be careful when adding new vdevs to an existing pool.
3316 	 * If other threads start allocating from these vdevs before we
3317 	 * sync the config cache, and we lose power, then upon reboot we may
3318 	 * fail to open the pool because there are DVAs that the config cache
3319 	 * can't translate.  Therefore, we first add the vdevs without
3320 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3321 	 * and then let spa_config_update() initialize the new metaslabs.
3322 	 *
3323 	 * spa_load() checks for added-but-not-initialized vdevs, so that
3324 	 * if we lose power at any point in this sequence, the remaining
3325 	 * steps will be completed the next time we load the pool.
3326 	 */
3327 	(void) spa_vdev_exit(spa, vd, txg, 0);
3328 
3329 	mutex_enter(&spa_namespace_lock);
3330 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3331 	mutex_exit(&spa_namespace_lock);
3332 
3333 	return (0);
3334 }
3335 
3336 /*
3337  * Attach a device to a mirror.  The arguments are the path to any device
3338  * in the mirror, and the nvroot for the new device.  If the path specifies
3339  * a device that is not mirrored, we automatically insert the mirror vdev.
3340  *
3341  * If 'replacing' is specified, the new device is intended to replace the
3342  * existing device; in this case the two devices are made into their own
3343  * mirror using the 'replacing' vdev, which is functionally identical to
3344  * the mirror vdev (it actually reuses all the same ops) but has a few
3345  * extra rules: you can't attach to it after it's been created, and upon
3346  * completion of resilvering, the first disk (the one being replaced)
3347  * is automatically detached.
3348  */
3349 int
3350 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3351 {
3352 	uint64_t txg, open_txg;
3353 	vdev_t *rvd = spa->spa_root_vdev;
3354 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
3355 	vdev_ops_t *pvops;
3356 	char *oldvdpath, *newvdpath;
3357 	int newvd_isspare;
3358 	int error;
3359 
3360 	txg = spa_vdev_enter(spa);
3361 
3362 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3363 
3364 	if (oldvd == NULL)
3365 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3366 
3367 	if (!oldvd->vdev_ops->vdev_op_leaf)
3368 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3369 
3370 	pvd = oldvd->vdev_parent;
3371 
3372 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
3373 	    VDEV_ALLOC_ADD)) != 0)
3374 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
3375 
3376 	if (newrootvd->vdev_children != 1)
3377 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3378 
3379 	newvd = newrootvd->vdev_child[0];
3380 
3381 	if (!newvd->vdev_ops->vdev_op_leaf)
3382 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3383 
3384 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3385 		return (spa_vdev_exit(spa, newrootvd, txg, error));
3386 
3387 	/*
3388 	 * Spares can't replace logs
3389 	 */
3390 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
3391 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3392 
3393 	if (!replacing) {
3394 		/*
3395 		 * For attach, the only allowable parent is a mirror or the root
3396 		 * vdev.
3397 		 */
3398 		if (pvd->vdev_ops != &vdev_mirror_ops &&
3399 		    pvd->vdev_ops != &vdev_root_ops)
3400 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3401 
3402 		pvops = &vdev_mirror_ops;
3403 	} else {
3404 		/*
3405 		 * Active hot spares can only be replaced by inactive hot
3406 		 * spares.
3407 		 */
3408 		if (pvd->vdev_ops == &vdev_spare_ops &&
3409 		    pvd->vdev_child[1] == oldvd &&
3410 		    !spa_has_spare(spa, newvd->vdev_guid))
3411 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3412 
3413 		/*
3414 		 * If the source is a hot spare, and the parent isn't already a
3415 		 * spare, then we want to create a new hot spare.  Otherwise, we
3416 		 * want to create a replacing vdev.  The user is not allowed to
3417 		 * attach to a spared vdev child unless the 'isspare' state is
3418 		 * the same (spare replaces spare, non-spare replaces
3419 		 * non-spare).
3420 		 */
3421 		if (pvd->vdev_ops == &vdev_replacing_ops)
3422 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3423 		else if (pvd->vdev_ops == &vdev_spare_ops &&
3424 		    newvd->vdev_isspare != oldvd->vdev_isspare)
3425 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3426 		else if (pvd->vdev_ops != &vdev_spare_ops &&
3427 		    newvd->vdev_isspare)
3428 			pvops = &vdev_spare_ops;
3429 		else
3430 			pvops = &vdev_replacing_ops;
3431 	}
3432 
3433 	/*
3434 	 * Make sure the new device is big enough.
3435 	 */
3436 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3437 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3438 
3439 	/*
3440 	 * The new device cannot have a higher alignment requirement
3441 	 * than the top-level vdev.
3442 	 */
3443 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3444 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3445 
3446 	/*
3447 	 * If this is an in-place replacement, update oldvd's path and devid
3448 	 * to make it distinguishable from newvd, and unopenable from now on.
3449 	 */
3450 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3451 		spa_strfree(oldvd->vdev_path);
3452 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3453 		    KM_SLEEP);
3454 		(void) sprintf(oldvd->vdev_path, "%s/%s",
3455 		    newvd->vdev_path, "old");
3456 		if (oldvd->vdev_devid != NULL) {
3457 			spa_strfree(oldvd->vdev_devid);
3458 			oldvd->vdev_devid = NULL;
3459 		}
3460 	}
3461 
3462 	/*
3463 	 * If the parent is not a mirror, or if we're replacing, insert the new
3464 	 * mirror/replacing/spare vdev above oldvd.
3465 	 */
3466 	if (pvd->vdev_ops != pvops)
3467 		pvd = vdev_add_parent(oldvd, pvops);
3468 
3469 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3470 	ASSERT(pvd->vdev_ops == pvops);
3471 	ASSERT(oldvd->vdev_parent == pvd);
3472 
3473 	/*
3474 	 * Extract the new device from its root and add it to pvd.
3475 	 */
3476 	vdev_remove_child(newrootvd, newvd);
3477 	newvd->vdev_id = pvd->vdev_children;
3478 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
3479 	vdev_add_child(pvd, newvd);
3480 
3481 	tvd = newvd->vdev_top;
3482 	ASSERT(pvd->vdev_top == tvd);
3483 	ASSERT(tvd->vdev_parent == rvd);
3484 
3485 	vdev_config_dirty(tvd);
3486 
3487 	/*
3488 	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3489 	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3490 	 */
3491 	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3492 
3493 	vdev_dtl_dirty(newvd, DTL_MISSING,
3494 	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3495 
3496 	if (newvd->vdev_isspare) {
3497 		spa_spare_activate(newvd);
3498 		spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
3499 	}
3500 
3501 	oldvdpath = spa_strdup(oldvd->vdev_path);
3502 	newvdpath = spa_strdup(newvd->vdev_path);
3503 	newvd_isspare = newvd->vdev_isspare;
3504 
3505 	/*
3506 	 * Mark newvd's DTL dirty in this txg.
3507 	 */
3508 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3509 
3510 	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3511 
3512 	spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL,
3513 	    CRED(),  "%s vdev=%s %s vdev=%s",
3514 	    replacing && newvd_isspare ? "spare in" :
3515 	    replacing ? "replace" : "attach", newvdpath,
3516 	    replacing ? "for" : "to", oldvdpath);
3517 
3518 	spa_strfree(oldvdpath);
3519 	spa_strfree(newvdpath);
3520 
3521 	/*
3522 	 * Kick off a resilver to update newvd.
3523 	 */
3524 	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3525 
3526 	return (0);
3527 }
3528 
3529 /*
3530  * Detach a device from a mirror or replacing vdev.
3531  * If 'replace_done' is specified, only detach if the parent
3532  * is a replacing vdev.
3533  */
3534 int
3535 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3536 {
3537 	uint64_t txg;
3538 	int error;
3539 	vdev_t *rvd = spa->spa_root_vdev;
3540 	vdev_t *vd, *pvd, *cvd, *tvd;
3541 	boolean_t unspare = B_FALSE;
3542 	uint64_t unspare_guid;
3543 	size_t len;
3544 
3545 	txg = spa_vdev_enter(spa);
3546 
3547 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3548 
3549 	if (vd == NULL)
3550 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3551 
3552 	if (!vd->vdev_ops->vdev_op_leaf)
3553 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3554 
3555 	pvd = vd->vdev_parent;
3556 
3557 	/*
3558 	 * If the parent/child relationship is not as expected, don't do it.
3559 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
3560 	 * vdev that's replacing B with C.  The user's intent in replacing
3561 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
3562 	 * the replace by detaching C, the expected behavior is to end up
3563 	 * M(A,B).  But suppose that right after deciding to detach C,
3564 	 * the replacement of B completes.  We would have M(A,C), and then
3565 	 * ask to detach C, which would leave us with just A -- not what
3566 	 * the user wanted.  To prevent this, we make sure that the
3567 	 * parent/child relationship hasn't changed -- in this example,
3568 	 * that C's parent is still the replacing vdev R.
3569 	 */
3570 	if (pvd->vdev_guid != pguid && pguid != 0)
3571 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3572 
3573 	/*
3574 	 * If replace_done is specified, only remove this device if it's
3575 	 * the first child of a replacing vdev.  For the 'spare' vdev, either
3576 	 * disk can be removed.
3577 	 */
3578 	if (replace_done) {
3579 		if (pvd->vdev_ops == &vdev_replacing_ops) {
3580 			if (vd->vdev_id != 0)
3581 				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3582 		} else if (pvd->vdev_ops != &vdev_spare_ops) {
3583 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3584 		}
3585 	}
3586 
3587 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
3588 	    spa_version(spa) >= SPA_VERSION_SPARES);
3589 
3590 	/*
3591 	 * Only mirror, replacing, and spare vdevs support detach.
3592 	 */
3593 	if (pvd->vdev_ops != &vdev_replacing_ops &&
3594 	    pvd->vdev_ops != &vdev_mirror_ops &&
3595 	    pvd->vdev_ops != &vdev_spare_ops)
3596 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3597 
3598 	/*
3599 	 * If this device has the only valid copy of some data,
3600 	 * we cannot safely detach it.
3601 	 */
3602 	if (vdev_dtl_required(vd))
3603 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3604 
3605 	ASSERT(pvd->vdev_children >= 2);
3606 
3607 	/*
3608 	 * If we are detaching the second disk from a replacing vdev, then
3609 	 * check to see if we changed the original vdev's path to have "/old"
3610 	 * at the end in spa_vdev_attach().  If so, undo that change now.
3611 	 */
3612 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
3613 	    pvd->vdev_child[0]->vdev_path != NULL &&
3614 	    pvd->vdev_child[1]->vdev_path != NULL) {
3615 		ASSERT(pvd->vdev_child[1] == vd);
3616 		cvd = pvd->vdev_child[0];
3617 		len = strlen(vd->vdev_path);
3618 		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
3619 		    strcmp(cvd->vdev_path + len, "/old") == 0) {
3620 			spa_strfree(cvd->vdev_path);
3621 			cvd->vdev_path = spa_strdup(vd->vdev_path);
3622 		}
3623 	}
3624 
3625 	/*
3626 	 * If we are detaching the original disk from a spare, then it implies
3627 	 * that the spare should become a real disk, and be removed from the
3628 	 * active spare list for the pool.
3629 	 */
3630 	if (pvd->vdev_ops == &vdev_spare_ops &&
3631 	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
3632 		unspare = B_TRUE;
3633 
3634 	/*
3635 	 * Erase the disk labels so the disk can be used for other things.
3636 	 * This must be done after all other error cases are handled,
3637 	 * but before we disembowel vd (so we can still do I/O to it).
3638 	 * But if we can't do it, don't treat the error as fatal --
3639 	 * it may be that the unwritability of the disk is the reason
3640 	 * it's being detached!
3641 	 */
3642 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3643 
3644 	/*
3645 	 * Remove vd from its parent and compact the parent's children.
3646 	 */
3647 	vdev_remove_child(pvd, vd);
3648 	vdev_compact_children(pvd);
3649 
3650 	/*
3651 	 * Remember one of the remaining children so we can get tvd below.
3652 	 */
3653 	cvd = pvd->vdev_child[0];
3654 
3655 	/*
3656 	 * If we need to remove the remaining child from the list of hot spares,
3657 	 * do it now, marking the vdev as no longer a spare in the process.
3658 	 * We must do this before vdev_remove_parent(), because that can
3659 	 * change the GUID if it creates a new toplevel GUID.  For a similar
3660 	 * reason, we must remove the spare now, in the same txg as the detach;
3661 	 * otherwise someone could attach a new sibling, change the GUID, and
3662 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
3663 	 */
3664 	if (unspare) {
3665 		ASSERT(cvd->vdev_isspare);
3666 		spa_spare_remove(cvd);
3667 		unspare_guid = cvd->vdev_guid;
3668 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
3669 	}
3670 
3671 	/*
3672 	 * If the parent mirror/replacing vdev only has one child,
3673 	 * the parent is no longer needed.  Remove it from the tree.
3674 	 */
3675 	if (pvd->vdev_children == 1)
3676 		vdev_remove_parent(cvd);
3677 
3678 	/*
3679 	 * We don't set tvd until now because the parent we just removed
3680 	 * may have been the previous top-level vdev.
3681 	 */
3682 	tvd = cvd->vdev_top;
3683 	ASSERT(tvd->vdev_parent == rvd);
3684 
3685 	/*
3686 	 * Reevaluate the parent vdev state.
3687 	 */
3688 	vdev_propagate_state(cvd);
3689 
3690 	/*
3691 	 * If the 'autoexpand' property is set on the pool then automatically
3692 	 * try to expand the size of the pool. For example if the device we
3693 	 * just detached was smaller than the others, it may be possible to
3694 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
3695 	 * first so that we can obtain the updated sizes of the leaf vdevs.
3696 	 */
3697 	if (spa->spa_autoexpand) {
3698 		vdev_reopen(tvd);
3699 		vdev_expand(tvd, txg);
3700 	}
3701 
3702 	vdev_config_dirty(tvd);
3703 
3704 	/*
3705 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
3706 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
3707 	 * But first make sure we're not on any *other* txg's DTL list, to
3708 	 * prevent vd from being accessed after it's freed.
3709 	 */
3710 	for (int t = 0; t < TXG_SIZE; t++)
3711 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
3712 	vd->vdev_detached = B_TRUE;
3713 	vdev_dirty(tvd, VDD_DTL, vd, txg);
3714 
3715 	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
3716 
3717 	error = spa_vdev_exit(spa, vd, txg, 0);
3718 
3719 	/*
3720 	 * If this was the removal of the original device in a hot spare vdev,
3721 	 * then we want to go through and remove the device from the hot spare
3722 	 * list of every other pool.
3723 	 */
3724 	if (unspare) {
3725 		spa_t *myspa = spa;
3726 		spa = NULL;
3727 		mutex_enter(&spa_namespace_lock);
3728 		while ((spa = spa_next(spa)) != NULL) {
3729 			if (spa->spa_state != POOL_STATE_ACTIVE)
3730 				continue;
3731 			if (spa == myspa)
3732 				continue;
3733 			spa_open_ref(spa, FTAG);
3734 			mutex_exit(&spa_namespace_lock);
3735 			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
3736 			mutex_enter(&spa_namespace_lock);
3737 			spa_close(spa, FTAG);
3738 		}
3739 		mutex_exit(&spa_namespace_lock);
3740 	}
3741 
3742 	return (error);
3743 }
3744 
3745 static nvlist_t *
3746 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
3747 {
3748 	for (int i = 0; i < count; i++) {
3749 		uint64_t guid;
3750 
3751 		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
3752 		    &guid) == 0);
3753 
3754 		if (guid == target_guid)
3755 			return (nvpp[i]);
3756 	}
3757 
3758 	return (NULL);
3759 }
3760 
3761 static void
3762 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
3763 	nvlist_t *dev_to_remove)
3764 {
3765 	nvlist_t **newdev = NULL;
3766 
3767 	if (count > 1)
3768 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
3769 
3770 	for (int i = 0, j = 0; i < count; i++) {
3771 		if (dev[i] == dev_to_remove)
3772 			continue;
3773 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
3774 	}
3775 
3776 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
3777 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
3778 
3779 	for (int i = 0; i < count - 1; i++)
3780 		nvlist_free(newdev[i]);
3781 
3782 	if (count > 1)
3783 		kmem_free(newdev, (count - 1) * sizeof (void *));
3784 }
3785 
3786 /*
3787  * Removing a device from the vdev namespace requires several steps
3788  * and can take a significant amount of time.  As a result we use
3789  * the spa_vdev_config_[enter/exit] functions which allow us to
3790  * grab and release the spa_config_lock while still holding the namespace
3791  * lock.  During each step the configuration is synced out.
3792  */
3793 
3794 /*
3795  * Evacuate the device.
3796  */
3797 int
3798 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
3799 {
3800 	int error = 0;
3801 	uint64_t txg;
3802 
3803 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3804 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
3805 	ASSERT(vd == vd->vdev_top);
3806 
3807 	/*
3808 	 * Evacuate the device.  We don't hold the config lock as writer
3809 	 * since we need to do I/O but we do keep the
3810 	 * spa_namespace_lock held.  Once this completes the device
3811 	 * should no longer have any blocks allocated on it.
3812 	 */
3813 	if (vd->vdev_islog) {
3814 		error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
3815 		    NULL, DS_FIND_CHILDREN);
3816 	} else {
3817 		error = ENOTSUP;	/* until we have bp rewrite */
3818 	}
3819 
3820 	txg_wait_synced(spa_get_dsl(spa), 0);
3821 
3822 	if (error)
3823 		return (error);
3824 
3825 	/*
3826 	 * The evacuation succeeded.  Remove any remaining MOS metadata
3827 	 * associated with this vdev, and wait for these changes to sync.
3828 	 */
3829 	txg = spa_vdev_config_enter(spa);
3830 	vd->vdev_removing = B_TRUE;
3831 	vdev_dirty(vd, 0, NULL, txg);
3832 	vdev_config_dirty(vd);
3833 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
3834 
3835 	return (0);
3836 }
3837 
3838 /*
3839  * Complete the removal by cleaning up the namespace.
3840  */
3841 void
3842 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
3843 {
3844 	vdev_t *rvd = spa->spa_root_vdev;
3845 	uint64_t id = vd->vdev_id;
3846 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
3847 
3848 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3849 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3850 	ASSERT(vd == vd->vdev_top);
3851 
3852 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3853 
3854 	if (list_link_active(&vd->vdev_state_dirty_node))
3855 		vdev_state_clean(vd);
3856 	if (list_link_active(&vd->vdev_config_dirty_node))
3857 		vdev_config_clean(vd);
3858 
3859 	vdev_free(vd);
3860 
3861 	if (last_vdev) {
3862 		vdev_compact_children(rvd);
3863 	} else {
3864 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
3865 		vdev_add_child(rvd, vd);
3866 	}
3867 	vdev_config_dirty(rvd);
3868 
3869 	/*
3870 	 * Reassess the health of our root vdev.
3871 	 */
3872 	vdev_reopen(rvd);
3873 }
3874 
3875 /*
3876  * Remove a device from the pool.  Currently, this supports removing only hot
3877  * spares, slogs, and level 2 ARC devices.
3878  */
3879 int
3880 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
3881 {
3882 	vdev_t *vd;
3883 	metaslab_group_t *mg;
3884 	nvlist_t **spares, **l2cache, *nv;
3885 	uint64_t txg = 0;
3886 	uint_t nspares, nl2cache;
3887 	int error = 0;
3888 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
3889 
3890 	if (!locked)
3891 		txg = spa_vdev_enter(spa);
3892 
3893 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3894 
3895 	if (spa->spa_spares.sav_vdevs != NULL &&
3896 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3897 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
3898 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
3899 		/*
3900 		 * Only remove the hot spare if it's not currently in use
3901 		 * in this pool.
3902 		 */
3903 		if (vd == NULL || unspare) {
3904 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
3905 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
3906 			spa_load_spares(spa);
3907 			spa->spa_spares.sav_sync = B_TRUE;
3908 		} else {
3909 			error = EBUSY;
3910 		}
3911 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
3912 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3913 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
3914 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
3915 		/*
3916 		 * Cache devices can always be removed.
3917 		 */
3918 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
3919 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
3920 		spa_load_l2cache(spa);
3921 		spa->spa_l2cache.sav_sync = B_TRUE;
3922 	} else if (vd != NULL && vd->vdev_islog) {
3923 		ASSERT(!locked);
3924 		ASSERT(vd == vd->vdev_top);
3925 
3926 		/*
3927 		 * XXX - Once we have bp-rewrite this should
3928 		 * become the common case.
3929 		 */
3930 
3931 		mg = vd->vdev_mg;
3932 
3933 		/*
3934 		 * Stop allocating from this vdev.
3935 		 */
3936 		metaslab_group_passivate(mg);
3937 
3938 		/*
3939 		 * Wait for the youngest allocations and frees to sync,
3940 		 * and then wait for the deferral of those frees to finish.
3941 		 */
3942 		spa_vdev_config_exit(spa, NULL,
3943 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
3944 
3945 		/*
3946 		 * Attempt to evacuate the vdev.
3947 		 */
3948 		error = spa_vdev_remove_evacuate(spa, vd);
3949 
3950 		txg = spa_vdev_config_enter(spa);
3951 
3952 		/*
3953 		 * If we couldn't evacuate the vdev, unwind.
3954 		 */
3955 		if (error) {
3956 			metaslab_group_activate(mg);
3957 			return (spa_vdev_exit(spa, NULL, txg, error));
3958 		}
3959 
3960 		/*
3961 		 * Clean up the vdev namespace.
3962 		 */
3963 		spa_vdev_remove_from_namespace(spa, vd);
3964 
3965 	} else if (vd != NULL) {
3966 		/*
3967 		 * Normal vdevs cannot be removed (yet).
3968 		 */
3969 		error = ENOTSUP;
3970 	} else {
3971 		/*
3972 		 * There is no vdev of any kind with the specified guid.
3973 		 */
3974 		error = ENOENT;
3975 	}
3976 
3977 	if (!locked)
3978 		return (spa_vdev_exit(spa, NULL, txg, error));
3979 
3980 	return (error);
3981 }
3982 
3983 /*
3984  * Find any device that's done replacing, or a vdev marked 'unspare' that's
3985  * current spared, so we can detach it.
3986  */
3987 static vdev_t *
3988 spa_vdev_resilver_done_hunt(vdev_t *vd)
3989 {
3990 	vdev_t *newvd, *oldvd;
3991 
3992 	for (int c = 0; c < vd->vdev_children; c++) {
3993 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
3994 		if (oldvd != NULL)
3995 			return (oldvd);
3996 	}
3997 
3998 	/*
3999 	 * Check for a completed replacement.
4000 	 */
4001 	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
4002 		oldvd = vd->vdev_child[0];
4003 		newvd = vd->vdev_child[1];
4004 
4005 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
4006 		    !vdev_dtl_required(oldvd))
4007 			return (oldvd);
4008 	}
4009 
4010 	/*
4011 	 * Check for a completed resilver with the 'unspare' flag set.
4012 	 */
4013 	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
4014 		newvd = vd->vdev_child[0];
4015 		oldvd = vd->vdev_child[1];
4016 
4017 		if (newvd->vdev_unspare &&
4018 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
4019 		    !vdev_dtl_required(oldvd)) {
4020 			newvd->vdev_unspare = 0;
4021 			return (oldvd);
4022 		}
4023 	}
4024 
4025 	return (NULL);
4026 }
4027 
4028 static void
4029 spa_vdev_resilver_done(spa_t *spa)
4030 {
4031 	vdev_t *vd, *pvd, *ppvd;
4032 	uint64_t guid, sguid, pguid, ppguid;
4033 
4034 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4035 
4036 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
4037 		pvd = vd->vdev_parent;
4038 		ppvd = pvd->vdev_parent;
4039 		guid = vd->vdev_guid;
4040 		pguid = pvd->vdev_guid;
4041 		ppguid = ppvd->vdev_guid;
4042 		sguid = 0;
4043 		/*
4044 		 * If we have just finished replacing a hot spared device, then
4045 		 * we need to detach the parent's first child (the original hot
4046 		 * spare) as well.
4047 		 */
4048 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
4049 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
4050 			ASSERT(ppvd->vdev_children == 2);
4051 			sguid = ppvd->vdev_child[1]->vdev_guid;
4052 		}
4053 		spa_config_exit(spa, SCL_ALL, FTAG);
4054 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
4055 			return;
4056 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
4057 			return;
4058 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4059 	}
4060 
4061 	spa_config_exit(spa, SCL_ALL, FTAG);
4062 }
4063 
4064 /*
4065  * Update the stored path or FRU for this vdev.
4066  */
4067 int
4068 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
4069     boolean_t ispath)
4070 {
4071 	vdev_t *vd;
4072 
4073 	spa_vdev_state_enter(spa, SCL_ALL);
4074 
4075 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4076 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
4077 
4078 	if (!vd->vdev_ops->vdev_op_leaf)
4079 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4080 
4081 	if (ispath) {
4082 		spa_strfree(vd->vdev_path);
4083 		vd->vdev_path = spa_strdup(value);
4084 	} else {
4085 		if (vd->vdev_fru != NULL)
4086 			spa_strfree(vd->vdev_fru);
4087 		vd->vdev_fru = spa_strdup(value);
4088 	}
4089 
4090 	return (spa_vdev_state_exit(spa, vd, 0));
4091 }
4092 
4093 int
4094 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
4095 {
4096 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
4097 }
4098 
4099 int
4100 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
4101 {
4102 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
4103 }
4104 
4105 /*
4106  * ==========================================================================
4107  * SPA Scrubbing
4108  * ==========================================================================
4109  */
4110 
4111 int
4112 spa_scrub(spa_t *spa, pool_scrub_type_t type)
4113 {
4114 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4115 
4116 	if ((uint_t)type >= POOL_SCRUB_TYPES)
4117 		return (ENOTSUP);
4118 
4119 	/*
4120 	 * If a resilver was requested, but there is no DTL on a
4121 	 * writeable leaf device, we have nothing to do.
4122 	 */
4123 	if (type == POOL_SCRUB_RESILVER &&
4124 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
4125 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4126 		return (0);
4127 	}
4128 
4129 	if (type == POOL_SCRUB_EVERYTHING &&
4130 	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
4131 	    spa->spa_dsl_pool->dp_scrub_isresilver)
4132 		return (EBUSY);
4133 
4134 	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
4135 		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
4136 	} else if (type == POOL_SCRUB_NONE) {
4137 		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
4138 	} else {
4139 		return (EINVAL);
4140 	}
4141 }
4142 
4143 /*
4144  * ==========================================================================
4145  * SPA async task processing
4146  * ==========================================================================
4147  */
4148 
4149 static void
4150 spa_async_remove(spa_t *spa, vdev_t *vd)
4151 {
4152 	if (vd->vdev_remove_wanted) {
4153 		vd->vdev_remove_wanted = 0;
4154 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
4155 
4156 		/*
4157 		 * We want to clear the stats, but we don't want to do a full
4158 		 * vdev_clear() as that will cause us to throw away
4159 		 * degraded/faulted state as well as attempt to reopen the
4160 		 * device, all of which is a waste.
4161 		 */
4162 		vd->vdev_stat.vs_read_errors = 0;
4163 		vd->vdev_stat.vs_write_errors = 0;
4164 		vd->vdev_stat.vs_checksum_errors = 0;
4165 
4166 		vdev_state_dirty(vd->vdev_top);
4167 	}
4168 
4169 	for (int c = 0; c < vd->vdev_children; c++)
4170 		spa_async_remove(spa, vd->vdev_child[c]);
4171 }
4172 
4173 static void
4174 spa_async_probe(spa_t *spa, vdev_t *vd)
4175 {
4176 	if (vd->vdev_probe_wanted) {
4177 		vd->vdev_probe_wanted = 0;
4178 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
4179 	}
4180 
4181 	for (int c = 0; c < vd->vdev_children; c++)
4182 		spa_async_probe(spa, vd->vdev_child[c]);
4183 }
4184 
4185 static void
4186 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
4187 {
4188 	sysevent_id_t eid;
4189 	nvlist_t *attr;
4190 	char *physpath;
4191 
4192 	if (!spa->spa_autoexpand)
4193 		return;
4194 
4195 	for (int c = 0; c < vd->vdev_children; c++) {
4196 		vdev_t *cvd = vd->vdev_child[c];
4197 		spa_async_autoexpand(spa, cvd);
4198 	}
4199 
4200 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
4201 		return;
4202 
4203 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
4204 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
4205 
4206 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4207 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
4208 
4209 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
4210 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
4211 
4212 	nvlist_free(attr);
4213 	kmem_free(physpath, MAXPATHLEN);
4214 }
4215 
4216 static void
4217 spa_async_thread(spa_t *spa)
4218 {
4219 	int tasks;
4220 
4221 	ASSERT(spa->spa_sync_on);
4222 
4223 	mutex_enter(&spa->spa_async_lock);
4224 	tasks = spa->spa_async_tasks;
4225 	spa->spa_async_tasks = 0;
4226 	mutex_exit(&spa->spa_async_lock);
4227 
4228 	/*
4229 	 * See if the config needs to be updated.
4230 	 */
4231 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
4232 		uint64_t old_space, new_space;
4233 
4234 		mutex_enter(&spa_namespace_lock);
4235 		old_space = metaslab_class_get_space(spa_normal_class(spa));
4236 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4237 		new_space = metaslab_class_get_space(spa_normal_class(spa));
4238 		mutex_exit(&spa_namespace_lock);
4239 
4240 		/*
4241 		 * If the pool grew as a result of the config update,
4242 		 * then log an internal history event.
4243 		 */
4244 		if (new_space != old_space) {
4245 			spa_history_internal_log(LOG_POOL_VDEV_ONLINE,
4246 			    spa, NULL, CRED(),
4247 			    "pool '%s' size: %llu(+%llu)",
4248 			    spa_name(spa), new_space, new_space - old_space);
4249 		}
4250 	}
4251 
4252 	/*
4253 	 * See if any devices need to be marked REMOVED.
4254 	 */
4255 	if (tasks & SPA_ASYNC_REMOVE) {
4256 		spa_vdev_state_enter(spa, SCL_NONE);
4257 		spa_async_remove(spa, spa->spa_root_vdev);
4258 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
4259 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
4260 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
4261 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
4262 		(void) spa_vdev_state_exit(spa, NULL, 0);
4263 	}
4264 
4265 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
4266 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4267 		spa_async_autoexpand(spa, spa->spa_root_vdev);
4268 		spa_config_exit(spa, SCL_CONFIG, FTAG);
4269 	}
4270 
4271 	/*
4272 	 * See if any devices need to be probed.
4273 	 */
4274 	if (tasks & SPA_ASYNC_PROBE) {
4275 		spa_vdev_state_enter(spa, SCL_NONE);
4276 		spa_async_probe(spa, spa->spa_root_vdev);
4277 		(void) spa_vdev_state_exit(spa, NULL, 0);
4278 	}
4279 
4280 	/*
4281 	 * If any devices are done replacing, detach them.
4282 	 */
4283 	if (tasks & SPA_ASYNC_RESILVER_DONE)
4284 		spa_vdev_resilver_done(spa);
4285 
4286 	/*
4287 	 * Kick off a resilver.
4288 	 */
4289 	if (tasks & SPA_ASYNC_RESILVER)
4290 		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
4291 
4292 	/*
4293 	 * Let the world know that we're done.
4294 	 */
4295 	mutex_enter(&spa->spa_async_lock);
4296 	spa->spa_async_thread = NULL;
4297 	cv_broadcast(&spa->spa_async_cv);
4298 	mutex_exit(&spa->spa_async_lock);
4299 	thread_exit();
4300 }
4301 
4302 void
4303 spa_async_suspend(spa_t *spa)
4304 {
4305 	mutex_enter(&spa->spa_async_lock);
4306 	spa->spa_async_suspended++;
4307 	while (spa->spa_async_thread != NULL)
4308 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
4309 	mutex_exit(&spa->spa_async_lock);
4310 }
4311 
4312 void
4313 spa_async_resume(spa_t *spa)
4314 {
4315 	mutex_enter(&spa->spa_async_lock);
4316 	ASSERT(spa->spa_async_suspended != 0);
4317 	spa->spa_async_suspended--;
4318 	mutex_exit(&spa->spa_async_lock);
4319 }
4320 
4321 static void
4322 spa_async_dispatch(spa_t *spa)
4323 {
4324 	mutex_enter(&spa->spa_async_lock);
4325 	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
4326 	    spa->spa_async_thread == NULL &&
4327 	    rootdir != NULL && !vn_is_readonly(rootdir))
4328 		spa->spa_async_thread = thread_create(NULL, 0,
4329 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
4330 	mutex_exit(&spa->spa_async_lock);
4331 }
4332 
4333 void
4334 spa_async_request(spa_t *spa, int task)
4335 {
4336 	mutex_enter(&spa->spa_async_lock);
4337 	spa->spa_async_tasks |= task;
4338 	mutex_exit(&spa->spa_async_lock);
4339 }
4340 
4341 /*
4342  * ==========================================================================
4343  * SPA syncing routines
4344  * ==========================================================================
4345  */
4346 static void
4347 spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg)
4348 {
4349 	blkptr_t blk;
4350 	uint64_t itor = 0;
4351 	uint8_t c = 1;
4352 
4353 	while (bplist_iterate(bpl, &itor, &blk) == 0) {
4354 		ASSERT(blk.blk_birth < txg);
4355 		zio_free(spa, txg, &blk);
4356 	}
4357 
4358 	bplist_vacate(bpl, tx);
4359 
4360 	/*
4361 	 * Pre-dirty the first block so we sync to convergence faster.
4362 	 * (Usually only the first block is needed.)
4363 	 */
4364 	dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx);
4365 }
4366 
4367 static void
4368 spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
4369 {
4370 	zio_t *zio = arg;
4371 
4372 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
4373 	    zio->io_flags));
4374 }
4375 
4376 static void
4377 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
4378 {
4379 	char *packed = NULL;
4380 	size_t bufsize;
4381 	size_t nvsize = 0;
4382 	dmu_buf_t *db;
4383 
4384 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
4385 
4386 	/*
4387 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
4388 	 * information.  This avoids the dbuf_will_dirty() path and
4389 	 * saves us a pre-read to get data we don't actually care about.
4390 	 */
4391 	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
4392 	packed = kmem_alloc(bufsize, KM_SLEEP);
4393 
4394 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
4395 	    KM_SLEEP) == 0);
4396 	bzero(packed + nvsize, bufsize - nvsize);
4397 
4398 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
4399 
4400 	kmem_free(packed, bufsize);
4401 
4402 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
4403 	dmu_buf_will_dirty(db, tx);
4404 	*(uint64_t *)db->db_data = nvsize;
4405 	dmu_buf_rele(db, FTAG);
4406 }
4407 
4408 static void
4409 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
4410     const char *config, const char *entry)
4411 {
4412 	nvlist_t *nvroot;
4413 	nvlist_t **list;
4414 	int i;
4415 
4416 	if (!sav->sav_sync)
4417 		return;
4418 
4419 	/*
4420 	 * Update the MOS nvlist describing the list of available devices.
4421 	 * spa_validate_aux() will have already made sure this nvlist is
4422 	 * valid and the vdevs are labeled appropriately.
4423 	 */
4424 	if (sav->sav_object == 0) {
4425 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
4426 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
4427 		    sizeof (uint64_t), tx);
4428 		VERIFY(zap_update(spa->spa_meta_objset,
4429 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
4430 		    &sav->sav_object, tx) == 0);
4431 	}
4432 
4433 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4434 	if (sav->sav_count == 0) {
4435 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
4436 	} else {
4437 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
4438 		for (i = 0; i < sav->sav_count; i++)
4439 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
4440 			    B_FALSE, B_FALSE, B_TRUE);
4441 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
4442 		    sav->sav_count) == 0);
4443 		for (i = 0; i < sav->sav_count; i++)
4444 			nvlist_free(list[i]);
4445 		kmem_free(list, sav->sav_count * sizeof (void *));
4446 	}
4447 
4448 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
4449 	nvlist_free(nvroot);
4450 
4451 	sav->sav_sync = B_FALSE;
4452 }
4453 
4454 static void
4455 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
4456 {
4457 	nvlist_t *config;
4458 
4459 	if (list_is_empty(&spa->spa_config_dirty_list))
4460 		return;
4461 
4462 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4463 
4464 	config = spa_config_generate(spa, spa->spa_root_vdev,
4465 	    dmu_tx_get_txg(tx), B_FALSE);
4466 
4467 	spa_config_exit(spa, SCL_STATE, FTAG);
4468 
4469 	if (spa->spa_config_syncing)
4470 		nvlist_free(spa->spa_config_syncing);
4471 	spa->spa_config_syncing = config;
4472 
4473 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
4474 }
4475 
4476 /*
4477  * Set zpool properties.
4478  */
4479 static void
4480 spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
4481 {
4482 	spa_t *spa = arg1;
4483 	objset_t *mos = spa->spa_meta_objset;
4484 	nvlist_t *nvp = arg2;
4485 	nvpair_t *elem;
4486 	uint64_t intval;
4487 	char *strval;
4488 	zpool_prop_t prop;
4489 	const char *propname;
4490 	zprop_type_t proptype;
4491 
4492 	mutex_enter(&spa->spa_props_lock);
4493 
4494 	elem = NULL;
4495 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
4496 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
4497 		case ZPOOL_PROP_VERSION:
4498 			/*
4499 			 * Only set version for non-zpool-creation cases
4500 			 * (set/import). spa_create() needs special care
4501 			 * for version setting.
4502 			 */
4503 			if (tx->tx_txg != TXG_INITIAL) {
4504 				VERIFY(nvpair_value_uint64(elem,
4505 				    &intval) == 0);
4506 				ASSERT(intval <= SPA_VERSION);
4507 				ASSERT(intval >= spa_version(spa));
4508 				spa->spa_uberblock.ub_version = intval;
4509 				vdev_config_dirty(spa->spa_root_vdev);
4510 			}
4511 			break;
4512 
4513 		case ZPOOL_PROP_ALTROOT:
4514 			/*
4515 			 * 'altroot' is a non-persistent property. It should
4516 			 * have been set temporarily at creation or import time.
4517 			 */
4518 			ASSERT(spa->spa_root != NULL);
4519 			break;
4520 
4521 		case ZPOOL_PROP_CACHEFILE:
4522 			/*
4523 			 * 'cachefile' is also a non-persisitent property.
4524 			 */
4525 			break;
4526 		default:
4527 			/*
4528 			 * Set pool property values in the poolprops mos object.
4529 			 */
4530 			if (spa->spa_pool_props_object == 0) {
4531 				VERIFY((spa->spa_pool_props_object =
4532 				    zap_create(mos, DMU_OT_POOL_PROPS,
4533 				    DMU_OT_NONE, 0, tx)) > 0);
4534 
4535 				VERIFY(zap_update(mos,
4536 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
4537 				    8, 1, &spa->spa_pool_props_object, tx)
4538 				    == 0);
4539 			}
4540 
4541 			/* normalize the property name */
4542 			propname = zpool_prop_to_name(prop);
4543 			proptype = zpool_prop_get_type(prop);
4544 
4545 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
4546 				ASSERT(proptype == PROP_TYPE_STRING);
4547 				VERIFY(nvpair_value_string(elem, &strval) == 0);
4548 				VERIFY(zap_update(mos,
4549 				    spa->spa_pool_props_object, propname,
4550 				    1, strlen(strval) + 1, strval, tx) == 0);
4551 
4552 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
4553 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
4554 
4555 				if (proptype == PROP_TYPE_INDEX) {
4556 					const char *unused;
4557 					VERIFY(zpool_prop_index_to_string(
4558 					    prop, intval, &unused) == 0);
4559 				}
4560 				VERIFY(zap_update(mos,
4561 				    spa->spa_pool_props_object, propname,
4562 				    8, 1, &intval, tx) == 0);
4563 			} else {
4564 				ASSERT(0); /* not allowed */
4565 			}
4566 
4567 			switch (prop) {
4568 			case ZPOOL_PROP_DELEGATION:
4569 				spa->spa_delegation = intval;
4570 				break;
4571 			case ZPOOL_PROP_BOOTFS:
4572 				spa->spa_bootfs = intval;
4573 				break;
4574 			case ZPOOL_PROP_FAILUREMODE:
4575 				spa->spa_failmode = intval;
4576 				break;
4577 			case ZPOOL_PROP_AUTOEXPAND:
4578 				spa->spa_autoexpand = intval;
4579 				spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4580 				break;
4581 			case ZPOOL_PROP_DEDUPDITTO:
4582 				spa->spa_dedup_ditto = intval;
4583 				break;
4584 			default:
4585 				break;
4586 			}
4587 		}
4588 
4589 		/* log internal history if this is not a zpool create */
4590 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
4591 		    tx->tx_txg != TXG_INITIAL) {
4592 			spa_history_internal_log(LOG_POOL_PROPSET,
4593 			    spa, tx, cr, "%s %lld %s",
4594 			    nvpair_name(elem), intval, spa_name(spa));
4595 		}
4596 	}
4597 
4598 	mutex_exit(&spa->spa_props_lock);
4599 }
4600 
4601 /*
4602  * Sync the specified transaction group.  New blocks may be dirtied as
4603  * part of the process, so we iterate until it converges.
4604  */
4605 void
4606 spa_sync(spa_t *spa, uint64_t txg)
4607 {
4608 	dsl_pool_t *dp = spa->spa_dsl_pool;
4609 	objset_t *mos = spa->spa_meta_objset;
4610 	bplist_t *defer_bpl = &spa->spa_deferred_bplist;
4611 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
4612 	vdev_t *rvd = spa->spa_root_vdev;
4613 	vdev_t *vd;
4614 	dmu_tx_t *tx;
4615 	int error;
4616 
4617 	/*
4618 	 * Lock out configuration changes.
4619 	 */
4620 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4621 
4622 	spa->spa_syncing_txg = txg;
4623 	spa->spa_sync_pass = 0;
4624 
4625 	/*
4626 	 * If there are any pending vdev state changes, convert them
4627 	 * into config changes that go out with this transaction group.
4628 	 */
4629 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4630 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
4631 		/*
4632 		 * We need the write lock here because, for aux vdevs,
4633 		 * calling vdev_config_dirty() modifies sav_config.
4634 		 * This is ugly and will become unnecessary when we
4635 		 * eliminate the aux vdev wart by integrating all vdevs
4636 		 * into the root vdev tree.
4637 		 */
4638 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
4639 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
4640 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
4641 			vdev_state_clean(vd);
4642 			vdev_config_dirty(vd);
4643 		}
4644 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
4645 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
4646 	}
4647 	spa_config_exit(spa, SCL_STATE, FTAG);
4648 
4649 	VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj));
4650 
4651 	tx = dmu_tx_create_assigned(dp, txg);
4652 
4653 	/*
4654 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
4655 	 * set spa_deflate if we have no raid-z vdevs.
4656 	 */
4657 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
4658 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
4659 		int i;
4660 
4661 		for (i = 0; i < rvd->vdev_children; i++) {
4662 			vd = rvd->vdev_child[i];
4663 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
4664 				break;
4665 		}
4666 		if (i == rvd->vdev_children) {
4667 			spa->spa_deflate = TRUE;
4668 			VERIFY(0 == zap_add(spa->spa_meta_objset,
4669 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
4670 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
4671 		}
4672 	}
4673 
4674 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
4675 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
4676 		dsl_pool_create_origin(dp, tx);
4677 
4678 		/* Keeping the origin open increases spa_minref */
4679 		spa->spa_minref += 3;
4680 	}
4681 
4682 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
4683 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
4684 		dsl_pool_upgrade_clones(dp, tx);
4685 	}
4686 
4687 	/*
4688 	 * If anything has changed in this txg, push the deferred frees
4689 	 * from the previous txg.  If not, leave them alone so that we
4690 	 * don't generate work on an otherwise idle system.
4691 	 */
4692 	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
4693 	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
4694 	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4695 		spa_sync_deferred_bplist(spa, defer_bpl, tx, txg);
4696 
4697 	/*
4698 	 * Iterate to convergence.
4699 	 */
4700 	do {
4701 		int pass = ++spa->spa_sync_pass;
4702 
4703 		spa_sync_config_object(spa, tx);
4704 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
4705 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
4706 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
4707 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
4708 		spa_errlog_sync(spa, txg);
4709 		dsl_pool_sync(dp, txg);
4710 
4711 		if (pass <= SYNC_PASS_DEFERRED_FREE) {
4712 			zio_t *zio = zio_root(spa, NULL, NULL, 0);
4713 			bplist_sync(free_bpl, spa_sync_free, zio, tx);
4714 			VERIFY(zio_wait(zio) == 0);
4715 		} else {
4716 			bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx);
4717 		}
4718 
4719 		ddt_sync(spa, txg);
4720 
4721 		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
4722 			vdev_sync(vd, txg);
4723 
4724 	} while (dmu_objset_is_dirty(mos, txg));
4725 
4726 	ASSERT(free_bpl->bpl_queue == NULL);
4727 
4728 	bplist_close(defer_bpl);
4729 
4730 	/*
4731 	 * Rewrite the vdev configuration (which includes the uberblock)
4732 	 * to commit the transaction group.
4733 	 *
4734 	 * If there are no dirty vdevs, we sync the uberblock to a few
4735 	 * random top-level vdevs that are known to be visible in the
4736 	 * config cache (see spa_vdev_add() for a complete description).
4737 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4738 	 */
4739 	for (;;) {
4740 		/*
4741 		 * We hold SCL_STATE to prevent vdev open/close/etc.
4742 		 * while we're attempting to write the vdev labels.
4743 		 */
4744 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4745 
4746 		if (list_is_empty(&spa->spa_config_dirty_list)) {
4747 			vdev_t *svd[SPA_DVAS_PER_BP];
4748 			int svdcount = 0;
4749 			int children = rvd->vdev_children;
4750 			int c0 = spa_get_random(children);
4751 
4752 			for (int c = 0; c < children; c++) {
4753 				vd = rvd->vdev_child[(c0 + c) % children];
4754 				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
4755 					continue;
4756 				svd[svdcount++] = vd;
4757 				if (svdcount == SPA_DVAS_PER_BP)
4758 					break;
4759 			}
4760 			error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
4761 			if (error != 0)
4762 				error = vdev_config_sync(svd, svdcount, txg,
4763 				    B_TRUE);
4764 		} else {
4765 			error = vdev_config_sync(rvd->vdev_child,
4766 			    rvd->vdev_children, txg, B_FALSE);
4767 			if (error != 0)
4768 				error = vdev_config_sync(rvd->vdev_child,
4769 				    rvd->vdev_children, txg, B_TRUE);
4770 		}
4771 
4772 		spa_config_exit(spa, SCL_STATE, FTAG);
4773 
4774 		if (error == 0)
4775 			break;
4776 		zio_suspend(spa, NULL);
4777 		zio_resume_wait(spa);
4778 	}
4779 	dmu_tx_commit(tx);
4780 
4781 	/*
4782 	 * Clear the dirty config list.
4783 	 */
4784 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
4785 		vdev_config_clean(vd);
4786 
4787 	/*
4788 	 * Now that the new config has synced transactionally,
4789 	 * let it become visible to the config cache.
4790 	 */
4791 	if (spa->spa_config_syncing != NULL) {
4792 		spa_config_set(spa, spa->spa_config_syncing);
4793 		spa->spa_config_txg = txg;
4794 		spa->spa_config_syncing = NULL;
4795 	}
4796 
4797 	spa->spa_ubsync = spa->spa_uberblock;
4798 
4799 	dsl_pool_sync_done(dp, txg);
4800 
4801 	/*
4802 	 * Update usable space statistics.
4803 	 */
4804 	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4805 		vdev_sync_done(vd, txg);
4806 
4807 	spa_update_dspace(spa);
4808 
4809 	/*
4810 	 * It had better be the case that we didn't dirty anything
4811 	 * since vdev_config_sync().
4812 	 */
4813 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4814 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4815 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4816 	ASSERT(defer_bpl->bpl_queue == NULL);
4817 	ASSERT(free_bpl->bpl_queue == NULL);
4818 
4819 	spa->spa_sync_pass = 0;
4820 
4821 	spa_config_exit(spa, SCL_CONFIG, FTAG);
4822 
4823 	spa_handle_ignored_writes(spa);
4824 
4825 	/*
4826 	 * If any async tasks have been requested, kick them off.
4827 	 */
4828 	spa_async_dispatch(spa);
4829 }
4830 
4831 /*
4832  * Sync all pools.  We don't want to hold the namespace lock across these
4833  * operations, so we take a reference on the spa_t and drop the lock during the
4834  * sync.
4835  */
4836 void
4837 spa_sync_allpools(void)
4838 {
4839 	spa_t *spa = NULL;
4840 	mutex_enter(&spa_namespace_lock);
4841 	while ((spa = spa_next(spa)) != NULL) {
4842 		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4843 			continue;
4844 		spa_open_ref(spa, FTAG);
4845 		mutex_exit(&spa_namespace_lock);
4846 		txg_wait_synced(spa_get_dsl(spa), 0);
4847 		mutex_enter(&spa_namespace_lock);
4848 		spa_close(spa, FTAG);
4849 	}
4850 	mutex_exit(&spa_namespace_lock);
4851 }
4852 
4853 /*
4854  * ==========================================================================
4855  * Miscellaneous routines
4856  * ==========================================================================
4857  */
4858 
4859 /*
4860  * Remove all pools in the system.
4861  */
4862 void
4863 spa_evict_all(void)
4864 {
4865 	spa_t *spa;
4866 
4867 	/*
4868 	 * Remove all cached state.  All pools should be closed now,
4869 	 * so every spa in the AVL tree should be unreferenced.
4870 	 */
4871 	mutex_enter(&spa_namespace_lock);
4872 	while ((spa = spa_next(NULL)) != NULL) {
4873 		/*
4874 		 * Stop async tasks.  The async thread may need to detach
4875 		 * a device that's been replaced, which requires grabbing
4876 		 * spa_namespace_lock, so we must drop it here.
4877 		 */
4878 		spa_open_ref(spa, FTAG);
4879 		mutex_exit(&spa_namespace_lock);
4880 		spa_async_suspend(spa);
4881 		mutex_enter(&spa_namespace_lock);
4882 		spa_close(spa, FTAG);
4883 
4884 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4885 			spa_unload(spa);
4886 			spa_deactivate(spa);
4887 		}
4888 		spa_remove(spa);
4889 	}
4890 	mutex_exit(&spa_namespace_lock);
4891 }
4892 
4893 vdev_t *
4894 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
4895 {
4896 	vdev_t *vd;
4897 	int i;
4898 
4899 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
4900 		return (vd);
4901 
4902 	if (aux) {
4903 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
4904 			vd = spa->spa_l2cache.sav_vdevs[i];
4905 			if (vd->vdev_guid == guid)
4906 				return (vd);
4907 		}
4908 
4909 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
4910 			vd = spa->spa_spares.sav_vdevs[i];
4911 			if (vd->vdev_guid == guid)
4912 				return (vd);
4913 		}
4914 	}
4915 
4916 	return (NULL);
4917 }
4918 
4919 void
4920 spa_upgrade(spa_t *spa, uint64_t version)
4921 {
4922 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4923 
4924 	/*
4925 	 * This should only be called for a non-faulted pool, and since a
4926 	 * future version would result in an unopenable pool, this shouldn't be
4927 	 * possible.
4928 	 */
4929 	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
4930 	ASSERT(version >= spa->spa_uberblock.ub_version);
4931 
4932 	spa->spa_uberblock.ub_version = version;
4933 	vdev_config_dirty(spa->spa_root_vdev);
4934 
4935 	spa_config_exit(spa, SCL_ALL, FTAG);
4936 
4937 	txg_wait_synced(spa_get_dsl(spa), 0);
4938 }
4939 
4940 boolean_t
4941 spa_has_spare(spa_t *spa, uint64_t guid)
4942 {
4943 	int i;
4944 	uint64_t spareguid;
4945 	spa_aux_vdev_t *sav = &spa->spa_spares;
4946 
4947 	for (i = 0; i < sav->sav_count; i++)
4948 		if (sav->sav_vdevs[i]->vdev_guid == guid)
4949 			return (B_TRUE);
4950 
4951 	for (i = 0; i < sav->sav_npending; i++) {
4952 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
4953 		    &spareguid) == 0 && spareguid == guid)
4954 			return (B_TRUE);
4955 	}
4956 
4957 	return (B_FALSE);
4958 }
4959 
4960 /*
4961  * Check if a pool has an active shared spare device.
4962  * Note: reference count of an active spare is 2, as a spare and as a replace
4963  */
4964 static boolean_t
4965 spa_has_active_shared_spare(spa_t *spa)
4966 {
4967 	int i, refcnt;
4968 	uint64_t pool;
4969 	spa_aux_vdev_t *sav = &spa->spa_spares;
4970 
4971 	for (i = 0; i < sav->sav_count; i++) {
4972 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
4973 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
4974 		    refcnt > 2)
4975 			return (B_TRUE);
4976 	}
4977 
4978 	return (B_FALSE);
4979 }
4980 
4981 /*
4982  * Post a sysevent corresponding to the given event.  The 'name' must be one of
4983  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
4984  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
4985  * in the userland libzpool, as we don't want consumers to misinterpret ztest
4986  * or zdb as real changes.
4987  */
4988 void
4989 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
4990 {
4991 #ifdef _KERNEL
4992 	sysevent_t		*ev;
4993 	sysevent_attr_list_t	*attr = NULL;
4994 	sysevent_value_t	value;
4995 	sysevent_id_t		eid;
4996 
4997 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
4998 	    SE_SLEEP);
4999 
5000 	value.value_type = SE_DATA_TYPE_STRING;
5001 	value.value.sv_string = spa_name(spa);
5002 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
5003 		goto done;
5004 
5005 	value.value_type = SE_DATA_TYPE_UINT64;
5006 	value.value.sv_uint64 = spa_guid(spa);
5007 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
5008 		goto done;
5009 
5010 	if (vd) {
5011 		value.value_type = SE_DATA_TYPE_UINT64;
5012 		value.value.sv_uint64 = vd->vdev_guid;
5013 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
5014 		    SE_SLEEP) != 0)
5015 			goto done;
5016 
5017 		if (vd->vdev_path) {
5018 			value.value_type = SE_DATA_TYPE_STRING;
5019 			value.value.sv_string = vd->vdev_path;
5020 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
5021 			    &value, SE_SLEEP) != 0)
5022 				goto done;
5023 		}
5024 	}
5025 
5026 	if (sysevent_attach_attributes(ev, attr) != 0)
5027 		goto done;
5028 	attr = NULL;
5029 
5030 	(void) log_sysevent(ev, SE_SLEEP, &eid);
5031 
5032 done:
5033 	if (attr)
5034 		sysevent_free_attr(attr);
5035 	sysevent_free(ev);
5036 #endif
5037 }
5038