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