xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa.c (revision 663207ad)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2015, Nexenta Systems, Inc.  All rights reserved.
26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27  * Copyright 2013 Saso Kiselkov. All rights reserved.
28  * Copyright (c) 2014 Integros [integros.com]
29  * Copyright 2016 Toomas Soome <tsoome@me.com>
30  * Copyright 2018 Joyent, Inc.
31  * Copyright (c) 2017, Intel Corporation.
32  * Copyright (c) 2017 Datto Inc.
33  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
34  */
35 
36 /*
37  * SPA: Storage Pool Allocator
38  *
39  * This file contains all the routines used when modifying on-disk SPA state.
40  * This includes opening, importing, destroying, exporting a pool, and syncing a
41  * pool.
42  */
43 
44 #include <sys/zfs_context.h>
45 #include <sys/fm/fs/zfs.h>
46 #include <sys/spa_impl.h>
47 #include <sys/zio.h>
48 #include <sys/zio_checksum.h>
49 #include <sys/dmu.h>
50 #include <sys/dmu_tx.h>
51 #include <sys/zap.h>
52 #include <sys/zil.h>
53 #include <sys/ddt.h>
54 #include <sys/vdev_impl.h>
55 #include <sys/vdev_removal.h>
56 #include <sys/vdev_indirect_mapping.h>
57 #include <sys/vdev_indirect_births.h>
58 #include <sys/vdev_initialize.h>
59 #include <sys/metaslab.h>
60 #include <sys/metaslab_impl.h>
61 #include <sys/mmp.h>
62 #include <sys/uberblock_impl.h>
63 #include <sys/txg.h>
64 #include <sys/avl.h>
65 #include <sys/bpobj.h>
66 #include <sys/dmu_traverse.h>
67 #include <sys/dmu_objset.h>
68 #include <sys/unique.h>
69 #include <sys/dsl_pool.h>
70 #include <sys/dsl_dataset.h>
71 #include <sys/dsl_dir.h>
72 #include <sys/dsl_prop.h>
73 #include <sys/dsl_synctask.h>
74 #include <sys/fs/zfs.h>
75 #include <sys/arc.h>
76 #include <sys/callb.h>
77 #include <sys/systeminfo.h>
78 #include <sys/spa_boot.h>
79 #include <sys/zfs_ioctl.h>
80 #include <sys/dsl_scan.h>
81 #include <sys/zfeature.h>
82 #include <sys/dsl_destroy.h>
83 #include <sys/abd.h>
84 
85 #ifdef	_KERNEL
86 #include <sys/bootprops.h>
87 #include <sys/callb.h>
88 #include <sys/cpupart.h>
89 #include <sys/pool.h>
90 #include <sys/sysdc.h>
91 #include <sys/zone.h>
92 #endif	/* _KERNEL */
93 
94 #include "zfs_prop.h"
95 #include "zfs_comutil.h"
96 
97 /*
98  * The interval, in seconds, at which failed configuration cache file writes
99  * should be retried.
100  */
101 int zfs_ccw_retry_interval = 300;
102 
103 typedef enum zti_modes {
104 	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
105 	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
106 	ZTI_MODE_NULL,			/* don't create a taskq */
107 	ZTI_NMODES
108 } zti_modes_t;
109 
110 #define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
111 #define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
112 #define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
113 
114 #define	ZTI_N(n)	ZTI_P(n, 1)
115 #define	ZTI_ONE		ZTI_N(1)
116 
117 typedef struct zio_taskq_info {
118 	zti_modes_t zti_mode;
119 	uint_t zti_value;
120 	uint_t zti_count;
121 } zio_taskq_info_t;
122 
123 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
124 	"issue", "issue_high", "intr", "intr_high"
125 };
126 
127 /*
128  * This table defines the taskq settings for each ZFS I/O type. When
129  * initializing a pool, we use this table to create an appropriately sized
130  * taskq. Some operations are low volume and therefore have a small, static
131  * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
132  * macros. Other operations process a large amount of data; the ZTI_BATCH
133  * macro causes us to create a taskq oriented for throughput. Some operations
134  * are so high frequency and short-lived that the taskq itself can become a a
135  * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
136  * additional degree of parallelism specified by the number of threads per-
137  * taskq and the number of taskqs; when dispatching an event in this case, the
138  * particular taskq is chosen at random.
139  *
140  * The different taskq priorities are to handle the different contexts (issue
141  * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
142  * need to be handled with minimum delay.
143  */
144 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
145 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
146 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
147 	{ ZTI_N(8),	ZTI_NULL,	ZTI_P(12, 8),	ZTI_NULL }, /* READ */
148 	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
149 	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
150 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
151 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
152 };
153 
154 static void spa_sync_version(void *arg, dmu_tx_t *tx);
155 static void spa_sync_props(void *arg, dmu_tx_t *tx);
156 static boolean_t spa_has_active_shared_spare(spa_t *spa);
157 static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport);
158 static void spa_vdev_resilver_done(spa_t *spa);
159 
160 uint_t		zio_taskq_batch_pct = 75;	/* 1 thread per cpu in pset */
161 id_t		zio_taskq_psrset_bind = PS_NONE;
162 boolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
163 uint_t		zio_taskq_basedc = 80;		/* base duty cycle */
164 
165 boolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
166 extern int	zfs_sync_pass_deferred_free;
167 
168 /*
169  * Report any spa_load_verify errors found, but do not fail spa_load.
170  * This is used by zdb to analyze non-idle pools.
171  */
172 boolean_t	spa_load_verify_dryrun = B_FALSE;
173 
174 /*
175  * This (illegal) pool name is used when temporarily importing a spa_t in order
176  * to get the vdev stats associated with the imported devices.
177  */
178 #define	TRYIMPORT_NAME	"$import"
179 
180 /*
181  * For debugging purposes: print out vdev tree during pool import.
182  */
183 boolean_t	spa_load_print_vdev_tree = B_FALSE;
184 
185 /*
186  * A non-zero value for zfs_max_missing_tvds means that we allow importing
187  * pools with missing top-level vdevs. This is strictly intended for advanced
188  * pool recovery cases since missing data is almost inevitable. Pools with
189  * missing devices can only be imported read-only for safety reasons, and their
190  * fail-mode will be automatically set to "continue".
191  *
192  * With 1 missing vdev we should be able to import the pool and mount all
193  * datasets. User data that was not modified after the missing device has been
194  * added should be recoverable. This means that snapshots created prior to the
195  * addition of that device should be completely intact.
196  *
197  * With 2 missing vdevs, some datasets may fail to mount since there are
198  * dataset statistics that are stored as regular metadata. Some data might be
199  * recoverable if those vdevs were added recently.
200  *
201  * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
202  * may be missing entirely. Chances of data recovery are very low. Note that
203  * there are also risks of performing an inadvertent rewind as we might be
204  * missing all the vdevs with the latest uberblocks.
205  */
206 uint64_t	zfs_max_missing_tvds = 0;
207 
208 /*
209  * The parameters below are similar to zfs_max_missing_tvds but are only
210  * intended for a preliminary open of the pool with an untrusted config which
211  * might be incomplete or out-dated.
212  *
213  * We are more tolerant for pools opened from a cachefile since we could have
214  * an out-dated cachefile where a device removal was not registered.
215  * We could have set the limit arbitrarily high but in the case where devices
216  * are really missing we would want to return the proper error codes; we chose
217  * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
218  * and we get a chance to retrieve the trusted config.
219  */
220 uint64_t	zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1;
221 
222 /*
223  * In the case where config was assembled by scanning device paths (/dev/dsks
224  * by default) we are less tolerant since all the existing devices should have
225  * been detected and we want spa_load to return the right error codes.
226  */
227 uint64_t	zfs_max_missing_tvds_scan = 0;
228 
229 /*
230  * Debugging aid that pauses spa_sync() towards the end.
231  */
232 boolean_t	zfs_pause_spa_sync = B_FALSE;
233 
234 /*
235  * ==========================================================================
236  * SPA properties routines
237  * ==========================================================================
238  */
239 
240 /*
241  * Add a (source=src, propname=propval) list to an nvlist.
242  */
243 static void
244 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
245     uint64_t intval, zprop_source_t src)
246 {
247 	const char *propname = zpool_prop_to_name(prop);
248 	nvlist_t *propval;
249 
250 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
251 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
252 
253 	if (strval != NULL)
254 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
255 	else
256 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
257 
258 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
259 	nvlist_free(propval);
260 }
261 
262 /*
263  * Get property values from the spa configuration.
264  */
265 static void
266 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
267 {
268 	vdev_t *rvd = spa->spa_root_vdev;
269 	dsl_pool_t *pool = spa->spa_dsl_pool;
270 	uint64_t size, alloc, cap, version;
271 	zprop_source_t src = ZPROP_SRC_NONE;
272 	spa_config_dirent_t *dp;
273 	metaslab_class_t *mc = spa_normal_class(spa);
274 
275 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
276 
277 	if (rvd != NULL) {
278 		alloc = metaslab_class_get_alloc(mc);
279 		alloc += metaslab_class_get_alloc(spa_special_class(spa));
280 		alloc += metaslab_class_get_alloc(spa_dedup_class(spa));
281 
282 		size = metaslab_class_get_space(mc);
283 		size += metaslab_class_get_space(spa_special_class(spa));
284 		size += metaslab_class_get_space(spa_dedup_class(spa));
285 
286 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
287 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
288 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
289 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
290 		    size - alloc, src);
291 		spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL,
292 		    spa->spa_checkpoint_info.sci_dspace, src);
293 
294 		spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
295 		    metaslab_class_fragmentation(mc), src);
296 		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
297 		    metaslab_class_expandable_space(mc), src);
298 		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
299 		    (spa_mode(spa) == FREAD), src);
300 
301 		cap = (size == 0) ? 0 : (alloc * 100 / size);
302 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
303 
304 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
305 		    ddt_get_pool_dedup_ratio(spa), src);
306 
307 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
308 		    rvd->vdev_state, src);
309 
310 		version = spa_version(spa);
311 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
312 			src = ZPROP_SRC_DEFAULT;
313 		else
314 			src = ZPROP_SRC_LOCAL;
315 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
316 	}
317 
318 	if (pool != NULL) {
319 		/*
320 		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
321 		 * when opening pools before this version freedir will be NULL.
322 		 */
323 		if (pool->dp_free_dir != NULL) {
324 			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
325 			    dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
326 			    src);
327 		} else {
328 			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
329 			    NULL, 0, src);
330 		}
331 
332 		if (pool->dp_leak_dir != NULL) {
333 			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
334 			    dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
335 			    src);
336 		} else {
337 			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
338 			    NULL, 0, src);
339 		}
340 	}
341 
342 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
343 
344 	if (spa->spa_comment != NULL) {
345 		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
346 		    0, ZPROP_SRC_LOCAL);
347 	}
348 
349 	if (spa->spa_root != NULL)
350 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
351 		    0, ZPROP_SRC_LOCAL);
352 
353 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
354 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
355 		    MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
356 	} else {
357 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
358 		    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
359 	}
360 
361 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) {
362 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
363 		    DNODE_MAX_SIZE, ZPROP_SRC_NONE);
364 	} else {
365 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
366 		    DNODE_MIN_SIZE, ZPROP_SRC_NONE);
367 	}
368 
369 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
370 		if (dp->scd_path == NULL) {
371 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
372 			    "none", 0, ZPROP_SRC_LOCAL);
373 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
374 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
375 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
376 		}
377 	}
378 }
379 
380 /*
381  * Get zpool property values.
382  */
383 int
384 spa_prop_get(spa_t *spa, nvlist_t **nvp)
385 {
386 	objset_t *mos = spa->spa_meta_objset;
387 	zap_cursor_t zc;
388 	zap_attribute_t za;
389 	int err;
390 
391 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
392 
393 	mutex_enter(&spa->spa_props_lock);
394 
395 	/*
396 	 * Get properties from the spa config.
397 	 */
398 	spa_prop_get_config(spa, nvp);
399 
400 	/* If no pool property object, no more prop to get. */
401 	if (mos == NULL || spa->spa_pool_props_object == 0) {
402 		mutex_exit(&spa->spa_props_lock);
403 		return (0);
404 	}
405 
406 	/*
407 	 * Get properties from the MOS pool property object.
408 	 */
409 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
410 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
411 	    zap_cursor_advance(&zc)) {
412 		uint64_t intval = 0;
413 		char *strval = NULL;
414 		zprop_source_t src = ZPROP_SRC_DEFAULT;
415 		zpool_prop_t prop;
416 
417 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL)
418 			continue;
419 
420 		switch (za.za_integer_length) {
421 		case 8:
422 			/* integer property */
423 			if (za.za_first_integer !=
424 			    zpool_prop_default_numeric(prop))
425 				src = ZPROP_SRC_LOCAL;
426 
427 			if (prop == ZPOOL_PROP_BOOTFS) {
428 				dsl_pool_t *dp;
429 				dsl_dataset_t *ds = NULL;
430 
431 				dp = spa_get_dsl(spa);
432 				dsl_pool_config_enter(dp, FTAG);
433 				err = dsl_dataset_hold_obj(dp,
434 				    za.za_first_integer, FTAG, &ds);
435 				if (err != 0) {
436 					dsl_pool_config_exit(dp, FTAG);
437 					break;
438 				}
439 
440 				strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
441 				    KM_SLEEP);
442 				dsl_dataset_name(ds, strval);
443 				dsl_dataset_rele(ds, FTAG);
444 				dsl_pool_config_exit(dp, FTAG);
445 			} else {
446 				strval = NULL;
447 				intval = za.za_first_integer;
448 			}
449 
450 			spa_prop_add_list(*nvp, prop, strval, intval, src);
451 
452 			if (strval != NULL)
453 				kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
454 
455 			break;
456 
457 		case 1:
458 			/* string property */
459 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
460 			err = zap_lookup(mos, spa->spa_pool_props_object,
461 			    za.za_name, 1, za.za_num_integers, strval);
462 			if (err) {
463 				kmem_free(strval, za.za_num_integers);
464 				break;
465 			}
466 			spa_prop_add_list(*nvp, prop, strval, 0, src);
467 			kmem_free(strval, za.za_num_integers);
468 			break;
469 
470 		default:
471 			break;
472 		}
473 	}
474 	zap_cursor_fini(&zc);
475 	mutex_exit(&spa->spa_props_lock);
476 out:
477 	if (err && err != ENOENT) {
478 		nvlist_free(*nvp);
479 		*nvp = NULL;
480 		return (err);
481 	}
482 
483 	return (0);
484 }
485 
486 /*
487  * Validate the given pool properties nvlist and modify the list
488  * for the property values to be set.
489  */
490 static int
491 spa_prop_validate(spa_t *spa, nvlist_t *props)
492 {
493 	nvpair_t *elem;
494 	int error = 0, reset_bootfs = 0;
495 	uint64_t objnum = 0;
496 	boolean_t has_feature = B_FALSE;
497 
498 	elem = NULL;
499 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
500 		uint64_t intval;
501 		char *strval, *slash, *check, *fname;
502 		const char *propname = nvpair_name(elem);
503 		zpool_prop_t prop = zpool_name_to_prop(propname);
504 
505 		switch (prop) {
506 		case ZPOOL_PROP_INVAL:
507 			if (!zpool_prop_feature(propname)) {
508 				error = SET_ERROR(EINVAL);
509 				break;
510 			}
511 
512 			/*
513 			 * Sanitize the input.
514 			 */
515 			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
516 				error = SET_ERROR(EINVAL);
517 				break;
518 			}
519 
520 			if (nvpair_value_uint64(elem, &intval) != 0) {
521 				error = SET_ERROR(EINVAL);
522 				break;
523 			}
524 
525 			if (intval != 0) {
526 				error = SET_ERROR(EINVAL);
527 				break;
528 			}
529 
530 			fname = strchr(propname, '@') + 1;
531 			if (zfeature_lookup_name(fname, NULL) != 0) {
532 				error = SET_ERROR(EINVAL);
533 				break;
534 			}
535 
536 			has_feature = B_TRUE;
537 			break;
538 
539 		case ZPOOL_PROP_VERSION:
540 			error = nvpair_value_uint64(elem, &intval);
541 			if (!error &&
542 			    (intval < spa_version(spa) ||
543 			    intval > SPA_VERSION_BEFORE_FEATURES ||
544 			    has_feature))
545 				error = SET_ERROR(EINVAL);
546 			break;
547 
548 		case ZPOOL_PROP_DELEGATION:
549 		case ZPOOL_PROP_AUTOREPLACE:
550 		case ZPOOL_PROP_LISTSNAPS:
551 		case ZPOOL_PROP_AUTOEXPAND:
552 			error = nvpair_value_uint64(elem, &intval);
553 			if (!error && intval > 1)
554 				error = SET_ERROR(EINVAL);
555 			break;
556 
557 		case ZPOOL_PROP_MULTIHOST:
558 			error = nvpair_value_uint64(elem, &intval);
559 			if (!error && intval > 1)
560 				error = SET_ERROR(EINVAL);
561 
562 			if (!error && !spa_get_hostid())
563 				error = SET_ERROR(ENOTSUP);
564 
565 			break;
566 
567 		case ZPOOL_PROP_BOOTFS:
568 			/*
569 			 * If the pool version is less than SPA_VERSION_BOOTFS,
570 			 * or the pool is still being created (version == 0),
571 			 * the bootfs property cannot be set.
572 			 */
573 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
574 				error = SET_ERROR(ENOTSUP);
575 				break;
576 			}
577 
578 			/*
579 			 * Make sure the vdev config is bootable
580 			 */
581 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
582 				error = SET_ERROR(ENOTSUP);
583 				break;
584 			}
585 
586 			reset_bootfs = 1;
587 
588 			error = nvpair_value_string(elem, &strval);
589 
590 			if (!error) {
591 				objset_t *os;
592 				uint64_t propval;
593 
594 				if (strval == NULL || strval[0] == '\0') {
595 					objnum = zpool_prop_default_numeric(
596 					    ZPOOL_PROP_BOOTFS);
597 					break;
598 				}
599 
600 				error = dmu_objset_hold(strval, FTAG, &os);
601 				if (error != 0)
602 					break;
603 
604 				/*
605 				 * Must be ZPL, and its property settings
606 				 * must be supported.
607 				 */
608 
609 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
610 					error = SET_ERROR(ENOTSUP);
611 				} else if ((error =
612 				    dsl_prop_get_int_ds(dmu_objset_ds(os),
613 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
614 				    &propval)) == 0 &&
615 				    !BOOTFS_COMPRESS_VALID(propval)) {
616 					error = SET_ERROR(ENOTSUP);
617 				} else {
618 					objnum = dmu_objset_id(os);
619 				}
620 				dmu_objset_rele(os, FTAG);
621 			}
622 			break;
623 
624 		case ZPOOL_PROP_FAILUREMODE:
625 			error = nvpair_value_uint64(elem, &intval);
626 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
627 			    intval > ZIO_FAILURE_MODE_PANIC))
628 				error = SET_ERROR(EINVAL);
629 
630 			/*
631 			 * This is a special case which only occurs when
632 			 * the pool has completely failed. This allows
633 			 * the user to change the in-core failmode property
634 			 * without syncing it out to disk (I/Os might
635 			 * currently be blocked). We do this by returning
636 			 * EIO to the caller (spa_prop_set) to trick it
637 			 * into thinking we encountered a property validation
638 			 * error.
639 			 */
640 			if (!error && spa_suspended(spa)) {
641 				spa->spa_failmode = intval;
642 				error = SET_ERROR(EIO);
643 			}
644 			break;
645 
646 		case ZPOOL_PROP_CACHEFILE:
647 			if ((error = nvpair_value_string(elem, &strval)) != 0)
648 				break;
649 
650 			if (strval[0] == '\0')
651 				break;
652 
653 			if (strcmp(strval, "none") == 0)
654 				break;
655 
656 			if (strval[0] != '/') {
657 				error = SET_ERROR(EINVAL);
658 				break;
659 			}
660 
661 			slash = strrchr(strval, '/');
662 			ASSERT(slash != NULL);
663 
664 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
665 			    strcmp(slash, "/..") == 0)
666 				error = SET_ERROR(EINVAL);
667 			break;
668 
669 		case ZPOOL_PROP_COMMENT:
670 			if ((error = nvpair_value_string(elem, &strval)) != 0)
671 				break;
672 			for (check = strval; *check != '\0'; check++) {
673 				/*
674 				 * The kernel doesn't have an easy isprint()
675 				 * check.  For this kernel check, we merely
676 				 * check ASCII apart from DEL.  Fix this if
677 				 * there is an easy-to-use kernel isprint().
678 				 */
679 				if (*check >= 0x7f) {
680 					error = SET_ERROR(EINVAL);
681 					break;
682 				}
683 			}
684 			if (strlen(strval) > ZPROP_MAX_COMMENT)
685 				error = E2BIG;
686 			break;
687 
688 		case ZPOOL_PROP_DEDUPDITTO:
689 			if (spa_version(spa) < SPA_VERSION_DEDUP)
690 				error = SET_ERROR(ENOTSUP);
691 			else
692 				error = nvpair_value_uint64(elem, &intval);
693 			if (error == 0 &&
694 			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
695 				error = SET_ERROR(EINVAL);
696 			break;
697 		}
698 
699 		if (error)
700 			break;
701 	}
702 
703 	if (!error && reset_bootfs) {
704 		error = nvlist_remove(props,
705 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
706 
707 		if (!error) {
708 			error = nvlist_add_uint64(props,
709 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
710 		}
711 	}
712 
713 	return (error);
714 }
715 
716 void
717 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
718 {
719 	char *cachefile;
720 	spa_config_dirent_t *dp;
721 
722 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
723 	    &cachefile) != 0)
724 		return;
725 
726 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
727 	    KM_SLEEP);
728 
729 	if (cachefile[0] == '\0')
730 		dp->scd_path = spa_strdup(spa_config_path);
731 	else if (strcmp(cachefile, "none") == 0)
732 		dp->scd_path = NULL;
733 	else
734 		dp->scd_path = spa_strdup(cachefile);
735 
736 	list_insert_head(&spa->spa_config_list, dp);
737 	if (need_sync)
738 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
739 }
740 
741 int
742 spa_prop_set(spa_t *spa, nvlist_t *nvp)
743 {
744 	int error;
745 	nvpair_t *elem = NULL;
746 	boolean_t need_sync = B_FALSE;
747 
748 	if ((error = spa_prop_validate(spa, nvp)) != 0)
749 		return (error);
750 
751 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
752 		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
753 
754 		if (prop == ZPOOL_PROP_CACHEFILE ||
755 		    prop == ZPOOL_PROP_ALTROOT ||
756 		    prop == ZPOOL_PROP_READONLY)
757 			continue;
758 
759 		if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) {
760 			uint64_t ver;
761 
762 			if (prop == ZPOOL_PROP_VERSION) {
763 				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
764 			} else {
765 				ASSERT(zpool_prop_feature(nvpair_name(elem)));
766 				ver = SPA_VERSION_FEATURES;
767 				need_sync = B_TRUE;
768 			}
769 
770 			/* Save time if the version is already set. */
771 			if (ver == spa_version(spa))
772 				continue;
773 
774 			/*
775 			 * In addition to the pool directory object, we might
776 			 * create the pool properties object, the features for
777 			 * read object, the features for write object, or the
778 			 * feature descriptions object.
779 			 */
780 			error = dsl_sync_task(spa->spa_name, NULL,
781 			    spa_sync_version, &ver,
782 			    6, ZFS_SPACE_CHECK_RESERVED);
783 			if (error)
784 				return (error);
785 			continue;
786 		}
787 
788 		need_sync = B_TRUE;
789 		break;
790 	}
791 
792 	if (need_sync) {
793 		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
794 		    nvp, 6, ZFS_SPACE_CHECK_RESERVED));
795 	}
796 
797 	return (0);
798 }
799 
800 /*
801  * If the bootfs property value is dsobj, clear it.
802  */
803 void
804 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
805 {
806 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
807 		VERIFY(zap_remove(spa->spa_meta_objset,
808 		    spa->spa_pool_props_object,
809 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
810 		spa->spa_bootfs = 0;
811 	}
812 }
813 
814 /*ARGSUSED*/
815 static int
816 spa_change_guid_check(void *arg, dmu_tx_t *tx)
817 {
818 	uint64_t *newguid = arg;
819 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
820 	vdev_t *rvd = spa->spa_root_vdev;
821 	uint64_t vdev_state;
822 
823 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
824 		int error = (spa_has_checkpoint(spa)) ?
825 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
826 		return (SET_ERROR(error));
827 	}
828 
829 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
830 	vdev_state = rvd->vdev_state;
831 	spa_config_exit(spa, SCL_STATE, FTAG);
832 
833 	if (vdev_state != VDEV_STATE_HEALTHY)
834 		return (SET_ERROR(ENXIO));
835 
836 	ASSERT3U(spa_guid(spa), !=, *newguid);
837 
838 	return (0);
839 }
840 
841 static void
842 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
843 {
844 	uint64_t *newguid = arg;
845 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
846 	uint64_t oldguid;
847 	vdev_t *rvd = spa->spa_root_vdev;
848 
849 	oldguid = spa_guid(spa);
850 
851 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
852 	rvd->vdev_guid = *newguid;
853 	rvd->vdev_guid_sum += (*newguid - oldguid);
854 	vdev_config_dirty(rvd);
855 	spa_config_exit(spa, SCL_STATE, FTAG);
856 
857 	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
858 	    oldguid, *newguid);
859 }
860 
861 /*
862  * Change the GUID for the pool.  This is done so that we can later
863  * re-import a pool built from a clone of our own vdevs.  We will modify
864  * the root vdev's guid, our own pool guid, and then mark all of our
865  * vdevs dirty.  Note that we must make sure that all our vdevs are
866  * online when we do this, or else any vdevs that weren't present
867  * would be orphaned from our pool.  We are also going to issue a
868  * sysevent to update any watchers.
869  */
870 int
871 spa_change_guid(spa_t *spa)
872 {
873 	int error;
874 	uint64_t guid;
875 
876 	mutex_enter(&spa->spa_vdev_top_lock);
877 	mutex_enter(&spa_namespace_lock);
878 	guid = spa_generate_guid(NULL);
879 
880 	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
881 	    spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
882 
883 	if (error == 0) {
884 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
885 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID);
886 	}
887 
888 	mutex_exit(&spa_namespace_lock);
889 	mutex_exit(&spa->spa_vdev_top_lock);
890 
891 	return (error);
892 }
893 
894 /*
895  * ==========================================================================
896  * SPA state manipulation (open/create/destroy/import/export)
897  * ==========================================================================
898  */
899 
900 static int
901 spa_error_entry_compare(const void *a, const void *b)
902 {
903 	spa_error_entry_t *sa = (spa_error_entry_t *)a;
904 	spa_error_entry_t *sb = (spa_error_entry_t *)b;
905 	int ret;
906 
907 	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
908 	    sizeof (zbookmark_phys_t));
909 
910 	if (ret < 0)
911 		return (-1);
912 	else if (ret > 0)
913 		return (1);
914 	else
915 		return (0);
916 }
917 
918 /*
919  * Utility function which retrieves copies of the current logs and
920  * re-initializes them in the process.
921  */
922 void
923 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
924 {
925 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
926 
927 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
928 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
929 
930 	avl_create(&spa->spa_errlist_scrub,
931 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
932 	    offsetof(spa_error_entry_t, se_avl));
933 	avl_create(&spa->spa_errlist_last,
934 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
935 	    offsetof(spa_error_entry_t, se_avl));
936 }
937 
938 static void
939 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
940 {
941 	const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
942 	enum zti_modes mode = ztip->zti_mode;
943 	uint_t value = ztip->zti_value;
944 	uint_t count = ztip->zti_count;
945 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
946 	char name[32];
947 	uint_t flags = 0;
948 	boolean_t batch = B_FALSE;
949 
950 	if (mode == ZTI_MODE_NULL) {
951 		tqs->stqs_count = 0;
952 		tqs->stqs_taskq = NULL;
953 		return;
954 	}
955 
956 	ASSERT3U(count, >, 0);
957 
958 	tqs->stqs_count = count;
959 	tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
960 
961 	switch (mode) {
962 	case ZTI_MODE_FIXED:
963 		ASSERT3U(value, >=, 1);
964 		value = MAX(value, 1);
965 		break;
966 
967 	case ZTI_MODE_BATCH:
968 		batch = B_TRUE;
969 		flags |= TASKQ_THREADS_CPU_PCT;
970 		value = zio_taskq_batch_pct;
971 		break;
972 
973 	default:
974 		panic("unrecognized mode for %s_%s taskq (%u:%u) in "
975 		    "spa_activate()",
976 		    zio_type_name[t], zio_taskq_types[q], mode, value);
977 		break;
978 	}
979 
980 	for (uint_t i = 0; i < count; i++) {
981 		taskq_t *tq;
982 
983 		if (count > 1) {
984 			(void) snprintf(name, sizeof (name), "%s_%s_%u",
985 			    zio_type_name[t], zio_taskq_types[q], i);
986 		} else {
987 			(void) snprintf(name, sizeof (name), "%s_%s",
988 			    zio_type_name[t], zio_taskq_types[q]);
989 		}
990 
991 		if (zio_taskq_sysdc && spa->spa_proc != &p0) {
992 			if (batch)
993 				flags |= TASKQ_DC_BATCH;
994 
995 			tq = taskq_create_sysdc(name, value, 50, INT_MAX,
996 			    spa->spa_proc, zio_taskq_basedc, flags);
997 		} else {
998 			pri_t pri = maxclsyspri;
999 			/*
1000 			 * The write issue taskq can be extremely CPU
1001 			 * intensive.  Run it at slightly lower priority
1002 			 * than the other taskqs.
1003 			 */
1004 			if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
1005 				pri--;
1006 
1007 			tq = taskq_create_proc(name, value, pri, 50,
1008 			    INT_MAX, spa->spa_proc, flags);
1009 		}
1010 
1011 		tqs->stqs_taskq[i] = tq;
1012 	}
1013 }
1014 
1015 static void
1016 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
1017 {
1018 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1019 
1020 	if (tqs->stqs_taskq == NULL) {
1021 		ASSERT0(tqs->stqs_count);
1022 		return;
1023 	}
1024 
1025 	for (uint_t i = 0; i < tqs->stqs_count; i++) {
1026 		ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
1027 		taskq_destroy(tqs->stqs_taskq[i]);
1028 	}
1029 
1030 	kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
1031 	tqs->stqs_taskq = NULL;
1032 }
1033 
1034 /*
1035  * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1036  * Note that a type may have multiple discrete taskqs to avoid lock contention
1037  * on the taskq itself. In that case we choose which taskq at random by using
1038  * the low bits of gethrtime().
1039  */
1040 void
1041 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
1042     task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
1043 {
1044 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1045 	taskq_t *tq;
1046 
1047 	ASSERT3P(tqs->stqs_taskq, !=, NULL);
1048 	ASSERT3U(tqs->stqs_count, !=, 0);
1049 
1050 	if (tqs->stqs_count == 1) {
1051 		tq = tqs->stqs_taskq[0];
1052 	} else {
1053 		tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
1054 	}
1055 
1056 	taskq_dispatch_ent(tq, func, arg, flags, ent);
1057 }
1058 
1059 static void
1060 spa_create_zio_taskqs(spa_t *spa)
1061 {
1062 	for (int t = 0; t < ZIO_TYPES; t++) {
1063 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1064 			spa_taskqs_init(spa, t, q);
1065 		}
1066 	}
1067 }
1068 
1069 #ifdef _KERNEL
1070 static void
1071 spa_thread(void *arg)
1072 {
1073 	callb_cpr_t cprinfo;
1074 
1075 	spa_t *spa = arg;
1076 	user_t *pu = PTOU(curproc);
1077 
1078 	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1079 	    spa->spa_name);
1080 
1081 	ASSERT(curproc != &p0);
1082 	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1083 	    "zpool-%s", spa->spa_name);
1084 	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1085 
1086 	/* bind this thread to the requested psrset */
1087 	if (zio_taskq_psrset_bind != PS_NONE) {
1088 		pool_lock();
1089 		mutex_enter(&cpu_lock);
1090 		mutex_enter(&pidlock);
1091 		mutex_enter(&curproc->p_lock);
1092 
1093 		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1094 		    0, NULL, NULL) == 0)  {
1095 			curthread->t_bind_pset = zio_taskq_psrset_bind;
1096 		} else {
1097 			cmn_err(CE_WARN,
1098 			    "Couldn't bind process for zfs pool \"%s\" to "
1099 			    "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1100 		}
1101 
1102 		mutex_exit(&curproc->p_lock);
1103 		mutex_exit(&pidlock);
1104 		mutex_exit(&cpu_lock);
1105 		pool_unlock();
1106 	}
1107 
1108 	if (zio_taskq_sysdc) {
1109 		sysdc_thread_enter(curthread, 100, 0);
1110 	}
1111 
1112 	spa->spa_proc = curproc;
1113 	spa->spa_did = curthread->t_did;
1114 
1115 	spa_create_zio_taskqs(spa);
1116 
1117 	mutex_enter(&spa->spa_proc_lock);
1118 	ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1119 
1120 	spa->spa_proc_state = SPA_PROC_ACTIVE;
1121 	cv_broadcast(&spa->spa_proc_cv);
1122 
1123 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
1124 	while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1125 		cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1126 	CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1127 
1128 	ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1129 	spa->spa_proc_state = SPA_PROC_GONE;
1130 	spa->spa_proc = &p0;
1131 	cv_broadcast(&spa->spa_proc_cv);
1132 	CALLB_CPR_EXIT(&cprinfo);	/* drops spa_proc_lock */
1133 
1134 	mutex_enter(&curproc->p_lock);
1135 	lwp_exit();
1136 }
1137 #endif
1138 
1139 /*
1140  * Activate an uninitialized pool.
1141  */
1142 static void
1143 spa_activate(spa_t *spa, int mode)
1144 {
1145 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1146 
1147 	spa->spa_state = POOL_STATE_ACTIVE;
1148 	spa->spa_mode = mode;
1149 
1150 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1151 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1152 	spa->spa_special_class = metaslab_class_create(spa, zfs_metaslab_ops);
1153 	spa->spa_dedup_class = metaslab_class_create(spa, zfs_metaslab_ops);
1154 
1155 	/* Try to create a covering process */
1156 	mutex_enter(&spa->spa_proc_lock);
1157 	ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1158 	ASSERT(spa->spa_proc == &p0);
1159 	spa->spa_did = 0;
1160 
1161 	/* Only create a process if we're going to be around a while. */
1162 	if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1163 		if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1164 		    NULL, 0) == 0) {
1165 			spa->spa_proc_state = SPA_PROC_CREATED;
1166 			while (spa->spa_proc_state == SPA_PROC_CREATED) {
1167 				cv_wait(&spa->spa_proc_cv,
1168 				    &spa->spa_proc_lock);
1169 			}
1170 			ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1171 			ASSERT(spa->spa_proc != &p0);
1172 			ASSERT(spa->spa_did != 0);
1173 		} else {
1174 #ifdef _KERNEL
1175 			cmn_err(CE_WARN,
1176 			    "Couldn't create process for zfs pool \"%s\"\n",
1177 			    spa->spa_name);
1178 #endif
1179 		}
1180 	}
1181 	mutex_exit(&spa->spa_proc_lock);
1182 
1183 	/* If we didn't create a process, we need to create our taskqs. */
1184 	if (spa->spa_proc == &p0) {
1185 		spa_create_zio_taskqs(spa);
1186 	}
1187 
1188 	for (size_t i = 0; i < TXG_SIZE; i++) {
1189 		spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL,
1190 		    ZIO_FLAG_CANFAIL);
1191 	}
1192 
1193 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1194 	    offsetof(vdev_t, vdev_config_dirty_node));
1195 	list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1196 	    offsetof(objset_t, os_evicting_node));
1197 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1198 	    offsetof(vdev_t, vdev_state_dirty_node));
1199 
1200 	txg_list_create(&spa->spa_vdev_txg_list, spa,
1201 	    offsetof(struct vdev, vdev_txg_node));
1202 
1203 	avl_create(&spa->spa_errlist_scrub,
1204 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1205 	    offsetof(spa_error_entry_t, se_avl));
1206 	avl_create(&spa->spa_errlist_last,
1207 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1208 	    offsetof(spa_error_entry_t, se_avl));
1209 }
1210 
1211 /*
1212  * Opposite of spa_activate().
1213  */
1214 static void
1215 spa_deactivate(spa_t *spa)
1216 {
1217 	ASSERT(spa->spa_sync_on == B_FALSE);
1218 	ASSERT(spa->spa_dsl_pool == NULL);
1219 	ASSERT(spa->spa_root_vdev == NULL);
1220 	ASSERT(spa->spa_async_zio_root == NULL);
1221 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1222 
1223 	spa_evicting_os_wait(spa);
1224 
1225 	txg_list_destroy(&spa->spa_vdev_txg_list);
1226 
1227 	list_destroy(&spa->spa_config_dirty_list);
1228 	list_destroy(&spa->spa_evicting_os_list);
1229 	list_destroy(&spa->spa_state_dirty_list);
1230 
1231 	for (int t = 0; t < ZIO_TYPES; t++) {
1232 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1233 			spa_taskqs_fini(spa, t, q);
1234 		}
1235 	}
1236 
1237 	for (size_t i = 0; i < TXG_SIZE; i++) {
1238 		ASSERT3P(spa->spa_txg_zio[i], !=, NULL);
1239 		VERIFY0(zio_wait(spa->spa_txg_zio[i]));
1240 		spa->spa_txg_zio[i] = NULL;
1241 	}
1242 
1243 	metaslab_class_destroy(spa->spa_normal_class);
1244 	spa->spa_normal_class = NULL;
1245 
1246 	metaslab_class_destroy(spa->spa_log_class);
1247 	spa->spa_log_class = NULL;
1248 
1249 	metaslab_class_destroy(spa->spa_special_class);
1250 	spa->spa_special_class = NULL;
1251 
1252 	metaslab_class_destroy(spa->spa_dedup_class);
1253 	spa->spa_dedup_class = NULL;
1254 
1255 	/*
1256 	 * If this was part of an import or the open otherwise failed, we may
1257 	 * still have errors left in the queues.  Empty them just in case.
1258 	 */
1259 	spa_errlog_drain(spa);
1260 
1261 	avl_destroy(&spa->spa_errlist_scrub);
1262 	avl_destroy(&spa->spa_errlist_last);
1263 
1264 	spa->spa_state = POOL_STATE_UNINITIALIZED;
1265 
1266 	mutex_enter(&spa->spa_proc_lock);
1267 	if (spa->spa_proc_state != SPA_PROC_NONE) {
1268 		ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1269 		spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1270 		cv_broadcast(&spa->spa_proc_cv);
1271 		while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1272 			ASSERT(spa->spa_proc != &p0);
1273 			cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1274 		}
1275 		ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1276 		spa->spa_proc_state = SPA_PROC_NONE;
1277 	}
1278 	ASSERT(spa->spa_proc == &p0);
1279 	mutex_exit(&spa->spa_proc_lock);
1280 
1281 	/*
1282 	 * We want to make sure spa_thread() has actually exited the ZFS
1283 	 * module, so that the module can't be unloaded out from underneath
1284 	 * it.
1285 	 */
1286 	if (spa->spa_did != 0) {
1287 		thread_join(spa->spa_did);
1288 		spa->spa_did = 0;
1289 	}
1290 }
1291 
1292 /*
1293  * Verify a pool configuration, and construct the vdev tree appropriately.  This
1294  * will create all the necessary vdevs in the appropriate layout, with each vdev
1295  * in the CLOSED state.  This will prep the pool before open/creation/import.
1296  * All vdev validation is done by the vdev_alloc() routine.
1297  */
1298 static int
1299 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1300     uint_t id, int atype)
1301 {
1302 	nvlist_t **child;
1303 	uint_t children;
1304 	int error;
1305 
1306 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1307 		return (error);
1308 
1309 	if ((*vdp)->vdev_ops->vdev_op_leaf)
1310 		return (0);
1311 
1312 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1313 	    &child, &children);
1314 
1315 	if (error == ENOENT)
1316 		return (0);
1317 
1318 	if (error) {
1319 		vdev_free(*vdp);
1320 		*vdp = NULL;
1321 		return (SET_ERROR(EINVAL));
1322 	}
1323 
1324 	for (int c = 0; c < children; c++) {
1325 		vdev_t *vd;
1326 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1327 		    atype)) != 0) {
1328 			vdev_free(*vdp);
1329 			*vdp = NULL;
1330 			return (error);
1331 		}
1332 	}
1333 
1334 	ASSERT(*vdp != NULL);
1335 
1336 	return (0);
1337 }
1338 
1339 /*
1340  * Opposite of spa_load().
1341  */
1342 static void
1343 spa_unload(spa_t *spa)
1344 {
1345 	int i;
1346 
1347 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1348 
1349 	spa_load_note(spa, "UNLOADING");
1350 
1351 	/*
1352 	 * Stop async tasks.
1353 	 */
1354 	spa_async_suspend(spa);
1355 
1356 	if (spa->spa_root_vdev) {
1357 		vdev_initialize_stop_all(spa->spa_root_vdev,
1358 		    VDEV_INITIALIZE_ACTIVE);
1359 	}
1360 
1361 	/*
1362 	 * Stop syncing.
1363 	 */
1364 	if (spa->spa_sync_on) {
1365 		txg_sync_stop(spa->spa_dsl_pool);
1366 		spa->spa_sync_on = B_FALSE;
1367 	}
1368 
1369 	/*
1370 	 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1371 	 * to call it earlier, before we wait for async i/o to complete.
1372 	 * This ensures that there is no async metaslab prefetching, by
1373 	 * calling taskq_wait(mg_taskq).
1374 	 */
1375 	if (spa->spa_root_vdev != NULL) {
1376 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1377 		for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++)
1378 			vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]);
1379 		spa_config_exit(spa, SCL_ALL, spa);
1380 	}
1381 
1382 	if (spa->spa_mmp.mmp_thread)
1383 		mmp_thread_stop(spa);
1384 
1385 	/*
1386 	 * Wait for any outstanding async I/O to complete.
1387 	 */
1388 	if (spa->spa_async_zio_root != NULL) {
1389 		for (int i = 0; i < max_ncpus; i++)
1390 			(void) zio_wait(spa->spa_async_zio_root[i]);
1391 		kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1392 		spa->spa_async_zio_root = NULL;
1393 	}
1394 
1395 	if (spa->spa_vdev_removal != NULL) {
1396 		spa_vdev_removal_destroy(spa->spa_vdev_removal);
1397 		spa->spa_vdev_removal = NULL;
1398 	}
1399 
1400 	if (spa->spa_condense_zthr != NULL) {
1401 		ASSERT(!zthr_isrunning(spa->spa_condense_zthr));
1402 		zthr_destroy(spa->spa_condense_zthr);
1403 		spa->spa_condense_zthr = NULL;
1404 	}
1405 
1406 	if (spa->spa_checkpoint_discard_zthr != NULL) {
1407 		ASSERT(!zthr_isrunning(spa->spa_checkpoint_discard_zthr));
1408 		zthr_destroy(spa->spa_checkpoint_discard_zthr);
1409 		spa->spa_checkpoint_discard_zthr = NULL;
1410 	}
1411 
1412 	spa_condense_fini(spa);
1413 
1414 	bpobj_close(&spa->spa_deferred_bpobj);
1415 
1416 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1417 
1418 	/*
1419 	 * Close all vdevs.
1420 	 */
1421 	if (spa->spa_root_vdev)
1422 		vdev_free(spa->spa_root_vdev);
1423 	ASSERT(spa->spa_root_vdev == NULL);
1424 
1425 	/*
1426 	 * Close the dsl pool.
1427 	 */
1428 	if (spa->spa_dsl_pool) {
1429 		dsl_pool_close(spa->spa_dsl_pool);
1430 		spa->spa_dsl_pool = NULL;
1431 		spa->spa_meta_objset = NULL;
1432 	}
1433 
1434 	ddt_unload(spa);
1435 
1436 	/*
1437 	 * Drop and purge level 2 cache
1438 	 */
1439 	spa_l2cache_drop(spa);
1440 
1441 	for (i = 0; i < spa->spa_spares.sav_count; i++)
1442 		vdev_free(spa->spa_spares.sav_vdevs[i]);
1443 	if (spa->spa_spares.sav_vdevs) {
1444 		kmem_free(spa->spa_spares.sav_vdevs,
1445 		    spa->spa_spares.sav_count * sizeof (void *));
1446 		spa->spa_spares.sav_vdevs = NULL;
1447 	}
1448 	if (spa->spa_spares.sav_config) {
1449 		nvlist_free(spa->spa_spares.sav_config);
1450 		spa->spa_spares.sav_config = NULL;
1451 	}
1452 	spa->spa_spares.sav_count = 0;
1453 
1454 	for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1455 		vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1456 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1457 	}
1458 	if (spa->spa_l2cache.sav_vdevs) {
1459 		kmem_free(spa->spa_l2cache.sav_vdevs,
1460 		    spa->spa_l2cache.sav_count * sizeof (void *));
1461 		spa->spa_l2cache.sav_vdevs = NULL;
1462 	}
1463 	if (spa->spa_l2cache.sav_config) {
1464 		nvlist_free(spa->spa_l2cache.sav_config);
1465 		spa->spa_l2cache.sav_config = NULL;
1466 	}
1467 	spa->spa_l2cache.sav_count = 0;
1468 
1469 	spa->spa_async_suspended = 0;
1470 
1471 	spa->spa_indirect_vdevs_loaded = B_FALSE;
1472 
1473 	if (spa->spa_comment != NULL) {
1474 		spa_strfree(spa->spa_comment);
1475 		spa->spa_comment = NULL;
1476 	}
1477 
1478 	spa_config_exit(spa, SCL_ALL, spa);
1479 }
1480 
1481 /*
1482  * Load (or re-load) the current list of vdevs describing the active spares for
1483  * this pool.  When this is called, we have some form of basic information in
1484  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
1485  * then re-generate a more complete list including status information.
1486  */
1487 void
1488 spa_load_spares(spa_t *spa)
1489 {
1490 	nvlist_t **spares;
1491 	uint_t nspares;
1492 	int i;
1493 	vdev_t *vd, *tvd;
1494 
1495 #ifndef _KERNEL
1496 	/*
1497 	 * zdb opens both the current state of the pool and the
1498 	 * checkpointed state (if present), with a different spa_t.
1499 	 *
1500 	 * As spare vdevs are shared among open pools, we skip loading
1501 	 * them when we load the checkpointed state of the pool.
1502 	 */
1503 	if (!spa_writeable(spa))
1504 		return;
1505 #endif
1506 
1507 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1508 
1509 	/*
1510 	 * First, close and free any existing spare vdevs.
1511 	 */
1512 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
1513 		vd = spa->spa_spares.sav_vdevs[i];
1514 
1515 		/* Undo the call to spa_activate() below */
1516 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1517 		    B_FALSE)) != NULL && tvd->vdev_isspare)
1518 			spa_spare_remove(tvd);
1519 		vdev_close(vd);
1520 		vdev_free(vd);
1521 	}
1522 
1523 	if (spa->spa_spares.sav_vdevs)
1524 		kmem_free(spa->spa_spares.sav_vdevs,
1525 		    spa->spa_spares.sav_count * sizeof (void *));
1526 
1527 	if (spa->spa_spares.sav_config == NULL)
1528 		nspares = 0;
1529 	else
1530 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1531 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1532 
1533 	spa->spa_spares.sav_count = (int)nspares;
1534 	spa->spa_spares.sav_vdevs = NULL;
1535 
1536 	if (nspares == 0)
1537 		return;
1538 
1539 	/*
1540 	 * Construct the array of vdevs, opening them to get status in the
1541 	 * process.   For each spare, there is potentially two different vdev_t
1542 	 * structures associated with it: one in the list of spares (used only
1543 	 * for basic validation purposes) and one in the active vdev
1544 	 * configuration (if it's spared in).  During this phase we open and
1545 	 * validate each vdev on the spare list.  If the vdev also exists in the
1546 	 * active configuration, then we also mark this vdev as an active spare.
1547 	 */
1548 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1549 	    KM_SLEEP);
1550 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
1551 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1552 		    VDEV_ALLOC_SPARE) == 0);
1553 		ASSERT(vd != NULL);
1554 
1555 		spa->spa_spares.sav_vdevs[i] = vd;
1556 
1557 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1558 		    B_FALSE)) != NULL) {
1559 			if (!tvd->vdev_isspare)
1560 				spa_spare_add(tvd);
1561 
1562 			/*
1563 			 * We only mark the spare active if we were successfully
1564 			 * able to load the vdev.  Otherwise, importing a pool
1565 			 * with a bad active spare would result in strange
1566 			 * behavior, because multiple pool would think the spare
1567 			 * is actively in use.
1568 			 *
1569 			 * There is a vulnerability here to an equally bizarre
1570 			 * circumstance, where a dead active spare is later
1571 			 * brought back to life (onlined or otherwise).  Given
1572 			 * the rarity of this scenario, and the extra complexity
1573 			 * it adds, we ignore the possibility.
1574 			 */
1575 			if (!vdev_is_dead(tvd))
1576 				spa_spare_activate(tvd);
1577 		}
1578 
1579 		vd->vdev_top = vd;
1580 		vd->vdev_aux = &spa->spa_spares;
1581 
1582 		if (vdev_open(vd) != 0)
1583 			continue;
1584 
1585 		if (vdev_validate_aux(vd) == 0)
1586 			spa_spare_add(vd);
1587 	}
1588 
1589 	/*
1590 	 * Recompute the stashed list of spares, with status information
1591 	 * this time.
1592 	 */
1593 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1594 	    DATA_TYPE_NVLIST_ARRAY) == 0);
1595 
1596 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1597 	    KM_SLEEP);
1598 	for (i = 0; i < spa->spa_spares.sav_count; i++)
1599 		spares[i] = vdev_config_generate(spa,
1600 		    spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1601 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1602 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1603 	for (i = 0; i < spa->spa_spares.sav_count; i++)
1604 		nvlist_free(spares[i]);
1605 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1606 }
1607 
1608 /*
1609  * Load (or re-load) the current list of vdevs describing the active l2cache for
1610  * this pool.  When this is called, we have some form of basic information in
1611  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
1612  * then re-generate a more complete list including status information.
1613  * Devices which are already active have their details maintained, and are
1614  * not re-opened.
1615  */
1616 void
1617 spa_load_l2cache(spa_t *spa)
1618 {
1619 	nvlist_t **l2cache;
1620 	uint_t nl2cache;
1621 	int i, j, oldnvdevs;
1622 	uint64_t guid;
1623 	vdev_t *vd, **oldvdevs, **newvdevs;
1624 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
1625 
1626 #ifndef _KERNEL
1627 	/*
1628 	 * zdb opens both the current state of the pool and the
1629 	 * checkpointed state (if present), with a different spa_t.
1630 	 *
1631 	 * As L2 caches are part of the ARC which is shared among open
1632 	 * pools, we skip loading them when we load the checkpointed
1633 	 * state of the pool.
1634 	 */
1635 	if (!spa_writeable(spa))
1636 		return;
1637 #endif
1638 
1639 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1640 
1641 	if (sav->sav_config != NULL) {
1642 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1643 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1644 		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1645 	} else {
1646 		nl2cache = 0;
1647 		newvdevs = NULL;
1648 	}
1649 
1650 	oldvdevs = sav->sav_vdevs;
1651 	oldnvdevs = sav->sav_count;
1652 	sav->sav_vdevs = NULL;
1653 	sav->sav_count = 0;
1654 
1655 	/*
1656 	 * Process new nvlist of vdevs.
1657 	 */
1658 	for (i = 0; i < nl2cache; i++) {
1659 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1660 		    &guid) == 0);
1661 
1662 		newvdevs[i] = NULL;
1663 		for (j = 0; j < oldnvdevs; j++) {
1664 			vd = oldvdevs[j];
1665 			if (vd != NULL && guid == vd->vdev_guid) {
1666 				/*
1667 				 * Retain previous vdev for add/remove ops.
1668 				 */
1669 				newvdevs[i] = vd;
1670 				oldvdevs[j] = NULL;
1671 				break;
1672 			}
1673 		}
1674 
1675 		if (newvdevs[i] == NULL) {
1676 			/*
1677 			 * Create new vdev
1678 			 */
1679 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1680 			    VDEV_ALLOC_L2CACHE) == 0);
1681 			ASSERT(vd != NULL);
1682 			newvdevs[i] = vd;
1683 
1684 			/*
1685 			 * Commit this vdev as an l2cache device,
1686 			 * even if it fails to open.
1687 			 */
1688 			spa_l2cache_add(vd);
1689 
1690 			vd->vdev_top = vd;
1691 			vd->vdev_aux = sav;
1692 
1693 			spa_l2cache_activate(vd);
1694 
1695 			if (vdev_open(vd) != 0)
1696 				continue;
1697 
1698 			(void) vdev_validate_aux(vd);
1699 
1700 			if (!vdev_is_dead(vd))
1701 				l2arc_add_vdev(spa, vd);
1702 		}
1703 	}
1704 
1705 	/*
1706 	 * Purge vdevs that were dropped
1707 	 */
1708 	for (i = 0; i < oldnvdevs; i++) {
1709 		uint64_t pool;
1710 
1711 		vd = oldvdevs[i];
1712 		if (vd != NULL) {
1713 			ASSERT(vd->vdev_isl2cache);
1714 
1715 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1716 			    pool != 0ULL && l2arc_vdev_present(vd))
1717 				l2arc_remove_vdev(vd);
1718 			vdev_clear_stats(vd);
1719 			vdev_free(vd);
1720 		}
1721 	}
1722 
1723 	if (oldvdevs)
1724 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1725 
1726 	if (sav->sav_config == NULL)
1727 		goto out;
1728 
1729 	sav->sav_vdevs = newvdevs;
1730 	sav->sav_count = (int)nl2cache;
1731 
1732 	/*
1733 	 * Recompute the stashed list of l2cache devices, with status
1734 	 * information this time.
1735 	 */
1736 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1737 	    DATA_TYPE_NVLIST_ARRAY) == 0);
1738 
1739 	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1740 	for (i = 0; i < sav->sav_count; i++)
1741 		l2cache[i] = vdev_config_generate(spa,
1742 		    sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1743 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1744 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1745 out:
1746 	for (i = 0; i < sav->sav_count; i++)
1747 		nvlist_free(l2cache[i]);
1748 	if (sav->sav_count)
1749 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
1750 }
1751 
1752 static int
1753 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1754 {
1755 	dmu_buf_t *db;
1756 	char *packed = NULL;
1757 	size_t nvsize = 0;
1758 	int error;
1759 	*value = NULL;
1760 
1761 	error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1762 	if (error != 0)
1763 		return (error);
1764 
1765 	nvsize = *(uint64_t *)db->db_data;
1766 	dmu_buf_rele(db, FTAG);
1767 
1768 	packed = kmem_alloc(nvsize, KM_SLEEP);
1769 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1770 	    DMU_READ_PREFETCH);
1771 	if (error == 0)
1772 		error = nvlist_unpack(packed, nvsize, value, 0);
1773 	kmem_free(packed, nvsize);
1774 
1775 	return (error);
1776 }
1777 
1778 /*
1779  * Concrete top-level vdevs that are not missing and are not logs. At every
1780  * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1781  */
1782 static uint64_t
1783 spa_healthy_core_tvds(spa_t *spa)
1784 {
1785 	vdev_t *rvd = spa->spa_root_vdev;
1786 	uint64_t tvds = 0;
1787 
1788 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
1789 		vdev_t *vd = rvd->vdev_child[i];
1790 		if (vd->vdev_islog)
1791 			continue;
1792 		if (vdev_is_concrete(vd) && !vdev_is_dead(vd))
1793 			tvds++;
1794 	}
1795 
1796 	return (tvds);
1797 }
1798 
1799 /*
1800  * Checks to see if the given vdev could not be opened, in which case we post a
1801  * sysevent to notify the autoreplace code that the device has been removed.
1802  */
1803 static void
1804 spa_check_removed(vdev_t *vd)
1805 {
1806 	for (uint64_t c = 0; c < vd->vdev_children; c++)
1807 		spa_check_removed(vd->vdev_child[c]);
1808 
1809 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1810 	    vdev_is_concrete(vd)) {
1811 		zfs_post_autoreplace(vd->vdev_spa, vd);
1812 		spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK);
1813 	}
1814 }
1815 
1816 static int
1817 spa_check_for_missing_logs(spa_t *spa)
1818 {
1819 	vdev_t *rvd = spa->spa_root_vdev;
1820 
1821 	/*
1822 	 * If we're doing a normal import, then build up any additional
1823 	 * diagnostic information about missing log devices.
1824 	 * We'll pass this up to the user for further processing.
1825 	 */
1826 	if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1827 		nvlist_t **child, *nv;
1828 		uint64_t idx = 0;
1829 
1830 		child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1831 		    KM_SLEEP);
1832 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1833 
1834 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
1835 			vdev_t *tvd = rvd->vdev_child[c];
1836 
1837 			/*
1838 			 * We consider a device as missing only if it failed
1839 			 * to open (i.e. offline or faulted is not considered
1840 			 * as missing).
1841 			 */
1842 			if (tvd->vdev_islog &&
1843 			    tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
1844 				child[idx++] = vdev_config_generate(spa, tvd,
1845 				    B_FALSE, VDEV_CONFIG_MISSING);
1846 			}
1847 		}
1848 
1849 		if (idx > 0) {
1850 			fnvlist_add_nvlist_array(nv,
1851 			    ZPOOL_CONFIG_CHILDREN, child, idx);
1852 			fnvlist_add_nvlist(spa->spa_load_info,
1853 			    ZPOOL_CONFIG_MISSING_DEVICES, nv);
1854 
1855 			for (uint64_t i = 0; i < idx; i++)
1856 				nvlist_free(child[i]);
1857 		}
1858 		nvlist_free(nv);
1859 		kmem_free(child, rvd->vdev_children * sizeof (char **));
1860 
1861 		if (idx > 0) {
1862 			spa_load_failed(spa, "some log devices are missing");
1863 			vdev_dbgmsg_print_tree(rvd, 2);
1864 			return (SET_ERROR(ENXIO));
1865 		}
1866 	} else {
1867 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
1868 			vdev_t *tvd = rvd->vdev_child[c];
1869 
1870 			if (tvd->vdev_islog &&
1871 			    tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
1872 				spa_set_log_state(spa, SPA_LOG_CLEAR);
1873 				spa_load_note(spa, "some log devices are "
1874 				    "missing, ZIL is dropped.");
1875 				vdev_dbgmsg_print_tree(rvd, 2);
1876 				break;
1877 			}
1878 		}
1879 	}
1880 
1881 	return (0);
1882 }
1883 
1884 /*
1885  * Check for missing log devices
1886  */
1887 static boolean_t
1888 spa_check_logs(spa_t *spa)
1889 {
1890 	boolean_t rv = B_FALSE;
1891 	dsl_pool_t *dp = spa_get_dsl(spa);
1892 
1893 	switch (spa->spa_log_state) {
1894 	case SPA_LOG_MISSING:
1895 		/* need to recheck in case slog has been restored */
1896 	case SPA_LOG_UNKNOWN:
1897 		rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1898 		    zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1899 		if (rv)
1900 			spa_set_log_state(spa, SPA_LOG_MISSING);
1901 		break;
1902 	}
1903 	return (rv);
1904 }
1905 
1906 static boolean_t
1907 spa_passivate_log(spa_t *spa)
1908 {
1909 	vdev_t *rvd = spa->spa_root_vdev;
1910 	boolean_t slog_found = B_FALSE;
1911 
1912 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1913 
1914 	if (!spa_has_slogs(spa))
1915 		return (B_FALSE);
1916 
1917 	for (int c = 0; c < rvd->vdev_children; c++) {
1918 		vdev_t *tvd = rvd->vdev_child[c];
1919 		metaslab_group_t *mg = tvd->vdev_mg;
1920 
1921 		if (tvd->vdev_islog) {
1922 			metaslab_group_passivate(mg);
1923 			slog_found = B_TRUE;
1924 		}
1925 	}
1926 
1927 	return (slog_found);
1928 }
1929 
1930 static void
1931 spa_activate_log(spa_t *spa)
1932 {
1933 	vdev_t *rvd = spa->spa_root_vdev;
1934 
1935 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1936 
1937 	for (int c = 0; c < rvd->vdev_children; c++) {
1938 		vdev_t *tvd = rvd->vdev_child[c];
1939 		metaslab_group_t *mg = tvd->vdev_mg;
1940 
1941 		if (tvd->vdev_islog)
1942 			metaslab_group_activate(mg);
1943 	}
1944 }
1945 
1946 int
1947 spa_reset_logs(spa_t *spa)
1948 {
1949 	int error;
1950 
1951 	error = dmu_objset_find(spa_name(spa), zil_reset,
1952 	    NULL, DS_FIND_CHILDREN);
1953 	if (error == 0) {
1954 		/*
1955 		 * We successfully offlined the log device, sync out the
1956 		 * current txg so that the "stubby" block can be removed
1957 		 * by zil_sync().
1958 		 */
1959 		txg_wait_synced(spa->spa_dsl_pool, 0);
1960 	}
1961 	return (error);
1962 }
1963 
1964 static void
1965 spa_aux_check_removed(spa_aux_vdev_t *sav)
1966 {
1967 	for (int i = 0; i < sav->sav_count; i++)
1968 		spa_check_removed(sav->sav_vdevs[i]);
1969 }
1970 
1971 void
1972 spa_claim_notify(zio_t *zio)
1973 {
1974 	spa_t *spa = zio->io_spa;
1975 
1976 	if (zio->io_error)
1977 		return;
1978 
1979 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
1980 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1981 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1982 	mutex_exit(&spa->spa_props_lock);
1983 }
1984 
1985 typedef struct spa_load_error {
1986 	uint64_t	sle_meta_count;
1987 	uint64_t	sle_data_count;
1988 } spa_load_error_t;
1989 
1990 static void
1991 spa_load_verify_done(zio_t *zio)
1992 {
1993 	blkptr_t *bp = zio->io_bp;
1994 	spa_load_error_t *sle = zio->io_private;
1995 	dmu_object_type_t type = BP_GET_TYPE(bp);
1996 	int error = zio->io_error;
1997 	spa_t *spa = zio->io_spa;
1998 
1999 	abd_free(zio->io_abd);
2000 	if (error) {
2001 		if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
2002 		    type != DMU_OT_INTENT_LOG)
2003 			atomic_inc_64(&sle->sle_meta_count);
2004 		else
2005 			atomic_inc_64(&sle->sle_data_count);
2006 	}
2007 
2008 	mutex_enter(&spa->spa_scrub_lock);
2009 	spa->spa_scrub_inflight--;
2010 	cv_broadcast(&spa->spa_scrub_io_cv);
2011 	mutex_exit(&spa->spa_scrub_lock);
2012 }
2013 
2014 /*
2015  * Maximum number of concurrent scrub i/os to create while verifying
2016  * a pool while importing it.
2017  */
2018 int spa_load_verify_maxinflight = 10000;
2019 boolean_t spa_load_verify_metadata = B_TRUE;
2020 boolean_t spa_load_verify_data = B_TRUE;
2021 
2022 /*ARGSUSED*/
2023 static int
2024 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2025     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2026 {
2027 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2028 		return (0);
2029 	/*
2030 	 * Note: normally this routine will not be called if
2031 	 * spa_load_verify_metadata is not set.  However, it may be useful
2032 	 * to manually set the flag after the traversal has begun.
2033 	 */
2034 	if (!spa_load_verify_metadata)
2035 		return (0);
2036 	if (!BP_IS_METADATA(bp) && !spa_load_verify_data)
2037 		return (0);
2038 
2039 	zio_t *rio = arg;
2040 	size_t size = BP_GET_PSIZE(bp);
2041 
2042 	mutex_enter(&spa->spa_scrub_lock);
2043 	while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
2044 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2045 	spa->spa_scrub_inflight++;
2046 	mutex_exit(&spa->spa_scrub_lock);
2047 
2048 	zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size,
2049 	    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
2050 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
2051 	    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
2052 	return (0);
2053 }
2054 
2055 /* ARGSUSED */
2056 int
2057 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2058 {
2059 	if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
2060 		return (SET_ERROR(ENAMETOOLONG));
2061 
2062 	return (0);
2063 }
2064 
2065 static int
2066 spa_load_verify(spa_t *spa)
2067 {
2068 	zio_t *rio;
2069 	spa_load_error_t sle = { 0 };
2070 	zpool_load_policy_t policy;
2071 	boolean_t verify_ok = B_FALSE;
2072 	int error = 0;
2073 
2074 	zpool_get_load_policy(spa->spa_config, &policy);
2075 
2076 	if (policy.zlp_rewind & ZPOOL_NEVER_REWIND)
2077 		return (0);
2078 
2079 	dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2080 	error = dmu_objset_find_dp(spa->spa_dsl_pool,
2081 	    spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2082 	    DS_FIND_CHILDREN);
2083 	dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2084 	if (error != 0)
2085 		return (error);
2086 
2087 	rio = zio_root(spa, NULL, &sle,
2088 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
2089 
2090 	if (spa_load_verify_metadata) {
2091 		if (spa->spa_extreme_rewind) {
2092 			spa_load_note(spa, "performing a complete scan of the "
2093 			    "pool since extreme rewind is on. This may take "
2094 			    "a very long time.\n  (spa_load_verify_data=%u, "
2095 			    "spa_load_verify_metadata=%u)",
2096 			    spa_load_verify_data, spa_load_verify_metadata);
2097 		}
2098 		error = traverse_pool(spa, spa->spa_verify_min_txg,
2099 		    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2100 		    spa_load_verify_cb, rio);
2101 	}
2102 
2103 	(void) zio_wait(rio);
2104 
2105 	spa->spa_load_meta_errors = sle.sle_meta_count;
2106 	spa->spa_load_data_errors = sle.sle_data_count;
2107 
2108 	if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) {
2109 		spa_load_note(spa, "spa_load_verify found %llu metadata errors "
2110 		    "and %llu data errors", (u_longlong_t)sle.sle_meta_count,
2111 		    (u_longlong_t)sle.sle_data_count);
2112 	}
2113 
2114 	if (spa_load_verify_dryrun ||
2115 	    (!error && sle.sle_meta_count <= policy.zlp_maxmeta &&
2116 	    sle.sle_data_count <= policy.zlp_maxdata)) {
2117 		int64_t loss = 0;
2118 
2119 		verify_ok = B_TRUE;
2120 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2121 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2122 
2123 		loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2124 		VERIFY(nvlist_add_uint64(spa->spa_load_info,
2125 		    ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2126 		VERIFY(nvlist_add_int64(spa->spa_load_info,
2127 		    ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2128 		VERIFY(nvlist_add_uint64(spa->spa_load_info,
2129 		    ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2130 	} else {
2131 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2132 	}
2133 
2134 	if (spa_load_verify_dryrun)
2135 		return (0);
2136 
2137 	if (error) {
2138 		if (error != ENXIO && error != EIO)
2139 			error = SET_ERROR(EIO);
2140 		return (error);
2141 	}
2142 
2143 	return (verify_ok ? 0 : EIO);
2144 }
2145 
2146 /*
2147  * Find a value in the pool props object.
2148  */
2149 static void
2150 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2151 {
2152 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2153 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2154 }
2155 
2156 /*
2157  * Find a value in the pool directory object.
2158  */
2159 static int
2160 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent)
2161 {
2162 	int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2163 	    name, sizeof (uint64_t), 1, val);
2164 
2165 	if (error != 0 && (error != ENOENT || log_enoent)) {
2166 		spa_load_failed(spa, "couldn't get '%s' value in MOS directory "
2167 		    "[error=%d]", name, error);
2168 	}
2169 
2170 	return (error);
2171 }
2172 
2173 static int
2174 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2175 {
2176 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2177 	return (SET_ERROR(err));
2178 }
2179 
2180 static void
2181 spa_spawn_aux_threads(spa_t *spa)
2182 {
2183 	ASSERT(spa_writeable(spa));
2184 
2185 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2186 
2187 	spa_start_indirect_condensing_thread(spa);
2188 
2189 	ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL);
2190 	spa->spa_checkpoint_discard_zthr =
2191 	    zthr_create(spa_checkpoint_discard_thread_check,
2192 	    spa_checkpoint_discard_thread, spa);
2193 }
2194 
2195 /*
2196  * Fix up config after a partly-completed split.  This is done with the
2197  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
2198  * pool have that entry in their config, but only the splitting one contains
2199  * a list of all the guids of the vdevs that are being split off.
2200  *
2201  * This function determines what to do with that list: either rejoin
2202  * all the disks to the pool, or complete the splitting process.  To attempt
2203  * the rejoin, each disk that is offlined is marked online again, and
2204  * we do a reopen() call.  If the vdev label for every disk that was
2205  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2206  * then we call vdev_split() on each disk, and complete the split.
2207  *
2208  * Otherwise we leave the config alone, with all the vdevs in place in
2209  * the original pool.
2210  */
2211 static void
2212 spa_try_repair(spa_t *spa, nvlist_t *config)
2213 {
2214 	uint_t extracted;
2215 	uint64_t *glist;
2216 	uint_t i, gcount;
2217 	nvlist_t *nvl;
2218 	vdev_t **vd;
2219 	boolean_t attempt_reopen;
2220 
2221 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2222 		return;
2223 
2224 	/* check that the config is complete */
2225 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2226 	    &glist, &gcount) != 0)
2227 		return;
2228 
2229 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2230 
2231 	/* attempt to online all the vdevs & validate */
2232 	attempt_reopen = B_TRUE;
2233 	for (i = 0; i < gcount; i++) {
2234 		if (glist[i] == 0)	/* vdev is hole */
2235 			continue;
2236 
2237 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2238 		if (vd[i] == NULL) {
2239 			/*
2240 			 * Don't bother attempting to reopen the disks;
2241 			 * just do the split.
2242 			 */
2243 			attempt_reopen = B_FALSE;
2244 		} else {
2245 			/* attempt to re-online it */
2246 			vd[i]->vdev_offline = B_FALSE;
2247 		}
2248 	}
2249 
2250 	if (attempt_reopen) {
2251 		vdev_reopen(spa->spa_root_vdev);
2252 
2253 		/* check each device to see what state it's in */
2254 		for (extracted = 0, i = 0; i < gcount; i++) {
2255 			if (vd[i] != NULL &&
2256 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2257 				break;
2258 			++extracted;
2259 		}
2260 	}
2261 
2262 	/*
2263 	 * If every disk has been moved to the new pool, or if we never
2264 	 * even attempted to look at them, then we split them off for
2265 	 * good.
2266 	 */
2267 	if (!attempt_reopen || gcount == extracted) {
2268 		for (i = 0; i < gcount; i++)
2269 			if (vd[i] != NULL)
2270 				vdev_split(vd[i]);
2271 		vdev_reopen(spa->spa_root_vdev);
2272 	}
2273 
2274 	kmem_free(vd, gcount * sizeof (vdev_t *));
2275 }
2276 
2277 static int
2278 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type)
2279 {
2280 	char *ereport = FM_EREPORT_ZFS_POOL;
2281 	int error;
2282 
2283 	spa->spa_load_state = state;
2284 
2285 	gethrestime(&spa->spa_loaded_ts);
2286 	error = spa_load_impl(spa, type, &ereport);
2287 
2288 	/*
2289 	 * Don't count references from objsets that are already closed
2290 	 * and are making their way through the eviction process.
2291 	 */
2292 	spa_evicting_os_wait(spa);
2293 	spa->spa_minref = zfs_refcount_count(&spa->spa_refcount);
2294 	if (error) {
2295 		if (error != EEXIST) {
2296 			spa->spa_loaded_ts.tv_sec = 0;
2297 			spa->spa_loaded_ts.tv_nsec = 0;
2298 		}
2299 		if (error != EBADF) {
2300 			zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2301 		}
2302 	}
2303 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2304 	spa->spa_ena = 0;
2305 
2306 	return (error);
2307 }
2308 
2309 /*
2310  * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2311  * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2312  * spa's per-vdev ZAP list.
2313  */
2314 static uint64_t
2315 vdev_count_verify_zaps(vdev_t *vd)
2316 {
2317 	spa_t *spa = vd->vdev_spa;
2318 	uint64_t total = 0;
2319 	if (vd->vdev_top_zap != 0) {
2320 		total++;
2321 		ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2322 		    spa->spa_all_vdev_zaps, vd->vdev_top_zap));
2323 	}
2324 	if (vd->vdev_leaf_zap != 0) {
2325 		total++;
2326 		ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2327 		    spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
2328 	}
2329 
2330 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
2331 		total += vdev_count_verify_zaps(vd->vdev_child[i]);
2332 	}
2333 
2334 	return (total);
2335 }
2336 
2337 /*
2338  * Determine whether the activity check is required.
2339  */
2340 static boolean_t
2341 spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label,
2342     nvlist_t *config)
2343 {
2344 	uint64_t state = 0;
2345 	uint64_t hostid = 0;
2346 	uint64_t tryconfig_txg = 0;
2347 	uint64_t tryconfig_timestamp = 0;
2348 	nvlist_t *nvinfo;
2349 
2350 	if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
2351 		nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2352 		(void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG,
2353 		    &tryconfig_txg);
2354 		(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
2355 		    &tryconfig_timestamp);
2356 	}
2357 
2358 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state);
2359 
2360 	/*
2361 	 * Disable the MMP activity check - This is used by zdb which
2362 	 * is intended to be used on potentially active pools.
2363 	 */
2364 	if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP)
2365 		return (B_FALSE);
2366 
2367 	/*
2368 	 * Skip the activity check when the MMP feature is disabled.
2369 	 */
2370 	if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0)
2371 		return (B_FALSE);
2372 	/*
2373 	 * If the tryconfig_* values are nonzero, they are the results of an
2374 	 * earlier tryimport.  If they match the uberblock we just found, then
2375 	 * the pool has not changed and we return false so we do not test a
2376 	 * second time.
2377 	 */
2378 	if (tryconfig_txg && tryconfig_txg == ub->ub_txg &&
2379 	    tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp)
2380 		return (B_FALSE);
2381 
2382 	/*
2383 	 * Allow the activity check to be skipped when importing the pool
2384 	 * on the same host which last imported it.  Since the hostid from
2385 	 * configuration may be stale use the one read from the label.
2386 	 */
2387 	if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID))
2388 		hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID);
2389 
2390 	if (hostid == spa_get_hostid())
2391 		return (B_FALSE);
2392 
2393 	/*
2394 	 * Skip the activity test when the pool was cleanly exported.
2395 	 */
2396 	if (state != POOL_STATE_ACTIVE)
2397 		return (B_FALSE);
2398 
2399 	return (B_TRUE);
2400 }
2401 
2402 /*
2403  * Perform the import activity check.  If the user canceled the import or
2404  * we detected activity then fail.
2405  */
2406 static int
2407 spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config)
2408 {
2409 	uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1);
2410 	uint64_t txg = ub->ub_txg;
2411 	uint64_t timestamp = ub->ub_timestamp;
2412 	uint64_t import_delay = NANOSEC;
2413 	hrtime_t import_expire;
2414 	nvlist_t *mmp_label = NULL;
2415 	vdev_t *rvd = spa->spa_root_vdev;
2416 	kcondvar_t cv;
2417 	kmutex_t mtx;
2418 	int error = 0;
2419 
2420 	cv_init(&cv, NULL, CV_DEFAULT, NULL);
2421 	mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL);
2422 	mutex_enter(&mtx);
2423 
2424 	/*
2425 	 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2426 	 * during the earlier tryimport.  If the txg recorded there is 0 then
2427 	 * the pool is known to be active on another host.
2428 	 *
2429 	 * Otherwise, the pool might be in use on another node.  Check for
2430 	 * changes in the uberblocks on disk if necessary.
2431 	 */
2432 	if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
2433 		nvlist_t *nvinfo = fnvlist_lookup_nvlist(config,
2434 		    ZPOOL_CONFIG_LOAD_INFO);
2435 
2436 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) &&
2437 		    fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) {
2438 			vdev_uberblock_load(rvd, ub, &mmp_label);
2439 			error = SET_ERROR(EREMOTEIO);
2440 			goto out;
2441 		}
2442 	}
2443 
2444 	/*
2445 	 * Preferentially use the zfs_multihost_interval from the node which
2446 	 * last imported the pool.  This value is stored in an MMP uberblock as.
2447 	 *
2448 	 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2449 	 */
2450 	if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay)
2451 		import_delay = MAX(import_delay, import_intervals *
2452 		    ub->ub_mmp_delay * MAX(vdev_count_leaves(spa), 1));
2453 
2454 	/* Apply a floor using the local default values. */
2455 	import_delay = MAX(import_delay, import_intervals *
2456 	    MSEC2NSEC(MAX(zfs_multihost_interval, MMP_MIN_INTERVAL)));
2457 
2458 	zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u "
2459 	    "leaves=%u", import_delay, ub->ub_mmp_delay, import_intervals,
2460 	    vdev_count_leaves(spa));
2461 
2462 	/* Add a small random factor in case of simultaneous imports (0-25%) */
2463 	import_expire = gethrtime() + import_delay +
2464 	    (import_delay * spa_get_random(250) / 1000);
2465 
2466 	while (gethrtime() < import_expire) {
2467 		vdev_uberblock_load(rvd, ub, &mmp_label);
2468 
2469 		if (txg != ub->ub_txg || timestamp != ub->ub_timestamp) {
2470 			error = SET_ERROR(EREMOTEIO);
2471 			break;
2472 		}
2473 
2474 		if (mmp_label) {
2475 			nvlist_free(mmp_label);
2476 			mmp_label = NULL;
2477 		}
2478 
2479 		error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz);
2480 		if (error != -1) {
2481 			error = SET_ERROR(EINTR);
2482 			break;
2483 		}
2484 		error = 0;
2485 	}
2486 
2487 out:
2488 	mutex_exit(&mtx);
2489 	mutex_destroy(&mtx);
2490 	cv_destroy(&cv);
2491 
2492 	/*
2493 	 * If the pool is determined to be active store the status in the
2494 	 * spa->spa_load_info nvlist.  If the remote hostname or hostid are
2495 	 * available from configuration read from disk store them as well.
2496 	 * This allows 'zpool import' to generate a more useful message.
2497 	 *
2498 	 * ZPOOL_CONFIG_MMP_STATE    - observed pool status (mandatory)
2499 	 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2500 	 * ZPOOL_CONFIG_MMP_HOSTID   - hostid from the active pool
2501 	 */
2502 	if (error == EREMOTEIO) {
2503 		char *hostname = "<unknown>";
2504 		uint64_t hostid = 0;
2505 
2506 		if (mmp_label) {
2507 			if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) {
2508 				hostname = fnvlist_lookup_string(mmp_label,
2509 				    ZPOOL_CONFIG_HOSTNAME);
2510 				fnvlist_add_string(spa->spa_load_info,
2511 				    ZPOOL_CONFIG_MMP_HOSTNAME, hostname);
2512 			}
2513 
2514 			if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) {
2515 				hostid = fnvlist_lookup_uint64(mmp_label,
2516 				    ZPOOL_CONFIG_HOSTID);
2517 				fnvlist_add_uint64(spa->spa_load_info,
2518 				    ZPOOL_CONFIG_MMP_HOSTID, hostid);
2519 			}
2520 		}
2521 
2522 		fnvlist_add_uint64(spa->spa_load_info,
2523 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE);
2524 		fnvlist_add_uint64(spa->spa_load_info,
2525 		    ZPOOL_CONFIG_MMP_TXG, 0);
2526 
2527 		error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO);
2528 	}
2529 
2530 	if (mmp_label)
2531 		nvlist_free(mmp_label);
2532 
2533 	return (error);
2534 }
2535 
2536 static int
2537 spa_verify_host(spa_t *spa, nvlist_t *mos_config)
2538 {
2539 	uint64_t hostid;
2540 	char *hostname;
2541 	uint64_t myhostid = 0;
2542 
2543 	if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config,
2544 	    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2545 		hostname = fnvlist_lookup_string(mos_config,
2546 		    ZPOOL_CONFIG_HOSTNAME);
2547 
2548 		myhostid = zone_get_hostid(NULL);
2549 
2550 		if (hostid != 0 && myhostid != 0 && hostid != myhostid) {
2551 			cmn_err(CE_WARN, "pool '%s' could not be "
2552 			    "loaded as it was last accessed by "
2553 			    "another system (host: %s hostid: 0x%llx). "
2554 			    "See: http://illumos.org/msg/ZFS-8000-EY",
2555 			    spa_name(spa), hostname, (u_longlong_t)hostid);
2556 			spa_load_failed(spa, "hostid verification failed: pool "
2557 			    "last accessed by host: %s (hostid: 0x%llx)",
2558 			    hostname, (u_longlong_t)hostid);
2559 			return (SET_ERROR(EBADF));
2560 		}
2561 	}
2562 
2563 	return (0);
2564 }
2565 
2566 static int
2567 spa_ld_parse_config(spa_t *spa, spa_import_type_t type)
2568 {
2569 	int error = 0;
2570 	nvlist_t *nvtree, *nvl, *config = spa->spa_config;
2571 	int parse;
2572 	vdev_t *rvd;
2573 	uint64_t pool_guid;
2574 	char *comment;
2575 
2576 	/*
2577 	 * Versioning wasn't explicitly added to the label until later, so if
2578 	 * it's not present treat it as the initial version.
2579 	 */
2580 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2581 	    &spa->spa_ubsync.ub_version) != 0)
2582 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2583 
2584 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
2585 		spa_load_failed(spa, "invalid config provided: '%s' missing",
2586 		    ZPOOL_CONFIG_POOL_GUID);
2587 		return (SET_ERROR(EINVAL));
2588 	}
2589 
2590 	/*
2591 	 * If we are doing an import, ensure that the pool is not already
2592 	 * imported by checking if its pool guid already exists in the
2593 	 * spa namespace.
2594 	 *
2595 	 * The only case that we allow an already imported pool to be
2596 	 * imported again, is when the pool is checkpointed and we want to
2597 	 * look at its checkpointed state from userland tools like zdb.
2598 	 */
2599 #ifdef _KERNEL
2600 	if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
2601 	    spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
2602 	    spa_guid_exists(pool_guid, 0)) {
2603 #else
2604 	if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
2605 	    spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
2606 	    spa_guid_exists(pool_guid, 0) &&
2607 	    !spa_importing_readonly_checkpoint(spa)) {
2608 #endif
2609 		spa_load_failed(spa, "a pool with guid %llu is already open",
2610 		    (u_longlong_t)pool_guid);
2611 		return (SET_ERROR(EEXIST));
2612 	}
2613 
2614 	spa->spa_config_guid = pool_guid;
2615 
2616 	nvlist_free(spa->spa_load_info);
2617 	spa->spa_load_info = fnvlist_alloc();
2618 
2619 	ASSERT(spa->spa_comment == NULL);
2620 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2621 		spa->spa_comment = spa_strdup(comment);
2622 
2623 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2624 	    &spa->spa_config_txg);
2625 
2626 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0)
2627 		spa->spa_config_splitting = fnvlist_dup(nvl);
2628 
2629 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) {
2630 		spa_load_failed(spa, "invalid config provided: '%s' missing",
2631 		    ZPOOL_CONFIG_VDEV_TREE);
2632 		return (SET_ERROR(EINVAL));
2633 	}
2634 
2635 	/*
2636 	 * Create "The Godfather" zio to hold all async IOs
2637 	 */
2638 	spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2639 	    KM_SLEEP);
2640 	for (int i = 0; i < max_ncpus; i++) {
2641 		spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2642 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2643 		    ZIO_FLAG_GODFATHER);
2644 	}
2645 
2646 	/*
2647 	 * Parse the configuration into a vdev tree.  We explicitly set the
2648 	 * value that will be returned by spa_version() since parsing the
2649 	 * configuration requires knowing the version number.
2650 	 */
2651 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2652 	parse = (type == SPA_IMPORT_EXISTING ?
2653 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2654 	error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse);
2655 	spa_config_exit(spa, SCL_ALL, FTAG);
2656 
2657 	if (error != 0) {
2658 		spa_load_failed(spa, "unable to parse config [error=%d]",
2659 		    error);
2660 		return (error);
2661 	}
2662 
2663 	ASSERT(spa->spa_root_vdev == rvd);
2664 	ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2665 	ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2666 
2667 	if (type != SPA_IMPORT_ASSEMBLE) {
2668 		ASSERT(spa_guid(spa) == pool_guid);
2669 	}
2670 
2671 	return (0);
2672 }
2673 
2674 /*
2675  * Recursively open all vdevs in the vdev tree. This function is called twice:
2676  * first with the untrusted config, then with the trusted config.
2677  */
2678 static int
2679 spa_ld_open_vdevs(spa_t *spa)
2680 {
2681 	int error = 0;
2682 
2683 	/*
2684 	 * spa_missing_tvds_allowed defines how many top-level vdevs can be
2685 	 * missing/unopenable for the root vdev to be still considered openable.
2686 	 */
2687 	if (spa->spa_trust_config) {
2688 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds;
2689 	} else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) {
2690 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile;
2691 	} else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) {
2692 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan;
2693 	} else {
2694 		spa->spa_missing_tvds_allowed = 0;
2695 	}
2696 
2697 	spa->spa_missing_tvds_allowed =
2698 	    MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed);
2699 
2700 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2701 	error = vdev_open(spa->spa_root_vdev);
2702 	spa_config_exit(spa, SCL_ALL, FTAG);
2703 
2704 	if (spa->spa_missing_tvds != 0) {
2705 		spa_load_note(spa, "vdev tree has %lld missing top-level "
2706 		    "vdevs.", (u_longlong_t)spa->spa_missing_tvds);
2707 		if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) {
2708 			/*
2709 			 * Although theoretically we could allow users to open
2710 			 * incomplete pools in RW mode, we'd need to add a lot
2711 			 * of extra logic (e.g. adjust pool space to account
2712 			 * for missing vdevs).
2713 			 * This limitation also prevents users from accidentally
2714 			 * opening the pool in RW mode during data recovery and
2715 			 * damaging it further.
2716 			 */
2717 			spa_load_note(spa, "pools with missing top-level "
2718 			    "vdevs can only be opened in read-only mode.");
2719 			error = SET_ERROR(ENXIO);
2720 		} else {
2721 			spa_load_note(spa, "current settings allow for maximum "
2722 			    "%lld missing top-level vdevs at this stage.",
2723 			    (u_longlong_t)spa->spa_missing_tvds_allowed);
2724 		}
2725 	}
2726 	if (error != 0) {
2727 		spa_load_failed(spa, "unable to open vdev tree [error=%d]",
2728 		    error);
2729 	}
2730 	if (spa->spa_missing_tvds != 0 || error != 0)
2731 		vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2);
2732 
2733 	return (error);
2734 }
2735 
2736 /*
2737  * We need to validate the vdev labels against the configuration that
2738  * we have in hand. This function is called twice: first with an untrusted
2739  * config, then with a trusted config. The validation is more strict when the
2740  * config is trusted.
2741  */
2742 static int
2743 spa_ld_validate_vdevs(spa_t *spa)
2744 {
2745 	int error = 0;
2746 	vdev_t *rvd = spa->spa_root_vdev;
2747 
2748 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2749 	error = vdev_validate(rvd);
2750 	spa_config_exit(spa, SCL_ALL, FTAG);
2751 
2752 	if (error != 0) {
2753 		spa_load_failed(spa, "vdev_validate failed [error=%d]", error);
2754 		return (error);
2755 	}
2756 
2757 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
2758 		spa_load_failed(spa, "cannot open vdev tree after invalidating "
2759 		    "some vdevs");
2760 		vdev_dbgmsg_print_tree(rvd, 2);
2761 		return (SET_ERROR(ENXIO));
2762 	}
2763 
2764 	return (0);
2765 }
2766 
2767 static void
2768 spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub)
2769 {
2770 	spa->spa_state = POOL_STATE_ACTIVE;
2771 	spa->spa_ubsync = spa->spa_uberblock;
2772 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2773 	    TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2774 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2775 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2776 	spa->spa_claim_max_txg = spa->spa_first_txg;
2777 	spa->spa_prev_software_version = ub->ub_software_version;
2778 }
2779 
2780 static int
2781 spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type)
2782 {
2783 	vdev_t *rvd = spa->spa_root_vdev;
2784 	nvlist_t *label;
2785 	uberblock_t *ub = &spa->spa_uberblock;
2786 	boolean_t activity_check = B_FALSE;
2787 
2788 	/*
2789 	 * If we are opening the checkpointed state of the pool by
2790 	 * rewinding to it, at this point we will have written the
2791 	 * checkpointed uberblock to the vdev labels, so searching
2792 	 * the labels will find the right uberblock.  However, if
2793 	 * we are opening the checkpointed state read-only, we have
2794 	 * not modified the labels. Therefore, we must ignore the
2795 	 * labels and continue using the spa_uberblock that was set
2796 	 * by spa_ld_checkpoint_rewind.
2797 	 *
2798 	 * Note that it would be fine to ignore the labels when
2799 	 * rewinding (opening writeable) as well. However, if we
2800 	 * crash just after writing the labels, we will end up
2801 	 * searching the labels. Doing so in the common case means
2802 	 * that this code path gets exercised normally, rather than
2803 	 * just in the edge case.
2804 	 */
2805 	if (ub->ub_checkpoint_txg != 0 &&
2806 	    spa_importing_readonly_checkpoint(spa)) {
2807 		spa_ld_select_uberblock_done(spa, ub);
2808 		return (0);
2809 	}
2810 
2811 	/*
2812 	 * Find the best uberblock.
2813 	 */
2814 	vdev_uberblock_load(rvd, ub, &label);
2815 
2816 	/*
2817 	 * If we weren't able to find a single valid uberblock, return failure.
2818 	 */
2819 	if (ub->ub_txg == 0) {
2820 		nvlist_free(label);
2821 		spa_load_failed(spa, "no valid uberblock found");
2822 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2823 	}
2824 
2825 	spa_load_note(spa, "using uberblock with txg=%llu",
2826 	    (u_longlong_t)ub->ub_txg);
2827 
2828 	/*
2829 	 * For pools which have the multihost property on determine if the
2830 	 * pool is truly inactive and can be safely imported.  Prevent
2831 	 * hosts which don't have a hostid set from importing the pool.
2832 	 */
2833 	activity_check = spa_activity_check_required(spa, ub, label,
2834 	    spa->spa_config);
2835 	if (activity_check) {
2836 		if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay &&
2837 		    spa_get_hostid() == 0) {
2838 			nvlist_free(label);
2839 			fnvlist_add_uint64(spa->spa_load_info,
2840 			    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
2841 			return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
2842 		}
2843 
2844 		int error = spa_activity_check(spa, ub, spa->spa_config);
2845 		if (error) {
2846 			nvlist_free(label);
2847 			return (error);
2848 		}
2849 
2850 		fnvlist_add_uint64(spa->spa_load_info,
2851 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE);
2852 		fnvlist_add_uint64(spa->spa_load_info,
2853 		    ZPOOL_CONFIG_MMP_TXG, ub->ub_txg);
2854 	}
2855 
2856 	/*
2857 	 * If the pool has an unsupported version we can't open it.
2858 	 */
2859 	if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2860 		nvlist_free(label);
2861 		spa_load_failed(spa, "version %llu is not supported",
2862 		    (u_longlong_t)ub->ub_version);
2863 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2864 	}
2865 
2866 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
2867 		nvlist_t *features;
2868 
2869 		/*
2870 		 * If we weren't able to find what's necessary for reading the
2871 		 * MOS in the label, return failure.
2872 		 */
2873 		if (label == NULL) {
2874 			spa_load_failed(spa, "label config unavailable");
2875 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2876 			    ENXIO));
2877 		}
2878 
2879 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ,
2880 		    &features) != 0) {
2881 			nvlist_free(label);
2882 			spa_load_failed(spa, "invalid label: '%s' missing",
2883 			    ZPOOL_CONFIG_FEATURES_FOR_READ);
2884 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2885 			    ENXIO));
2886 		}
2887 
2888 		/*
2889 		 * Update our in-core representation with the definitive values
2890 		 * from the label.
2891 		 */
2892 		nvlist_free(spa->spa_label_features);
2893 		VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2894 	}
2895 
2896 	nvlist_free(label);
2897 
2898 	/*
2899 	 * Look through entries in the label nvlist's features_for_read. If
2900 	 * there is a feature listed there which we don't understand then we
2901 	 * cannot open a pool.
2902 	 */
2903 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
2904 		nvlist_t *unsup_feat;
2905 
2906 		VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2907 		    0);
2908 
2909 		for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2910 		    NULL); nvp != NULL;
2911 		    nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2912 			if (!zfeature_is_supported(nvpair_name(nvp))) {
2913 				VERIFY(nvlist_add_string(unsup_feat,
2914 				    nvpair_name(nvp), "") == 0);
2915 			}
2916 		}
2917 
2918 		if (!nvlist_empty(unsup_feat)) {
2919 			VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2920 			    ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2921 			nvlist_free(unsup_feat);
2922 			spa_load_failed(spa, "some features are unsupported");
2923 			return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2924 			    ENOTSUP));
2925 		}
2926 
2927 		nvlist_free(unsup_feat);
2928 	}
2929 
2930 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2931 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2932 		spa_try_repair(spa, spa->spa_config);
2933 		spa_config_exit(spa, SCL_ALL, FTAG);
2934 		nvlist_free(spa->spa_config_splitting);
2935 		spa->spa_config_splitting = NULL;
2936 	}
2937 
2938 	/*
2939 	 * Initialize internal SPA structures.
2940 	 */
2941 	spa_ld_select_uberblock_done(spa, ub);
2942 
2943 	return (0);
2944 }
2945 
2946 static int
2947 spa_ld_open_rootbp(spa_t *spa)
2948 {
2949 	int error = 0;
2950 	vdev_t *rvd = spa->spa_root_vdev;
2951 
2952 	error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2953 	if (error != 0) {
2954 		spa_load_failed(spa, "unable to open rootbp in dsl_pool_init "
2955 		    "[error=%d]", error);
2956 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2957 	}
2958 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2959 
2960 	return (0);
2961 }
2962 
2963 static int
2964 spa_ld_trusted_config(spa_t *spa, spa_import_type_t type,
2965     boolean_t reloading)
2966 {
2967 	vdev_t *mrvd, *rvd = spa->spa_root_vdev;
2968 	nvlist_t *nv, *mos_config, *policy;
2969 	int error = 0, copy_error;
2970 	uint64_t healthy_tvds, healthy_tvds_mos;
2971 	uint64_t mos_config_txg;
2972 
2973 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE)
2974 	    != 0)
2975 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2976 
2977 	/*
2978 	 * If we're assembling a pool from a split, the config provided is
2979 	 * already trusted so there is nothing to do.
2980 	 */
2981 	if (type == SPA_IMPORT_ASSEMBLE)
2982 		return (0);
2983 
2984 	healthy_tvds = spa_healthy_core_tvds(spa);
2985 
2986 	if (load_nvlist(spa, spa->spa_config_object, &mos_config)
2987 	    != 0) {
2988 		spa_load_failed(spa, "unable to retrieve MOS config");
2989 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2990 	}
2991 
2992 	/*
2993 	 * If we are doing an open, pool owner wasn't verified yet, thus do
2994 	 * the verification here.
2995 	 */
2996 	if (spa->spa_load_state == SPA_LOAD_OPEN) {
2997 		error = spa_verify_host(spa, mos_config);
2998 		if (error != 0) {
2999 			nvlist_free(mos_config);
3000 			return (error);
3001 		}
3002 	}
3003 
3004 	nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE);
3005 
3006 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3007 
3008 	/*
3009 	 * Build a new vdev tree from the trusted config
3010 	 */
3011 	VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
3012 
3013 	/*
3014 	 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3015 	 * obtained by scanning /dev/dsk, then it will have the right vdev
3016 	 * paths. We update the trusted MOS config with this information.
3017 	 * We first try to copy the paths with vdev_copy_path_strict, which
3018 	 * succeeds only when both configs have exactly the same vdev tree.
3019 	 * If that fails, we fall back to a more flexible method that has a
3020 	 * best effort policy.
3021 	 */
3022 	copy_error = vdev_copy_path_strict(rvd, mrvd);
3023 	if (copy_error != 0 || spa_load_print_vdev_tree) {
3024 		spa_load_note(spa, "provided vdev tree:");
3025 		vdev_dbgmsg_print_tree(rvd, 2);
3026 		spa_load_note(spa, "MOS vdev tree:");
3027 		vdev_dbgmsg_print_tree(mrvd, 2);
3028 	}
3029 	if (copy_error != 0) {
3030 		spa_load_note(spa, "vdev_copy_path_strict failed, falling "
3031 		    "back to vdev_copy_path_relaxed");
3032 		vdev_copy_path_relaxed(rvd, mrvd);
3033 	}
3034 
3035 	vdev_close(rvd);
3036 	vdev_free(rvd);
3037 	spa->spa_root_vdev = mrvd;
3038 	rvd = mrvd;
3039 	spa_config_exit(spa, SCL_ALL, FTAG);
3040 
3041 	/*
3042 	 * We will use spa_config if we decide to reload the spa or if spa_load
3043 	 * fails and we rewind. We must thus regenerate the config using the
3044 	 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3045 	 * pass settings on how to load the pool and is not stored in the MOS.
3046 	 * We copy it over to our new, trusted config.
3047 	 */
3048 	mos_config_txg = fnvlist_lookup_uint64(mos_config,
3049 	    ZPOOL_CONFIG_POOL_TXG);
3050 	nvlist_free(mos_config);
3051 	mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE);
3052 	if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY,
3053 	    &policy) == 0)
3054 		fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy);
3055 	spa_config_set(spa, mos_config);
3056 	spa->spa_config_source = SPA_CONFIG_SRC_MOS;
3057 
3058 	/*
3059 	 * Now that we got the config from the MOS, we should be more strict
3060 	 * in checking blkptrs and can make assumptions about the consistency
3061 	 * of the vdev tree. spa_trust_config must be set to true before opening
3062 	 * vdevs in order for them to be writeable.
3063 	 */
3064 	spa->spa_trust_config = B_TRUE;
3065 
3066 	/*
3067 	 * Open and validate the new vdev tree
3068 	 */
3069 	error = spa_ld_open_vdevs(spa);
3070 	if (error != 0)
3071 		return (error);
3072 
3073 	error = spa_ld_validate_vdevs(spa);
3074 	if (error != 0)
3075 		return (error);
3076 
3077 	if (copy_error != 0 || spa_load_print_vdev_tree) {
3078 		spa_load_note(spa, "final vdev tree:");
3079 		vdev_dbgmsg_print_tree(rvd, 2);
3080 	}
3081 
3082 	if (spa->spa_load_state != SPA_LOAD_TRYIMPORT &&
3083 	    !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) {
3084 		/*
3085 		 * Sanity check to make sure that we are indeed loading the
3086 		 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3087 		 * in the config provided and they happened to be the only ones
3088 		 * to have the latest uberblock, we could involuntarily perform
3089 		 * an extreme rewind.
3090 		 */
3091 		healthy_tvds_mos = spa_healthy_core_tvds(spa);
3092 		if (healthy_tvds_mos - healthy_tvds >=
3093 		    SPA_SYNC_MIN_VDEVS) {
3094 			spa_load_note(spa, "config provided misses too many "
3095 			    "top-level vdevs compared to MOS (%lld vs %lld). ",
3096 			    (u_longlong_t)healthy_tvds,
3097 			    (u_longlong_t)healthy_tvds_mos);
3098 			spa_load_note(spa, "vdev tree:");
3099 			vdev_dbgmsg_print_tree(rvd, 2);
3100 			if (reloading) {
3101 				spa_load_failed(spa, "config was already "
3102 				    "provided from MOS. Aborting.");
3103 				return (spa_vdev_err(rvd,
3104 				    VDEV_AUX_CORRUPT_DATA, EIO));
3105 			}
3106 			spa_load_note(spa, "spa must be reloaded using MOS "
3107 			    "config");
3108 			return (SET_ERROR(EAGAIN));
3109 		}
3110 	}
3111 
3112 	error = spa_check_for_missing_logs(spa);
3113 	if (error != 0)
3114 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
3115 
3116 	if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) {
3117 		spa_load_failed(spa, "uberblock guid sum doesn't match MOS "
3118 		    "guid sum (%llu != %llu)",
3119 		    (u_longlong_t)spa->spa_uberblock.ub_guid_sum,
3120 		    (u_longlong_t)rvd->vdev_guid_sum);
3121 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
3122 		    ENXIO));
3123 	}
3124 
3125 	return (0);
3126 }
3127 
3128 static int
3129 spa_ld_open_indirect_vdev_metadata(spa_t *spa)
3130 {
3131 	int error = 0;
3132 	vdev_t *rvd = spa->spa_root_vdev;
3133 
3134 	/*
3135 	 * Everything that we read before spa_remove_init() must be stored
3136 	 * on concreted vdevs.  Therefore we do this as early as possible.
3137 	 */
3138 	error = spa_remove_init(spa);
3139 	if (error != 0) {
3140 		spa_load_failed(spa, "spa_remove_init failed [error=%d]",
3141 		    error);
3142 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3143 	}
3144 
3145 	/*
3146 	 * Retrieve information needed to condense indirect vdev mappings.
3147 	 */
3148 	error = spa_condense_init(spa);
3149 	if (error != 0) {
3150 		spa_load_failed(spa, "spa_condense_init failed [error=%d]",
3151 		    error);
3152 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
3153 	}
3154 
3155 	return (0);
3156 }
3157 
3158 static int
3159 spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep)
3160 {
3161 	int error = 0;
3162 	vdev_t *rvd = spa->spa_root_vdev;
3163 
3164 	if (spa_version(spa) >= SPA_VERSION_FEATURES) {
3165 		boolean_t missing_feat_read = B_FALSE;
3166 		nvlist_t *unsup_feat, *enabled_feat;
3167 
3168 		if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
3169 		    &spa->spa_feat_for_read_obj, B_TRUE) != 0) {
3170 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3171 		}
3172 
3173 		if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
3174 		    &spa->spa_feat_for_write_obj, B_TRUE) != 0) {
3175 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3176 		}
3177 
3178 		if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
3179 		    &spa->spa_feat_desc_obj, B_TRUE) != 0) {
3180 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3181 		}
3182 
3183 		enabled_feat = fnvlist_alloc();
3184 		unsup_feat = fnvlist_alloc();
3185 
3186 		if (!spa_features_check(spa, B_FALSE,
3187 		    unsup_feat, enabled_feat))
3188 			missing_feat_read = B_TRUE;
3189 
3190 		if (spa_writeable(spa) ||
3191 		    spa->spa_load_state == SPA_LOAD_TRYIMPORT) {
3192 			if (!spa_features_check(spa, B_TRUE,
3193 			    unsup_feat, enabled_feat)) {
3194 				*missing_feat_writep = B_TRUE;
3195 			}
3196 		}
3197 
3198 		fnvlist_add_nvlist(spa->spa_load_info,
3199 		    ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
3200 
3201 		if (!nvlist_empty(unsup_feat)) {
3202 			fnvlist_add_nvlist(spa->spa_load_info,
3203 			    ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
3204 		}
3205 
3206 		fnvlist_free(enabled_feat);
3207 		fnvlist_free(unsup_feat);
3208 
3209 		if (!missing_feat_read) {
3210 			fnvlist_add_boolean(spa->spa_load_info,
3211 			    ZPOOL_CONFIG_CAN_RDONLY);
3212 		}
3213 
3214 		/*
3215 		 * If the state is SPA_LOAD_TRYIMPORT, our objective is
3216 		 * twofold: to determine whether the pool is available for
3217 		 * import in read-write mode and (if it is not) whether the
3218 		 * pool is available for import in read-only mode. If the pool
3219 		 * is available for import in read-write mode, it is displayed
3220 		 * as available in userland; if it is not available for import
3221 		 * in read-only mode, it is displayed as unavailable in
3222 		 * userland. If the pool is available for import in read-only
3223 		 * mode but not read-write mode, it is displayed as unavailable
3224 		 * in userland with a special note that the pool is actually
3225 		 * available for open in read-only mode.
3226 		 *
3227 		 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
3228 		 * missing a feature for write, we must first determine whether
3229 		 * the pool can be opened read-only before returning to
3230 		 * userland in order to know whether to display the
3231 		 * abovementioned note.
3232 		 */
3233 		if (missing_feat_read || (*missing_feat_writep &&
3234 		    spa_writeable(spa))) {
3235 			spa_load_failed(spa, "pool uses unsupported features");
3236 			return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
3237 			    ENOTSUP));
3238 		}
3239 
3240 		/*
3241 		 * Load refcounts for ZFS features from disk into an in-memory
3242 		 * cache during SPA initialization.
3243 		 */
3244 		for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
3245 			uint64_t refcount;
3246 
3247 			error = feature_get_refcount_from_disk(spa,
3248 			    &spa_feature_table[i], &refcount);
3249 			if (error == 0) {
3250 				spa->spa_feat_refcount_cache[i] = refcount;
3251 			} else if (error == ENOTSUP) {
3252 				spa->spa_feat_refcount_cache[i] =
3253 				    SPA_FEATURE_DISABLED;
3254 			} else {
3255 				spa_load_failed(spa, "error getting refcount "
3256 				    "for feature %s [error=%d]",
3257 				    spa_feature_table[i].fi_guid, error);
3258 				return (spa_vdev_err(rvd,
3259 				    VDEV_AUX_CORRUPT_DATA, EIO));
3260 			}
3261 		}
3262 	}
3263 
3264 	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
3265 		if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
3266 		    &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0)
3267 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3268 	}
3269 
3270 	return (0);
3271 }
3272 
3273 static int
3274 spa_ld_load_special_directories(spa_t *spa)
3275 {
3276 	int error = 0;
3277 	vdev_t *rvd = spa->spa_root_vdev;
3278 
3279 	spa->spa_is_initializing = B_TRUE;
3280 	error = dsl_pool_open(spa->spa_dsl_pool);
3281 	spa->spa_is_initializing = B_FALSE;
3282 	if (error != 0) {
3283 		spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error);
3284 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3285 	}
3286 
3287 	return (0);
3288 }
3289 
3290 static int
3291 spa_ld_get_props(spa_t *spa)
3292 {
3293 	int error = 0;
3294 	uint64_t obj;
3295 	vdev_t *rvd = spa->spa_root_vdev;
3296 
3297 	/* Grab the secret checksum salt from the MOS. */
3298 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3299 	    DMU_POOL_CHECKSUM_SALT, 1,
3300 	    sizeof (spa->spa_cksum_salt.zcs_bytes),
3301 	    spa->spa_cksum_salt.zcs_bytes);
3302 	if (error == ENOENT) {
3303 		/* Generate a new salt for subsequent use */
3304 		(void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3305 		    sizeof (spa->spa_cksum_salt.zcs_bytes));
3306 	} else if (error != 0) {
3307 		spa_load_failed(spa, "unable to retrieve checksum salt from "
3308 		    "MOS [error=%d]", error);
3309 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3310 	}
3311 
3312 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0)
3313 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3314 	error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
3315 	if (error != 0) {
3316 		spa_load_failed(spa, "error opening deferred-frees bpobj "
3317 		    "[error=%d]", error);
3318 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3319 	}
3320 
3321 	/*
3322 	 * Load the bit that tells us to use the new accounting function
3323 	 * (raid-z deflation).  If we have an older pool, this will not
3324 	 * be present.
3325 	 */
3326 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE);
3327 	if (error != 0 && error != ENOENT)
3328 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3329 
3330 	error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
3331 	    &spa->spa_creation_version, B_FALSE);
3332 	if (error != 0 && error != ENOENT)
3333 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3334 
3335 	/*
3336 	 * Load the persistent error log.  If we have an older pool, this will
3337 	 * not be present.
3338 	 */
3339 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last,
3340 	    B_FALSE);
3341 	if (error != 0 && error != ENOENT)
3342 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3343 
3344 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
3345 	    &spa->spa_errlog_scrub, B_FALSE);
3346 	if (error != 0 && error != ENOENT)
3347 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3348 
3349 	/*
3350 	 * Load the history object.  If we have an older pool, this
3351 	 * will not be present.
3352 	 */
3353 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE);
3354 	if (error != 0 && error != ENOENT)
3355 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3356 
3357 	/*
3358 	 * Load the per-vdev ZAP map. If we have an older pool, this will not
3359 	 * be present; in this case, defer its creation to a later time to
3360 	 * avoid dirtying the MOS this early / out of sync context. See
3361 	 * spa_sync_config_object.
3362 	 */
3363 
3364 	/* The sentinel is only available in the MOS config. */
3365 	nvlist_t *mos_config;
3366 	if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) {
3367 		spa_load_failed(spa, "unable to retrieve MOS config");
3368 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3369 	}
3370 
3371 	error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP,
3372 	    &spa->spa_all_vdev_zaps, B_FALSE);
3373 
3374 	if (error == ENOENT) {
3375 		VERIFY(!nvlist_exists(mos_config,
3376 		    ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
3377 		spa->spa_avz_action = AVZ_ACTION_INITIALIZE;
3378 		ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
3379 	} else if (error != 0) {
3380 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3381 	} else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) {
3382 		/*
3383 		 * An older version of ZFS overwrote the sentinel value, so
3384 		 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3385 		 * destruction to later; see spa_sync_config_object.
3386 		 */
3387 		spa->spa_avz_action = AVZ_ACTION_DESTROY;
3388 		/*
3389 		 * We're assuming that no vdevs have had their ZAPs created
3390 		 * before this. Better be sure of it.
3391 		 */
3392 		ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
3393 	}
3394 	nvlist_free(mos_config);
3395 
3396 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3397 
3398 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object,
3399 	    B_FALSE);
3400 	if (error && error != ENOENT)
3401 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3402 
3403 	if (error == 0) {
3404 		uint64_t autoreplace;
3405 
3406 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
3407 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
3408 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
3409 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
3410 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
3411 		spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost);
3412 		spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
3413 		    &spa->spa_dedup_ditto);
3414 
3415 		spa->spa_autoreplace = (autoreplace != 0);
3416 	}
3417 
3418 	/*
3419 	 * If we are importing a pool with missing top-level vdevs,
3420 	 * we enforce that the pool doesn't panic or get suspended on
3421 	 * error since the likelihood of missing data is extremely high.
3422 	 */
3423 	if (spa->spa_missing_tvds > 0 &&
3424 	    spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE &&
3425 	    spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
3426 		spa_load_note(spa, "forcing failmode to 'continue' "
3427 		    "as some top level vdevs are missing");
3428 		spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE;
3429 	}
3430 
3431 	return (0);
3432 }
3433 
3434 static int
3435 spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type)
3436 {
3437 	int error = 0;
3438 	vdev_t *rvd = spa->spa_root_vdev;
3439 
3440 	/*
3441 	 * If we're assembling the pool from the split-off vdevs of
3442 	 * an existing pool, we don't want to attach the spares & cache
3443 	 * devices.
3444 	 */
3445 
3446 	/*
3447 	 * Load any hot spares for this pool.
3448 	 */
3449 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object,
3450 	    B_FALSE);
3451 	if (error != 0 && error != ENOENT)
3452 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3453 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
3454 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
3455 		if (load_nvlist(spa, spa->spa_spares.sav_object,
3456 		    &spa->spa_spares.sav_config) != 0) {
3457 			spa_load_failed(spa, "error loading spares nvlist");
3458 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3459 		}
3460 
3461 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3462 		spa_load_spares(spa);
3463 		spa_config_exit(spa, SCL_ALL, FTAG);
3464 	} else if (error == 0) {
3465 		spa->spa_spares.sav_sync = B_TRUE;
3466 	}
3467 
3468 	/*
3469 	 * Load any level 2 ARC devices for this pool.
3470 	 */
3471 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
3472 	    &spa->spa_l2cache.sav_object, B_FALSE);
3473 	if (error != 0 && error != ENOENT)
3474 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3475 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
3476 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
3477 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
3478 		    &spa->spa_l2cache.sav_config) != 0) {
3479 			spa_load_failed(spa, "error loading l2cache nvlist");
3480 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3481 		}
3482 
3483 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3484 		spa_load_l2cache(spa);
3485 		spa_config_exit(spa, SCL_ALL, FTAG);
3486 	} else if (error == 0) {
3487 		spa->spa_l2cache.sav_sync = B_TRUE;
3488 	}
3489 
3490 	return (0);
3491 }
3492 
3493 static int
3494 spa_ld_load_vdev_metadata(spa_t *spa)
3495 {
3496 	int error = 0;
3497 	vdev_t *rvd = spa->spa_root_vdev;
3498 
3499 	/*
3500 	 * If the 'multihost' property is set, then never allow a pool to
3501 	 * be imported when the system hostid is zero.  The exception to
3502 	 * this rule is zdb which is always allowed to access pools.
3503 	 */
3504 	if (spa_multihost(spa) && spa_get_hostid() == 0 &&
3505 	    (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) {
3506 		fnvlist_add_uint64(spa->spa_load_info,
3507 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
3508 		return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
3509 	}
3510 
3511 	/*
3512 	 * If the 'autoreplace' property is set, then post a resource notifying
3513 	 * the ZFS DE that it should not issue any faults for unopenable
3514 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
3515 	 * unopenable vdevs so that the normal autoreplace handler can take
3516 	 * over.
3517 	 */
3518 	if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
3519 		spa_check_removed(spa->spa_root_vdev);
3520 		/*
3521 		 * For the import case, this is done in spa_import(), because
3522 		 * at this point we're using the spare definitions from
3523 		 * the MOS config, not necessarily from the userland config.
3524 		 */
3525 		if (spa->spa_load_state != SPA_LOAD_IMPORT) {
3526 			spa_aux_check_removed(&spa->spa_spares);
3527 			spa_aux_check_removed(&spa->spa_l2cache);
3528 		}
3529 	}
3530 
3531 	/*
3532 	 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
3533 	 */
3534 	error = vdev_load(rvd);
3535 	if (error != 0) {
3536 		spa_load_failed(spa, "vdev_load failed [error=%d]", error);
3537 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
3538 	}
3539 
3540 	/*
3541 	 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
3542 	 */
3543 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3544 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
3545 	spa_config_exit(spa, SCL_ALL, FTAG);
3546 
3547 	return (0);
3548 }
3549 
3550 static int
3551 spa_ld_load_dedup_tables(spa_t *spa)
3552 {
3553 	int error = 0;
3554 	vdev_t *rvd = spa->spa_root_vdev;
3555 
3556 	error = ddt_load(spa);
3557 	if (error != 0) {
3558 		spa_load_failed(spa, "ddt_load failed [error=%d]", error);
3559 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3560 	}
3561 
3562 	return (0);
3563 }
3564 
3565 static int
3566 spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport)
3567 {
3568 	vdev_t *rvd = spa->spa_root_vdev;
3569 
3570 	if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) {
3571 		boolean_t missing = spa_check_logs(spa);
3572 		if (missing) {
3573 			if (spa->spa_missing_tvds != 0) {
3574 				spa_load_note(spa, "spa_check_logs failed "
3575 				    "so dropping the logs");
3576 			} else {
3577 				*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
3578 				spa_load_failed(spa, "spa_check_logs failed");
3579 				return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG,
3580 				    ENXIO));
3581 			}
3582 		}
3583 	}
3584 
3585 	return (0);
3586 }
3587 
3588 static int
3589 spa_ld_verify_pool_data(spa_t *spa)
3590 {
3591 	int error = 0;
3592 	vdev_t *rvd = spa->spa_root_vdev;
3593 
3594 	/*
3595 	 * We've successfully opened the pool, verify that we're ready
3596 	 * to start pushing transactions.
3597 	 */
3598 	if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
3599 		error = spa_load_verify(spa);
3600 		if (error != 0) {
3601 			spa_load_failed(spa, "spa_load_verify failed "
3602 			    "[error=%d]", error);
3603 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
3604 			    error));
3605 		}
3606 	}
3607 
3608 	return (0);
3609 }
3610 
3611 static void
3612 spa_ld_claim_log_blocks(spa_t *spa)
3613 {
3614 	dmu_tx_t *tx;
3615 	dsl_pool_t *dp = spa_get_dsl(spa);
3616 
3617 	/*
3618 	 * Claim log blocks that haven't been committed yet.
3619 	 * This must all happen in a single txg.
3620 	 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3621 	 * invoked from zil_claim_log_block()'s i/o done callback.
3622 	 * Price of rollback is that we abandon the log.
3623 	 */
3624 	spa->spa_claiming = B_TRUE;
3625 
3626 	tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
3627 	(void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
3628 	    zil_claim, tx, DS_FIND_CHILDREN);
3629 	dmu_tx_commit(tx);
3630 
3631 	spa->spa_claiming = B_FALSE;
3632 
3633 	spa_set_log_state(spa, SPA_LOG_GOOD);
3634 }
3635 
3636 static void
3637 spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg,
3638     boolean_t update_config_cache)
3639 {
3640 	vdev_t *rvd = spa->spa_root_vdev;
3641 	int need_update = B_FALSE;
3642 
3643 	/*
3644 	 * If the config cache is stale, or we have uninitialized
3645 	 * metaslabs (see spa_vdev_add()), then update the config.
3646 	 *
3647 	 * If this is a verbatim import, trust the current
3648 	 * in-core spa_config and update the disk labels.
3649 	 */
3650 	if (update_config_cache || config_cache_txg != spa->spa_config_txg ||
3651 	    spa->spa_load_state == SPA_LOAD_IMPORT ||
3652 	    spa->spa_load_state == SPA_LOAD_RECOVER ||
3653 	    (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
3654 		need_update = B_TRUE;
3655 
3656 	for (int c = 0; c < rvd->vdev_children; c++)
3657 		if (rvd->vdev_child[c]->vdev_ms_array == 0)
3658 			need_update = B_TRUE;
3659 
3660 	/*
3661 	 * Update the config cache asychronously in case we're the
3662 	 * root pool, in which case the config cache isn't writable yet.
3663 	 */
3664 	if (need_update)
3665 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3666 }
3667 
3668 static void
3669 spa_ld_prepare_for_reload(spa_t *spa)
3670 {
3671 	int mode = spa->spa_mode;
3672 	int async_suspended = spa->spa_async_suspended;
3673 
3674 	spa_unload(spa);
3675 	spa_deactivate(spa);
3676 	spa_activate(spa, mode);
3677 
3678 	/*
3679 	 * We save the value of spa_async_suspended as it gets reset to 0 by
3680 	 * spa_unload(). We want to restore it back to the original value before
3681 	 * returning as we might be calling spa_async_resume() later.
3682 	 */
3683 	spa->spa_async_suspended = async_suspended;
3684 }
3685 
3686 static int
3687 spa_ld_read_checkpoint_txg(spa_t *spa)
3688 {
3689 	uberblock_t checkpoint;
3690 	int error = 0;
3691 
3692 	ASSERT0(spa->spa_checkpoint_txg);
3693 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3694 
3695 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3696 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
3697 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
3698 
3699 	if (error == ENOENT)
3700 		return (0);
3701 
3702 	if (error != 0)
3703 		return (error);
3704 
3705 	ASSERT3U(checkpoint.ub_txg, !=, 0);
3706 	ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0);
3707 	ASSERT3U(checkpoint.ub_timestamp, !=, 0);
3708 	spa->spa_checkpoint_txg = checkpoint.ub_txg;
3709 	spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp;
3710 
3711 	return (0);
3712 }
3713 
3714 static int
3715 spa_ld_mos_init(spa_t *spa, spa_import_type_t type)
3716 {
3717 	int error = 0;
3718 
3719 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3720 	ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE);
3721 
3722 	/*
3723 	 * Never trust the config that is provided unless we are assembling
3724 	 * a pool following a split.
3725 	 * This means don't trust blkptrs and the vdev tree in general. This
3726 	 * also effectively puts the spa in read-only mode since
3727 	 * spa_writeable() checks for spa_trust_config to be true.
3728 	 * We will later load a trusted config from the MOS.
3729 	 */
3730 	if (type != SPA_IMPORT_ASSEMBLE)
3731 		spa->spa_trust_config = B_FALSE;
3732 
3733 	/*
3734 	 * Parse the config provided to create a vdev tree.
3735 	 */
3736 	error = spa_ld_parse_config(spa, type);
3737 	if (error != 0)
3738 		return (error);
3739 
3740 	/*
3741 	 * Now that we have the vdev tree, try to open each vdev. This involves
3742 	 * opening the underlying physical device, retrieving its geometry and
3743 	 * probing the vdev with a dummy I/O. The state of each vdev will be set
3744 	 * based on the success of those operations. After this we'll be ready
3745 	 * to read from the vdevs.
3746 	 */
3747 	error = spa_ld_open_vdevs(spa);
3748 	if (error != 0)
3749 		return (error);
3750 
3751 	/*
3752 	 * Read the label of each vdev and make sure that the GUIDs stored
3753 	 * there match the GUIDs in the config provided.
3754 	 * If we're assembling a new pool that's been split off from an
3755 	 * existing pool, the labels haven't yet been updated so we skip
3756 	 * validation for now.
3757 	 */
3758 	if (type != SPA_IMPORT_ASSEMBLE) {
3759 		error = spa_ld_validate_vdevs(spa);
3760 		if (error != 0)
3761 			return (error);
3762 	}
3763 
3764 	/*
3765 	 * Read all vdev labels to find the best uberblock (i.e. latest,
3766 	 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
3767 	 * get the list of features required to read blkptrs in the MOS from
3768 	 * the vdev label with the best uberblock and verify that our version
3769 	 * of zfs supports them all.
3770 	 */
3771 	error = spa_ld_select_uberblock(spa, type);
3772 	if (error != 0)
3773 		return (error);
3774 
3775 	/*
3776 	 * Pass that uberblock to the dsl_pool layer which will open the root
3777 	 * blkptr. This blkptr points to the latest version of the MOS and will
3778 	 * allow us to read its contents.
3779 	 */
3780 	error = spa_ld_open_rootbp(spa);
3781 	if (error != 0)
3782 		return (error);
3783 
3784 	return (0);
3785 }
3786 
3787 static int
3788 spa_ld_checkpoint_rewind(spa_t *spa)
3789 {
3790 	uberblock_t checkpoint;
3791 	int error = 0;
3792 
3793 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3794 	ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
3795 
3796 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3797 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
3798 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
3799 
3800 	if (error != 0) {
3801 		spa_load_failed(spa, "unable to retrieve checkpointed "
3802 		    "uberblock from the MOS config [error=%d]", error);
3803 
3804 		if (error == ENOENT)
3805 			error = ZFS_ERR_NO_CHECKPOINT;
3806 
3807 		return (error);
3808 	}
3809 
3810 	ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg);
3811 	ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg);
3812 
3813 	/*
3814 	 * We need to update the txg and timestamp of the checkpointed
3815 	 * uberblock to be higher than the latest one. This ensures that
3816 	 * the checkpointed uberblock is selected if we were to close and
3817 	 * reopen the pool right after we've written it in the vdev labels.
3818 	 * (also see block comment in vdev_uberblock_compare)
3819 	 */
3820 	checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1;
3821 	checkpoint.ub_timestamp = gethrestime_sec();
3822 
3823 	/*
3824 	 * Set current uberblock to be the checkpointed uberblock.
3825 	 */
3826 	spa->spa_uberblock = checkpoint;
3827 
3828 	/*
3829 	 * If we are doing a normal rewind, then the pool is open for
3830 	 * writing and we sync the "updated" checkpointed uberblock to
3831 	 * disk. Once this is done, we've basically rewound the whole
3832 	 * pool and there is no way back.
3833 	 *
3834 	 * There are cases when we don't want to attempt and sync the
3835 	 * checkpointed uberblock to disk because we are opening a
3836 	 * pool as read-only. Specifically, verifying the checkpointed
3837 	 * state with zdb, and importing the checkpointed state to get
3838 	 * a "preview" of its content.
3839 	 */
3840 	if (spa_writeable(spa)) {
3841 		vdev_t *rvd = spa->spa_root_vdev;
3842 
3843 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3844 		vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL };
3845 		int svdcount = 0;
3846 		int children = rvd->vdev_children;
3847 		int c0 = spa_get_random(children);
3848 
3849 		for (int c = 0; c < children; c++) {
3850 			vdev_t *vd = rvd->vdev_child[(c0 + c) % children];
3851 
3852 			/* Stop when revisiting the first vdev */
3853 			if (c > 0 && svd[0] == vd)
3854 				break;
3855 
3856 			if (vd->vdev_ms_array == 0 || vd->vdev_islog ||
3857 			    !vdev_is_concrete(vd))
3858 				continue;
3859 
3860 			svd[svdcount++] = vd;
3861 			if (svdcount == SPA_SYNC_MIN_VDEVS)
3862 				break;
3863 		}
3864 		error = vdev_config_sync(svd, svdcount, spa->spa_first_txg);
3865 		if (error == 0)
3866 			spa->spa_last_synced_guid = rvd->vdev_guid;
3867 		spa_config_exit(spa, SCL_ALL, FTAG);
3868 
3869 		if (error != 0) {
3870 			spa_load_failed(spa, "failed to write checkpointed "
3871 			    "uberblock to the vdev labels [error=%d]", error);
3872 			return (error);
3873 		}
3874 	}
3875 
3876 	return (0);
3877 }
3878 
3879 static int
3880 spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type,
3881     boolean_t *update_config_cache)
3882 {
3883 	int error;
3884 
3885 	/*
3886 	 * Parse the config for pool, open and validate vdevs,
3887 	 * select an uberblock, and use that uberblock to open
3888 	 * the MOS.
3889 	 */
3890 	error = spa_ld_mos_init(spa, type);
3891 	if (error != 0)
3892 		return (error);
3893 
3894 	/*
3895 	 * Retrieve the trusted config stored in the MOS and use it to create
3896 	 * a new, exact version of the vdev tree, then reopen all vdevs.
3897 	 */
3898 	error = spa_ld_trusted_config(spa, type, B_FALSE);
3899 	if (error == EAGAIN) {
3900 		if (update_config_cache != NULL)
3901 			*update_config_cache = B_TRUE;
3902 
3903 		/*
3904 		 * Redo the loading process with the trusted config if it is
3905 		 * too different from the untrusted config.
3906 		 */
3907 		spa_ld_prepare_for_reload(spa);
3908 		spa_load_note(spa, "RELOADING");
3909 		error = spa_ld_mos_init(spa, type);
3910 		if (error != 0)
3911 			return (error);
3912 
3913 		error = spa_ld_trusted_config(spa, type, B_TRUE);
3914 		if (error != 0)
3915 			return (error);
3916 
3917 	} else if (error != 0) {
3918 		return (error);
3919 	}
3920 
3921 	return (0);
3922 }
3923 
3924 /*
3925  * Load an existing storage pool, using the config provided. This config
3926  * describes which vdevs are part of the pool and is later validated against
3927  * partial configs present in each vdev's label and an entire copy of the
3928  * config stored in the MOS.
3929  */
3930 static int
3931 spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport)
3932 {
3933 	int error = 0;
3934 	boolean_t missing_feat_write = B_FALSE;
3935 	boolean_t checkpoint_rewind =
3936 	    (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
3937 	boolean_t update_config_cache = B_FALSE;
3938 
3939 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3940 	ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE);
3941 
3942 	spa_load_note(spa, "LOADING");
3943 
3944 	error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache);
3945 	if (error != 0)
3946 		return (error);
3947 
3948 	/*
3949 	 * If we are rewinding to the checkpoint then we need to repeat
3950 	 * everything we've done so far in this function but this time
3951 	 * selecting the checkpointed uberblock and using that to open
3952 	 * the MOS.
3953 	 */
3954 	if (checkpoint_rewind) {
3955 		/*
3956 		 * If we are rewinding to the checkpoint update config cache
3957 		 * anyway.
3958 		 */
3959 		update_config_cache = B_TRUE;
3960 
3961 		/*
3962 		 * Extract the checkpointed uberblock from the current MOS
3963 		 * and use this as the pool's uberblock from now on. If the
3964 		 * pool is imported as writeable we also write the checkpoint
3965 		 * uberblock to the labels, making the rewind permanent.
3966 		 */
3967 		error = spa_ld_checkpoint_rewind(spa);
3968 		if (error != 0)
3969 			return (error);
3970 
3971 		/*
3972 		 * Redo the loading process process again with the
3973 		 * checkpointed uberblock.
3974 		 */
3975 		spa_ld_prepare_for_reload(spa);
3976 		spa_load_note(spa, "LOADING checkpointed uberblock");
3977 		error = spa_ld_mos_with_trusted_config(spa, type, NULL);
3978 		if (error != 0)
3979 			return (error);
3980 	}
3981 
3982 	/*
3983 	 * Retrieve the checkpoint txg if the pool has a checkpoint.
3984 	 */
3985 	error = spa_ld_read_checkpoint_txg(spa);
3986 	if (error != 0)
3987 		return (error);
3988 
3989 	/*
3990 	 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
3991 	 * from the pool and their contents were re-mapped to other vdevs. Note
3992 	 * that everything that we read before this step must have been
3993 	 * rewritten on concrete vdevs after the last device removal was
3994 	 * initiated. Otherwise we could be reading from indirect vdevs before
3995 	 * we have loaded their mappings.
3996 	 */
3997 	error = spa_ld_open_indirect_vdev_metadata(spa);
3998 	if (error != 0)
3999 		return (error);
4000 
4001 	/*
4002 	 * Retrieve the full list of active features from the MOS and check if
4003 	 * they are all supported.
4004 	 */
4005 	error = spa_ld_check_features(spa, &missing_feat_write);
4006 	if (error != 0)
4007 		return (error);
4008 
4009 	/*
4010 	 * Load several special directories from the MOS needed by the dsl_pool
4011 	 * layer.
4012 	 */
4013 	error = spa_ld_load_special_directories(spa);
4014 	if (error != 0)
4015 		return (error);
4016 
4017 	/*
4018 	 * Retrieve pool properties from the MOS.
4019 	 */
4020 	error = spa_ld_get_props(spa);
4021 	if (error != 0)
4022 		return (error);
4023 
4024 	/*
4025 	 * Retrieve the list of auxiliary devices - cache devices and spares -
4026 	 * and open them.
4027 	 */
4028 	error = spa_ld_open_aux_vdevs(spa, type);
4029 	if (error != 0)
4030 		return (error);
4031 
4032 	/*
4033 	 * Load the metadata for all vdevs. Also check if unopenable devices
4034 	 * should be autoreplaced.
4035 	 */
4036 	error = spa_ld_load_vdev_metadata(spa);
4037 	if (error != 0)
4038 		return (error);
4039 
4040 	error = spa_ld_load_dedup_tables(spa);
4041 	if (error != 0)
4042 		return (error);
4043 
4044 	/*
4045 	 * Verify the logs now to make sure we don't have any unexpected errors
4046 	 * when we claim log blocks later.
4047 	 */
4048 	error = spa_ld_verify_logs(spa, type, ereport);
4049 	if (error != 0)
4050 		return (error);
4051 
4052 	if (missing_feat_write) {
4053 		ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT);
4054 
4055 		/*
4056 		 * At this point, we know that we can open the pool in
4057 		 * read-only mode but not read-write mode. We now have enough
4058 		 * information and can return to userland.
4059 		 */
4060 		return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT,
4061 		    ENOTSUP));
4062 	}
4063 
4064 	/*
4065 	 * Traverse the last txgs to make sure the pool was left off in a safe
4066 	 * state. When performing an extreme rewind, we verify the whole pool,
4067 	 * which can take a very long time.
4068 	 */
4069 	error = spa_ld_verify_pool_data(spa);
4070 	if (error != 0)
4071 		return (error);
4072 
4073 	/*
4074 	 * Calculate the deflated space for the pool. This must be done before
4075 	 * we write anything to the pool because we'd need to update the space
4076 	 * accounting using the deflated sizes.
4077 	 */
4078 	spa_update_dspace(spa);
4079 
4080 	/*
4081 	 * We have now retrieved all the information we needed to open the
4082 	 * pool. If we are importing the pool in read-write mode, a few
4083 	 * additional steps must be performed to finish the import.
4084 	 */
4085 	if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER ||
4086 	    spa->spa_load_max_txg == UINT64_MAX)) {
4087 		uint64_t config_cache_txg = spa->spa_config_txg;
4088 
4089 		ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT);
4090 
4091 		/*
4092 		 * In case of a checkpoint rewind, log the original txg
4093 		 * of the checkpointed uberblock.
4094 		 */
4095 		if (checkpoint_rewind) {
4096 			spa_history_log_internal(spa, "checkpoint rewind",
4097 			    NULL, "rewound state to txg=%llu",
4098 			    (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg);
4099 		}
4100 
4101 		/*
4102 		 * Traverse the ZIL and claim all blocks.
4103 		 */
4104 		spa_ld_claim_log_blocks(spa);
4105 
4106 		/*
4107 		 * Kick-off the syncing thread.
4108 		 */
4109 		spa->spa_sync_on = B_TRUE;
4110 		txg_sync_start(spa->spa_dsl_pool);
4111 		mmp_thread_start(spa);
4112 
4113 		/*
4114 		 * Wait for all claims to sync.  We sync up to the highest
4115 		 * claimed log block birth time so that claimed log blocks
4116 		 * don't appear to be from the future.  spa_claim_max_txg
4117 		 * will have been set for us by ZIL traversal operations
4118 		 * performed above.
4119 		 */
4120 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
4121 
4122 		/*
4123 		 * Check if we need to request an update of the config. On the
4124 		 * next sync, we would update the config stored in vdev labels
4125 		 * and the cachefile (by default /etc/zfs/zpool.cache).
4126 		 */
4127 		spa_ld_check_for_config_update(spa, config_cache_txg,
4128 		    update_config_cache);
4129 
4130 		/*
4131 		 * Check all DTLs to see if anything needs resilvering.
4132 		 */
4133 		if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
4134 		    vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
4135 			spa_async_request(spa, SPA_ASYNC_RESILVER);
4136 
4137 		/*
4138 		 * Log the fact that we booted up (so that we can detect if
4139 		 * we rebooted in the middle of an operation).
4140 		 */
4141 		spa_history_log_version(spa, "open");
4142 
4143 		spa_restart_removal(spa);
4144 		spa_spawn_aux_threads(spa);
4145 
4146 		/*
4147 		 * Delete any inconsistent datasets.
4148 		 *
4149 		 * Note:
4150 		 * Since we may be issuing deletes for clones here,
4151 		 * we make sure to do so after we've spawned all the
4152 		 * auxiliary threads above (from which the livelist
4153 		 * deletion zthr is part of).
4154 		 */
4155 		(void) dmu_objset_find(spa_name(spa),
4156 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
4157 
4158 		/*
4159 		 * Clean up any stale temporary dataset userrefs.
4160 		 */
4161 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
4162 
4163 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4164 		vdev_initialize_restart(spa->spa_root_vdev);
4165 		spa_config_exit(spa, SCL_CONFIG, FTAG);
4166 	}
4167 
4168 	spa_load_note(spa, "LOADED");
4169 
4170 	return (0);
4171 }
4172 
4173 static int
4174 spa_load_retry(spa_t *spa, spa_load_state_t state)
4175 {
4176 	int mode = spa->spa_mode;
4177 
4178 	spa_unload(spa);
4179 	spa_deactivate(spa);
4180 
4181 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
4182 
4183 	spa_activate(spa, mode);
4184 	spa_async_suspend(spa);
4185 
4186 	spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu",
4187 	    (u_longlong_t)spa->spa_load_max_txg);
4188 
4189 	return (spa_load(spa, state, SPA_IMPORT_EXISTING));
4190 }
4191 
4192 /*
4193  * If spa_load() fails this function will try loading prior txg's. If
4194  * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
4195  * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
4196  * function will not rewind the pool and will return the same error as
4197  * spa_load().
4198  */
4199 static int
4200 spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request,
4201     int rewind_flags)
4202 {
4203 	nvlist_t *loadinfo = NULL;
4204 	nvlist_t *config = NULL;
4205 	int load_error, rewind_error;
4206 	uint64_t safe_rewind_txg;
4207 	uint64_t min_txg;
4208 
4209 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
4210 		spa->spa_load_max_txg = spa->spa_load_txg;
4211 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4212 	} else {
4213 		spa->spa_load_max_txg = max_request;
4214 		if (max_request != UINT64_MAX)
4215 			spa->spa_extreme_rewind = B_TRUE;
4216 	}
4217 
4218 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING);
4219 	if (load_error == 0)
4220 		return (0);
4221 	if (load_error == ZFS_ERR_NO_CHECKPOINT) {
4222 		/*
4223 		 * When attempting checkpoint-rewind on a pool with no
4224 		 * checkpoint, we should not attempt to load uberblocks
4225 		 * from previous txgs when spa_load fails.
4226 		 */
4227 		ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
4228 		return (load_error);
4229 	}
4230 
4231 	if (spa->spa_root_vdev != NULL)
4232 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4233 
4234 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
4235 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
4236 
4237 	if (rewind_flags & ZPOOL_NEVER_REWIND) {
4238 		nvlist_free(config);
4239 		return (load_error);
4240 	}
4241 
4242 	if (state == SPA_LOAD_RECOVER) {
4243 		/* Price of rolling back is discarding txgs, including log */
4244 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4245 	} else {
4246 		/*
4247 		 * If we aren't rolling back save the load info from our first
4248 		 * import attempt so that we can restore it after attempting
4249 		 * to rewind.
4250 		 */
4251 		loadinfo = spa->spa_load_info;
4252 		spa->spa_load_info = fnvlist_alloc();
4253 	}
4254 
4255 	spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
4256 	safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
4257 	min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
4258 	    TXG_INITIAL : safe_rewind_txg;
4259 
4260 	/*
4261 	 * Continue as long as we're finding errors, we're still within
4262 	 * the acceptable rewind range, and we're still finding uberblocks
4263 	 */
4264 	while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
4265 	    spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
4266 		if (spa->spa_load_max_txg < safe_rewind_txg)
4267 			spa->spa_extreme_rewind = B_TRUE;
4268 		rewind_error = spa_load_retry(spa, state);
4269 	}
4270 
4271 	spa->spa_extreme_rewind = B_FALSE;
4272 	spa->spa_load_max_txg = UINT64_MAX;
4273 
4274 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
4275 		spa_config_set(spa, config);
4276 	else
4277 		nvlist_free(config);
4278 
4279 	if (state == SPA_LOAD_RECOVER) {
4280 		ASSERT3P(loadinfo, ==, NULL);
4281 		return (rewind_error);
4282 	} else {
4283 		/* Store the rewind info as part of the initial load info */
4284 		fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
4285 		    spa->spa_load_info);
4286 
4287 		/* Restore the initial load info */
4288 		fnvlist_free(spa->spa_load_info);
4289 		spa->spa_load_info = loadinfo;
4290 
4291 		return (load_error);
4292 	}
4293 }
4294 
4295 /*
4296  * Pool Open/Import
4297  *
4298  * The import case is identical to an open except that the configuration is sent
4299  * down from userland, instead of grabbed from the configuration cache.  For the
4300  * case of an open, the pool configuration will exist in the
4301  * POOL_STATE_UNINITIALIZED state.
4302  *
4303  * The stats information (gen/count/ustats) is used to gather vdev statistics at
4304  * the same time open the pool, without having to keep around the spa_t in some
4305  * ambiguous state.
4306  */
4307 static int
4308 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
4309     nvlist_t **config)
4310 {
4311 	spa_t *spa;
4312 	spa_load_state_t state = SPA_LOAD_OPEN;
4313 	int error;
4314 	int locked = B_FALSE;
4315 
4316 	*spapp = NULL;
4317 
4318 	/*
4319 	 * As disgusting as this is, we need to support recursive calls to this
4320 	 * function because dsl_dir_open() is called during spa_load(), and ends
4321 	 * up calling spa_open() again.  The real fix is to figure out how to
4322 	 * avoid dsl_dir_open() calling this in the first place.
4323 	 */
4324 	if (mutex_owner(&spa_namespace_lock) != curthread) {
4325 		mutex_enter(&spa_namespace_lock);
4326 		locked = B_TRUE;
4327 	}
4328 
4329 	if ((spa = spa_lookup(pool)) == NULL) {
4330 		if (locked)
4331 			mutex_exit(&spa_namespace_lock);
4332 		return (SET_ERROR(ENOENT));
4333 	}
4334 
4335 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
4336 		zpool_load_policy_t policy;
4337 
4338 		zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config,
4339 		    &policy);
4340 		if (policy.zlp_rewind & ZPOOL_DO_REWIND)
4341 			state = SPA_LOAD_RECOVER;
4342 
4343 		spa_activate(spa, spa_mode_global);
4344 
4345 		if (state != SPA_LOAD_RECOVER)
4346 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4347 		spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE;
4348 
4349 		zfs_dbgmsg("spa_open_common: opening %s", pool);
4350 		error = spa_load_best(spa, state, policy.zlp_txg,
4351 		    policy.zlp_rewind);
4352 
4353 		if (error == EBADF) {
4354 			/*
4355 			 * If vdev_validate() returns failure (indicated by
4356 			 * EBADF), it indicates that one of the vdevs indicates
4357 			 * that the pool has been exported or destroyed.  If
4358 			 * this is the case, the config cache is out of sync and
4359 			 * we should remove the pool from the namespace.
4360 			 */
4361 			spa_unload(spa);
4362 			spa_deactivate(spa);
4363 			spa_write_cachefile(spa, B_TRUE, B_TRUE);
4364 			spa_remove(spa);
4365 			if (locked)
4366 				mutex_exit(&spa_namespace_lock);
4367 			return (SET_ERROR(ENOENT));
4368 		}
4369 
4370 		if (error) {
4371 			/*
4372 			 * We can't open the pool, but we still have useful
4373 			 * information: the state of each vdev after the
4374 			 * attempted vdev_open().  Return this to the user.
4375 			 */
4376 			if (config != NULL && spa->spa_config) {
4377 				VERIFY(nvlist_dup(spa->spa_config, config,
4378 				    KM_SLEEP) == 0);
4379 				VERIFY(nvlist_add_nvlist(*config,
4380 				    ZPOOL_CONFIG_LOAD_INFO,
4381 				    spa->spa_load_info) == 0);
4382 			}
4383 			spa_unload(spa);
4384 			spa_deactivate(spa);
4385 			spa->spa_last_open_failed = error;
4386 			if (locked)
4387 				mutex_exit(&spa_namespace_lock);
4388 			*spapp = NULL;
4389 			return (error);
4390 		}
4391 	}
4392 
4393 	spa_open_ref(spa, tag);
4394 
4395 	if (config != NULL)
4396 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4397 
4398 	/*
4399 	 * If we've recovered the pool, pass back any information we
4400 	 * gathered while doing the load.
4401 	 */
4402 	if (state == SPA_LOAD_RECOVER) {
4403 		VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
4404 		    spa->spa_load_info) == 0);
4405 	}
4406 
4407 	if (locked) {
4408 		spa->spa_last_open_failed = 0;
4409 		spa->spa_last_ubsync_txg = 0;
4410 		spa->spa_load_txg = 0;
4411 		mutex_exit(&spa_namespace_lock);
4412 	}
4413 
4414 	*spapp = spa;
4415 
4416 	return (0);
4417 }
4418 
4419 int
4420 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
4421     nvlist_t **config)
4422 {
4423 	return (spa_open_common(name, spapp, tag, policy, config));
4424 }
4425 
4426 int
4427 spa_open(const char *name, spa_t **spapp, void *tag)
4428 {
4429 	return (spa_open_common(name, spapp, tag, NULL, NULL));
4430 }
4431 
4432 /*
4433  * Lookup the given spa_t, incrementing the inject count in the process,
4434  * preventing it from being exported or destroyed.
4435  */
4436 spa_t *
4437 spa_inject_addref(char *name)
4438 {
4439 	spa_t *spa;
4440 
4441 	mutex_enter(&spa_namespace_lock);
4442 	if ((spa = spa_lookup(name)) == NULL) {
4443 		mutex_exit(&spa_namespace_lock);
4444 		return (NULL);
4445 	}
4446 	spa->spa_inject_ref++;
4447 	mutex_exit(&spa_namespace_lock);
4448 
4449 	return (spa);
4450 }
4451 
4452 void
4453 spa_inject_delref(spa_t *spa)
4454 {
4455 	mutex_enter(&spa_namespace_lock);
4456 	spa->spa_inject_ref--;
4457 	mutex_exit(&spa_namespace_lock);
4458 }
4459 
4460 /*
4461  * Add spares device information to the nvlist.
4462  */
4463 static void
4464 spa_add_spares(spa_t *spa, nvlist_t *config)
4465 {
4466 	nvlist_t **spares;
4467 	uint_t i, nspares;
4468 	nvlist_t *nvroot;
4469 	uint64_t guid;
4470 	vdev_stat_t *vs;
4471 	uint_t vsc;
4472 	uint64_t pool;
4473 
4474 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
4475 
4476 	if (spa->spa_spares.sav_count == 0)
4477 		return;
4478 
4479 	VERIFY(nvlist_lookup_nvlist(config,
4480 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4481 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4482 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
4483 	if (nspares != 0) {
4484 		VERIFY(nvlist_add_nvlist_array(nvroot,
4485 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4486 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
4487 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
4488 
4489 		/*
4490 		 * Go through and find any spares which have since been
4491 		 * repurposed as an active spare.  If this is the case, update
4492 		 * their status appropriately.
4493 		 */
4494 		for (i = 0; i < nspares; i++) {
4495 			VERIFY(nvlist_lookup_uint64(spares[i],
4496 			    ZPOOL_CONFIG_GUID, &guid) == 0);
4497 			if (spa_spare_exists(guid, &pool, NULL) &&
4498 			    pool != 0ULL) {
4499 				VERIFY(nvlist_lookup_uint64_array(
4500 				    spares[i], ZPOOL_CONFIG_VDEV_STATS,
4501 				    (uint64_t **)&vs, &vsc) == 0);
4502 				vs->vs_state = VDEV_STATE_CANT_OPEN;
4503 				vs->vs_aux = VDEV_AUX_SPARED;
4504 			}
4505 		}
4506 	}
4507 }
4508 
4509 /*
4510  * Add l2cache device information to the nvlist, including vdev stats.
4511  */
4512 static void
4513 spa_add_l2cache(spa_t *spa, nvlist_t *config)
4514 {
4515 	nvlist_t **l2cache;
4516 	uint_t i, j, nl2cache;
4517 	nvlist_t *nvroot;
4518 	uint64_t guid;
4519 	vdev_t *vd;
4520 	vdev_stat_t *vs;
4521 	uint_t vsc;
4522 
4523 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
4524 
4525 	if (spa->spa_l2cache.sav_count == 0)
4526 		return;
4527 
4528 	VERIFY(nvlist_lookup_nvlist(config,
4529 	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4530 	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4531 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
4532 	if (nl2cache != 0) {
4533 		VERIFY(nvlist_add_nvlist_array(nvroot,
4534 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4535 		VERIFY(nvlist_lookup_nvlist_array(nvroot,
4536 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
4537 
4538 		/*
4539 		 * Update level 2 cache device stats.
4540 		 */
4541 
4542 		for (i = 0; i < nl2cache; i++) {
4543 			VERIFY(nvlist_lookup_uint64(l2cache[i],
4544 			    ZPOOL_CONFIG_GUID, &guid) == 0);
4545 
4546 			vd = NULL;
4547 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
4548 				if (guid ==
4549 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
4550 					vd = spa->spa_l2cache.sav_vdevs[j];
4551 					break;
4552 				}
4553 			}
4554 			ASSERT(vd != NULL);
4555 
4556 			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
4557 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
4558 			    == 0);
4559 			vdev_get_stats(vd, vs);
4560 		}
4561 	}
4562 }
4563 
4564 static void
4565 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
4566 {
4567 	nvlist_t *features;
4568 	zap_cursor_t zc;
4569 	zap_attribute_t za;
4570 
4571 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
4572 	VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4573 
4574 	if (spa->spa_feat_for_read_obj != 0) {
4575 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
4576 		    spa->spa_feat_for_read_obj);
4577 		    zap_cursor_retrieve(&zc, &za) == 0;
4578 		    zap_cursor_advance(&zc)) {
4579 			ASSERT(za.za_integer_length == sizeof (uint64_t) &&
4580 			    za.za_num_integers == 1);
4581 			VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
4582 			    za.za_first_integer));
4583 		}
4584 		zap_cursor_fini(&zc);
4585 	}
4586 
4587 	if (spa->spa_feat_for_write_obj != 0) {
4588 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
4589 		    spa->spa_feat_for_write_obj);
4590 		    zap_cursor_retrieve(&zc, &za) == 0;
4591 		    zap_cursor_advance(&zc)) {
4592 			ASSERT(za.za_integer_length == sizeof (uint64_t) &&
4593 			    za.za_num_integers == 1);
4594 			VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
4595 			    za.za_first_integer));
4596 		}
4597 		zap_cursor_fini(&zc);
4598 	}
4599 
4600 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
4601 	    features) == 0);
4602 	nvlist_free(features);
4603 }
4604 
4605 int
4606 spa_get_stats(const char *name, nvlist_t **config,
4607     char *altroot, size_t buflen)
4608 {
4609 	int error;
4610 	spa_t *spa;
4611 
4612 	*config = NULL;
4613 	error = spa_open_common(name, &spa, FTAG, NULL, config);
4614 
4615 	if (spa != NULL) {
4616 		/*
4617 		 * This still leaves a window of inconsistency where the spares
4618 		 * or l2cache devices could change and the config would be
4619 		 * self-inconsistent.
4620 		 */
4621 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4622 
4623 		if (*config != NULL) {
4624 			uint64_t loadtimes[2];
4625 
4626 			loadtimes[0] = spa->spa_loaded_ts.tv_sec;
4627 			loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
4628 			VERIFY(nvlist_add_uint64_array(*config,
4629 			    ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
4630 
4631 			VERIFY(nvlist_add_uint64(*config,
4632 			    ZPOOL_CONFIG_ERRCOUNT,
4633 			    spa_get_errlog_size(spa)) == 0);
4634 
4635 			if (spa_suspended(spa)) {
4636 				VERIFY(nvlist_add_uint64(*config,
4637 				    ZPOOL_CONFIG_SUSPENDED,
4638 				    spa->spa_failmode) == 0);
4639 				VERIFY(nvlist_add_uint64(*config,
4640 				    ZPOOL_CONFIG_SUSPENDED_REASON,
4641 				    spa->spa_suspended) == 0);
4642 			}
4643 
4644 			spa_add_spares(spa, *config);
4645 			spa_add_l2cache(spa, *config);
4646 			spa_add_feature_stats(spa, *config);
4647 		}
4648 	}
4649 
4650 	/*
4651 	 * We want to get the alternate root even for faulted pools, so we cheat
4652 	 * and call spa_lookup() directly.
4653 	 */
4654 	if (altroot) {
4655 		if (spa == NULL) {
4656 			mutex_enter(&spa_namespace_lock);
4657 			spa = spa_lookup(name);
4658 			if (spa)
4659 				spa_altroot(spa, altroot, buflen);
4660 			else
4661 				altroot[0] = '\0';
4662 			spa = NULL;
4663 			mutex_exit(&spa_namespace_lock);
4664 		} else {
4665 			spa_altroot(spa, altroot, buflen);
4666 		}
4667 	}
4668 
4669 	if (spa != NULL) {
4670 		spa_config_exit(spa, SCL_CONFIG, FTAG);
4671 		spa_close(spa, FTAG);
4672 	}
4673 
4674 	return (error);
4675 }
4676 
4677 /*
4678  * Validate that the auxiliary device array is well formed.  We must have an
4679  * array of nvlists, each which describes a valid leaf vdev.  If this is an
4680  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
4681  * specified, as long as they are well-formed.
4682  */
4683 static int
4684 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
4685     spa_aux_vdev_t *sav, const char *config, uint64_t version,
4686     vdev_labeltype_t label)
4687 {
4688 	nvlist_t **dev;
4689 	uint_t i, ndev;
4690 	vdev_t *vd;
4691 	int error;
4692 
4693 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4694 
4695 	/*
4696 	 * It's acceptable to have no devs specified.
4697 	 */
4698 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
4699 		return (0);
4700 
4701 	if (ndev == 0)
4702 		return (SET_ERROR(EINVAL));
4703 
4704 	/*
4705 	 * Make sure the pool is formatted with a version that supports this
4706 	 * device type.
4707 	 */
4708 	if (spa_version(spa) < version)
4709 		return (SET_ERROR(ENOTSUP));
4710 
4711 	/*
4712 	 * Set the pending device list so we correctly handle device in-use
4713 	 * checking.
4714 	 */
4715 	sav->sav_pending = dev;
4716 	sav->sav_npending = ndev;
4717 
4718 	for (i = 0; i < ndev; i++) {
4719 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
4720 		    mode)) != 0)
4721 			goto out;
4722 
4723 		if (!vd->vdev_ops->vdev_op_leaf) {
4724 			vdev_free(vd);
4725 			error = SET_ERROR(EINVAL);
4726 			goto out;
4727 		}
4728 
4729 		vd->vdev_top = vd;
4730 
4731 		if ((error = vdev_open(vd)) == 0 &&
4732 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
4733 			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
4734 			    vd->vdev_guid) == 0);
4735 		}
4736 
4737 		vdev_free(vd);
4738 
4739 		if (error &&
4740 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
4741 			goto out;
4742 		else
4743 			error = 0;
4744 	}
4745 
4746 out:
4747 	sav->sav_pending = NULL;
4748 	sav->sav_npending = 0;
4749 	return (error);
4750 }
4751 
4752 static int
4753 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
4754 {
4755 	int error;
4756 
4757 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4758 
4759 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
4760 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
4761 	    VDEV_LABEL_SPARE)) != 0) {
4762 		return (error);
4763 	}
4764 
4765 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
4766 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
4767 	    VDEV_LABEL_L2CACHE));
4768 }
4769 
4770 static void
4771 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
4772     const char *config)
4773 {
4774 	int i;
4775 
4776 	if (sav->sav_config != NULL) {
4777 		nvlist_t **olddevs;
4778 		uint_t oldndevs;
4779 		nvlist_t **newdevs;
4780 
4781 		/*
4782 		 * Generate new dev list by concatentating with the
4783 		 * current dev list.
4784 		 */
4785 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
4786 		    &olddevs, &oldndevs) == 0);
4787 
4788 		newdevs = kmem_alloc(sizeof (void *) *
4789 		    (ndevs + oldndevs), KM_SLEEP);
4790 		for (i = 0; i < oldndevs; i++)
4791 			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
4792 			    KM_SLEEP) == 0);
4793 		for (i = 0; i < ndevs; i++)
4794 			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
4795 			    KM_SLEEP) == 0);
4796 
4797 		VERIFY(nvlist_remove(sav->sav_config, config,
4798 		    DATA_TYPE_NVLIST_ARRAY) == 0);
4799 
4800 		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
4801 		    config, newdevs, ndevs + oldndevs) == 0);
4802 		for (i = 0; i < oldndevs + ndevs; i++)
4803 			nvlist_free(newdevs[i]);
4804 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
4805 	} else {
4806 		/*
4807 		 * Generate a new dev list.
4808 		 */
4809 		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
4810 		    KM_SLEEP) == 0);
4811 		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
4812 		    devs, ndevs) == 0);
4813 	}
4814 }
4815 
4816 /*
4817  * Stop and drop level 2 ARC devices
4818  */
4819 void
4820 spa_l2cache_drop(spa_t *spa)
4821 {
4822 	vdev_t *vd;
4823 	int i;
4824 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
4825 
4826 	for (i = 0; i < sav->sav_count; i++) {
4827 		uint64_t pool;
4828 
4829 		vd = sav->sav_vdevs[i];
4830 		ASSERT(vd != NULL);
4831 
4832 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
4833 		    pool != 0ULL && l2arc_vdev_present(vd))
4834 			l2arc_remove_vdev(vd);
4835 	}
4836 }
4837 
4838 /*
4839  * Pool Creation
4840  */
4841 int
4842 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
4843     nvlist_t *zplprops)
4844 {
4845 	spa_t *spa;
4846 	char *altroot = NULL;
4847 	vdev_t *rvd;
4848 	dsl_pool_t *dp;
4849 	dmu_tx_t *tx;
4850 	int error = 0;
4851 	uint64_t txg = TXG_INITIAL;
4852 	nvlist_t **spares, **l2cache;
4853 	uint_t nspares, nl2cache;
4854 	uint64_t version, obj;
4855 	boolean_t has_features;
4856 	char *poolname;
4857 	nvlist_t *nvl;
4858 
4859 	if (props == NULL ||
4860 	    nvlist_lookup_string(props,
4861 	    zpool_prop_to_name(ZPOOL_PROP_TNAME), &poolname) != 0)
4862 		poolname = (char *)pool;
4863 
4864 	/*
4865 	 * If this pool already exists, return failure.
4866 	 */
4867 	mutex_enter(&spa_namespace_lock);
4868 	if (spa_lookup(poolname) != NULL) {
4869 		mutex_exit(&spa_namespace_lock);
4870 		return (SET_ERROR(EEXIST));
4871 	}
4872 
4873 	/*
4874 	 * Allocate a new spa_t structure.
4875 	 */
4876 	nvl = fnvlist_alloc();
4877 	fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool);
4878 	(void) nvlist_lookup_string(props,
4879 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4880 	spa = spa_add(poolname, nvl, altroot);
4881 	fnvlist_free(nvl);
4882 	spa_activate(spa, spa_mode_global);
4883 
4884 	if (props && (error = spa_prop_validate(spa, props))) {
4885 		spa_deactivate(spa);
4886 		spa_remove(spa);
4887 		mutex_exit(&spa_namespace_lock);
4888 		return (error);
4889 	}
4890 
4891 	/*
4892 	 * Temporary pool names should never be written to disk.
4893 	 */
4894 	if (poolname != pool)
4895 		spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME;
4896 
4897 	has_features = B_FALSE;
4898 	for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
4899 	    elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
4900 		if (zpool_prop_feature(nvpair_name(elem)))
4901 			has_features = B_TRUE;
4902 	}
4903 
4904 	if (has_features || nvlist_lookup_uint64(props,
4905 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
4906 		version = SPA_VERSION;
4907 	}
4908 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
4909 
4910 	spa->spa_first_txg = txg;
4911 	spa->spa_uberblock.ub_txg = txg - 1;
4912 	spa->spa_uberblock.ub_version = version;
4913 	spa->spa_ubsync = spa->spa_uberblock;
4914 	spa->spa_load_state = SPA_LOAD_CREATE;
4915 	spa->spa_removing_phys.sr_state = DSS_NONE;
4916 	spa->spa_removing_phys.sr_removing_vdev = -1;
4917 	spa->spa_removing_phys.sr_prev_indirect_vdev = -1;
4918 
4919 	/*
4920 	 * Create "The Godfather" zio to hold all async IOs
4921 	 */
4922 	spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
4923 	    KM_SLEEP);
4924 	for (int i = 0; i < max_ncpus; i++) {
4925 		spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
4926 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
4927 		    ZIO_FLAG_GODFATHER);
4928 	}
4929 
4930 	/*
4931 	 * Create the root vdev.
4932 	 */
4933 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4934 
4935 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
4936 
4937 	ASSERT(error != 0 || rvd != NULL);
4938 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
4939 
4940 	if (error == 0 && !zfs_allocatable_devs(nvroot))
4941 		error = SET_ERROR(EINVAL);
4942 
4943 	if (error == 0 &&
4944 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
4945 	    (error = spa_validate_aux(spa, nvroot, txg,
4946 	    VDEV_ALLOC_ADD)) == 0) {
4947 		/*
4948 		 * instantiate the metaslab groups (this will dirty the vdevs)
4949 		 * we can no longer error exit past this point
4950 		 */
4951 		for (int c = 0; error == 0 && c < rvd->vdev_children; c++) {
4952 			vdev_t *vd = rvd->vdev_child[c];
4953 
4954 			vdev_metaslab_set_size(vd);
4955 			vdev_expand(vd, txg);
4956 		}
4957 	}
4958 
4959 	spa_config_exit(spa, SCL_ALL, FTAG);
4960 
4961 	if (error != 0) {
4962 		spa_unload(spa);
4963 		spa_deactivate(spa);
4964 		spa_remove(spa);
4965 		mutex_exit(&spa_namespace_lock);
4966 		return (error);
4967 	}
4968 
4969 	/*
4970 	 * Get the list of spares, if specified.
4971 	 */
4972 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4973 	    &spares, &nspares) == 0) {
4974 		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
4975 		    KM_SLEEP) == 0);
4976 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4977 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4978 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4979 		spa_load_spares(spa);
4980 		spa_config_exit(spa, SCL_ALL, FTAG);
4981 		spa->spa_spares.sav_sync = B_TRUE;
4982 	}
4983 
4984 	/*
4985 	 * Get the list of level 2 cache devices, if specified.
4986 	 */
4987 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4988 	    &l2cache, &nl2cache) == 0) {
4989 		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4990 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
4991 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4992 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4993 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4994 		spa_load_l2cache(spa);
4995 		spa_config_exit(spa, SCL_ALL, FTAG);
4996 		spa->spa_l2cache.sav_sync = B_TRUE;
4997 	}
4998 
4999 	spa->spa_is_initializing = B_TRUE;
5000 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
5001 	spa->spa_meta_objset = dp->dp_meta_objset;
5002 	spa->spa_is_initializing = B_FALSE;
5003 
5004 	/*
5005 	 * Create DDTs (dedup tables).
5006 	 */
5007 	ddt_create(spa);
5008 
5009 	spa_update_dspace(spa);
5010 
5011 	tx = dmu_tx_create_assigned(dp, txg);
5012 
5013 	/*
5014 	 * Create the pool config object.
5015 	 */
5016 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
5017 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
5018 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
5019 
5020 	if (zap_add(spa->spa_meta_objset,
5021 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
5022 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
5023 		cmn_err(CE_PANIC, "failed to add pool config");
5024 	}
5025 
5026 	if (spa_version(spa) >= SPA_VERSION_FEATURES)
5027 		spa_feature_create_zap_objects(spa, tx);
5028 
5029 	if (zap_add(spa->spa_meta_objset,
5030 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
5031 	    sizeof (uint64_t), 1, &version, tx) != 0) {
5032 		cmn_err(CE_PANIC, "failed to add pool version");
5033 	}
5034 
5035 	/* Newly created pools with the right version are always deflated. */
5036 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
5037 		spa->spa_deflate = TRUE;
5038 		if (zap_add(spa->spa_meta_objset,
5039 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5040 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
5041 			cmn_err(CE_PANIC, "failed to add deflate");
5042 		}
5043 	}
5044 
5045 	/*
5046 	 * Create the deferred-free bpobj.  Turn off compression
5047 	 * because sync-to-convergence takes longer if the blocksize
5048 	 * keeps changing.
5049 	 */
5050 	obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
5051 	dmu_object_set_compress(spa->spa_meta_objset, obj,
5052 	    ZIO_COMPRESS_OFF, tx);
5053 	if (zap_add(spa->spa_meta_objset,
5054 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
5055 	    sizeof (uint64_t), 1, &obj, tx) != 0) {
5056 		cmn_err(CE_PANIC, "failed to add bpobj");
5057 	}
5058 	VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
5059 	    spa->spa_meta_objset, obj));
5060 
5061 	/*
5062 	 * Create the pool's history object.
5063 	 */
5064 	if (version >= SPA_VERSION_ZPOOL_HISTORY)
5065 		spa_history_create_obj(spa, tx);
5066 
5067 	/*
5068 	 * Generate some random noise for salted checksums to operate on.
5069 	 */
5070 	(void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
5071 	    sizeof (spa->spa_cksum_salt.zcs_bytes));
5072 
5073 	/*
5074 	 * Set pool properties.
5075 	 */
5076 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
5077 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
5078 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
5079 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
5080 	spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST);
5081 
5082 	if (props != NULL) {
5083 		spa_configfile_set(spa, props, B_FALSE);
5084 		spa_sync_props(props, tx);
5085 	}
5086 
5087 	dmu_tx_commit(tx);
5088 
5089 	spa->spa_sync_on = B_TRUE;
5090 	txg_sync_start(spa->spa_dsl_pool);
5091 	mmp_thread_start(spa);
5092 
5093 	/*
5094 	 * We explicitly wait for the first transaction to complete so that our
5095 	 * bean counters are appropriately updated.
5096 	 */
5097 	txg_wait_synced(spa->spa_dsl_pool, txg);
5098 
5099 	spa_spawn_aux_threads(spa);
5100 
5101 	spa_write_cachefile(spa, B_FALSE, B_TRUE);
5102 	spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE);
5103 
5104 	spa_history_log_version(spa, "create");
5105 
5106 	/*
5107 	 * Don't count references from objsets that are already closed
5108 	 * and are making their way through the eviction process.
5109 	 */
5110 	spa_evicting_os_wait(spa);
5111 	spa->spa_minref = zfs_refcount_count(&spa->spa_refcount);
5112 	spa->spa_load_state = SPA_LOAD_NONE;
5113 
5114 	mutex_exit(&spa_namespace_lock);
5115 
5116 	return (0);
5117 }
5118 
5119 #ifdef _KERNEL
5120 /*
5121  * Get the root pool information from the root disk, then import the root pool
5122  * during the system boot up time.
5123  */
5124 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
5125 
5126 static nvlist_t *
5127 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
5128 {
5129 	nvlist_t *config;
5130 	nvlist_t *nvtop, *nvroot;
5131 	uint64_t pgid;
5132 
5133 	if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
5134 		return (NULL);
5135 
5136 	/*
5137 	 * Add this top-level vdev to the child array.
5138 	 */
5139 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5140 	    &nvtop) == 0);
5141 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5142 	    &pgid) == 0);
5143 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
5144 
5145 	/*
5146 	 * Put this pool's top-level vdevs into a root vdev.
5147 	 */
5148 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5149 	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
5150 	    VDEV_TYPE_ROOT) == 0);
5151 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
5152 	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
5153 	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
5154 	    &nvtop, 1) == 0);
5155 
5156 	/*
5157 	 * Replace the existing vdev_tree with the new root vdev in
5158 	 * this pool's configuration (remove the old, add the new).
5159 	 */
5160 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
5161 	nvlist_free(nvroot);
5162 	return (config);
5163 }
5164 
5165 /*
5166  * Walk the vdev tree and see if we can find a device with "better"
5167  * configuration. A configuration is "better" if the label on that
5168  * device has a more recent txg.
5169  */
5170 static void
5171 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
5172 {
5173 	for (int c = 0; c < vd->vdev_children; c++)
5174 		spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
5175 
5176 	if (vd->vdev_ops->vdev_op_leaf) {
5177 		nvlist_t *label;
5178 		uint64_t label_txg;
5179 
5180 		if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
5181 		    &label) != 0)
5182 			return;
5183 
5184 		VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
5185 		    &label_txg) == 0);
5186 
5187 		/*
5188 		 * Do we have a better boot device?
5189 		 */
5190 		if (label_txg > *txg) {
5191 			*txg = label_txg;
5192 			*avd = vd;
5193 		}
5194 		nvlist_free(label);
5195 	}
5196 }
5197 
5198 /*
5199  * Import a root pool.
5200  *
5201  * For x86. devpath_list will consist of devid and/or physpath name of
5202  * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
5203  * The GRUB "findroot" command will return the vdev we should boot.
5204  *
5205  * For Sparc, devpath_list consists the physpath name of the booting device
5206  * no matter the rootpool is a single device pool or a mirrored pool.
5207  * e.g.
5208  *	"/pci@1f,0/ide@d/disk@0,0:a"
5209  */
5210 int
5211 spa_import_rootpool(char *devpath, char *devid)
5212 {
5213 	spa_t *spa;
5214 	vdev_t *rvd, *bvd, *avd = NULL;
5215 	nvlist_t *config, *nvtop;
5216 	uint64_t guid, txg;
5217 	char *pname;
5218 	int error;
5219 
5220 	/*
5221 	 * Read the label from the boot device and generate a configuration.
5222 	 */
5223 	config = spa_generate_rootconf(devpath, devid, &guid);
5224 #if defined(_OBP) && defined(_KERNEL)
5225 	if (config == NULL) {
5226 		if (strstr(devpath, "/iscsi/ssd") != NULL) {
5227 			/* iscsi boot */
5228 			get_iscsi_bootpath_phy(devpath);
5229 			config = spa_generate_rootconf(devpath, devid, &guid);
5230 		}
5231 	}
5232 #endif
5233 	if (config == NULL) {
5234 		cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
5235 		    devpath);
5236 		return (SET_ERROR(EIO));
5237 	}
5238 
5239 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
5240 	    &pname) == 0);
5241 	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
5242 
5243 	mutex_enter(&spa_namespace_lock);
5244 	if ((spa = spa_lookup(pname)) != NULL) {
5245 		/*
5246 		 * Remove the existing root pool from the namespace so that we
5247 		 * can replace it with the correct config we just read in.
5248 		 */
5249 		spa_remove(spa);
5250 	}
5251 
5252 	spa = spa_add(pname, config, NULL);
5253 	spa->spa_is_root = B_TRUE;
5254 	spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
5255 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5256 	    &spa->spa_ubsync.ub_version) != 0)
5257 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
5258 
5259 	/*
5260 	 * Build up a vdev tree based on the boot device's label config.
5261 	 */
5262 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5263 	    &nvtop) == 0);
5264 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5265 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
5266 	    VDEV_ALLOC_ROOTPOOL);
5267 	spa_config_exit(spa, SCL_ALL, FTAG);
5268 	if (error) {
5269 		mutex_exit(&spa_namespace_lock);
5270 		nvlist_free(config);
5271 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
5272 		    pname);
5273 		return (error);
5274 	}
5275 
5276 	/*
5277 	 * Get the boot vdev.
5278 	 */
5279 	if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
5280 		cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
5281 		    (u_longlong_t)guid);
5282 		error = SET_ERROR(ENOENT);
5283 		goto out;
5284 	}
5285 
5286 	/*
5287 	 * Determine if there is a better boot device.
5288 	 */
5289 	avd = bvd;
5290 	spa_alt_rootvdev(rvd, &avd, &txg);
5291 	if (avd != bvd) {
5292 		cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
5293 		    "try booting from '%s'", avd->vdev_path);
5294 		error = SET_ERROR(EINVAL);
5295 		goto out;
5296 	}
5297 
5298 	/*
5299 	 * If the boot device is part of a spare vdev then ensure that
5300 	 * we're booting off the active spare.
5301 	 */
5302 	if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
5303 	    !bvd->vdev_isspare) {
5304 		cmn_err(CE_NOTE, "The boot device is currently spared. Please "
5305 		    "try booting from '%s'",
5306 		    bvd->vdev_parent->
5307 		    vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
5308 		error = SET_ERROR(EINVAL);
5309 		goto out;
5310 	}
5311 
5312 	error = 0;
5313 out:
5314 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5315 	vdev_free(rvd);
5316 	spa_config_exit(spa, SCL_ALL, FTAG);
5317 	mutex_exit(&spa_namespace_lock);
5318 
5319 	nvlist_free(config);
5320 	return (error);
5321 }
5322 
5323 #endif
5324 
5325 /*
5326  * Import a non-root pool into the system.
5327  */
5328 int
5329 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
5330 {
5331 	spa_t *spa;
5332 	char *altroot = NULL;
5333 	spa_load_state_t state = SPA_LOAD_IMPORT;
5334 	zpool_load_policy_t policy;
5335 	uint64_t mode = spa_mode_global;
5336 	uint64_t readonly = B_FALSE;
5337 	int error;
5338 	nvlist_t *nvroot;
5339 	nvlist_t **spares, **l2cache;
5340 	uint_t nspares, nl2cache;
5341 
5342 	/*
5343 	 * If a pool with this name exists, return failure.
5344 	 */
5345 	mutex_enter(&spa_namespace_lock);
5346 	if (spa_lookup(pool) != NULL) {
5347 		mutex_exit(&spa_namespace_lock);
5348 		return (SET_ERROR(EEXIST));
5349 	}
5350 
5351 	/*
5352 	 * Create and initialize the spa structure.
5353 	 */
5354 	(void) nvlist_lookup_string(props,
5355 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5356 	(void) nvlist_lookup_uint64(props,
5357 	    zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
5358 	if (readonly)
5359 		mode = FREAD;
5360 	spa = spa_add(pool, config, altroot);
5361 	spa->spa_import_flags = flags;
5362 
5363 	/*
5364 	 * Verbatim import - Take a pool and insert it into the namespace
5365 	 * as if it had been loaded at boot.
5366 	 */
5367 	if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
5368 		if (props != NULL)
5369 			spa_configfile_set(spa, props, B_FALSE);
5370 
5371 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
5372 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
5373 		zfs_dbgmsg("spa_import: verbatim import of %s", pool);
5374 		mutex_exit(&spa_namespace_lock);
5375 		return (0);
5376 	}
5377 
5378 	spa_activate(spa, mode);
5379 
5380 	/*
5381 	 * Don't start async tasks until we know everything is healthy.
5382 	 */
5383 	spa_async_suspend(spa);
5384 
5385 	zpool_get_load_policy(config, &policy);
5386 	if (policy.zlp_rewind & ZPOOL_DO_REWIND)
5387 		state = SPA_LOAD_RECOVER;
5388 
5389 	spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT;
5390 
5391 	if (state != SPA_LOAD_RECOVER) {
5392 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
5393 		zfs_dbgmsg("spa_import: importing %s", pool);
5394 	} else {
5395 		zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
5396 		    "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg);
5397 	}
5398 	error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind);
5399 
5400 	/*
5401 	 * Propagate anything learned while loading the pool and pass it
5402 	 * back to caller (i.e. rewind info, missing devices, etc).
5403 	 */
5404 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
5405 	    spa->spa_load_info) == 0);
5406 
5407 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5408 	/*
5409 	 * Toss any existing sparelist, as it doesn't have any validity
5410 	 * anymore, and conflicts with spa_has_spare().
5411 	 */
5412 	if (spa->spa_spares.sav_config) {
5413 		nvlist_free(spa->spa_spares.sav_config);
5414 		spa->spa_spares.sav_config = NULL;
5415 		spa_load_spares(spa);
5416 	}
5417 	if (spa->spa_l2cache.sav_config) {
5418 		nvlist_free(spa->spa_l2cache.sav_config);
5419 		spa->spa_l2cache.sav_config = NULL;
5420 		spa_load_l2cache(spa);
5421 	}
5422 
5423 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5424 	    &nvroot) == 0);
5425 	if (error == 0)
5426 		error = spa_validate_aux(spa, nvroot, -1ULL,
5427 		    VDEV_ALLOC_SPARE);
5428 	if (error == 0)
5429 		error = spa_validate_aux(spa, nvroot, -1ULL,
5430 		    VDEV_ALLOC_L2CACHE);
5431 	spa_config_exit(spa, SCL_ALL, FTAG);
5432 
5433 	if (props != NULL)
5434 		spa_configfile_set(spa, props, B_FALSE);
5435 
5436 	if (error != 0 || (props && spa_writeable(spa) &&
5437 	    (error = spa_prop_set(spa, props)))) {
5438 		spa_unload(spa);
5439 		spa_deactivate(spa);
5440 		spa_remove(spa);
5441 		mutex_exit(&spa_namespace_lock);
5442 		return (error);
5443 	}
5444 
5445 	spa_async_resume(spa);
5446 
5447 	/*
5448 	 * Override any spares and level 2 cache devices as specified by
5449 	 * the user, as these may have correct device names/devids, etc.
5450 	 */
5451 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
5452 	    &spares, &nspares) == 0) {
5453 		if (spa->spa_spares.sav_config)
5454 			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
5455 			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
5456 		else
5457 			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
5458 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
5459 		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
5460 		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
5461 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5462 		spa_load_spares(spa);
5463 		spa_config_exit(spa, SCL_ALL, FTAG);
5464 		spa->spa_spares.sav_sync = B_TRUE;
5465 	}
5466 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
5467 	    &l2cache, &nl2cache) == 0) {
5468 		if (spa->spa_l2cache.sav_config)
5469 			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
5470 			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
5471 		else
5472 			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
5473 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
5474 		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
5475 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
5476 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5477 		spa_load_l2cache(spa);
5478 		spa_config_exit(spa, SCL_ALL, FTAG);
5479 		spa->spa_l2cache.sav_sync = B_TRUE;
5480 	}
5481 
5482 	/*
5483 	 * Check for any removed devices.
5484 	 */
5485 	if (spa->spa_autoreplace) {
5486 		spa_aux_check_removed(&spa->spa_spares);
5487 		spa_aux_check_removed(&spa->spa_l2cache);
5488 	}
5489 
5490 	if (spa_writeable(spa)) {
5491 		/*
5492 		 * Update the config cache to include the newly-imported pool.
5493 		 */
5494 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5495 	}
5496 
5497 	/*
5498 	 * It's possible that the pool was expanded while it was exported.
5499 	 * We kick off an async task to handle this for us.
5500 	 */
5501 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
5502 
5503 	spa_history_log_version(spa, "import");
5504 
5505 	spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
5506 
5507 	mutex_exit(&spa_namespace_lock);
5508 
5509 	return (0);
5510 }
5511 
5512 nvlist_t *
5513 spa_tryimport(nvlist_t *tryconfig)
5514 {
5515 	nvlist_t *config = NULL;
5516 	char *poolname, *cachefile;
5517 	spa_t *spa;
5518 	uint64_t state;
5519 	int error;
5520 	zpool_load_policy_t policy;
5521 
5522 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
5523 		return (NULL);
5524 
5525 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
5526 		return (NULL);
5527 
5528 	/*
5529 	 * Create and initialize the spa structure.
5530 	 */
5531 	mutex_enter(&spa_namespace_lock);
5532 	spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
5533 	spa_activate(spa, FREAD);
5534 
5535 	/*
5536 	 * Rewind pool if a max txg was provided.
5537 	 */
5538 	zpool_get_load_policy(spa->spa_config, &policy);
5539 	if (policy.zlp_txg != UINT64_MAX) {
5540 		spa->spa_load_max_txg = policy.zlp_txg;
5541 		spa->spa_extreme_rewind = B_TRUE;
5542 		zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
5543 		    poolname, (longlong_t)policy.zlp_txg);
5544 	} else {
5545 		zfs_dbgmsg("spa_tryimport: importing %s", poolname);
5546 	}
5547 
5548 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile)
5549 	    == 0) {
5550 		zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile);
5551 		spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE;
5552 	} else {
5553 		spa->spa_config_source = SPA_CONFIG_SRC_SCAN;
5554 	}
5555 
5556 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING);
5557 
5558 	/*
5559 	 * If 'tryconfig' was at least parsable, return the current config.
5560 	 */
5561 	if (spa->spa_root_vdev != NULL) {
5562 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
5563 		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
5564 		    poolname) == 0);
5565 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5566 		    state) == 0);
5567 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
5568 		    spa->spa_uberblock.ub_timestamp) == 0);
5569 		VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
5570 		    spa->spa_load_info) == 0);
5571 
5572 		/*
5573 		 * If the bootfs property exists on this pool then we
5574 		 * copy it out so that external consumers can tell which
5575 		 * pools are bootable.
5576 		 */
5577 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
5578 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5579 
5580 			/*
5581 			 * We have to play games with the name since the
5582 			 * pool was opened as TRYIMPORT_NAME.
5583 			 */
5584 			if (dsl_dsobj_to_dsname(spa_name(spa),
5585 			    spa->spa_bootfs, tmpname) == 0) {
5586 				char *cp;
5587 				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5588 
5589 				cp = strchr(tmpname, '/');
5590 				if (cp == NULL) {
5591 					(void) strlcpy(dsname, tmpname,
5592 					    MAXPATHLEN);
5593 				} else {
5594 					(void) snprintf(dsname, MAXPATHLEN,
5595 					    "%s/%s", poolname, ++cp);
5596 				}
5597 				VERIFY(nvlist_add_string(config,
5598 				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
5599 				kmem_free(dsname, MAXPATHLEN);
5600 			}
5601 			kmem_free(tmpname, MAXPATHLEN);
5602 		}
5603 
5604 		/*
5605 		 * Add the list of hot spares and level 2 cache devices.
5606 		 */
5607 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5608 		spa_add_spares(spa, config);
5609 		spa_add_l2cache(spa, config);
5610 		spa_config_exit(spa, SCL_CONFIG, FTAG);
5611 	}
5612 
5613 	spa_unload(spa);
5614 	spa_deactivate(spa);
5615 	spa_remove(spa);
5616 	mutex_exit(&spa_namespace_lock);
5617 
5618 	return (config);
5619 }
5620 
5621 /*
5622  * Pool export/destroy
5623  *
5624  * The act of destroying or exporting a pool is very simple.  We make sure there
5625  * is no more pending I/O and any references to the pool are gone.  Then, we
5626  * update the pool state and sync all the labels to disk, removing the
5627  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
5628  * we don't sync the labels or remove the configuration cache.
5629  */
5630 static int
5631 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
5632     boolean_t force, boolean_t hardforce)
5633 {
5634 	spa_t *spa;
5635 
5636 	if (oldconfig)
5637 		*oldconfig = NULL;
5638 
5639 	if (!(spa_mode_global & FWRITE))
5640 		return (SET_ERROR(EROFS));
5641 
5642 	mutex_enter(&spa_namespace_lock);
5643 	if ((spa = spa_lookup(pool)) == NULL) {
5644 		mutex_exit(&spa_namespace_lock);
5645 		return (SET_ERROR(ENOENT));
5646 	}
5647 
5648 	/*
5649 	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
5650 	 * reacquire the namespace lock, and see if we can export.
5651 	 */
5652 	spa_open_ref(spa, FTAG);
5653 	mutex_exit(&spa_namespace_lock);
5654 	spa_async_suspend(spa);
5655 	mutex_enter(&spa_namespace_lock);
5656 	spa_close(spa, FTAG);
5657 
5658 	/*
5659 	 * The pool will be in core if it's openable,
5660 	 * in which case we can modify its state.
5661 	 */
5662 	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
5663 
5664 		/*
5665 		 * Objsets may be open only because they're dirty, so we
5666 		 * have to force it to sync before checking spa_refcnt.
5667 		 */
5668 		txg_wait_synced(spa->spa_dsl_pool, 0);
5669 		spa_evicting_os_wait(spa);
5670 
5671 		/*
5672 		 * A pool cannot be exported or destroyed if there are active
5673 		 * references.  If we are resetting a pool, allow references by
5674 		 * fault injection handlers.
5675 		 */
5676 		if (!spa_refcount_zero(spa) ||
5677 		    (spa->spa_inject_ref != 0 &&
5678 		    new_state != POOL_STATE_UNINITIALIZED)) {
5679 			spa_async_resume(spa);
5680 			mutex_exit(&spa_namespace_lock);
5681 			return (SET_ERROR(EBUSY));
5682 		}
5683 
5684 		/*
5685 		 * A pool cannot be exported if it has an active shared spare.
5686 		 * This is to prevent other pools stealing the active spare
5687 		 * from an exported pool. At user's own will, such pool can
5688 		 * be forcedly exported.
5689 		 */
5690 		if (!force && new_state == POOL_STATE_EXPORTED &&
5691 		    spa_has_active_shared_spare(spa)) {
5692 			spa_async_resume(spa);
5693 			mutex_exit(&spa_namespace_lock);
5694 			return (SET_ERROR(EXDEV));
5695 		}
5696 
5697 		/*
5698 		 * We're about to export or destroy this pool. Make sure
5699 		 * we stop all initializtion activity here before we
5700 		 * set the spa_final_txg. This will ensure that all
5701 		 * dirty data resulting from the initialization is
5702 		 * committed to disk before we unload the pool.
5703 		 */
5704 		if (spa->spa_root_vdev != NULL) {
5705 			vdev_initialize_stop_all(spa->spa_root_vdev,
5706 			    VDEV_INITIALIZE_ACTIVE);
5707 		}
5708 
5709 		/*
5710 		 * We want this to be reflected on every label,
5711 		 * so mark them all dirty.  spa_unload() will do the
5712 		 * final sync that pushes these changes out.
5713 		 */
5714 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
5715 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5716 			spa->spa_state = new_state;
5717 			spa->spa_final_txg = spa_last_synced_txg(spa) +
5718 			    TXG_DEFER_SIZE + 1;
5719 			vdev_config_dirty(spa->spa_root_vdev);
5720 			spa_config_exit(spa, SCL_ALL, FTAG);
5721 		}
5722 	}
5723 
5724 	spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY);
5725 
5726 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5727 		spa_unload(spa);
5728 		spa_deactivate(spa);
5729 	}
5730 
5731 	if (oldconfig && spa->spa_config)
5732 		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
5733 
5734 	if (new_state != POOL_STATE_UNINITIALIZED) {
5735 		if (!hardforce)
5736 			spa_write_cachefile(spa, B_TRUE, B_TRUE);
5737 		spa_remove(spa);
5738 	}
5739 	mutex_exit(&spa_namespace_lock);
5740 
5741 	return (0);
5742 }
5743 
5744 /*
5745  * Destroy a storage pool.
5746  */
5747 int
5748 spa_destroy(char *pool)
5749 {
5750 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
5751 	    B_FALSE, B_FALSE));
5752 }
5753 
5754 /*
5755  * Export a storage pool.
5756  */
5757 int
5758 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
5759     boolean_t hardforce)
5760 {
5761 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
5762 	    force, hardforce));
5763 }
5764 
5765 /*
5766  * Similar to spa_export(), this unloads the spa_t without actually removing it
5767  * from the namespace in any way.
5768  */
5769 int
5770 spa_reset(char *pool)
5771 {
5772 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
5773 	    B_FALSE, B_FALSE));
5774 }
5775 
5776 /*
5777  * ==========================================================================
5778  * Device manipulation
5779  * ==========================================================================
5780  */
5781 
5782 /*
5783  * Add a device to a storage pool.
5784  */
5785 int
5786 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
5787 {
5788 	uint64_t txg, id;
5789 	int error;
5790 	vdev_t *rvd = spa->spa_root_vdev;
5791 	vdev_t *vd, *tvd;
5792 	nvlist_t **spares, **l2cache;
5793 	uint_t nspares, nl2cache;
5794 
5795 	ASSERT(spa_writeable(spa));
5796 
5797 	txg = spa_vdev_enter(spa);
5798 
5799 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
5800 	    VDEV_ALLOC_ADD)) != 0)
5801 		return (spa_vdev_exit(spa, NULL, txg, error));
5802 
5803 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
5804 
5805 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
5806 	    &nspares) != 0)
5807 		nspares = 0;
5808 
5809 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
5810 	    &nl2cache) != 0)
5811 		nl2cache = 0;
5812 
5813 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
5814 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
5815 
5816 	if (vd->vdev_children != 0 &&
5817 	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
5818 		return (spa_vdev_exit(spa, vd, txg, error));
5819 
5820 	/*
5821 	 * We must validate the spares and l2cache devices after checking the
5822 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
5823 	 */
5824 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
5825 		return (spa_vdev_exit(spa, vd, txg, error));
5826 
5827 	/*
5828 	 * If we are in the middle of a device removal, we can only add
5829 	 * devices which match the existing devices in the pool.
5830 	 * If we are in the middle of a removal, or have some indirect
5831 	 * vdevs, we can not add raidz toplevels.
5832 	 */
5833 	if (spa->spa_vdev_removal != NULL ||
5834 	    spa->spa_removing_phys.sr_prev_indirect_vdev != -1) {
5835 		for (int c = 0; c < vd->vdev_children; c++) {
5836 			tvd = vd->vdev_child[c];
5837 			if (spa->spa_vdev_removal != NULL &&
5838 			    tvd->vdev_ashift != spa->spa_max_ashift) {
5839 				return (spa_vdev_exit(spa, vd, txg, EINVAL));
5840 			}
5841 			/* Fail if top level vdev is raidz */
5842 			if (tvd->vdev_ops == &vdev_raidz_ops) {
5843 				return (spa_vdev_exit(spa, vd, txg, EINVAL));
5844 			}
5845 			/*
5846 			 * Need the top level mirror to be
5847 			 * a mirror of leaf vdevs only
5848 			 */
5849 			if (tvd->vdev_ops == &vdev_mirror_ops) {
5850 				for (uint64_t cid = 0;
5851 				    cid < tvd->vdev_children; cid++) {
5852 					vdev_t *cvd = tvd->vdev_child[cid];
5853 					if (!cvd->vdev_ops->vdev_op_leaf) {
5854 						return (spa_vdev_exit(spa, vd,
5855 						    txg, EINVAL));
5856 					}
5857 				}
5858 			}
5859 		}
5860 	}
5861 
5862 	for (int c = 0; c < vd->vdev_children; c++) {
5863 
5864 		/*
5865 		 * Set the vdev id to the first hole, if one exists.
5866 		 */
5867 		for (id = 0; id < rvd->vdev_children; id++) {
5868 			if (rvd->vdev_child[id]->vdev_ishole) {
5869 				vdev_free(rvd->vdev_child[id]);
5870 				break;
5871 			}
5872 		}
5873 		tvd = vd->vdev_child[c];
5874 		vdev_remove_child(vd, tvd);
5875 		tvd->vdev_id = id;
5876 		vdev_add_child(rvd, tvd);
5877 		vdev_config_dirty(tvd);
5878 	}
5879 
5880 	if (nspares != 0) {
5881 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
5882 		    ZPOOL_CONFIG_SPARES);
5883 		spa_load_spares(spa);
5884 		spa->spa_spares.sav_sync = B_TRUE;
5885 	}
5886 
5887 	if (nl2cache != 0) {
5888 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
5889 		    ZPOOL_CONFIG_L2CACHE);
5890 		spa_load_l2cache(spa);
5891 		spa->spa_l2cache.sav_sync = B_TRUE;
5892 	}
5893 
5894 	/*
5895 	 * We have to be careful when adding new vdevs to an existing pool.
5896 	 * If other threads start allocating from these vdevs before we
5897 	 * sync the config cache, and we lose power, then upon reboot we may
5898 	 * fail to open the pool because there are DVAs that the config cache
5899 	 * can't translate.  Therefore, we first add the vdevs without
5900 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
5901 	 * and then let spa_config_update() initialize the new metaslabs.
5902 	 *
5903 	 * spa_load() checks for added-but-not-initialized vdevs, so that
5904 	 * if we lose power at any point in this sequence, the remaining
5905 	 * steps will be completed the next time we load the pool.
5906 	 */
5907 	(void) spa_vdev_exit(spa, vd, txg, 0);
5908 
5909 	mutex_enter(&spa_namespace_lock);
5910 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5911 	spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD);
5912 	mutex_exit(&spa_namespace_lock);
5913 
5914 	return (0);
5915 }
5916 
5917 /*
5918  * Attach a device to a mirror.  The arguments are the path to any device
5919  * in the mirror, and the nvroot for the new device.  If the path specifies
5920  * a device that is not mirrored, we automatically insert the mirror vdev.
5921  *
5922  * If 'replacing' is specified, the new device is intended to replace the
5923  * existing device; in this case the two devices are made into their own
5924  * mirror using the 'replacing' vdev, which is functionally identical to
5925  * the mirror vdev (it actually reuses all the same ops) but has a few
5926  * extra rules: you can't attach to it after it's been created, and upon
5927  * completion of resilvering, the first disk (the one being replaced)
5928  * is automatically detached.
5929  */
5930 int
5931 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
5932 {
5933 	uint64_t txg, dtl_max_txg;
5934 	vdev_t *rvd = spa->spa_root_vdev;
5935 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
5936 	vdev_ops_t *pvops;
5937 	char *oldvdpath, *newvdpath;
5938 	int newvd_isspare;
5939 	int error;
5940 
5941 	ASSERT(spa_writeable(spa));
5942 
5943 	txg = spa_vdev_enter(spa);
5944 
5945 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
5946 
5947 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
5948 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
5949 		error = (spa_has_checkpoint(spa)) ?
5950 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
5951 		return (spa_vdev_exit(spa, NULL, txg, error));
5952 	}
5953 
5954 	if (spa->spa_vdev_removal != NULL)
5955 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5956 
5957 	if (oldvd == NULL)
5958 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
5959 
5960 	if (!oldvd->vdev_ops->vdev_op_leaf)
5961 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5962 
5963 	pvd = oldvd->vdev_parent;
5964 
5965 	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
5966 	    VDEV_ALLOC_ATTACH)) != 0)
5967 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5968 
5969 	if (newrootvd->vdev_children != 1)
5970 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
5971 
5972 	newvd = newrootvd->vdev_child[0];
5973 
5974 	if (!newvd->vdev_ops->vdev_op_leaf)
5975 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
5976 
5977 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
5978 		return (spa_vdev_exit(spa, newrootvd, txg, error));
5979 
5980 	/*
5981 	 * Spares can't replace logs
5982 	 */
5983 	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
5984 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
5985 
5986 	if (!replacing) {
5987 		/*
5988 		 * For attach, the only allowable parent is a mirror or the root
5989 		 * vdev.
5990 		 */
5991 		if (pvd->vdev_ops != &vdev_mirror_ops &&
5992 		    pvd->vdev_ops != &vdev_root_ops)
5993 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
5994 
5995 		pvops = &vdev_mirror_ops;
5996 	} else {
5997 		/*
5998 		 * Active hot spares can only be replaced by inactive hot
5999 		 * spares.
6000 		 */
6001 		if (pvd->vdev_ops == &vdev_spare_ops &&
6002 		    oldvd->vdev_isspare &&
6003 		    !spa_has_spare(spa, newvd->vdev_guid))
6004 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
6005 
6006 		/*
6007 		 * If the source is a hot spare, and the parent isn't already a
6008 		 * spare, then we want to create a new hot spare.  Otherwise, we
6009 		 * want to create a replacing vdev.  The user is not allowed to
6010 		 * attach to a spared vdev child unless the 'isspare' state is
6011 		 * the same (spare replaces spare, non-spare replaces
6012 		 * non-spare).
6013 		 */
6014 		if (pvd->vdev_ops == &vdev_replacing_ops &&
6015 		    spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
6016 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
6017 		} else if (pvd->vdev_ops == &vdev_spare_ops &&
6018 		    newvd->vdev_isspare != oldvd->vdev_isspare) {
6019 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
6020 		}
6021 
6022 		if (newvd->vdev_isspare)
6023 			pvops = &vdev_spare_ops;
6024 		else
6025 			pvops = &vdev_replacing_ops;
6026 	}
6027 
6028 	/*
6029 	 * Make sure the new device is big enough.
6030 	 */
6031 	if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
6032 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
6033 
6034 	/*
6035 	 * The new device cannot have a higher alignment requirement
6036 	 * than the top-level vdev.
6037 	 */
6038 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
6039 		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
6040 
6041 	/*
6042 	 * If this is an in-place replacement, update oldvd's path and devid
6043 	 * to make it distinguishable from newvd, and unopenable from now on.
6044 	 */
6045 	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
6046 		spa_strfree(oldvd->vdev_path);
6047 		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
6048 		    KM_SLEEP);
6049 		(void) sprintf(oldvd->vdev_path, "%s/%s",
6050 		    newvd->vdev_path, "old");
6051 		if (oldvd->vdev_devid != NULL) {
6052 			spa_strfree(oldvd->vdev_devid);
6053 			oldvd->vdev_devid = NULL;
6054 		}
6055 	}
6056 
6057 	/* mark the device being resilvered */
6058 	newvd->vdev_resilver_txg = txg;
6059 
6060 	/*
6061 	 * If the parent is not a mirror, or if we're replacing, insert the new
6062 	 * mirror/replacing/spare vdev above oldvd.
6063 	 */
6064 	if (pvd->vdev_ops != pvops)
6065 		pvd = vdev_add_parent(oldvd, pvops);
6066 
6067 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
6068 	ASSERT(pvd->vdev_ops == pvops);
6069 	ASSERT(oldvd->vdev_parent == pvd);
6070 
6071 	/*
6072 	 * Extract the new device from its root and add it to pvd.
6073 	 */
6074 	vdev_remove_child(newrootvd, newvd);
6075 	newvd->vdev_id = pvd->vdev_children;
6076 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
6077 	vdev_add_child(pvd, newvd);
6078 
6079 	tvd = newvd->vdev_top;
6080 	ASSERT(pvd->vdev_top == tvd);
6081 	ASSERT(tvd->vdev_parent == rvd);
6082 
6083 	vdev_config_dirty(tvd);
6084 
6085 	/*
6086 	 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
6087 	 * for any dmu_sync-ed blocks.  It will propagate upward when
6088 	 * spa_vdev_exit() calls vdev_dtl_reassess().
6089 	 */
6090 	dtl_max_txg = txg + TXG_CONCURRENT_STATES;
6091 
6092 	vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
6093 	    dtl_max_txg - TXG_INITIAL);
6094 
6095 	if (newvd->vdev_isspare) {
6096 		spa_spare_activate(newvd);
6097 		spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE);
6098 	}
6099 
6100 	oldvdpath = spa_strdup(oldvd->vdev_path);
6101 	newvdpath = spa_strdup(newvd->vdev_path);
6102 	newvd_isspare = newvd->vdev_isspare;
6103 
6104 	/*
6105 	 * Mark newvd's DTL dirty in this txg.
6106 	 */
6107 	vdev_dirty(tvd, VDD_DTL, newvd, txg);
6108 
6109 	/*
6110 	 * Schedule the resilver to restart in the future. We do this to
6111 	 * ensure that dmu_sync-ed blocks have been stitched into the
6112 	 * respective datasets.
6113 	 */
6114 	dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
6115 
6116 	if (spa->spa_bootfs)
6117 		spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH);
6118 
6119 	spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH);
6120 
6121 	/*
6122 	 * Commit the config
6123 	 */
6124 	(void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
6125 
6126 	spa_history_log_internal(spa, "vdev attach", NULL,
6127 	    "%s vdev=%s %s vdev=%s",
6128 	    replacing && newvd_isspare ? "spare in" :
6129 	    replacing ? "replace" : "attach", newvdpath,
6130 	    replacing ? "for" : "to", oldvdpath);
6131 
6132 	spa_strfree(oldvdpath);
6133 	spa_strfree(newvdpath);
6134 
6135 	return (0);
6136 }
6137 
6138 /*
6139  * Detach a device from a mirror or replacing vdev.
6140  *
6141  * If 'replace_done' is specified, only detach if the parent
6142  * is a replacing vdev.
6143  */
6144 int
6145 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
6146 {
6147 	uint64_t txg;
6148 	int error;
6149 	vdev_t *rvd = spa->spa_root_vdev;
6150 	vdev_t *vd, *pvd, *cvd, *tvd;
6151 	boolean_t unspare = B_FALSE;
6152 	uint64_t unspare_guid = 0;
6153 	char *vdpath;
6154 
6155 	ASSERT(spa_writeable(spa));
6156 
6157 	txg = spa_vdev_enter(spa);
6158 
6159 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
6160 
6161 	/*
6162 	 * Besides being called directly from the userland through the
6163 	 * ioctl interface, spa_vdev_detach() can be potentially called
6164 	 * at the end of spa_vdev_resilver_done().
6165 	 *
6166 	 * In the regular case, when we have a checkpoint this shouldn't
6167 	 * happen as we never empty the DTLs of a vdev during the scrub
6168 	 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
6169 	 * should never get here when we have a checkpoint.
6170 	 *
6171 	 * That said, even in a case when we checkpoint the pool exactly
6172 	 * as spa_vdev_resilver_done() calls this function everything
6173 	 * should be fine as the resilver will return right away.
6174 	 */
6175 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
6176 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
6177 		error = (spa_has_checkpoint(spa)) ?
6178 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
6179 		return (spa_vdev_exit(spa, NULL, txg, error));
6180 	}
6181 
6182 	if (vd == NULL)
6183 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
6184 
6185 	if (!vd->vdev_ops->vdev_op_leaf)
6186 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
6187 
6188 	pvd = vd->vdev_parent;
6189 
6190 	/*
6191 	 * If the parent/child relationship is not as expected, don't do it.
6192 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
6193 	 * vdev that's replacing B with C.  The user's intent in replacing
6194 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
6195 	 * the replace by detaching C, the expected behavior is to end up
6196 	 * M(A,B).  But suppose that right after deciding to detach C,
6197 	 * the replacement of B completes.  We would have M(A,C), and then
6198 	 * ask to detach C, which would leave us with just A -- not what
6199 	 * the user wanted.  To prevent this, we make sure that the
6200 	 * parent/child relationship hasn't changed -- in this example,
6201 	 * that C's parent is still the replacing vdev R.
6202 	 */
6203 	if (pvd->vdev_guid != pguid && pguid != 0)
6204 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
6205 
6206 	/*
6207 	 * Only 'replacing' or 'spare' vdevs can be replaced.
6208 	 */
6209 	if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
6210 	    pvd->vdev_ops != &vdev_spare_ops)
6211 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
6212 
6213 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
6214 	    spa_version(spa) >= SPA_VERSION_SPARES);
6215 
6216 	/*
6217 	 * Only mirror, replacing, and spare vdevs support detach.
6218 	 */
6219 	if (pvd->vdev_ops != &vdev_replacing_ops &&
6220 	    pvd->vdev_ops != &vdev_mirror_ops &&
6221 	    pvd->vdev_ops != &vdev_spare_ops)
6222 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
6223 
6224 	/*
6225 	 * If this device has the only valid copy of some data,
6226 	 * we cannot safely detach it.
6227 	 */
6228 	if (vdev_dtl_required(vd))
6229 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
6230 
6231 	ASSERT(pvd->vdev_children >= 2);
6232 
6233 	/*
6234 	 * If we are detaching the second disk from a replacing vdev, then
6235 	 * check to see if we changed the original vdev's path to have "/old"
6236 	 * at the end in spa_vdev_attach().  If so, undo that change now.
6237 	 */
6238 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
6239 	    vd->vdev_path != NULL) {
6240 		size_t len = strlen(vd->vdev_path);
6241 
6242 		for (int c = 0; c < pvd->vdev_children; c++) {
6243 			cvd = pvd->vdev_child[c];
6244 
6245 			if (cvd == vd || cvd->vdev_path == NULL)
6246 				continue;
6247 
6248 			if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
6249 			    strcmp(cvd->vdev_path + len, "/old") == 0) {
6250 				spa_strfree(cvd->vdev_path);
6251 				cvd->vdev_path = spa_strdup(vd->vdev_path);
6252 				break;
6253 			}
6254 		}
6255 	}
6256 
6257 	/*
6258 	 * If we are detaching the original disk from a spare, then it implies
6259 	 * that the spare should become a real disk, and be removed from the
6260 	 * active spare list for the pool.
6261 	 */
6262 	if (pvd->vdev_ops == &vdev_spare_ops &&
6263 	    vd->vdev_id == 0 &&
6264 	    pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
6265 		unspare = B_TRUE;
6266 
6267 	/*
6268 	 * Erase the disk labels so the disk can be used for other things.
6269 	 * This must be done after all other error cases are handled,
6270 	 * but before we disembowel vd (so we can still do I/O to it).
6271 	 * But if we can't do it, don't treat the error as fatal --
6272 	 * it may be that the unwritability of the disk is the reason
6273 	 * it's being detached!
6274 	 */
6275 	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
6276 
6277 	/*
6278 	 * Remove vd from its parent and compact the parent's children.
6279 	 */
6280 	vdev_remove_child(pvd, vd);
6281 	vdev_compact_children(pvd);
6282 
6283 	/*
6284 	 * Remember one of the remaining children so we can get tvd below.
6285 	 */
6286 	cvd = pvd->vdev_child[pvd->vdev_children - 1];
6287 
6288 	/*
6289 	 * If we need to remove the remaining child from the list of hot spares,
6290 	 * do it now, marking the vdev as no longer a spare in the process.
6291 	 * We must do this before vdev_remove_parent(), because that can
6292 	 * change the GUID if it creates a new toplevel GUID.  For a similar
6293 	 * reason, we must remove the spare now, in the same txg as the detach;
6294 	 * otherwise someone could attach a new sibling, change the GUID, and
6295 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
6296 	 */
6297 	if (unspare) {
6298 		ASSERT(cvd->vdev_isspare);
6299 		spa_spare_remove(cvd);
6300 		unspare_guid = cvd->vdev_guid;
6301 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
6302 		cvd->vdev_unspare = B_TRUE;
6303 	}
6304 
6305 	/*
6306 	 * If the parent mirror/replacing vdev only has one child,
6307 	 * the parent is no longer needed.  Remove it from the tree.
6308 	 */
6309 	if (pvd->vdev_children == 1) {
6310 		if (pvd->vdev_ops == &vdev_spare_ops)
6311 			cvd->vdev_unspare = B_FALSE;
6312 		vdev_remove_parent(cvd);
6313 	}
6314 
6315 
6316 	/*
6317 	 * We don't set tvd until now because the parent we just removed
6318 	 * may have been the previous top-level vdev.
6319 	 */
6320 	tvd = cvd->vdev_top;
6321 	ASSERT(tvd->vdev_parent == rvd);
6322 
6323 	/*
6324 	 * Reevaluate the parent vdev state.
6325 	 */
6326 	vdev_propagate_state(cvd);
6327 
6328 	/*
6329 	 * If the 'autoexpand' property is set on the pool then automatically
6330 	 * try to expand the size of the pool. For example if the device we
6331 	 * just detached was smaller than the others, it may be possible to
6332 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
6333 	 * first so that we can obtain the updated sizes of the leaf vdevs.
6334 	 */
6335 	if (spa->spa_autoexpand) {
6336 		vdev_reopen(tvd);
6337 		vdev_expand(tvd, txg);
6338 	}
6339 
6340 	vdev_config_dirty(tvd);
6341 
6342 	/*
6343 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
6344 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
6345 	 * But first make sure we're not on any *other* txg's DTL list, to
6346 	 * prevent vd from being accessed after it's freed.
6347 	 */
6348 	vdpath = spa_strdup(vd->vdev_path);
6349 	for (int t = 0; t < TXG_SIZE; t++)
6350 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
6351 	vd->vdev_detached = B_TRUE;
6352 	vdev_dirty(tvd, VDD_DTL, vd, txg);
6353 
6354 	spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE);
6355 
6356 	/* hang on to the spa before we release the lock */
6357 	spa_open_ref(spa, FTAG);
6358 
6359 	error = spa_vdev_exit(spa, vd, txg, 0);
6360 
6361 	spa_history_log_internal(spa, "detach", NULL,
6362 	    "vdev=%s", vdpath);
6363 	spa_strfree(vdpath);
6364 
6365 	/*
6366 	 * If this was the removal of the original device in a hot spare vdev,
6367 	 * then we want to go through and remove the device from the hot spare
6368 	 * list of every other pool.
6369 	 */
6370 	if (unspare) {
6371 		spa_t *altspa = NULL;
6372 
6373 		mutex_enter(&spa_namespace_lock);
6374 		while ((altspa = spa_next(altspa)) != NULL) {
6375 			if (altspa->spa_state != POOL_STATE_ACTIVE ||
6376 			    altspa == spa)
6377 				continue;
6378 
6379 			spa_open_ref(altspa, FTAG);
6380 			mutex_exit(&spa_namespace_lock);
6381 			(void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
6382 			mutex_enter(&spa_namespace_lock);
6383 			spa_close(altspa, FTAG);
6384 		}
6385 		mutex_exit(&spa_namespace_lock);
6386 
6387 		/* search the rest of the vdevs for spares to remove */
6388 		spa_vdev_resilver_done(spa);
6389 	}
6390 
6391 	/* all done with the spa; OK to release */
6392 	mutex_enter(&spa_namespace_lock);
6393 	spa_close(spa, FTAG);
6394 	mutex_exit(&spa_namespace_lock);
6395 
6396 	return (error);
6397 }
6398 
6399 int
6400 spa_vdev_initialize(spa_t *spa, uint64_t guid, uint64_t cmd_type)
6401 {
6402 	/*
6403 	 * We hold the namespace lock through the whole function
6404 	 * to prevent any changes to the pool while we're starting or
6405 	 * stopping initialization. The config and state locks are held so that
6406 	 * we can properly assess the vdev state before we commit to
6407 	 * the initializing operation.
6408 	 */
6409 	mutex_enter(&spa_namespace_lock);
6410 	spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6411 
6412 	/* Look up vdev and ensure it's a leaf. */
6413 	vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
6414 	if (vd == NULL || vd->vdev_detached) {
6415 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6416 		mutex_exit(&spa_namespace_lock);
6417 		return (SET_ERROR(ENODEV));
6418 	} else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) {
6419 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6420 		mutex_exit(&spa_namespace_lock);
6421 		return (SET_ERROR(EINVAL));
6422 	} else if (!vdev_writeable(vd)) {
6423 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6424 		mutex_exit(&spa_namespace_lock);
6425 		return (SET_ERROR(EROFS));
6426 	}
6427 	mutex_enter(&vd->vdev_initialize_lock);
6428 	spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6429 
6430 	/*
6431 	 * When we activate an initialize action we check to see
6432 	 * if the vdev_initialize_thread is NULL. We do this instead
6433 	 * of using the vdev_initialize_state since there might be
6434 	 * a previous initialization process which has completed but
6435 	 * the thread is not exited.
6436 	 */
6437 	if (cmd_type == POOL_INITIALIZE_DO &&
6438 	    (vd->vdev_initialize_thread != NULL ||
6439 	    vd->vdev_top->vdev_removing)) {
6440 		mutex_exit(&vd->vdev_initialize_lock);
6441 		mutex_exit(&spa_namespace_lock);
6442 		return (SET_ERROR(EBUSY));
6443 	} else if (cmd_type == POOL_INITIALIZE_CANCEL &&
6444 	    (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE &&
6445 	    vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) {
6446 		mutex_exit(&vd->vdev_initialize_lock);
6447 		mutex_exit(&spa_namespace_lock);
6448 		return (SET_ERROR(ESRCH));
6449 	} else if (cmd_type == POOL_INITIALIZE_SUSPEND &&
6450 	    vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) {
6451 		mutex_exit(&vd->vdev_initialize_lock);
6452 		mutex_exit(&spa_namespace_lock);
6453 		return (SET_ERROR(ESRCH));
6454 	}
6455 
6456 	switch (cmd_type) {
6457 	case POOL_INITIALIZE_DO:
6458 		vdev_initialize(vd);
6459 		break;
6460 	case POOL_INITIALIZE_CANCEL:
6461 		vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED);
6462 		break;
6463 	case POOL_INITIALIZE_SUSPEND:
6464 		vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED);
6465 		break;
6466 	default:
6467 		panic("invalid cmd_type %llu", (unsigned long long)cmd_type);
6468 	}
6469 	mutex_exit(&vd->vdev_initialize_lock);
6470 
6471 	/* Sync out the initializing state */
6472 	txg_wait_synced(spa->spa_dsl_pool, 0);
6473 	mutex_exit(&spa_namespace_lock);
6474 
6475 	return (0);
6476 }
6477 
6478 
6479 /*
6480  * Split a set of devices from their mirrors, and create a new pool from them.
6481  */
6482 int
6483 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
6484     nvlist_t *props, boolean_t exp)
6485 {
6486 	int error = 0;
6487 	uint64_t txg, *glist;
6488 	spa_t *newspa;
6489 	uint_t c, children, lastlog;
6490 	nvlist_t **child, *nvl, *tmp;
6491 	dmu_tx_t *tx;
6492 	char *altroot = NULL;
6493 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
6494 	boolean_t activate_slog;
6495 
6496 	ASSERT(spa_writeable(spa));
6497 
6498 	txg = spa_vdev_enter(spa);
6499 
6500 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
6501 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
6502 		error = (spa_has_checkpoint(spa)) ?
6503 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
6504 		return (spa_vdev_exit(spa, NULL, txg, error));
6505 	}
6506 
6507 	/* clear the log and flush everything up to now */
6508 	activate_slog = spa_passivate_log(spa);
6509 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
6510 	error = spa_reset_logs(spa);
6511 	txg = spa_vdev_config_enter(spa);
6512 
6513 	if (activate_slog)
6514 		spa_activate_log(spa);
6515 
6516 	if (error != 0)
6517 		return (spa_vdev_exit(spa, NULL, txg, error));
6518 
6519 	/* check new spa name before going any further */
6520 	if (spa_lookup(newname) != NULL)
6521 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
6522 
6523 	/*
6524 	 * scan through all the children to ensure they're all mirrors
6525 	 */
6526 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
6527 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
6528 	    &children) != 0)
6529 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
6530 
6531 	/* first, check to ensure we've got the right child count */
6532 	rvd = spa->spa_root_vdev;
6533 	lastlog = 0;
6534 	for (c = 0; c < rvd->vdev_children; c++) {
6535 		vdev_t *vd = rvd->vdev_child[c];
6536 
6537 		/* don't count the holes & logs as children */
6538 		if (vd->vdev_islog || !vdev_is_concrete(vd)) {
6539 			if (lastlog == 0)
6540 				lastlog = c;
6541 			continue;
6542 		}
6543 
6544 		lastlog = 0;
6545 	}
6546 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
6547 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
6548 
6549 	/* next, ensure no spare or cache devices are part of the split */
6550 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
6551 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
6552 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
6553 
6554 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
6555 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
6556 
6557 	/* then, loop over each vdev and validate it */
6558 	for (c = 0; c < children; c++) {
6559 		uint64_t is_hole = 0;
6560 
6561 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
6562 		    &is_hole);
6563 
6564 		if (is_hole != 0) {
6565 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
6566 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
6567 				continue;
6568 			} else {
6569 				error = SET_ERROR(EINVAL);
6570 				break;
6571 			}
6572 		}
6573 
6574 		/* which disk is going to be split? */
6575 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
6576 		    &glist[c]) != 0) {
6577 			error = SET_ERROR(EINVAL);
6578 			break;
6579 		}
6580 
6581 		/* look it up in the spa */
6582 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
6583 		if (vml[c] == NULL) {
6584 			error = SET_ERROR(ENODEV);
6585 			break;
6586 		}
6587 
6588 		/* make sure there's nothing stopping the split */
6589 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
6590 		    vml[c]->vdev_islog ||
6591 		    !vdev_is_concrete(vml[c]) ||
6592 		    vml[c]->vdev_isspare ||
6593 		    vml[c]->vdev_isl2cache ||
6594 		    !vdev_writeable(vml[c]) ||
6595 		    vml[c]->vdev_children != 0 ||
6596 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
6597 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
6598 			error = SET_ERROR(EINVAL);
6599 			break;
6600 		}
6601 
6602 		if (vdev_dtl_required(vml[c])) {
6603 			error = SET_ERROR(EBUSY);
6604 			break;
6605 		}
6606 
6607 		/* we need certain info from the top level */
6608 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
6609 		    vml[c]->vdev_top->vdev_ms_array) == 0);
6610 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
6611 		    vml[c]->vdev_top->vdev_ms_shift) == 0);
6612 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
6613 		    vml[c]->vdev_top->vdev_asize) == 0);
6614 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
6615 		    vml[c]->vdev_top->vdev_ashift) == 0);
6616 
6617 		/* transfer per-vdev ZAPs */
6618 		ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0);
6619 		VERIFY0(nvlist_add_uint64(child[c],
6620 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap));
6621 
6622 		ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0);
6623 		VERIFY0(nvlist_add_uint64(child[c],
6624 		    ZPOOL_CONFIG_VDEV_TOP_ZAP,
6625 		    vml[c]->vdev_parent->vdev_top_zap));
6626 	}
6627 
6628 	if (error != 0) {
6629 		kmem_free(vml, children * sizeof (vdev_t *));
6630 		kmem_free(glist, children * sizeof (uint64_t));
6631 		return (spa_vdev_exit(spa, NULL, txg, error));
6632 	}
6633 
6634 	/* stop writers from using the disks */
6635 	for (c = 0; c < children; c++) {
6636 		if (vml[c] != NULL)
6637 			vml[c]->vdev_offline = B_TRUE;
6638 	}
6639 	vdev_reopen(spa->spa_root_vdev);
6640 
6641 	/*
6642 	 * Temporarily record the splitting vdevs in the spa config.  This
6643 	 * will disappear once the config is regenerated.
6644 	 */
6645 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6646 	VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
6647 	    glist, children) == 0);
6648 	kmem_free(glist, children * sizeof (uint64_t));
6649 
6650 	mutex_enter(&spa->spa_props_lock);
6651 	VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
6652 	    nvl) == 0);
6653 	mutex_exit(&spa->spa_props_lock);
6654 	spa->spa_config_splitting = nvl;
6655 	vdev_config_dirty(spa->spa_root_vdev);
6656 
6657 	/* configure and create the new pool */
6658 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
6659 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
6660 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
6661 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6662 	    spa_version(spa)) == 0);
6663 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
6664 	    spa->spa_config_txg) == 0);
6665 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
6666 	    spa_generate_guid(NULL)) == 0);
6667 	VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
6668 	(void) nvlist_lookup_string(props,
6669 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
6670 
6671 	/* add the new pool to the namespace */
6672 	newspa = spa_add(newname, config, altroot);
6673 	newspa->spa_avz_action = AVZ_ACTION_REBUILD;
6674 	newspa->spa_config_txg = spa->spa_config_txg;
6675 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
6676 
6677 	/* release the spa config lock, retaining the namespace lock */
6678 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
6679 
6680 	if (zio_injection_enabled)
6681 		zio_handle_panic_injection(spa, FTAG, 1);
6682 
6683 	spa_activate(newspa, spa_mode_global);
6684 	spa_async_suspend(newspa);
6685 
6686 	for (c = 0; c < children; c++) {
6687 		if (vml[c] != NULL) {
6688 			/*
6689 			 * Temporarily stop the initializing activity. We set
6690 			 * the state to ACTIVE so that we know to resume
6691 			 * the initializing once the split has completed.
6692 			 */
6693 			mutex_enter(&vml[c]->vdev_initialize_lock);
6694 			vdev_initialize_stop(vml[c], VDEV_INITIALIZE_ACTIVE);
6695 			mutex_exit(&vml[c]->vdev_initialize_lock);
6696 		}
6697 	}
6698 
6699 	newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT;
6700 
6701 	/* create the new pool from the disks of the original pool */
6702 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE);
6703 	if (error)
6704 		goto out;
6705 
6706 	/* if that worked, generate a real config for the new pool */
6707 	if (newspa->spa_root_vdev != NULL) {
6708 		VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
6709 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
6710 		VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
6711 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
6712 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
6713 		    B_TRUE));
6714 	}
6715 
6716 	/* set the props */
6717 	if (props != NULL) {
6718 		spa_configfile_set(newspa, props, B_FALSE);
6719 		error = spa_prop_set(newspa, props);
6720 		if (error)
6721 			goto out;
6722 	}
6723 
6724 	/* flush everything */
6725 	txg = spa_vdev_config_enter(newspa);
6726 	vdev_config_dirty(newspa->spa_root_vdev);
6727 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
6728 
6729 	if (zio_injection_enabled)
6730 		zio_handle_panic_injection(spa, FTAG, 2);
6731 
6732 	spa_async_resume(newspa);
6733 
6734 	/* finally, update the original pool's config */
6735 	txg = spa_vdev_config_enter(spa);
6736 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
6737 	error = dmu_tx_assign(tx, TXG_WAIT);
6738 	if (error != 0)
6739 		dmu_tx_abort(tx);
6740 	for (c = 0; c < children; c++) {
6741 		if (vml[c] != NULL) {
6742 			vdev_split(vml[c]);
6743 			if (error == 0)
6744 				spa_history_log_internal(spa, "detach", tx,
6745 				    "vdev=%s", vml[c]->vdev_path);
6746 
6747 			vdev_free(vml[c]);
6748 		}
6749 	}
6750 	spa->spa_avz_action = AVZ_ACTION_REBUILD;
6751 	vdev_config_dirty(spa->spa_root_vdev);
6752 	spa->spa_config_splitting = NULL;
6753 	nvlist_free(nvl);
6754 	if (error == 0)
6755 		dmu_tx_commit(tx);
6756 	(void) spa_vdev_exit(spa, NULL, txg, 0);
6757 
6758 	if (zio_injection_enabled)
6759 		zio_handle_panic_injection(spa, FTAG, 3);
6760 
6761 	/* split is complete; log a history record */
6762 	spa_history_log_internal(newspa, "split", NULL,
6763 	    "from pool %s", spa_name(spa));
6764 
6765 	kmem_free(vml, children * sizeof (vdev_t *));
6766 
6767 	/* if we're not going to mount the filesystems in userland, export */
6768 	if (exp)
6769 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
6770 		    B_FALSE, B_FALSE);
6771 
6772 	return (error);
6773 
6774 out:
6775 	spa_unload(newspa);
6776 	spa_deactivate(newspa);
6777 	spa_remove(newspa);
6778 
6779 	txg = spa_vdev_config_enter(spa);
6780 
6781 	/* re-online all offlined disks */
6782 	for (c = 0; c < children; c++) {
6783 		if (vml[c] != NULL)
6784 			vml[c]->vdev_offline = B_FALSE;
6785 	}
6786 
6787 	/* restart initializing disks as necessary */
6788 	spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART);
6789 
6790 	vdev_reopen(spa->spa_root_vdev);
6791 
6792 	nvlist_free(spa->spa_config_splitting);
6793 	spa->spa_config_splitting = NULL;
6794 	(void) spa_vdev_exit(spa, NULL, txg, error);
6795 
6796 	kmem_free(vml, children * sizeof (vdev_t *));
6797 	return (error);
6798 }
6799 
6800 /*
6801  * Find any device that's done replacing, or a vdev marked 'unspare' that's
6802  * currently spared, so we can detach it.
6803  */
6804 static vdev_t *
6805 spa_vdev_resilver_done_hunt(vdev_t *vd)
6806 {
6807 	vdev_t *newvd, *oldvd;
6808 
6809 	for (int c = 0; c < vd->vdev_children; c++) {
6810 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
6811 		if (oldvd != NULL)
6812 			return (oldvd);
6813 	}
6814 
6815 	/*
6816 	 * Check for a completed replacement.  We always consider the first
6817 	 * vdev in the list to be the oldest vdev, and the last one to be
6818 	 * the newest (see spa_vdev_attach() for how that works).  In
6819 	 * the case where the newest vdev is faulted, we will not automatically
6820 	 * remove it after a resilver completes.  This is OK as it will require
6821 	 * user intervention to determine which disk the admin wishes to keep.
6822 	 */
6823 	if (vd->vdev_ops == &vdev_replacing_ops) {
6824 		ASSERT(vd->vdev_children > 1);
6825 
6826 		newvd = vd->vdev_child[vd->vdev_children - 1];
6827 		oldvd = vd->vdev_child[0];
6828 
6829 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
6830 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
6831 		    !vdev_dtl_required(oldvd))
6832 			return (oldvd);
6833 	}
6834 
6835 	/*
6836 	 * Check for a completed resilver with the 'unspare' flag set.
6837 	 * Also potentially update faulted state.
6838 	 */
6839 	if (vd->vdev_ops == &vdev_spare_ops) {
6840 		vdev_t *first = vd->vdev_child[0];
6841 		vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
6842 
6843 		if (last->vdev_unspare) {
6844 			oldvd = first;
6845 			newvd = last;
6846 		} else if (first->vdev_unspare) {
6847 			oldvd = last;
6848 			newvd = first;
6849 		} else {
6850 			oldvd = NULL;
6851 		}
6852 
6853 		if (oldvd != NULL &&
6854 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
6855 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
6856 		    !vdev_dtl_required(oldvd))
6857 			return (oldvd);
6858 
6859 		vdev_propagate_state(vd);
6860 
6861 		/*
6862 		 * If there are more than two spares attached to a disk,
6863 		 * and those spares are not required, then we want to
6864 		 * attempt to free them up now so that they can be used
6865 		 * by other pools.  Once we're back down to a single
6866 		 * disk+spare, we stop removing them.
6867 		 */
6868 		if (vd->vdev_children > 2) {
6869 			newvd = vd->vdev_child[1];
6870 
6871 			if (newvd->vdev_isspare && last->vdev_isspare &&
6872 			    vdev_dtl_empty(last, DTL_MISSING) &&
6873 			    vdev_dtl_empty(last, DTL_OUTAGE) &&
6874 			    !vdev_dtl_required(newvd))
6875 				return (newvd);
6876 		}
6877 	}
6878 
6879 	return (NULL);
6880 }
6881 
6882 static void
6883 spa_vdev_resilver_done(spa_t *spa)
6884 {
6885 	vdev_t *vd, *pvd, *ppvd;
6886 	uint64_t guid, sguid, pguid, ppguid;
6887 
6888 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6889 
6890 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
6891 		pvd = vd->vdev_parent;
6892 		ppvd = pvd->vdev_parent;
6893 		guid = vd->vdev_guid;
6894 		pguid = pvd->vdev_guid;
6895 		ppguid = ppvd->vdev_guid;
6896 		sguid = 0;
6897 		/*
6898 		 * If we have just finished replacing a hot spared device, then
6899 		 * we need to detach the parent's first child (the original hot
6900 		 * spare) as well.
6901 		 */
6902 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
6903 		    ppvd->vdev_children == 2) {
6904 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
6905 			sguid = ppvd->vdev_child[1]->vdev_guid;
6906 		}
6907 		ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
6908 
6909 		spa_config_exit(spa, SCL_ALL, FTAG);
6910 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
6911 			return;
6912 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
6913 			return;
6914 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6915 	}
6916 
6917 	spa_config_exit(spa, SCL_ALL, FTAG);
6918 }
6919 
6920 /*
6921  * Update the stored path or FRU for this vdev.
6922  */
6923 int
6924 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
6925     boolean_t ispath)
6926 {
6927 	vdev_t *vd;
6928 	boolean_t sync = B_FALSE;
6929 
6930 	ASSERT(spa_writeable(spa));
6931 
6932 	spa_vdev_state_enter(spa, SCL_ALL);
6933 
6934 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
6935 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
6936 
6937 	if (!vd->vdev_ops->vdev_op_leaf)
6938 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
6939 
6940 	if (ispath) {
6941 		if (strcmp(value, vd->vdev_path) != 0) {
6942 			spa_strfree(vd->vdev_path);
6943 			vd->vdev_path = spa_strdup(value);
6944 			sync = B_TRUE;
6945 		}
6946 	} else {
6947 		if (vd->vdev_fru == NULL) {
6948 			vd->vdev_fru = spa_strdup(value);
6949 			sync = B_TRUE;
6950 		} else if (strcmp(value, vd->vdev_fru) != 0) {
6951 			spa_strfree(vd->vdev_fru);
6952 			vd->vdev_fru = spa_strdup(value);
6953 			sync = B_TRUE;
6954 		}
6955 	}
6956 
6957 	return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
6958 }
6959 
6960 int
6961 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
6962 {
6963 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
6964 }
6965 
6966 int
6967 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
6968 {
6969 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
6970 }
6971 
6972 /*
6973  * ==========================================================================
6974  * SPA Scanning
6975  * ==========================================================================
6976  */
6977 int
6978 spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd)
6979 {
6980 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6981 
6982 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
6983 		return (SET_ERROR(EBUSY));
6984 
6985 	return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd));
6986 }
6987 
6988 int
6989 spa_scan_stop(spa_t *spa)
6990 {
6991 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6992 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
6993 		return (SET_ERROR(EBUSY));
6994 	return (dsl_scan_cancel(spa->spa_dsl_pool));
6995 }
6996 
6997 int
6998 spa_scan(spa_t *spa, pool_scan_func_t func)
6999 {
7000 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
7001 
7002 	if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
7003 		return (SET_ERROR(ENOTSUP));
7004 
7005 	/*
7006 	 * If a resilver was requested, but there is no DTL on a
7007 	 * writeable leaf device, we have nothing to do.
7008 	 */
7009 	if (func == POOL_SCAN_RESILVER &&
7010 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
7011 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
7012 		return (0);
7013 	}
7014 
7015 	return (dsl_scan(spa->spa_dsl_pool, func));
7016 }
7017 
7018 /*
7019  * ==========================================================================
7020  * SPA async task processing
7021  * ==========================================================================
7022  */
7023 
7024 static void
7025 spa_async_remove(spa_t *spa, vdev_t *vd)
7026 {
7027 	if (vd->vdev_remove_wanted) {
7028 		vd->vdev_remove_wanted = B_FALSE;
7029 		vd->vdev_delayed_close = B_FALSE;
7030 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
7031 
7032 		/*
7033 		 * We want to clear the stats, but we don't want to do a full
7034 		 * vdev_clear() as that will cause us to throw away
7035 		 * degraded/faulted state as well as attempt to reopen the
7036 		 * device, all of which is a waste.
7037 		 */
7038 		vd->vdev_stat.vs_read_errors = 0;
7039 		vd->vdev_stat.vs_write_errors = 0;
7040 		vd->vdev_stat.vs_checksum_errors = 0;
7041 
7042 		vdev_state_dirty(vd->vdev_top);
7043 	}
7044 
7045 	for (int c = 0; c < vd->vdev_children; c++)
7046 		spa_async_remove(spa, vd->vdev_child[c]);
7047 }
7048 
7049 static void
7050 spa_async_probe(spa_t *spa, vdev_t *vd)
7051 {
7052 	if (vd->vdev_probe_wanted) {
7053 		vd->vdev_probe_wanted = B_FALSE;
7054 		vdev_reopen(vd);	/* vdev_open() does the actual probe */
7055 	}
7056 
7057 	for (int c = 0; c < vd->vdev_children; c++)
7058 		spa_async_probe(spa, vd->vdev_child[c]);
7059 }
7060 
7061 static void
7062 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
7063 {
7064 	sysevent_id_t eid;
7065 	nvlist_t *attr;
7066 	char *physpath;
7067 
7068 	if (!spa->spa_autoexpand)
7069 		return;
7070 
7071 	for (int c = 0; c < vd->vdev_children; c++) {
7072 		vdev_t *cvd = vd->vdev_child[c];
7073 		spa_async_autoexpand(spa, cvd);
7074 	}
7075 
7076 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
7077 		return;
7078 
7079 	physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
7080 	(void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
7081 
7082 	VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7083 	VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
7084 
7085 	(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
7086 	    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
7087 
7088 	nvlist_free(attr);
7089 	kmem_free(physpath, MAXPATHLEN);
7090 }
7091 
7092 static void
7093 spa_async_thread(void *arg)
7094 {
7095 	spa_t *spa = (spa_t *)arg;
7096 	int tasks;
7097 
7098 	ASSERT(spa->spa_sync_on);
7099 
7100 	mutex_enter(&spa->spa_async_lock);
7101 	tasks = spa->spa_async_tasks;
7102 	spa->spa_async_tasks = 0;
7103 	mutex_exit(&spa->spa_async_lock);
7104 
7105 	/*
7106 	 * See if the config needs to be updated.
7107 	 */
7108 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
7109 		uint64_t old_space, new_space;
7110 
7111 		mutex_enter(&spa_namespace_lock);
7112 		old_space = metaslab_class_get_space(spa_normal_class(spa));
7113 		old_space += metaslab_class_get_space(spa_special_class(spa));
7114 		old_space += metaslab_class_get_space(spa_dedup_class(spa));
7115 
7116 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
7117 
7118 		new_space = metaslab_class_get_space(spa_normal_class(spa));
7119 		new_space += metaslab_class_get_space(spa_special_class(spa));
7120 		new_space += metaslab_class_get_space(spa_dedup_class(spa));
7121 		mutex_exit(&spa_namespace_lock);
7122 
7123 		/*
7124 		 * If the pool grew as a result of the config update,
7125 		 * then log an internal history event.
7126 		 */
7127 		if (new_space != old_space) {
7128 			spa_history_log_internal(spa, "vdev online", NULL,
7129 			    "pool '%s' size: %llu(+%llu)",
7130 			    spa_name(spa), new_space, new_space - old_space);
7131 		}
7132 	}
7133 
7134 	/*
7135 	 * See if any devices need to be marked REMOVED.
7136 	 */
7137 	if (tasks & SPA_ASYNC_REMOVE) {
7138 		spa_vdev_state_enter(spa, SCL_NONE);
7139 		spa_async_remove(spa, spa->spa_root_vdev);
7140 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
7141 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
7142 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
7143 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
7144 		(void) spa_vdev_state_exit(spa, NULL, 0);
7145 	}
7146 
7147 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
7148 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
7149 		spa_async_autoexpand(spa, spa->spa_root_vdev);
7150 		spa_config_exit(spa, SCL_CONFIG, FTAG);
7151 	}
7152 
7153 	/*
7154 	 * See if any devices need to be probed.
7155 	 */
7156 	if (tasks & SPA_ASYNC_PROBE) {
7157 		spa_vdev_state_enter(spa, SCL_NONE);
7158 		spa_async_probe(spa, spa->spa_root_vdev);
7159 		(void) spa_vdev_state_exit(spa, NULL, 0);
7160 	}
7161 
7162 	/*
7163 	 * If any devices are done replacing, detach them.
7164 	 */
7165 	if (tasks & SPA_ASYNC_RESILVER_DONE)
7166 		spa_vdev_resilver_done(spa);
7167 
7168 	/*
7169 	 * Kick off a resilver.
7170 	 */
7171 	if (tasks & SPA_ASYNC_RESILVER)
7172 		dsl_resilver_restart(spa->spa_dsl_pool, 0);
7173 
7174 	if (tasks & SPA_ASYNC_INITIALIZE_RESTART) {
7175 		mutex_enter(&spa_namespace_lock);
7176 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
7177 		vdev_initialize_restart(spa->spa_root_vdev);
7178 		spa_config_exit(spa, SCL_CONFIG, FTAG);
7179 		mutex_exit(&spa_namespace_lock);
7180 	}
7181 
7182 	/*
7183 	 * Let the world know that we're done.
7184 	 */
7185 	mutex_enter(&spa->spa_async_lock);
7186 	spa->spa_async_thread = NULL;
7187 	cv_broadcast(&spa->spa_async_cv);
7188 	mutex_exit(&spa->spa_async_lock);
7189 	thread_exit();
7190 }
7191 
7192 void
7193 spa_async_suspend(spa_t *spa)
7194 {
7195 	mutex_enter(&spa->spa_async_lock);
7196 	spa->spa_async_suspended++;
7197 	while (spa->spa_async_thread != NULL)
7198 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
7199 	mutex_exit(&spa->spa_async_lock);
7200 
7201 	spa_vdev_remove_suspend(spa);
7202 
7203 	zthr_t *condense_thread = spa->spa_condense_zthr;
7204 	if (condense_thread != NULL && zthr_isrunning(condense_thread))
7205 		VERIFY0(zthr_cancel(condense_thread));
7206 
7207 	zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr;
7208 	if (discard_thread != NULL && zthr_isrunning(discard_thread))
7209 		VERIFY0(zthr_cancel(discard_thread));
7210 }
7211 
7212 void
7213 spa_async_resume(spa_t *spa)
7214 {
7215 	mutex_enter(&spa->spa_async_lock);
7216 	ASSERT(spa->spa_async_suspended != 0);
7217 	spa->spa_async_suspended--;
7218 	mutex_exit(&spa->spa_async_lock);
7219 	spa_restart_removal(spa);
7220 
7221 	zthr_t *condense_thread = spa->spa_condense_zthr;
7222 	if (condense_thread != NULL && !zthr_isrunning(condense_thread))
7223 		zthr_resume(condense_thread);
7224 
7225 	zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr;
7226 	if (discard_thread != NULL && !zthr_isrunning(discard_thread))
7227 		zthr_resume(discard_thread);
7228 }
7229 
7230 static boolean_t
7231 spa_async_tasks_pending(spa_t *spa)
7232 {
7233 	uint_t non_config_tasks;
7234 	uint_t config_task;
7235 	boolean_t config_task_suspended;
7236 
7237 	non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
7238 	config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
7239 	if (spa->spa_ccw_fail_time == 0) {
7240 		config_task_suspended = B_FALSE;
7241 	} else {
7242 		config_task_suspended =
7243 		    (gethrtime() - spa->spa_ccw_fail_time) <
7244 		    (zfs_ccw_retry_interval * NANOSEC);
7245 	}
7246 
7247 	return (non_config_tasks || (config_task && !config_task_suspended));
7248 }
7249 
7250 static void
7251 spa_async_dispatch(spa_t *spa)
7252 {
7253 	mutex_enter(&spa->spa_async_lock);
7254 	if (spa_async_tasks_pending(spa) &&
7255 	    !spa->spa_async_suspended &&
7256 	    spa->spa_async_thread == NULL &&
7257 	    rootdir != NULL)
7258 		spa->spa_async_thread = thread_create(NULL, 0,
7259 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
7260 	mutex_exit(&spa->spa_async_lock);
7261 }
7262 
7263 void
7264 spa_async_request(spa_t *spa, int task)
7265 {
7266 	zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
7267 	mutex_enter(&spa->spa_async_lock);
7268 	spa->spa_async_tasks |= task;
7269 	mutex_exit(&spa->spa_async_lock);
7270 }
7271 
7272 /*
7273  * ==========================================================================
7274  * SPA syncing routines
7275  * ==========================================================================
7276  */
7277 
7278 static int
7279 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
7280 {
7281 	bpobj_t *bpo = arg;
7282 	bpobj_enqueue(bpo, bp, tx);
7283 	return (0);
7284 }
7285 
7286 static int
7287 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
7288 {
7289 	zio_t *zio = arg;
7290 
7291 	zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
7292 	    zio->io_flags));
7293 	return (0);
7294 }
7295 
7296 /*
7297  * Note: this simple function is not inlined to make it easier to dtrace the
7298  * amount of time spent syncing frees.
7299  */
7300 static void
7301 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
7302 {
7303 	zio_t *zio = zio_root(spa, NULL, NULL, 0);
7304 	bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
7305 	VERIFY(zio_wait(zio) == 0);
7306 }
7307 
7308 /*
7309  * Note: this simple function is not inlined to make it easier to dtrace the
7310  * amount of time spent syncing deferred frees.
7311  */
7312 static void
7313 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
7314 {
7315 	zio_t *zio = zio_root(spa, NULL, NULL, 0);
7316 	VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
7317 	    spa_free_sync_cb, zio, tx), ==, 0);
7318 	VERIFY0(zio_wait(zio));
7319 }
7320 
7321 
7322 static void
7323 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
7324 {
7325 	char *packed = NULL;
7326 	size_t bufsize;
7327 	size_t nvsize = 0;
7328 	dmu_buf_t *db;
7329 
7330 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
7331 
7332 	/*
7333 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
7334 	 * information.  This avoids the dmu_buf_will_dirty() path and
7335 	 * saves us a pre-read to get data we don't actually care about.
7336 	 */
7337 	bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
7338 	packed = kmem_alloc(bufsize, KM_SLEEP);
7339 
7340 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
7341 	    KM_SLEEP) == 0);
7342 	bzero(packed + nvsize, bufsize - nvsize);
7343 
7344 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
7345 
7346 	kmem_free(packed, bufsize);
7347 
7348 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
7349 	dmu_buf_will_dirty(db, tx);
7350 	*(uint64_t *)db->db_data = nvsize;
7351 	dmu_buf_rele(db, FTAG);
7352 }
7353 
7354 static void
7355 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
7356     const char *config, const char *entry)
7357 {
7358 	nvlist_t *nvroot;
7359 	nvlist_t **list;
7360 	int i;
7361 
7362 	if (!sav->sav_sync)
7363 		return;
7364 
7365 	/*
7366 	 * Update the MOS nvlist describing the list of available devices.
7367 	 * spa_validate_aux() will have already made sure this nvlist is
7368 	 * valid and the vdevs are labeled appropriately.
7369 	 */
7370 	if (sav->sav_object == 0) {
7371 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
7372 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
7373 		    sizeof (uint64_t), tx);
7374 		VERIFY(zap_update(spa->spa_meta_objset,
7375 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
7376 		    &sav->sav_object, tx) == 0);
7377 	}
7378 
7379 	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7380 	if (sav->sav_count == 0) {
7381 		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
7382 	} else {
7383 		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
7384 		for (i = 0; i < sav->sav_count; i++)
7385 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
7386 			    B_FALSE, VDEV_CONFIG_L2CACHE);
7387 		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
7388 		    sav->sav_count) == 0);
7389 		for (i = 0; i < sav->sav_count; i++)
7390 			nvlist_free(list[i]);
7391 		kmem_free(list, sav->sav_count * sizeof (void *));
7392 	}
7393 
7394 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
7395 	nvlist_free(nvroot);
7396 
7397 	sav->sav_sync = B_FALSE;
7398 }
7399 
7400 /*
7401  * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
7402  * The all-vdev ZAP must be empty.
7403  */
7404 static void
7405 spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx)
7406 {
7407 	spa_t *spa = vd->vdev_spa;
7408 	if (vd->vdev_top_zap != 0) {
7409 		VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
7410 		    vd->vdev_top_zap, tx));
7411 	}
7412 	if (vd->vdev_leaf_zap != 0) {
7413 		VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
7414 		    vd->vdev_leaf_zap, tx));
7415 	}
7416 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
7417 		spa_avz_build(vd->vdev_child[i], avz, tx);
7418 	}
7419 }
7420 
7421 static void
7422 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
7423 {
7424 	nvlist_t *config;
7425 
7426 	/*
7427 	 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
7428 	 * its config may not be dirty but we still need to build per-vdev ZAPs.
7429 	 * Similarly, if the pool is being assembled (e.g. after a split), we
7430 	 * need to rebuild the AVZ although the config may not be dirty.
7431 	 */
7432 	if (list_is_empty(&spa->spa_config_dirty_list) &&
7433 	    spa->spa_avz_action == AVZ_ACTION_NONE)
7434 		return;
7435 
7436 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
7437 
7438 	ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE ||
7439 	    spa->spa_avz_action == AVZ_ACTION_INITIALIZE ||
7440 	    spa->spa_all_vdev_zaps != 0);
7441 
7442 	if (spa->spa_avz_action == AVZ_ACTION_REBUILD) {
7443 		/* Make and build the new AVZ */
7444 		uint64_t new_avz = zap_create(spa->spa_meta_objset,
7445 		    DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
7446 		spa_avz_build(spa->spa_root_vdev, new_avz, tx);
7447 
7448 		/* Diff old AVZ with new one */
7449 		zap_cursor_t zc;
7450 		zap_attribute_t za;
7451 
7452 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
7453 		    spa->spa_all_vdev_zaps);
7454 		    zap_cursor_retrieve(&zc, &za) == 0;
7455 		    zap_cursor_advance(&zc)) {
7456 			uint64_t vdzap = za.za_first_integer;
7457 			if (zap_lookup_int(spa->spa_meta_objset, new_avz,
7458 			    vdzap) == ENOENT) {
7459 				/*
7460 				 * ZAP is listed in old AVZ but not in new one;
7461 				 * destroy it
7462 				 */
7463 				VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap,
7464 				    tx));
7465 			}
7466 		}
7467 
7468 		zap_cursor_fini(&zc);
7469 
7470 		/* Destroy the old AVZ */
7471 		VERIFY0(zap_destroy(spa->spa_meta_objset,
7472 		    spa->spa_all_vdev_zaps, tx));
7473 
7474 		/* Replace the old AVZ in the dir obj with the new one */
7475 		VERIFY0(zap_update(spa->spa_meta_objset,
7476 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP,
7477 		    sizeof (new_avz), 1, &new_avz, tx));
7478 
7479 		spa->spa_all_vdev_zaps = new_avz;
7480 	} else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) {
7481 		zap_cursor_t zc;
7482 		zap_attribute_t za;
7483 
7484 		/* Walk through the AVZ and destroy all listed ZAPs */
7485 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
7486 		    spa->spa_all_vdev_zaps);
7487 		    zap_cursor_retrieve(&zc, &za) == 0;
7488 		    zap_cursor_advance(&zc)) {
7489 			uint64_t zap = za.za_first_integer;
7490 			VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx));
7491 		}
7492 
7493 		zap_cursor_fini(&zc);
7494 
7495 		/* Destroy and unlink the AVZ itself */
7496 		VERIFY0(zap_destroy(spa->spa_meta_objset,
7497 		    spa->spa_all_vdev_zaps, tx));
7498 		VERIFY0(zap_remove(spa->spa_meta_objset,
7499 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx));
7500 		spa->spa_all_vdev_zaps = 0;
7501 	}
7502 
7503 	if (spa->spa_all_vdev_zaps == 0) {
7504 		spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset,
7505 		    DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
7506 		    DMU_POOL_VDEV_ZAP_MAP, tx);
7507 	}
7508 	spa->spa_avz_action = AVZ_ACTION_NONE;
7509 
7510 	/* Create ZAPs for vdevs that don't have them. */
7511 	vdev_construct_zaps(spa->spa_root_vdev, tx);
7512 
7513 	config = spa_config_generate(spa, spa->spa_root_vdev,
7514 	    dmu_tx_get_txg(tx), B_FALSE);
7515 
7516 	/*
7517 	 * If we're upgrading the spa version then make sure that
7518 	 * the config object gets updated with the correct version.
7519 	 */
7520 	if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
7521 		fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
7522 		    spa->spa_uberblock.ub_version);
7523 
7524 	spa_config_exit(spa, SCL_STATE, FTAG);
7525 
7526 	nvlist_free(spa->spa_config_syncing);
7527 	spa->spa_config_syncing = config;
7528 
7529 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
7530 }
7531 
7532 static void
7533 spa_sync_version(void *arg, dmu_tx_t *tx)
7534 {
7535 	uint64_t *versionp = arg;
7536 	uint64_t version = *versionp;
7537 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7538 
7539 	/*
7540 	 * Setting the version is special cased when first creating the pool.
7541 	 */
7542 	ASSERT(tx->tx_txg != TXG_INITIAL);
7543 
7544 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
7545 	ASSERT(version >= spa_version(spa));
7546 
7547 	spa->spa_uberblock.ub_version = version;
7548 	vdev_config_dirty(spa->spa_root_vdev);
7549 	spa_history_log_internal(spa, "set", tx, "version=%lld", version);
7550 }
7551 
7552 /*
7553  * Set zpool properties.
7554  */
7555 static void
7556 spa_sync_props(void *arg, dmu_tx_t *tx)
7557 {
7558 	nvlist_t *nvp = arg;
7559 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7560 	objset_t *mos = spa->spa_meta_objset;
7561 	nvpair_t *elem = NULL;
7562 
7563 	mutex_enter(&spa->spa_props_lock);
7564 
7565 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
7566 		uint64_t intval;
7567 		char *strval, *fname;
7568 		zpool_prop_t prop;
7569 		const char *propname;
7570 		zprop_type_t proptype;
7571 		spa_feature_t fid;
7572 
7573 		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
7574 		case ZPOOL_PROP_INVAL:
7575 			/*
7576 			 * We checked this earlier in spa_prop_validate().
7577 			 */
7578 			ASSERT(zpool_prop_feature(nvpair_name(elem)));
7579 
7580 			fname = strchr(nvpair_name(elem), '@') + 1;
7581 			VERIFY0(zfeature_lookup_name(fname, &fid));
7582 
7583 			spa_feature_enable(spa, fid, tx);
7584 			spa_history_log_internal(spa, "set", tx,
7585 			    "%s=enabled", nvpair_name(elem));
7586 			break;
7587 
7588 		case ZPOOL_PROP_VERSION:
7589 			intval = fnvpair_value_uint64(elem);
7590 			/*
7591 			 * The version is synced seperatly before other
7592 			 * properties and should be correct by now.
7593 			 */
7594 			ASSERT3U(spa_version(spa), >=, intval);
7595 			break;
7596 
7597 		case ZPOOL_PROP_ALTROOT:
7598 			/*
7599 			 * 'altroot' is a non-persistent property. It should
7600 			 * have been set temporarily at creation or import time.
7601 			 */
7602 			ASSERT(spa->spa_root != NULL);
7603 			break;
7604 
7605 		case ZPOOL_PROP_READONLY:
7606 		case ZPOOL_PROP_CACHEFILE:
7607 			/*
7608 			 * 'readonly' and 'cachefile' are also non-persisitent
7609 			 * properties.
7610 			 */
7611 			break;
7612 		case ZPOOL_PROP_COMMENT:
7613 			strval = fnvpair_value_string(elem);
7614 			if (spa->spa_comment != NULL)
7615 				spa_strfree(spa->spa_comment);
7616 			spa->spa_comment = spa_strdup(strval);
7617 			/*
7618 			 * We need to dirty the configuration on all the vdevs
7619 			 * so that their labels get updated.  It's unnecessary
7620 			 * to do this for pool creation since the vdev's
7621 			 * configuratoin has already been dirtied.
7622 			 */
7623 			if (tx->tx_txg != TXG_INITIAL)
7624 				vdev_config_dirty(spa->spa_root_vdev);
7625 			spa_history_log_internal(spa, "set", tx,
7626 			    "%s=%s", nvpair_name(elem), strval);
7627 			break;
7628 		default:
7629 			/*
7630 			 * Set pool property values in the poolprops mos object.
7631 			 */
7632 			if (spa->spa_pool_props_object == 0) {
7633 				spa->spa_pool_props_object =
7634 				    zap_create_link(mos, DMU_OT_POOL_PROPS,
7635 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
7636 				    tx);
7637 			}
7638 
7639 			/* normalize the property name */
7640 			propname = zpool_prop_to_name(prop);
7641 			proptype = zpool_prop_get_type(prop);
7642 
7643 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
7644 				ASSERT(proptype == PROP_TYPE_STRING);
7645 				strval = fnvpair_value_string(elem);
7646 				VERIFY0(zap_update(mos,
7647 				    spa->spa_pool_props_object, propname,
7648 				    1, strlen(strval) + 1, strval, tx));
7649 				spa_history_log_internal(spa, "set", tx,
7650 				    "%s=%s", nvpair_name(elem), strval);
7651 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
7652 				intval = fnvpair_value_uint64(elem);
7653 
7654 				if (proptype == PROP_TYPE_INDEX) {
7655 					const char *unused;
7656 					VERIFY0(zpool_prop_index_to_string(
7657 					    prop, intval, &unused));
7658 				}
7659 				VERIFY0(zap_update(mos,
7660 				    spa->spa_pool_props_object, propname,
7661 				    8, 1, &intval, tx));
7662 				spa_history_log_internal(spa, "set", tx,
7663 				    "%s=%lld", nvpair_name(elem), intval);
7664 			} else {
7665 				ASSERT(0); /* not allowed */
7666 			}
7667 
7668 			switch (prop) {
7669 			case ZPOOL_PROP_DELEGATION:
7670 				spa->spa_delegation = intval;
7671 				break;
7672 			case ZPOOL_PROP_BOOTFS:
7673 				spa->spa_bootfs = intval;
7674 				break;
7675 			case ZPOOL_PROP_FAILUREMODE:
7676 				spa->spa_failmode = intval;
7677 				break;
7678 			case ZPOOL_PROP_AUTOEXPAND:
7679 				spa->spa_autoexpand = intval;
7680 				if (tx->tx_txg != TXG_INITIAL)
7681 					spa_async_request(spa,
7682 					    SPA_ASYNC_AUTOEXPAND);
7683 				break;
7684 			case ZPOOL_PROP_MULTIHOST:
7685 				spa->spa_multihost = intval;
7686 				break;
7687 			case ZPOOL_PROP_DEDUPDITTO:
7688 				spa->spa_dedup_ditto = intval;
7689 				break;
7690 			default:
7691 				break;
7692 			}
7693 		}
7694 
7695 	}
7696 
7697 	mutex_exit(&spa->spa_props_lock);
7698 }
7699 
7700 /*
7701  * Perform one-time upgrade on-disk changes.  spa_version() does not
7702  * reflect the new version this txg, so there must be no changes this
7703  * txg to anything that the upgrade code depends on after it executes.
7704  * Therefore this must be called after dsl_pool_sync() does the sync
7705  * tasks.
7706  */
7707 static void
7708 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
7709 {
7710 	dsl_pool_t *dp = spa->spa_dsl_pool;
7711 
7712 	ASSERT(spa->spa_sync_pass == 1);
7713 
7714 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
7715 
7716 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
7717 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
7718 		dsl_pool_create_origin(dp, tx);
7719 
7720 		/* Keeping the origin open increases spa_minref */
7721 		spa->spa_minref += 3;
7722 	}
7723 
7724 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
7725 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
7726 		dsl_pool_upgrade_clones(dp, tx);
7727 	}
7728 
7729 	if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
7730 	    spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
7731 		dsl_pool_upgrade_dir_clones(dp, tx);
7732 
7733 		/* Keeping the freedir open increases spa_minref */
7734 		spa->spa_minref += 3;
7735 	}
7736 
7737 	if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
7738 	    spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
7739 		spa_feature_create_zap_objects(spa, tx);
7740 	}
7741 
7742 	/*
7743 	 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
7744 	 * when possibility to use lz4 compression for metadata was added
7745 	 * Old pools that have this feature enabled must be upgraded to have
7746 	 * this feature active
7747 	 */
7748 	if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
7749 		boolean_t lz4_en = spa_feature_is_enabled(spa,
7750 		    SPA_FEATURE_LZ4_COMPRESS);
7751 		boolean_t lz4_ac = spa_feature_is_active(spa,
7752 		    SPA_FEATURE_LZ4_COMPRESS);
7753 
7754 		if (lz4_en && !lz4_ac)
7755 			spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
7756 	}
7757 
7758 	/*
7759 	 * If we haven't written the salt, do so now.  Note that the
7760 	 * feature may not be activated yet, but that's fine since
7761 	 * the presence of this ZAP entry is backwards compatible.
7762 	 */
7763 	if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
7764 	    DMU_POOL_CHECKSUM_SALT) == ENOENT) {
7765 		VERIFY0(zap_add(spa->spa_meta_objset,
7766 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
7767 		    sizeof (spa->spa_cksum_salt.zcs_bytes),
7768 		    spa->spa_cksum_salt.zcs_bytes, tx));
7769 	}
7770 
7771 	rrw_exit(&dp->dp_config_rwlock, FTAG);
7772 }
7773 
7774 static void
7775 vdev_indirect_state_sync_verify(vdev_t *vd)
7776 {
7777 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
7778 	vdev_indirect_births_t *vib = vd->vdev_indirect_births;
7779 
7780 	if (vd->vdev_ops == &vdev_indirect_ops) {
7781 		ASSERT(vim != NULL);
7782 		ASSERT(vib != NULL);
7783 	}
7784 
7785 	if (vdev_obsolete_sm_object(vd) != 0) {
7786 		ASSERT(vd->vdev_obsolete_sm != NULL);
7787 		ASSERT(vd->vdev_removing ||
7788 		    vd->vdev_ops == &vdev_indirect_ops);
7789 		ASSERT(vdev_indirect_mapping_num_entries(vim) > 0);
7790 		ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0);
7791 
7792 		ASSERT3U(vdev_obsolete_sm_object(vd), ==,
7793 		    space_map_object(vd->vdev_obsolete_sm));
7794 		ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=,
7795 		    space_map_allocated(vd->vdev_obsolete_sm));
7796 	}
7797 	ASSERT(vd->vdev_obsolete_segments != NULL);
7798 
7799 	/*
7800 	 * Since frees / remaps to an indirect vdev can only
7801 	 * happen in syncing context, the obsolete segments
7802 	 * tree must be empty when we start syncing.
7803 	 */
7804 	ASSERT0(range_tree_space(vd->vdev_obsolete_segments));
7805 }
7806 
7807 /*
7808  * Sync the specified transaction group.  New blocks may be dirtied as
7809  * part of the process, so we iterate until it converges.
7810  */
7811 void
7812 spa_sync(spa_t *spa, uint64_t txg)
7813 {
7814 	dsl_pool_t *dp = spa->spa_dsl_pool;
7815 	objset_t *mos = spa->spa_meta_objset;
7816 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
7817 	metaslab_class_t *normal = spa_normal_class(spa);
7818 	metaslab_class_t *special = spa_special_class(spa);
7819 	metaslab_class_t *dedup = spa_dedup_class(spa);
7820 	vdev_t *rvd = spa->spa_root_vdev;
7821 	vdev_t *vd;
7822 	dmu_tx_t *tx;
7823 	int error;
7824 	uint32_t max_queue_depth = zfs_vdev_async_write_max_active *
7825 	    zfs_vdev_queue_depth_pct / 100;
7826 
7827 	VERIFY(spa_writeable(spa));
7828 
7829 	/*
7830 	 * Wait for i/os issued in open context that need to complete
7831 	 * before this txg syncs.
7832 	 */
7833 	(void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]);
7834 	spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL,
7835 	    ZIO_FLAG_CANFAIL);
7836 
7837 	/*
7838 	 * Lock out configuration changes.
7839 	 */
7840 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
7841 
7842 	spa->spa_syncing_txg = txg;
7843 	spa->spa_sync_pass = 0;
7844 
7845 	for (int i = 0; i < spa->spa_alloc_count; i++) {
7846 		mutex_enter(&spa->spa_alloc_locks[i]);
7847 		VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i]));
7848 		mutex_exit(&spa->spa_alloc_locks[i]);
7849 	}
7850 
7851 	/*
7852 	 * If there are any pending vdev state changes, convert them
7853 	 * into config changes that go out with this transaction group.
7854 	 */
7855 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
7856 	while (list_head(&spa->spa_state_dirty_list) != NULL) {
7857 		/*
7858 		 * We need the write lock here because, for aux vdevs,
7859 		 * calling vdev_config_dirty() modifies sav_config.
7860 		 * This is ugly and will become unnecessary when we
7861 		 * eliminate the aux vdev wart by integrating all vdevs
7862 		 * into the root vdev tree.
7863 		 */
7864 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
7865 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
7866 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
7867 			vdev_state_clean(vd);
7868 			vdev_config_dirty(vd);
7869 		}
7870 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
7871 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
7872 	}
7873 	spa_config_exit(spa, SCL_STATE, FTAG);
7874 
7875 	tx = dmu_tx_create_assigned(dp, txg);
7876 
7877 	spa->spa_sync_starttime = gethrtime();
7878 	VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
7879 	    spa->spa_sync_starttime + spa->spa_deadman_synctime));
7880 
7881 	/*
7882 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
7883 	 * set spa_deflate if we have no raid-z vdevs.
7884 	 */
7885 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
7886 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
7887 		int i;
7888 
7889 		for (i = 0; i < rvd->vdev_children; i++) {
7890 			vd = rvd->vdev_child[i];
7891 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
7892 				break;
7893 		}
7894 		if (i == rvd->vdev_children) {
7895 			spa->spa_deflate = TRUE;
7896 			VERIFY(0 == zap_add(spa->spa_meta_objset,
7897 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
7898 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
7899 		}
7900 	}
7901 
7902 	/*
7903 	 * Set the top-level vdev's max queue depth. Evaluate each
7904 	 * top-level's async write queue depth in case it changed.
7905 	 * The max queue depth will not change in the middle of syncing
7906 	 * out this txg.
7907 	 */
7908 	uint64_t slots_per_allocator = 0;
7909 	for (int c = 0; c < rvd->vdev_children; c++) {
7910 		vdev_t *tvd = rvd->vdev_child[c];
7911 		metaslab_group_t *mg = tvd->vdev_mg;
7912 		metaslab_class_t *mc;
7913 
7914 		if (mg == NULL || !metaslab_group_initialized(mg))
7915 			continue;
7916 
7917 		mc = mg->mg_class;
7918 		if (mc != normal && mc != special && mc != dedup)
7919 			continue;
7920 
7921 		/*
7922 		 * It is safe to do a lock-free check here because only async
7923 		 * allocations look at mg_max_alloc_queue_depth, and async
7924 		 * allocations all happen from spa_sync().
7925 		 */
7926 		for (int i = 0; i < spa->spa_alloc_count; i++)
7927 			ASSERT0(zfs_refcount_count(
7928 			    &(mg->mg_alloc_queue_depth[i])));
7929 		mg->mg_max_alloc_queue_depth = max_queue_depth;
7930 
7931 		for (int i = 0; i < spa->spa_alloc_count; i++) {
7932 			mg->mg_cur_max_alloc_queue_depth[i] =
7933 			    zfs_vdev_def_queue_depth;
7934 		}
7935 		slots_per_allocator += zfs_vdev_def_queue_depth;
7936 	}
7937 
7938 	for (int i = 0; i < spa->spa_alloc_count; i++) {
7939 		ASSERT0(zfs_refcount_count(&normal->mc_alloc_slots[i]));
7940 		ASSERT0(zfs_refcount_count(&special->mc_alloc_slots[i]));
7941 		ASSERT0(zfs_refcount_count(&dedup->mc_alloc_slots[i]));
7942 		normal->mc_alloc_max_slots[i] = slots_per_allocator;
7943 		special->mc_alloc_max_slots[i] = slots_per_allocator;
7944 		dedup->mc_alloc_max_slots[i] = slots_per_allocator;
7945 	}
7946 	normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
7947 	special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
7948 	dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
7949 
7950 	for (int c = 0; c < rvd->vdev_children; c++) {
7951 		vdev_t *vd = rvd->vdev_child[c];
7952 		vdev_indirect_state_sync_verify(vd);
7953 
7954 		if (vdev_indirect_should_condense(vd)) {
7955 			spa_condense_indirect_start_sync(vd, tx);
7956 			break;
7957 		}
7958 	}
7959 
7960 	/*
7961 	 * Iterate to convergence.
7962 	 */
7963 	do {
7964 		int pass = ++spa->spa_sync_pass;
7965 
7966 		spa_sync_config_object(spa, tx);
7967 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
7968 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
7969 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
7970 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
7971 		spa_errlog_sync(spa, txg);
7972 		dsl_pool_sync(dp, txg);
7973 
7974 		if (pass < zfs_sync_pass_deferred_free) {
7975 			spa_sync_frees(spa, free_bpl, tx);
7976 		} else {
7977 			/*
7978 			 * We can not defer frees in pass 1, because
7979 			 * we sync the deferred frees later in pass 1.
7980 			 */
7981 			ASSERT3U(pass, >, 1);
7982 			bplist_iterate(free_bpl, bpobj_enqueue_cb,
7983 			    &spa->spa_deferred_bpobj, tx);
7984 		}
7985 
7986 		ddt_sync(spa, txg);
7987 		dsl_scan_sync(dp, tx);
7988 
7989 		if (spa->spa_vdev_removal != NULL)
7990 			svr_sync(spa, tx);
7991 
7992 		while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
7993 		    != NULL)
7994 			vdev_sync(vd, txg);
7995 
7996 		if (pass == 1) {
7997 			spa_sync_upgrades(spa, tx);
7998 			ASSERT3U(txg, >=,
7999 			    spa->spa_uberblock.ub_rootbp.blk_birth);
8000 			/*
8001 			 * Note: We need to check if the MOS is dirty
8002 			 * because we could have marked the MOS dirty
8003 			 * without updating the uberblock (e.g. if we
8004 			 * have sync tasks but no dirty user data).  We
8005 			 * need to check the uberblock's rootbp because
8006 			 * it is updated if we have synced out dirty
8007 			 * data (though in this case the MOS will most
8008 			 * likely also be dirty due to second order
8009 			 * effects, we don't want to rely on that here).
8010 			 */
8011 			if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
8012 			    !dmu_objset_is_dirty(mos, txg)) {
8013 				/*
8014 				 * Nothing changed on the first pass,
8015 				 * therefore this TXG is a no-op.  Avoid
8016 				 * syncing deferred frees, so that we
8017 				 * can keep this TXG as a no-op.
8018 				 */
8019 				ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
8020 				    txg));
8021 				ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
8022 				ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
8023 				ASSERT(txg_list_empty(&dp->dp_early_sync_tasks,
8024 				    txg));
8025 				break;
8026 			}
8027 			spa_sync_deferred_frees(spa, tx);
8028 		}
8029 
8030 	} while (dmu_objset_is_dirty(mos, txg));
8031 
8032 	if (!list_is_empty(&spa->spa_config_dirty_list)) {
8033 		/*
8034 		 * Make sure that the number of ZAPs for all the vdevs matches
8035 		 * the number of ZAPs in the per-vdev ZAP list. This only gets
8036 		 * called if the config is dirty; otherwise there may be
8037 		 * outstanding AVZ operations that weren't completed in
8038 		 * spa_sync_config_object.
8039 		 */
8040 		uint64_t all_vdev_zap_entry_count;
8041 		ASSERT0(zap_count(spa->spa_meta_objset,
8042 		    spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count));
8043 		ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==,
8044 		    all_vdev_zap_entry_count);
8045 	}
8046 
8047 	if (spa->spa_vdev_removal != NULL) {
8048 		ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]);
8049 	}
8050 
8051 	/*
8052 	 * Rewrite the vdev configuration (which includes the uberblock)
8053 	 * to commit the transaction group.
8054 	 *
8055 	 * If there are no dirty vdevs, we sync the uberblock to a few
8056 	 * random top-level vdevs that are known to be visible in the
8057 	 * config cache (see spa_vdev_add() for a complete description).
8058 	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
8059 	 */
8060 	for (;;) {
8061 		/*
8062 		 * We hold SCL_STATE to prevent vdev open/close/etc.
8063 		 * while we're attempting to write the vdev labels.
8064 		 */
8065 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
8066 
8067 		if (list_is_empty(&spa->spa_config_dirty_list)) {
8068 			vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL };
8069 			int svdcount = 0;
8070 			int children = rvd->vdev_children;
8071 			int c0 = spa_get_random(children);
8072 
8073 			for (int c = 0; c < children; c++) {
8074 				vd = rvd->vdev_child[(c0 + c) % children];
8075 
8076 				/* Stop when revisiting the first vdev */
8077 				if (c > 0 && svd[0] == vd)
8078 					break;
8079 
8080 				if (vd->vdev_ms_array == 0 || vd->vdev_islog ||
8081 				    !vdev_is_concrete(vd))
8082 					continue;
8083 
8084 				svd[svdcount++] = vd;
8085 				if (svdcount == SPA_SYNC_MIN_VDEVS)
8086 					break;
8087 			}
8088 			error = vdev_config_sync(svd, svdcount, txg);
8089 		} else {
8090 			error = vdev_config_sync(rvd->vdev_child,
8091 			    rvd->vdev_children, txg);
8092 		}
8093 
8094 		if (error == 0)
8095 			spa->spa_last_synced_guid = rvd->vdev_guid;
8096 
8097 		spa_config_exit(spa, SCL_STATE, FTAG);
8098 
8099 		if (error == 0)
8100 			break;
8101 		zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR);
8102 		zio_resume_wait(spa);
8103 	}
8104 	dmu_tx_commit(tx);
8105 
8106 	VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
8107 
8108 	/*
8109 	 * Clear the dirty config list.
8110 	 */
8111 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
8112 		vdev_config_clean(vd);
8113 
8114 	/*
8115 	 * Now that the new config has synced transactionally,
8116 	 * let it become visible to the config cache.
8117 	 */
8118 	if (spa->spa_config_syncing != NULL) {
8119 		spa_config_set(spa, spa->spa_config_syncing);
8120 		spa->spa_config_txg = txg;
8121 		spa->spa_config_syncing = NULL;
8122 	}
8123 
8124 	dsl_pool_sync_done(dp, txg);
8125 
8126 	for (int i = 0; i < spa->spa_alloc_count; i++) {
8127 		mutex_enter(&spa->spa_alloc_locks[i]);
8128 		VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i]));
8129 		mutex_exit(&spa->spa_alloc_locks[i]);
8130 	}
8131 
8132 	/*
8133 	 * Update usable space statistics.
8134 	 */
8135 	while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
8136 	    != NULL)
8137 		vdev_sync_done(vd, txg);
8138 
8139 	spa_update_dspace(spa);
8140 
8141 	/*
8142 	 * It had better be the case that we didn't dirty anything
8143 	 * since vdev_config_sync().
8144 	 */
8145 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
8146 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
8147 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
8148 
8149 	while (zfs_pause_spa_sync)
8150 		delay(1);
8151 
8152 	spa->spa_sync_pass = 0;
8153 
8154 	/*
8155 	 * Update the last synced uberblock here. We want to do this at
8156 	 * the end of spa_sync() so that consumers of spa_last_synced_txg()
8157 	 * will be guaranteed that all the processing associated with
8158 	 * that txg has been completed.
8159 	 */
8160 	spa->spa_ubsync = spa->spa_uberblock;
8161 	spa_config_exit(spa, SCL_CONFIG, FTAG);
8162 
8163 	spa_handle_ignored_writes(spa);
8164 
8165 	/*
8166 	 * If any async tasks have been requested, kick them off.
8167 	 */
8168 	spa_async_dispatch(spa);
8169 }
8170 
8171 /*
8172  * Sync all pools.  We don't want to hold the namespace lock across these
8173  * operations, so we take a reference on the spa_t and drop the lock during the
8174  * sync.
8175  */
8176 void
8177 spa_sync_allpools(void)
8178 {
8179 	spa_t *spa = NULL;
8180 	mutex_enter(&spa_namespace_lock);
8181 	while ((spa = spa_next(spa)) != NULL) {
8182 		if (spa_state(spa) != POOL_STATE_ACTIVE ||
8183 		    !spa_writeable(spa) || spa_suspended(spa))
8184 			continue;
8185 		spa_open_ref(spa, FTAG);
8186 		mutex_exit(&spa_namespace_lock);
8187 		txg_wait_synced(spa_get_dsl(spa), 0);
8188 		mutex_enter(&spa_namespace_lock);
8189 		spa_close(spa, FTAG);
8190 	}
8191 	mutex_exit(&spa_namespace_lock);
8192 }
8193 
8194 /*
8195  * ==========================================================================
8196  * Miscellaneous routines
8197  * ==========================================================================
8198  */
8199 
8200 /*
8201  * Remove all pools in the system.
8202  */
8203 void
8204 spa_evict_all(void)
8205 {
8206 	spa_t *spa;
8207 
8208 	/*
8209 	 * Remove all cached state.  All pools should be closed now,
8210 	 * so every spa in the AVL tree should be unreferenced.
8211 	 */
8212 	mutex_enter(&spa_namespace_lock);
8213 	while ((spa = spa_next(NULL)) != NULL) {
8214 		/*
8215 		 * Stop async tasks.  The async thread may need to detach
8216 		 * a device that's been replaced, which requires grabbing
8217 		 * spa_namespace_lock, so we must drop it here.
8218 		 */
8219 		spa_open_ref(spa, FTAG);
8220 		mutex_exit(&spa_namespace_lock);
8221 		spa_async_suspend(spa);
8222 		mutex_enter(&spa_namespace_lock);
8223 		spa_close(spa, FTAG);
8224 
8225 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
8226 			spa_unload(spa);
8227 			spa_deactivate(spa);
8228 		}
8229 		spa_remove(spa);
8230 	}
8231 	mutex_exit(&spa_namespace_lock);
8232 }
8233 
8234 vdev_t *
8235 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
8236 {
8237 	vdev_t *vd;
8238 	int i;
8239 
8240 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
8241 		return (vd);
8242 
8243 	if (aux) {
8244 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
8245 			vd = spa->spa_l2cache.sav_vdevs[i];
8246 			if (vd->vdev_guid == guid)
8247 				return (vd);
8248 		}
8249 
8250 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
8251 			vd = spa->spa_spares.sav_vdevs[i];
8252 			if (vd->vdev_guid == guid)
8253 				return (vd);
8254 		}
8255 	}
8256 
8257 	return (NULL);
8258 }
8259 
8260 void
8261 spa_upgrade(spa_t *spa, uint64_t version)
8262 {
8263 	ASSERT(spa_writeable(spa));
8264 
8265 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
8266 
8267 	/*
8268 	 * This should only be called for a non-faulted pool, and since a
8269 	 * future version would result in an unopenable pool, this shouldn't be
8270 	 * possible.
8271 	 */
8272 	ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
8273 	ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
8274 
8275 	spa->spa_uberblock.ub_version = version;
8276 	vdev_config_dirty(spa->spa_root_vdev);
8277 
8278 	spa_config_exit(spa, SCL_ALL, FTAG);
8279 
8280 	txg_wait_synced(spa_get_dsl(spa), 0);
8281 }
8282 
8283 boolean_t
8284 spa_has_spare(spa_t *spa, uint64_t guid)
8285 {
8286 	int i;
8287 	uint64_t spareguid;
8288 	spa_aux_vdev_t *sav = &spa->spa_spares;
8289 
8290 	for (i = 0; i < sav->sav_count; i++)
8291 		if (sav->sav_vdevs[i]->vdev_guid == guid)
8292 			return (B_TRUE);
8293 
8294 	for (i = 0; i < sav->sav_npending; i++) {
8295 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
8296 		    &spareguid) == 0 && spareguid == guid)
8297 			return (B_TRUE);
8298 	}
8299 
8300 	return (B_FALSE);
8301 }
8302 
8303 /*
8304  * Check if a pool has an active shared spare device.
8305  * Note: reference count of an active spare is 2, as a spare and as a replace
8306  */
8307 static boolean_t
8308 spa_has_active_shared_spare(spa_t *spa)
8309 {
8310 	int i, refcnt;
8311 	uint64_t pool;
8312 	spa_aux_vdev_t *sav = &spa->spa_spares;
8313 
8314 	for (i = 0; i < sav->sav_count; i++) {
8315 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
8316 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
8317 		    refcnt > 2)
8318 			return (B_TRUE);
8319 	}
8320 
8321 	return (B_FALSE);
8322 }
8323 
8324 sysevent_t *
8325 spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
8326 {
8327 	sysevent_t		*ev = NULL;
8328 #ifdef _KERNEL
8329 	sysevent_attr_list_t	*attr = NULL;
8330 	sysevent_value_t	value;
8331 
8332 	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
8333 	    SE_SLEEP);
8334 	ASSERT(ev != NULL);
8335 
8336 	value.value_type = SE_DATA_TYPE_STRING;
8337 	value.value.sv_string = spa_name(spa);
8338 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
8339 		goto done;
8340 
8341 	value.value_type = SE_DATA_TYPE_UINT64;
8342 	value.value.sv_uint64 = spa_guid(spa);
8343 	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
8344 		goto done;
8345 
8346 	if (vd) {
8347 		value.value_type = SE_DATA_TYPE_UINT64;
8348 		value.value.sv_uint64 = vd->vdev_guid;
8349 		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
8350 		    SE_SLEEP) != 0)
8351 			goto done;
8352 
8353 		if (vd->vdev_path) {
8354 			value.value_type = SE_DATA_TYPE_STRING;
8355 			value.value.sv_string = vd->vdev_path;
8356 			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
8357 			    &value, SE_SLEEP) != 0)
8358 				goto done;
8359 		}
8360 	}
8361 
8362 	if (hist_nvl != NULL) {
8363 		fnvlist_merge((nvlist_t *)attr, hist_nvl);
8364 	}
8365 
8366 	if (sysevent_attach_attributes(ev, attr) != 0)
8367 		goto done;
8368 	attr = NULL;
8369 
8370 done:
8371 	if (attr)
8372 		sysevent_free_attr(attr);
8373 
8374 #endif
8375 	return (ev);
8376 }
8377 
8378 void
8379 spa_event_post(sysevent_t *ev)
8380 {
8381 #ifdef _KERNEL
8382 	sysevent_id_t		eid;
8383 
8384 	(void) log_sysevent(ev, SE_SLEEP, &eid);
8385 	sysevent_free(ev);
8386 #endif
8387 }
8388 
8389 void
8390 spa_event_discard(sysevent_t *ev)
8391 {
8392 #ifdef _KERNEL
8393 	sysevent_free(ev);
8394 #endif
8395 }
8396 
8397 /*
8398  * Post a sysevent corresponding to the given event.  The 'name' must be one of
8399  * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
8400  * filled in from the spa and (optionally) the vdev and history nvl.  This
8401  * doesn't do anything in the userland libzpool, as we don't want consumers to
8402  * misinterpret ztest or zdb as real changes.
8403  */
8404 void
8405 spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
8406 {
8407 	spa_event_post(spa_event_create(spa, vd, hist_nvl, name));
8408 }
8409