xref: /freebsd/sys/contrib/openzfs/module/zfs/spa.c (revision c27f7d6b9cf6d4ab01cb3d0972726c14e0aca146)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2011, 2024 by Delphix. All rights reserved.
26  * Copyright (c) 2018, Nexenta Systems, Inc.  All rights reserved.
27  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28  * Copyright 2013 Saso Kiselkov. All rights reserved.
29  * Copyright (c) 2014 Integros [integros.com]
30  * Copyright 2016 Toomas Soome <tsoome@me.com>
31  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
32  * Copyright 2018 Joyent, Inc.
33  * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
34  * Copyright 2017 Joyent, Inc.
35  * Copyright (c) 2017, Intel Corporation.
36  * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
37  * Copyright (c) 2023 Hewlett Packard Enterprise Development LP.
38  * Copyright (c) 2023, 2024, Klara Inc.
39  */
40 
41 /*
42  * SPA: Storage Pool Allocator
43  *
44  * This file contains all the routines used when modifying on-disk SPA state.
45  * This includes opening, importing, destroying, exporting a pool, and syncing a
46  * pool.
47  */
48 
49 #include <sys/zfs_context.h>
50 #include <sys/fm/fs/zfs.h>
51 #include <sys/spa_impl.h>
52 #include <sys/zio.h>
53 #include <sys/zio_checksum.h>
54 #include <sys/dmu.h>
55 #include <sys/dmu_tx.h>
56 #include <sys/zap.h>
57 #include <sys/zil.h>
58 #include <sys/brt.h>
59 #include <sys/ddt.h>
60 #include <sys/vdev_impl.h>
61 #include <sys/vdev_removal.h>
62 #include <sys/vdev_indirect_mapping.h>
63 #include <sys/vdev_indirect_births.h>
64 #include <sys/vdev_initialize.h>
65 #include <sys/vdev_rebuild.h>
66 #include <sys/vdev_trim.h>
67 #include <sys/vdev_disk.h>
68 #include <sys/vdev_raidz.h>
69 #include <sys/vdev_draid.h>
70 #include <sys/metaslab.h>
71 #include <sys/metaslab_impl.h>
72 #include <sys/mmp.h>
73 #include <sys/uberblock_impl.h>
74 #include <sys/txg.h>
75 #include <sys/avl.h>
76 #include <sys/bpobj.h>
77 #include <sys/dmu_traverse.h>
78 #include <sys/dmu_objset.h>
79 #include <sys/unique.h>
80 #include <sys/dsl_pool.h>
81 #include <sys/dsl_dataset.h>
82 #include <sys/dsl_dir.h>
83 #include <sys/dsl_prop.h>
84 #include <sys/dsl_synctask.h>
85 #include <sys/fs/zfs.h>
86 #include <sys/arc.h>
87 #include <sys/callb.h>
88 #include <sys/systeminfo.h>
89 #include <sys/zfs_ioctl.h>
90 #include <sys/dsl_scan.h>
91 #include <sys/zfeature.h>
92 #include <sys/dsl_destroy.h>
93 #include <sys/zvol.h>
94 
95 #ifdef	_KERNEL
96 #include <sys/fm/protocol.h>
97 #include <sys/fm/util.h>
98 #include <sys/callb.h>
99 #include <sys/zone.h>
100 #include <sys/vmsystm.h>
101 #endif	/* _KERNEL */
102 
103 #include "zfs_prop.h"
104 #include "zfs_comutil.h"
105 #include <cityhash.h>
106 
107 /*
108  * spa_thread() existed on Illumos as a parent thread for the various worker
109  * threads that actually run the pool, as a way to both reference the entire
110  * pool work as a single object, and to share properties like scheduling
111  * options. It has not yet been adapted to Linux or FreeBSD. This define is
112  * used to mark related parts of the code to make things easier for the reader,
113  * and to compile this code out. It can be removed when someone implements it,
114  * moves it to some Illumos-specific place, or removes it entirely.
115  */
116 #undef HAVE_SPA_THREAD
117 
118 /*
119  * The "System Duty Cycle" scheduling class is an Illumos feature to help
120  * prevent CPU-intensive kernel threads from affecting latency on interactive
121  * threads. It doesn't exist on Linux or FreeBSD, so the supporting code is
122  * gated behind a define. On Illumos SDC depends on spa_thread(), but
123  * spa_thread() also has other uses, so this is a separate define.
124  */
125 #undef HAVE_SYSDC
126 
127 /*
128  * The interval, in seconds, at which failed configuration cache file writes
129  * should be retried.
130  */
131 int zfs_ccw_retry_interval = 300;
132 
133 typedef enum zti_modes {
134 	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
135 	ZTI_MODE_SCALE,			/* Taskqs scale with CPUs. */
136 	ZTI_MODE_SYNC,			/* sync thread assigned */
137 	ZTI_MODE_NULL,			/* don't create a taskq */
138 	ZTI_NMODES
139 } zti_modes_t;
140 
141 #define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
142 #define	ZTI_PCT(n)	{ ZTI_MODE_ONLINE_PERCENT, (n), 1 }
143 #define	ZTI_SCALE	{ ZTI_MODE_SCALE, 0, 1 }
144 #define	ZTI_SYNC	{ ZTI_MODE_SYNC, 0, 1 }
145 #define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
146 
147 #define	ZTI_N(n)	ZTI_P(n, 1)
148 #define	ZTI_ONE		ZTI_N(1)
149 
150 typedef struct zio_taskq_info {
151 	zti_modes_t zti_mode;
152 	uint_t zti_value;
153 	uint_t zti_count;
154 } zio_taskq_info_t;
155 
156 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
157 	"iss", "iss_h", "int", "int_h"
158 };
159 
160 /*
161  * This table defines the taskq settings for each ZFS I/O type. When
162  * initializing a pool, we use this table to create an appropriately sized
163  * taskq. Some operations are low volume and therefore have a small, static
164  * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
165  * macros. Other operations process a large amount of data; the ZTI_SCALE
166  * macro causes us to create a taskq oriented for throughput. Some operations
167  * are so high frequency and short-lived that the taskq itself can become a
168  * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
169  * additional degree of parallelism specified by the number of threads per-
170  * taskq and the number of taskqs; when dispatching an event in this case, the
171  * particular taskq is chosen at random. ZTI_SCALE uses a number of taskqs
172  * that scales with the number of CPUs.
173  *
174  * The different taskq priorities are to handle the different contexts (issue
175  * and interrupt) and then to reserve threads for high priority I/Os that
176  * need to be handled with minimum delay.  Illumos taskq has unfair TQ_FRONT
177  * implementation, so separate high priority threads are used there.
178  */
179 static zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
180 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
181 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
182 	{ ZTI_N(8),	ZTI_NULL,	ZTI_SCALE,	ZTI_NULL }, /* READ */
183 #ifdef illumos
184 	{ ZTI_SYNC,	ZTI_N(5),	ZTI_SCALE,	ZTI_N(5) }, /* WRITE */
185 #else
186 	{ ZTI_SYNC,	ZTI_NULL,	ZTI_SCALE,	ZTI_NULL }, /* WRITE */
187 #endif
188 	{ ZTI_SCALE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
189 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
190 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FLUSH */
191 	{ ZTI_N(4),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* TRIM */
192 };
193 
194 static void spa_sync_version(void *arg, dmu_tx_t *tx);
195 static void spa_sync_props(void *arg, dmu_tx_t *tx);
196 static boolean_t spa_has_active_shared_spare(spa_t *spa);
197 static int spa_load_impl(spa_t *spa, spa_import_type_t type,
198     const char **ereport);
199 static void spa_vdev_resilver_done(spa_t *spa);
200 
201 /*
202  * Percentage of all CPUs that can be used by the metaslab preload taskq.
203  */
204 static uint_t metaslab_preload_pct = 50;
205 
206 static uint_t	zio_taskq_batch_pct = 80;	  /* 1 thread per cpu in pset */
207 static uint_t	zio_taskq_batch_tpq;		  /* threads per taskq */
208 
209 #ifdef HAVE_SYSDC
210 static const boolean_t	zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
211 static const uint_t	zio_taskq_basedc = 80;	  /* base duty cycle */
212 #endif
213 
214 #ifdef HAVE_SPA_THREAD
215 static const boolean_t spa_create_process = B_TRUE; /* no process => no sysdc */
216 #endif
217 
218 static uint_t	zio_taskq_write_tpq = 16;
219 
220 /*
221  * Report any spa_load_verify errors found, but do not fail spa_load.
222  * This is used by zdb to analyze non-idle pools.
223  */
224 boolean_t	spa_load_verify_dryrun = B_FALSE;
225 
226 /*
227  * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
228  * This is used by zdb for spacemaps verification.
229  */
230 boolean_t	spa_mode_readable_spacemaps = B_FALSE;
231 
232 /*
233  * This (illegal) pool name is used when temporarily importing a spa_t in order
234  * to get the vdev stats associated with the imported devices.
235  */
236 #define	TRYIMPORT_NAME	"$import"
237 
238 /*
239  * For debugging purposes: print out vdev tree during pool import.
240  */
241 static int		spa_load_print_vdev_tree = B_FALSE;
242 
243 /*
244  * A non-zero value for zfs_max_missing_tvds means that we allow importing
245  * pools with missing top-level vdevs. This is strictly intended for advanced
246  * pool recovery cases since missing data is almost inevitable. Pools with
247  * missing devices can only be imported read-only for safety reasons, and their
248  * fail-mode will be automatically set to "continue".
249  *
250  * With 1 missing vdev we should be able to import the pool and mount all
251  * datasets. User data that was not modified after the missing device has been
252  * added should be recoverable. This means that snapshots created prior to the
253  * addition of that device should be completely intact.
254  *
255  * With 2 missing vdevs, some datasets may fail to mount since there are
256  * dataset statistics that are stored as regular metadata. Some data might be
257  * recoverable if those vdevs were added recently.
258  *
259  * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
260  * may be missing entirely. Chances of data recovery are very low. Note that
261  * there are also risks of performing an inadvertent rewind as we might be
262  * missing all the vdevs with the latest uberblocks.
263  */
264 uint64_t	zfs_max_missing_tvds = 0;
265 
266 /*
267  * The parameters below are similar to zfs_max_missing_tvds but are only
268  * intended for a preliminary open of the pool with an untrusted config which
269  * might be incomplete or out-dated.
270  *
271  * We are more tolerant for pools opened from a cachefile since we could have
272  * an out-dated cachefile where a device removal was not registered.
273  * We could have set the limit arbitrarily high but in the case where devices
274  * are really missing we would want to return the proper error codes; we chose
275  * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
276  * and we get a chance to retrieve the trusted config.
277  */
278 uint64_t	zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1;
279 
280 /*
281  * In the case where config was assembled by scanning device paths (/dev/dsks
282  * by default) we are less tolerant since all the existing devices should have
283  * been detected and we want spa_load to return the right error codes.
284  */
285 uint64_t	zfs_max_missing_tvds_scan = 0;
286 
287 /*
288  * Debugging aid that pauses spa_sync() towards the end.
289  */
290 static const boolean_t	zfs_pause_spa_sync = B_FALSE;
291 
292 /*
293  * Variables to indicate the livelist condense zthr func should wait at certain
294  * points for the livelist to be removed - used to test condense/destroy races
295  */
296 static int zfs_livelist_condense_zthr_pause = 0;
297 static int zfs_livelist_condense_sync_pause = 0;
298 
299 /*
300  * Variables to track whether or not condense cancellation has been
301  * triggered in testing.
302  */
303 static int zfs_livelist_condense_sync_cancel = 0;
304 static int zfs_livelist_condense_zthr_cancel = 0;
305 
306 /*
307  * Variable to track whether or not extra ALLOC blkptrs were added to a
308  * livelist entry while it was being condensed (caused by the way we track
309  * remapped blkptrs in dbuf_remap_impl)
310  */
311 static int zfs_livelist_condense_new_alloc = 0;
312 
313 /*
314  * ==========================================================================
315  * SPA properties routines
316  * ==========================================================================
317  */
318 
319 /*
320  * Add a (source=src, propname=propval) list to an nvlist.
321  */
322 static void
323 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, const char *strval,
324     uint64_t intval, zprop_source_t src)
325 {
326 	const char *propname = zpool_prop_to_name(prop);
327 	nvlist_t *propval;
328 
329 	propval = fnvlist_alloc();
330 	fnvlist_add_uint64(propval, ZPROP_SOURCE, src);
331 
332 	if (strval != NULL)
333 		fnvlist_add_string(propval, ZPROP_VALUE, strval);
334 	else
335 		fnvlist_add_uint64(propval, ZPROP_VALUE, intval);
336 
337 	fnvlist_add_nvlist(nvl, propname, propval);
338 	nvlist_free(propval);
339 }
340 
341 static int
342 spa_prop_add(spa_t *spa, const char *propname, nvlist_t *outnvl)
343 {
344 	zpool_prop_t prop = zpool_name_to_prop(propname);
345 	zprop_source_t src = ZPROP_SRC_NONE;
346 	uint64_t intval;
347 	int err;
348 
349 	/*
350 	 * NB: Not all properties lookups via this API require
351 	 * the spa props lock, so they must explicitly grab it here.
352 	 */
353 	switch (prop) {
354 	case ZPOOL_PROP_DEDUPCACHED:
355 		err = ddt_get_pool_dedup_cached(spa, &intval);
356 		if (err != 0)
357 			return (SET_ERROR(err));
358 		break;
359 	default:
360 		return (SET_ERROR(EINVAL));
361 	}
362 
363 	spa_prop_add_list(outnvl, prop, NULL, intval, src);
364 
365 	return (0);
366 }
367 
368 int
369 spa_prop_get_nvlist(spa_t *spa, char **props, unsigned int n_props,
370     nvlist_t *outnvl)
371 {
372 	int err = 0;
373 
374 	if (props == NULL)
375 		return (0);
376 
377 	for (unsigned int i = 0; i < n_props && err == 0; i++) {
378 		err = spa_prop_add(spa, props[i], outnvl);
379 	}
380 
381 	return (err);
382 }
383 
384 /*
385  * Add a user property (source=src, propname=propval) to an nvlist.
386  */
387 static void
388 spa_prop_add_user(nvlist_t *nvl, const char *propname, char *strval,
389     zprop_source_t src)
390 {
391 	nvlist_t *propval;
392 
393 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
394 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
395 	VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
396 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
397 	nvlist_free(propval);
398 }
399 
400 /*
401  * Get property values from the spa configuration.
402  */
403 static void
404 spa_prop_get_config(spa_t *spa, nvlist_t *nv)
405 {
406 	vdev_t *rvd = spa->spa_root_vdev;
407 	dsl_pool_t *pool = spa->spa_dsl_pool;
408 	uint64_t size, alloc, cap, version;
409 	const zprop_source_t src = ZPROP_SRC_NONE;
410 	spa_config_dirent_t *dp;
411 	metaslab_class_t *mc = spa_normal_class(spa);
412 
413 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
414 
415 	if (rvd != NULL) {
416 		alloc = metaslab_class_get_alloc(mc);
417 		alloc += metaslab_class_get_alloc(spa_special_class(spa));
418 		alloc += metaslab_class_get_alloc(spa_dedup_class(spa));
419 		alloc += metaslab_class_get_alloc(spa_embedded_log_class(spa));
420 
421 		size = metaslab_class_get_space(mc);
422 		size += metaslab_class_get_space(spa_special_class(spa));
423 		size += metaslab_class_get_space(spa_dedup_class(spa));
424 		size += metaslab_class_get_space(spa_embedded_log_class(spa));
425 
426 		spa_prop_add_list(nv, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
427 		spa_prop_add_list(nv, ZPOOL_PROP_SIZE, NULL, size, src);
428 		spa_prop_add_list(nv, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
429 		spa_prop_add_list(nv, ZPOOL_PROP_FREE, NULL,
430 		    size - alloc, src);
431 		spa_prop_add_list(nv, ZPOOL_PROP_CHECKPOINT, NULL,
432 		    spa->spa_checkpoint_info.sci_dspace, src);
433 
434 		spa_prop_add_list(nv, ZPOOL_PROP_FRAGMENTATION, NULL,
435 		    metaslab_class_fragmentation(mc), src);
436 		spa_prop_add_list(nv, ZPOOL_PROP_EXPANDSZ, NULL,
437 		    metaslab_class_expandable_space(mc), src);
438 		spa_prop_add_list(nv, ZPOOL_PROP_READONLY, NULL,
439 		    (spa_mode(spa) == SPA_MODE_READ), src);
440 
441 		cap = (size == 0) ? 0 : (alloc * 100 / size);
442 		spa_prop_add_list(nv, ZPOOL_PROP_CAPACITY, NULL, cap, src);
443 
444 		spa_prop_add_list(nv, ZPOOL_PROP_DEDUPRATIO, NULL,
445 		    ddt_get_pool_dedup_ratio(spa), src);
446 		spa_prop_add_list(nv, ZPOOL_PROP_BCLONEUSED, NULL,
447 		    brt_get_used(spa), src);
448 		spa_prop_add_list(nv, ZPOOL_PROP_BCLONESAVED, NULL,
449 		    brt_get_saved(spa), src);
450 		spa_prop_add_list(nv, ZPOOL_PROP_BCLONERATIO, NULL,
451 		    brt_get_ratio(spa), src);
452 
453 		spa_prop_add_list(nv, ZPOOL_PROP_DEDUP_TABLE_SIZE, NULL,
454 		    ddt_get_ddt_dsize(spa), src);
455 		spa_prop_add_list(nv, ZPOOL_PROP_HEALTH, NULL,
456 		    rvd->vdev_state, src);
457 		spa_prop_add_list(nv, ZPOOL_PROP_LAST_SCRUBBED_TXG, NULL,
458 		    spa_get_last_scrubbed_txg(spa), src);
459 
460 		version = spa_version(spa);
461 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) {
462 			spa_prop_add_list(nv, ZPOOL_PROP_VERSION, NULL,
463 			    version, ZPROP_SRC_DEFAULT);
464 		} else {
465 			spa_prop_add_list(nv, ZPOOL_PROP_VERSION, NULL,
466 			    version, ZPROP_SRC_LOCAL);
467 		}
468 		spa_prop_add_list(nv, ZPOOL_PROP_LOAD_GUID,
469 		    NULL, spa_load_guid(spa), src);
470 	}
471 
472 	if (pool != NULL) {
473 		/*
474 		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
475 		 * when opening pools before this version freedir will be NULL.
476 		 */
477 		if (pool->dp_free_dir != NULL) {
478 			spa_prop_add_list(nv, ZPOOL_PROP_FREEING, NULL,
479 			    dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
480 			    src);
481 		} else {
482 			spa_prop_add_list(nv, ZPOOL_PROP_FREEING,
483 			    NULL, 0, src);
484 		}
485 
486 		if (pool->dp_leak_dir != NULL) {
487 			spa_prop_add_list(nv, ZPOOL_PROP_LEAKED, NULL,
488 			    dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
489 			    src);
490 		} else {
491 			spa_prop_add_list(nv, ZPOOL_PROP_LEAKED,
492 			    NULL, 0, src);
493 		}
494 	}
495 
496 	spa_prop_add_list(nv, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
497 
498 	if (spa->spa_comment != NULL) {
499 		spa_prop_add_list(nv, ZPOOL_PROP_COMMENT, spa->spa_comment,
500 		    0, ZPROP_SRC_LOCAL);
501 	}
502 
503 	if (spa->spa_compatibility != NULL) {
504 		spa_prop_add_list(nv, ZPOOL_PROP_COMPATIBILITY,
505 		    spa->spa_compatibility, 0, ZPROP_SRC_LOCAL);
506 	}
507 
508 	if (spa->spa_root != NULL)
509 		spa_prop_add_list(nv, ZPOOL_PROP_ALTROOT, spa->spa_root,
510 		    0, ZPROP_SRC_LOCAL);
511 
512 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
513 		spa_prop_add_list(nv, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
514 		    MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
515 	} else {
516 		spa_prop_add_list(nv, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
517 		    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
518 	}
519 
520 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) {
521 		spa_prop_add_list(nv, ZPOOL_PROP_MAXDNODESIZE, NULL,
522 		    DNODE_MAX_SIZE, ZPROP_SRC_NONE);
523 	} else {
524 		spa_prop_add_list(nv, ZPOOL_PROP_MAXDNODESIZE, NULL,
525 		    DNODE_MIN_SIZE, ZPROP_SRC_NONE);
526 	}
527 
528 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
529 		if (dp->scd_path == NULL) {
530 			spa_prop_add_list(nv, ZPOOL_PROP_CACHEFILE,
531 			    "none", 0, ZPROP_SRC_LOCAL);
532 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
533 			spa_prop_add_list(nv, ZPOOL_PROP_CACHEFILE,
534 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
535 		}
536 	}
537 }
538 
539 /*
540  * Get zpool property values.
541  */
542 int
543 spa_prop_get(spa_t *spa, nvlist_t *nv)
544 {
545 	objset_t *mos = spa->spa_meta_objset;
546 	zap_cursor_t zc;
547 	zap_attribute_t *za;
548 	dsl_pool_t *dp;
549 	int err = 0;
550 
551 	dp = spa_get_dsl(spa);
552 	dsl_pool_config_enter(dp, FTAG);
553 	za = zap_attribute_alloc();
554 	mutex_enter(&spa->spa_props_lock);
555 
556 	/*
557 	 * Get properties from the spa config.
558 	 */
559 	spa_prop_get_config(spa, nv);
560 
561 	/* If no pool property object, no more prop to get. */
562 	if (mos == NULL || spa->spa_pool_props_object == 0)
563 		goto out;
564 
565 	/*
566 	 * Get properties from the MOS pool property object.
567 	 */
568 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
569 	    (err = zap_cursor_retrieve(&zc, za)) == 0;
570 	    zap_cursor_advance(&zc)) {
571 		uint64_t intval = 0;
572 		char *strval = NULL;
573 		zprop_source_t src = ZPROP_SRC_DEFAULT;
574 		zpool_prop_t prop;
575 
576 		if ((prop = zpool_name_to_prop(za->za_name)) ==
577 		    ZPOOL_PROP_INVAL && !zfs_prop_user(za->za_name))
578 			continue;
579 
580 		switch (za->za_integer_length) {
581 		case 8:
582 			/* integer property */
583 			if (za->za_first_integer !=
584 			    zpool_prop_default_numeric(prop))
585 				src = ZPROP_SRC_LOCAL;
586 
587 			if (prop == ZPOOL_PROP_BOOTFS) {
588 				dsl_dataset_t *ds = NULL;
589 
590 				err = dsl_dataset_hold_obj(dp,
591 				    za->za_first_integer, FTAG, &ds);
592 				if (err != 0)
593 					break;
594 
595 				strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
596 				    KM_SLEEP);
597 				dsl_dataset_name(ds, strval);
598 				dsl_dataset_rele(ds, FTAG);
599 			} else {
600 				strval = NULL;
601 				intval = za->za_first_integer;
602 			}
603 
604 			spa_prop_add_list(nv, prop, strval, intval, src);
605 
606 			if (strval != NULL)
607 				kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
608 
609 			break;
610 
611 		case 1:
612 			/* string property */
613 			strval = kmem_alloc(za->za_num_integers, KM_SLEEP);
614 			err = zap_lookup(mos, spa->spa_pool_props_object,
615 			    za->za_name, 1, za->za_num_integers, strval);
616 			if (err) {
617 				kmem_free(strval, za->za_num_integers);
618 				break;
619 			}
620 			if (prop != ZPOOL_PROP_INVAL) {
621 				spa_prop_add_list(nv, prop, strval, 0, src);
622 			} else {
623 				src = ZPROP_SRC_LOCAL;
624 				spa_prop_add_user(nv, za->za_name, strval,
625 				    src);
626 			}
627 			kmem_free(strval, za->za_num_integers);
628 			break;
629 
630 		default:
631 			break;
632 		}
633 	}
634 	zap_cursor_fini(&zc);
635 out:
636 	mutex_exit(&spa->spa_props_lock);
637 	dsl_pool_config_exit(dp, FTAG);
638 	zap_attribute_free(za);
639 
640 	if (err && err != ENOENT)
641 		return (err);
642 
643 	return (0);
644 }
645 
646 /*
647  * Validate the given pool properties nvlist and modify the list
648  * for the property values to be set.
649  */
650 static int
651 spa_prop_validate(spa_t *spa, nvlist_t *props)
652 {
653 	nvpair_t *elem;
654 	int error = 0, reset_bootfs = 0;
655 	uint64_t objnum = 0;
656 	boolean_t has_feature = B_FALSE;
657 
658 	elem = NULL;
659 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
660 		uint64_t intval;
661 		const char *strval, *slash, *check, *fname;
662 		const char *propname = nvpair_name(elem);
663 		zpool_prop_t prop = zpool_name_to_prop(propname);
664 
665 		switch (prop) {
666 		case ZPOOL_PROP_INVAL:
667 			/*
668 			 * Sanitize the input.
669 			 */
670 			if (zfs_prop_user(propname)) {
671 				if (strlen(propname) >= ZAP_MAXNAMELEN) {
672 					error = SET_ERROR(ENAMETOOLONG);
673 					break;
674 				}
675 
676 				if (strlen(fnvpair_value_string(elem)) >=
677 				    ZAP_MAXVALUELEN) {
678 					error = SET_ERROR(E2BIG);
679 					break;
680 				}
681 			} else if (zpool_prop_feature(propname)) {
682 				if (nvpair_type(elem) != DATA_TYPE_UINT64) {
683 					error = SET_ERROR(EINVAL);
684 					break;
685 				}
686 
687 				if (nvpair_value_uint64(elem, &intval) != 0) {
688 					error = SET_ERROR(EINVAL);
689 					break;
690 				}
691 
692 				if (intval != 0) {
693 					error = SET_ERROR(EINVAL);
694 					break;
695 				}
696 
697 				fname = strchr(propname, '@') + 1;
698 				if (zfeature_lookup_name(fname, NULL) != 0) {
699 					error = SET_ERROR(EINVAL);
700 					break;
701 				}
702 
703 				has_feature = B_TRUE;
704 			} else {
705 				error = SET_ERROR(EINVAL);
706 				break;
707 			}
708 			break;
709 
710 		case ZPOOL_PROP_VERSION:
711 			error = nvpair_value_uint64(elem, &intval);
712 			if (!error &&
713 			    (intval < spa_version(spa) ||
714 			    intval > SPA_VERSION_BEFORE_FEATURES ||
715 			    has_feature))
716 				error = SET_ERROR(EINVAL);
717 			break;
718 
719 		case ZPOOL_PROP_DEDUP_TABLE_QUOTA:
720 			error = nvpair_value_uint64(elem, &intval);
721 			break;
722 
723 		case ZPOOL_PROP_DELEGATION:
724 		case ZPOOL_PROP_AUTOREPLACE:
725 		case ZPOOL_PROP_LISTSNAPS:
726 		case ZPOOL_PROP_AUTOEXPAND:
727 		case ZPOOL_PROP_AUTOTRIM:
728 			error = nvpair_value_uint64(elem, &intval);
729 			if (!error && intval > 1)
730 				error = SET_ERROR(EINVAL);
731 			break;
732 
733 		case ZPOOL_PROP_MULTIHOST:
734 			error = nvpair_value_uint64(elem, &intval);
735 			if (!error && intval > 1)
736 				error = SET_ERROR(EINVAL);
737 
738 			if (!error) {
739 				uint32_t hostid = zone_get_hostid(NULL);
740 				if (hostid)
741 					spa->spa_hostid = hostid;
742 				else
743 					error = SET_ERROR(ENOTSUP);
744 			}
745 
746 			break;
747 
748 		case ZPOOL_PROP_BOOTFS:
749 			/*
750 			 * If the pool version is less than SPA_VERSION_BOOTFS,
751 			 * or the pool is still being created (version == 0),
752 			 * the bootfs property cannot be set.
753 			 */
754 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
755 				error = SET_ERROR(ENOTSUP);
756 				break;
757 			}
758 
759 			/*
760 			 * Make sure the vdev config is bootable
761 			 */
762 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
763 				error = SET_ERROR(ENOTSUP);
764 				break;
765 			}
766 
767 			reset_bootfs = 1;
768 
769 			error = nvpair_value_string(elem, &strval);
770 
771 			if (!error) {
772 				objset_t *os;
773 
774 				if (strval == NULL || strval[0] == '\0') {
775 					objnum = zpool_prop_default_numeric(
776 					    ZPOOL_PROP_BOOTFS);
777 					break;
778 				}
779 
780 				error = dmu_objset_hold(strval, FTAG, &os);
781 				if (error != 0)
782 					break;
783 
784 				/* Must be ZPL. */
785 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
786 					error = SET_ERROR(ENOTSUP);
787 				} else {
788 					objnum = dmu_objset_id(os);
789 				}
790 				dmu_objset_rele(os, FTAG);
791 			}
792 			break;
793 
794 		case ZPOOL_PROP_FAILUREMODE:
795 			error = nvpair_value_uint64(elem, &intval);
796 			if (!error && intval > ZIO_FAILURE_MODE_PANIC)
797 				error = SET_ERROR(EINVAL);
798 
799 			/*
800 			 * This is a special case which only occurs when
801 			 * the pool has completely failed. This allows
802 			 * the user to change the in-core failmode property
803 			 * without syncing it out to disk (I/Os might
804 			 * currently be blocked). We do this by returning
805 			 * EIO to the caller (spa_prop_set) to trick it
806 			 * into thinking we encountered a property validation
807 			 * error.
808 			 */
809 			if (!error && spa_suspended(spa)) {
810 				spa->spa_failmode = intval;
811 				error = SET_ERROR(EIO);
812 			}
813 			break;
814 
815 		case ZPOOL_PROP_CACHEFILE:
816 			if ((error = nvpair_value_string(elem, &strval)) != 0)
817 				break;
818 
819 			if (strval[0] == '\0')
820 				break;
821 
822 			if (strcmp(strval, "none") == 0)
823 				break;
824 
825 			if (strval[0] != '/') {
826 				error = SET_ERROR(EINVAL);
827 				break;
828 			}
829 
830 			slash = strrchr(strval, '/');
831 			ASSERT(slash != NULL);
832 
833 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
834 			    strcmp(slash, "/..") == 0)
835 				error = SET_ERROR(EINVAL);
836 			break;
837 
838 		case ZPOOL_PROP_COMMENT:
839 			if ((error = nvpair_value_string(elem, &strval)) != 0)
840 				break;
841 			for (check = strval; *check != '\0'; check++) {
842 				if (!isprint(*check)) {
843 					error = SET_ERROR(EINVAL);
844 					break;
845 				}
846 			}
847 			if (strlen(strval) > ZPROP_MAX_COMMENT)
848 				error = SET_ERROR(E2BIG);
849 			break;
850 
851 		default:
852 			break;
853 		}
854 
855 		if (error)
856 			break;
857 	}
858 
859 	(void) nvlist_remove_all(props,
860 	    zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO));
861 
862 	if (!error && reset_bootfs) {
863 		error = nvlist_remove(props,
864 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
865 
866 		if (!error) {
867 			error = nvlist_add_uint64(props,
868 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
869 		}
870 	}
871 
872 	return (error);
873 }
874 
875 void
876 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
877 {
878 	const char *cachefile;
879 	spa_config_dirent_t *dp;
880 
881 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
882 	    &cachefile) != 0)
883 		return;
884 
885 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
886 	    KM_SLEEP);
887 
888 	if (cachefile[0] == '\0')
889 		dp->scd_path = spa_strdup(spa_config_path);
890 	else if (strcmp(cachefile, "none") == 0)
891 		dp->scd_path = NULL;
892 	else
893 		dp->scd_path = spa_strdup(cachefile);
894 
895 	list_insert_head(&spa->spa_config_list, dp);
896 	if (need_sync)
897 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
898 }
899 
900 int
901 spa_prop_set(spa_t *spa, nvlist_t *nvp)
902 {
903 	int error;
904 	nvpair_t *elem = NULL;
905 	boolean_t need_sync = B_FALSE;
906 
907 	if ((error = spa_prop_validate(spa, nvp)) != 0)
908 		return (error);
909 
910 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
911 		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
912 
913 		if (prop == ZPOOL_PROP_CACHEFILE ||
914 		    prop == ZPOOL_PROP_ALTROOT ||
915 		    prop == ZPOOL_PROP_READONLY)
916 			continue;
917 
918 		if (prop == ZPOOL_PROP_INVAL &&
919 		    zfs_prop_user(nvpair_name(elem))) {
920 			need_sync = B_TRUE;
921 			break;
922 		}
923 
924 		if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) {
925 			uint64_t ver = 0;
926 
927 			if (prop == ZPOOL_PROP_VERSION) {
928 				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
929 			} else {
930 				ASSERT(zpool_prop_feature(nvpair_name(elem)));
931 				ver = SPA_VERSION_FEATURES;
932 				need_sync = B_TRUE;
933 			}
934 
935 			/* Save time if the version is already set. */
936 			if (ver == spa_version(spa))
937 				continue;
938 
939 			/*
940 			 * In addition to the pool directory object, we might
941 			 * create the pool properties object, the features for
942 			 * read object, the features for write object, or the
943 			 * feature descriptions object.
944 			 */
945 			error = dsl_sync_task(spa->spa_name, NULL,
946 			    spa_sync_version, &ver,
947 			    6, ZFS_SPACE_CHECK_RESERVED);
948 			if (error)
949 				return (error);
950 			continue;
951 		}
952 
953 		need_sync = B_TRUE;
954 		break;
955 	}
956 
957 	if (need_sync) {
958 		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
959 		    nvp, 6, ZFS_SPACE_CHECK_RESERVED));
960 	}
961 
962 	return (0);
963 }
964 
965 /*
966  * If the bootfs property value is dsobj, clear it.
967  */
968 void
969 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
970 {
971 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
972 		VERIFY(zap_remove(spa->spa_meta_objset,
973 		    spa->spa_pool_props_object,
974 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
975 		spa->spa_bootfs = 0;
976 	}
977 }
978 
979 static int
980 spa_change_guid_check(void *arg, dmu_tx_t *tx)
981 {
982 	uint64_t *newguid __maybe_unused = arg;
983 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
984 	vdev_t *rvd = spa->spa_root_vdev;
985 	uint64_t vdev_state;
986 
987 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
988 		int error = (spa_has_checkpoint(spa)) ?
989 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
990 		return (SET_ERROR(error));
991 	}
992 
993 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
994 	vdev_state = rvd->vdev_state;
995 	spa_config_exit(spa, SCL_STATE, FTAG);
996 
997 	if (vdev_state != VDEV_STATE_HEALTHY)
998 		return (SET_ERROR(ENXIO));
999 
1000 	ASSERT3U(spa_guid(spa), !=, *newguid);
1001 
1002 	return (0);
1003 }
1004 
1005 static void
1006 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
1007 {
1008 	uint64_t *newguid = arg;
1009 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1010 	uint64_t oldguid;
1011 	vdev_t *rvd = spa->spa_root_vdev;
1012 
1013 	oldguid = spa_guid(spa);
1014 
1015 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1016 	rvd->vdev_guid = *newguid;
1017 	rvd->vdev_guid_sum += (*newguid - oldguid);
1018 	vdev_config_dirty(rvd);
1019 	spa_config_exit(spa, SCL_STATE, FTAG);
1020 
1021 	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
1022 	    (u_longlong_t)oldguid, (u_longlong_t)*newguid);
1023 }
1024 
1025 /*
1026  * Change the GUID for the pool.  This is done so that we can later
1027  * re-import a pool built from a clone of our own vdevs.  We will modify
1028  * the root vdev's guid, our own pool guid, and then mark all of our
1029  * vdevs dirty.  Note that we must make sure that all our vdevs are
1030  * online when we do this, or else any vdevs that weren't present
1031  * would be orphaned from our pool.  We are also going to issue a
1032  * sysevent to update any watchers.
1033  *
1034  * The GUID of the pool will be changed to the value pointed to by guidp.
1035  * The GUID may not be set to the reserverd value of 0.
1036  * The new GUID will be generated if guidp is NULL.
1037  */
1038 int
1039 spa_change_guid(spa_t *spa, const uint64_t *guidp)
1040 {
1041 	uint64_t guid;
1042 	int error;
1043 
1044 	mutex_enter(&spa->spa_vdev_top_lock);
1045 	mutex_enter(&spa_namespace_lock);
1046 
1047 	if (guidp != NULL) {
1048 		guid = *guidp;
1049 		if (guid == 0) {
1050 			error = SET_ERROR(EINVAL);
1051 			goto out;
1052 		}
1053 
1054 		if (spa_guid_exists(guid, 0)) {
1055 			error = SET_ERROR(EEXIST);
1056 			goto out;
1057 		}
1058 	} else {
1059 		guid = spa_generate_guid(NULL);
1060 	}
1061 
1062 	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
1063 	    spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
1064 
1065 	if (error == 0) {
1066 		/*
1067 		 * Clear the kobj flag from all the vdevs to allow
1068 		 * vdev_cache_process_kobj_evt() to post events to all the
1069 		 * vdevs since GUID is updated.
1070 		 */
1071 		vdev_clear_kobj_evt(spa->spa_root_vdev);
1072 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
1073 			vdev_clear_kobj_evt(spa->spa_l2cache.sav_vdevs[i]);
1074 
1075 		spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE);
1076 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID);
1077 	}
1078 
1079 out:
1080 	mutex_exit(&spa_namespace_lock);
1081 	mutex_exit(&spa->spa_vdev_top_lock);
1082 
1083 	return (error);
1084 }
1085 
1086 /*
1087  * ==========================================================================
1088  * SPA state manipulation (open/create/destroy/import/export)
1089  * ==========================================================================
1090  */
1091 
1092 static int
1093 spa_error_entry_compare(const void *a, const void *b)
1094 {
1095 	const spa_error_entry_t *sa = (const spa_error_entry_t *)a;
1096 	const spa_error_entry_t *sb = (const spa_error_entry_t *)b;
1097 	int ret;
1098 
1099 	ret = memcmp(&sa->se_bookmark, &sb->se_bookmark,
1100 	    sizeof (zbookmark_phys_t));
1101 
1102 	return (TREE_ISIGN(ret));
1103 }
1104 
1105 /*
1106  * Utility function which retrieves copies of the current logs and
1107  * re-initializes them in the process.
1108  */
1109 void
1110 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
1111 {
1112 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
1113 
1114 	memcpy(last, &spa->spa_errlist_last, sizeof (avl_tree_t));
1115 	memcpy(scrub, &spa->spa_errlist_scrub, sizeof (avl_tree_t));
1116 
1117 	avl_create(&spa->spa_errlist_scrub,
1118 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1119 	    offsetof(spa_error_entry_t, se_avl));
1120 	avl_create(&spa->spa_errlist_last,
1121 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1122 	    offsetof(spa_error_entry_t, se_avl));
1123 }
1124 
1125 static void
1126 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
1127 {
1128 	const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
1129 	enum zti_modes mode = ztip->zti_mode;
1130 	uint_t value = ztip->zti_value;
1131 	uint_t count = ztip->zti_count;
1132 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1133 	uint_t cpus, flags = TASKQ_DYNAMIC;
1134 
1135 	switch (mode) {
1136 	case ZTI_MODE_FIXED:
1137 		ASSERT3U(value, >, 0);
1138 		break;
1139 
1140 	case ZTI_MODE_SYNC:
1141 
1142 		/*
1143 		 * Create one wr_iss taskq for every 'zio_taskq_write_tpq' CPUs,
1144 		 * not to exceed the number of spa allocators, and align to it.
1145 		 */
1146 		cpus = MAX(1, boot_ncpus * zio_taskq_batch_pct / 100);
1147 		count = MAX(1, cpus / MAX(1, zio_taskq_write_tpq));
1148 		count = MAX(count, (zio_taskq_batch_pct + 99) / 100);
1149 		count = MIN(count, spa->spa_alloc_count);
1150 		while (spa->spa_alloc_count % count != 0 &&
1151 		    spa->spa_alloc_count < count * 2)
1152 			count--;
1153 
1154 		/*
1155 		 * zio_taskq_batch_pct is unbounded and may exceed 100%, but no
1156 		 * single taskq may have more threads than 100% of online cpus.
1157 		 */
1158 		value = (zio_taskq_batch_pct + count / 2) / count;
1159 		value = MIN(value, 100);
1160 		flags |= TASKQ_THREADS_CPU_PCT;
1161 		break;
1162 
1163 	case ZTI_MODE_SCALE:
1164 		flags |= TASKQ_THREADS_CPU_PCT;
1165 		/*
1166 		 * We want more taskqs to reduce lock contention, but we want
1167 		 * less for better request ordering and CPU utilization.
1168 		 */
1169 		cpus = MAX(1, boot_ncpus * zio_taskq_batch_pct / 100);
1170 		if (zio_taskq_batch_tpq > 0) {
1171 			count = MAX(1, (cpus + zio_taskq_batch_tpq / 2) /
1172 			    zio_taskq_batch_tpq);
1173 		} else {
1174 			/*
1175 			 * Prefer 6 threads per taskq, but no more taskqs
1176 			 * than threads in them on large systems. For 80%:
1177 			 *
1178 			 *                 taskq   taskq   total
1179 			 * cpus    taskqs  percent threads threads
1180 			 * ------- ------- ------- ------- -------
1181 			 * 1       1       80%     1       1
1182 			 * 2       1       80%     1       1
1183 			 * 4       1       80%     3       3
1184 			 * 8       2       40%     3       6
1185 			 * 16      3       27%     4       12
1186 			 * 32      5       16%     5       25
1187 			 * 64      7       11%     7       49
1188 			 * 128     10      8%      10      100
1189 			 * 256     14      6%      15      210
1190 			 */
1191 			count = 1 + cpus / 6;
1192 			while (count * count > cpus)
1193 				count--;
1194 		}
1195 		/* Limit each taskq within 100% to not trigger assertion. */
1196 		count = MAX(count, (zio_taskq_batch_pct + 99) / 100);
1197 		value = (zio_taskq_batch_pct + count / 2) / count;
1198 		break;
1199 
1200 	case ZTI_MODE_NULL:
1201 		tqs->stqs_count = 0;
1202 		tqs->stqs_taskq = NULL;
1203 		return;
1204 
1205 	default:
1206 		panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1207 		    "spa_taskqs_init()",
1208 		    zio_type_name[t], zio_taskq_types[q], mode, value);
1209 		break;
1210 	}
1211 
1212 	ASSERT3U(count, >, 0);
1213 	tqs->stqs_count = count;
1214 	tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
1215 
1216 	for (uint_t i = 0; i < count; i++) {
1217 		taskq_t *tq;
1218 		char name[32];
1219 
1220 		if (count > 1)
1221 			(void) snprintf(name, sizeof (name), "%s_%s_%u",
1222 			    zio_type_name[t], zio_taskq_types[q], i);
1223 		else
1224 			(void) snprintf(name, sizeof (name), "%s_%s",
1225 			    zio_type_name[t], zio_taskq_types[q]);
1226 
1227 #ifdef HAVE_SYSDC
1228 		if (zio_taskq_sysdc && spa->spa_proc != &p0) {
1229 			(void) zio_taskq_basedc;
1230 			tq = taskq_create_sysdc(name, value, 50, INT_MAX,
1231 			    spa->spa_proc, zio_taskq_basedc, flags);
1232 		} else {
1233 #endif
1234 			pri_t pri = maxclsyspri;
1235 			/*
1236 			 * The write issue taskq can be extremely CPU
1237 			 * intensive.  Run it at slightly less important
1238 			 * priority than the other taskqs.
1239 			 *
1240 			 * Under Linux and FreeBSD this means incrementing
1241 			 * the priority value as opposed to platforms like
1242 			 * illumos where it should be decremented.
1243 			 *
1244 			 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1245 			 * are equal then a difference between them is
1246 			 * insignificant.
1247 			 */
1248 			if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) {
1249 #if defined(__linux__)
1250 				pri++;
1251 #elif defined(__FreeBSD__)
1252 				pri += 4;
1253 #else
1254 #error "unknown OS"
1255 #endif
1256 			}
1257 			tq = taskq_create_proc(name, value, pri, 50,
1258 			    INT_MAX, spa->spa_proc, flags);
1259 #ifdef HAVE_SYSDC
1260 		}
1261 #endif
1262 
1263 		tqs->stqs_taskq[i] = tq;
1264 	}
1265 }
1266 
1267 static void
1268 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
1269 {
1270 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1271 
1272 	if (tqs->stqs_taskq == NULL) {
1273 		ASSERT3U(tqs->stqs_count, ==, 0);
1274 		return;
1275 	}
1276 
1277 	for (uint_t i = 0; i < tqs->stqs_count; i++) {
1278 		ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
1279 		taskq_destroy(tqs->stqs_taskq[i]);
1280 	}
1281 
1282 	kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
1283 	tqs->stqs_taskq = NULL;
1284 }
1285 
1286 #ifdef _KERNEL
1287 /*
1288  * The READ and WRITE rows of zio_taskqs are configurable at module load time
1289  * by setting zio_taskq_read or zio_taskq_write.
1290  *
1291  * Example (the defaults for READ and WRITE)
1292  *   zio_taskq_read='fixed,1,8 null scale null'
1293  *   zio_taskq_write='sync null scale null'
1294  *
1295  * Each sets the entire row at a time.
1296  *
1297  * 'fixed' is parameterised: fixed,Q,T where Q is number of taskqs, T is number
1298  * of threads per taskq.
1299  *
1300  * 'null' can only be set on the high-priority queues (queue selection for
1301  * high-priority queues will fall back to the regular queue if the high-pri
1302  * is NULL.
1303  */
1304 static const char *const modes[ZTI_NMODES] = {
1305 	"fixed", "scale", "sync", "null"
1306 };
1307 
1308 /* Parse the incoming config string. Modifies cfg */
1309 static int
1310 spa_taskq_param_set(zio_type_t t, char *cfg)
1311 {
1312 	int err = 0;
1313 
1314 	zio_taskq_info_t row[ZIO_TASKQ_TYPES] = {{0}};
1315 
1316 	char *next = cfg, *tok, *c;
1317 
1318 	/*
1319 	 * Parse out each element from the string and fill `row`. The entire
1320 	 * row has to be set at once, so any errors are flagged by just
1321 	 * breaking out of this loop early.
1322 	 */
1323 	uint_t q;
1324 	for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
1325 		/* `next` is the start of the config */
1326 		if (next == NULL)
1327 			break;
1328 
1329 		/* Eat up leading space */
1330 		while (isspace(*next))
1331 			next++;
1332 		if (*next == '\0')
1333 			break;
1334 
1335 		/* Mode ends at space or end of string */
1336 		tok = next;
1337 		next = strchr(tok, ' ');
1338 		if (next != NULL) *next++ = '\0';
1339 
1340 		/* Parameters start after a comma */
1341 		c = strchr(tok, ',');
1342 		if (c != NULL) *c++ = '\0';
1343 
1344 		/* Match mode string */
1345 		uint_t mode;
1346 		for (mode = 0; mode < ZTI_NMODES; mode++)
1347 			if (strcmp(tok, modes[mode]) == 0)
1348 				break;
1349 		if (mode == ZTI_NMODES)
1350 			break;
1351 
1352 		/* Invalid canary */
1353 		row[q].zti_mode = ZTI_NMODES;
1354 
1355 		/* Per-mode setup */
1356 		switch (mode) {
1357 
1358 		/*
1359 		 * FIXED is parameterised: number of queues, and number of
1360 		 * threads per queue.
1361 		 */
1362 		case ZTI_MODE_FIXED: {
1363 			/* No parameters? */
1364 			if (c == NULL || *c == '\0')
1365 				break;
1366 
1367 			/* Find next parameter */
1368 			tok = c;
1369 			c = strchr(tok, ',');
1370 			if (c == NULL)
1371 				break;
1372 
1373 			/* Take digits and convert */
1374 			unsigned long long nq;
1375 			if (!(isdigit(*tok)))
1376 				break;
1377 			err = ddi_strtoull(tok, &tok, 10, &nq);
1378 			/* Must succeed and also end at the next param sep */
1379 			if (err != 0 || tok != c)
1380 				break;
1381 
1382 			/* Move past the comma */
1383 			tok++;
1384 			/* Need another number */
1385 			if (!(isdigit(*tok)))
1386 				break;
1387 			/* Remember start to make sure we moved */
1388 			c = tok;
1389 
1390 			/* Take digits */
1391 			unsigned long long ntpq;
1392 			err = ddi_strtoull(tok, &tok, 10, &ntpq);
1393 			/* Must succeed, and moved forward */
1394 			if (err != 0 || tok == c || *tok != '\0')
1395 				break;
1396 
1397 			/*
1398 			 * sanity; zero queues/threads make no sense, and
1399 			 * 16K is almost certainly more than anyone will ever
1400 			 * need and avoids silly numbers like UINT32_MAX
1401 			 */
1402 			if (nq == 0 || nq >= 16384 ||
1403 			    ntpq == 0 || ntpq >= 16384)
1404 				break;
1405 
1406 			const zio_taskq_info_t zti = ZTI_P(ntpq, nq);
1407 			row[q] = zti;
1408 			break;
1409 		}
1410 
1411 		case ZTI_MODE_SCALE: {
1412 			const zio_taskq_info_t zti = ZTI_SCALE;
1413 			row[q] = zti;
1414 			break;
1415 		}
1416 
1417 		case ZTI_MODE_SYNC: {
1418 			const zio_taskq_info_t zti = ZTI_SYNC;
1419 			row[q] = zti;
1420 			break;
1421 		}
1422 
1423 		case ZTI_MODE_NULL: {
1424 			/*
1425 			 * Can only null the high-priority queues; the general-
1426 			 * purpose ones have to exist.
1427 			 */
1428 			if (q != ZIO_TASKQ_ISSUE_HIGH &&
1429 			    q != ZIO_TASKQ_INTERRUPT_HIGH)
1430 				break;
1431 
1432 			const zio_taskq_info_t zti = ZTI_NULL;
1433 			row[q] = zti;
1434 			break;
1435 		}
1436 
1437 		default:
1438 			break;
1439 		}
1440 
1441 		/* Ensure we set a mode */
1442 		if (row[q].zti_mode == ZTI_NMODES)
1443 			break;
1444 	}
1445 
1446 	/* Didn't get a full row, fail */
1447 	if (q < ZIO_TASKQ_TYPES)
1448 		return (SET_ERROR(EINVAL));
1449 
1450 	/* Eat trailing space */
1451 	if (next != NULL)
1452 		while (isspace(*next))
1453 			next++;
1454 
1455 	/* If there's anything left over then fail */
1456 	if (next != NULL && *next != '\0')
1457 		return (SET_ERROR(EINVAL));
1458 
1459 	/* Success! Copy it into the real config */
1460 	for (q = 0; q < ZIO_TASKQ_TYPES; q++)
1461 		zio_taskqs[t][q] = row[q];
1462 
1463 	return (0);
1464 }
1465 
1466 static int
1467 spa_taskq_param_get(zio_type_t t, char *buf, boolean_t add_newline)
1468 {
1469 	int pos = 0;
1470 
1471 	/* Build paramater string from live config */
1472 	const char *sep = "";
1473 	for (uint_t q = 0; q < ZIO_TASKQ_TYPES; q++) {
1474 		const zio_taskq_info_t *zti = &zio_taskqs[t][q];
1475 		if (zti->zti_mode == ZTI_MODE_FIXED)
1476 			pos += sprintf(&buf[pos], "%s%s,%u,%u", sep,
1477 			    modes[zti->zti_mode], zti->zti_count,
1478 			    zti->zti_value);
1479 		else
1480 			pos += sprintf(&buf[pos], "%s%s", sep,
1481 			    modes[zti->zti_mode]);
1482 		sep = " ";
1483 	}
1484 
1485 	if (add_newline)
1486 		buf[pos++] = '\n';
1487 	buf[pos] = '\0';
1488 
1489 	return (pos);
1490 }
1491 
1492 #ifdef __linux__
1493 static int
1494 spa_taskq_read_param_set(const char *val, zfs_kernel_param_t *kp)
1495 {
1496 	char *cfg = kmem_strdup(val);
1497 	int err = spa_taskq_param_set(ZIO_TYPE_READ, cfg);
1498 	kmem_free(cfg, strlen(val)+1);
1499 	return (-err);
1500 }
1501 static int
1502 spa_taskq_read_param_get(char *buf, zfs_kernel_param_t *kp)
1503 {
1504 	return (spa_taskq_param_get(ZIO_TYPE_READ, buf, TRUE));
1505 }
1506 
1507 static int
1508 spa_taskq_write_param_set(const char *val, zfs_kernel_param_t *kp)
1509 {
1510 	char *cfg = kmem_strdup(val);
1511 	int err = spa_taskq_param_set(ZIO_TYPE_WRITE, cfg);
1512 	kmem_free(cfg, strlen(val)+1);
1513 	return (-err);
1514 }
1515 static int
1516 spa_taskq_write_param_get(char *buf, zfs_kernel_param_t *kp)
1517 {
1518 	return (spa_taskq_param_get(ZIO_TYPE_WRITE, buf, TRUE));
1519 }
1520 #else
1521 /*
1522  * On FreeBSD load-time parameters can be set up before malloc() is available,
1523  * so we have to do all the parsing work on the stack.
1524  */
1525 #define	SPA_TASKQ_PARAM_MAX	(128)
1526 
1527 static int
1528 spa_taskq_read_param(ZFS_MODULE_PARAM_ARGS)
1529 {
1530 	char buf[SPA_TASKQ_PARAM_MAX];
1531 	int err;
1532 
1533 	(void) spa_taskq_param_get(ZIO_TYPE_READ, buf, FALSE);
1534 	err = sysctl_handle_string(oidp, buf, sizeof (buf), req);
1535 	if (err || req->newptr == NULL)
1536 		return (err);
1537 	return (spa_taskq_param_set(ZIO_TYPE_READ, buf));
1538 }
1539 
1540 static int
1541 spa_taskq_write_param(ZFS_MODULE_PARAM_ARGS)
1542 {
1543 	char buf[SPA_TASKQ_PARAM_MAX];
1544 	int err;
1545 
1546 	(void) spa_taskq_param_get(ZIO_TYPE_WRITE, buf, FALSE);
1547 	err = sysctl_handle_string(oidp, buf, sizeof (buf), req);
1548 	if (err || req->newptr == NULL)
1549 		return (err);
1550 	return (spa_taskq_param_set(ZIO_TYPE_WRITE, buf));
1551 }
1552 #endif
1553 #endif /* _KERNEL */
1554 
1555 /*
1556  * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1557  * Note that a type may have multiple discrete taskqs to avoid lock contention
1558  * on the taskq itself.
1559  */
1560 void
1561 spa_taskq_dispatch(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
1562     task_func_t *func, zio_t *zio, boolean_t cutinline)
1563 {
1564 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1565 	taskq_t *tq;
1566 
1567 	ASSERT3P(tqs->stqs_taskq, !=, NULL);
1568 	ASSERT3U(tqs->stqs_count, !=, 0);
1569 
1570 	/*
1571 	 * NB: We are assuming that the zio can only be dispatched
1572 	 * to a single taskq at a time.  It would be a grievous error
1573 	 * to dispatch the zio to another taskq at the same time.
1574 	 */
1575 	ASSERT(zio);
1576 	ASSERT(taskq_empty_ent(&zio->io_tqent));
1577 
1578 	if (tqs->stqs_count == 1) {
1579 		tq = tqs->stqs_taskq[0];
1580 	} else if ((t == ZIO_TYPE_WRITE) && (q == ZIO_TASKQ_ISSUE) &&
1581 	    ZIO_HAS_ALLOCATOR(zio)) {
1582 		tq = tqs->stqs_taskq[zio->io_allocator % tqs->stqs_count];
1583 	} else {
1584 		tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count];
1585 	}
1586 
1587 	taskq_dispatch_ent(tq, func, zio, cutinline ? TQ_FRONT : 0,
1588 	    &zio->io_tqent);
1589 }
1590 
1591 static void
1592 spa_create_zio_taskqs(spa_t *spa)
1593 {
1594 	for (int t = 0; t < ZIO_TYPES; t++) {
1595 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1596 			spa_taskqs_init(spa, t, q);
1597 		}
1598 	}
1599 }
1600 
1601 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1602 static void
1603 spa_thread(void *arg)
1604 {
1605 	psetid_t zio_taskq_psrset_bind = PS_NONE;
1606 	callb_cpr_t cprinfo;
1607 
1608 	spa_t *spa = arg;
1609 	user_t *pu = PTOU(curproc);
1610 
1611 	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1612 	    spa->spa_name);
1613 
1614 	ASSERT(curproc != &p0);
1615 	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1616 	    "zpool-%s", spa->spa_name);
1617 	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1618 
1619 	/* bind this thread to the requested psrset */
1620 	if (zio_taskq_psrset_bind != PS_NONE) {
1621 		pool_lock();
1622 		mutex_enter(&cpu_lock);
1623 		mutex_enter(&pidlock);
1624 		mutex_enter(&curproc->p_lock);
1625 
1626 		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1627 		    0, NULL, NULL) == 0)  {
1628 			curthread->t_bind_pset = zio_taskq_psrset_bind;
1629 		} else {
1630 			cmn_err(CE_WARN,
1631 			    "Couldn't bind process for zfs pool \"%s\" to "
1632 			    "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1633 		}
1634 
1635 		mutex_exit(&curproc->p_lock);
1636 		mutex_exit(&pidlock);
1637 		mutex_exit(&cpu_lock);
1638 		pool_unlock();
1639 	}
1640 
1641 #ifdef HAVE_SYSDC
1642 	if (zio_taskq_sysdc) {
1643 		sysdc_thread_enter(curthread, 100, 0);
1644 	}
1645 #endif
1646 
1647 	spa->spa_proc = curproc;
1648 	spa->spa_did = curthread->t_did;
1649 
1650 	spa_create_zio_taskqs(spa);
1651 
1652 	mutex_enter(&spa->spa_proc_lock);
1653 	ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1654 
1655 	spa->spa_proc_state = SPA_PROC_ACTIVE;
1656 	cv_broadcast(&spa->spa_proc_cv);
1657 
1658 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
1659 	while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1660 		cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1661 	CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1662 
1663 	ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1664 	spa->spa_proc_state = SPA_PROC_GONE;
1665 	spa->spa_proc = &p0;
1666 	cv_broadcast(&spa->spa_proc_cv);
1667 	CALLB_CPR_EXIT(&cprinfo);	/* drops spa_proc_lock */
1668 
1669 	mutex_enter(&curproc->p_lock);
1670 	lwp_exit();
1671 }
1672 #endif
1673 
1674 extern metaslab_ops_t *metaslab_allocator(spa_t *spa);
1675 
1676 /*
1677  * Activate an uninitialized pool.
1678  */
1679 static void
1680 spa_activate(spa_t *spa, spa_mode_t mode)
1681 {
1682 	metaslab_ops_t *msp = metaslab_allocator(spa);
1683 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1684 
1685 	spa->spa_state = POOL_STATE_ACTIVE;
1686 	spa->spa_final_txg = UINT64_MAX;
1687 	spa->spa_mode = mode;
1688 	spa->spa_read_spacemaps = spa_mode_readable_spacemaps;
1689 
1690 	spa->spa_normal_class = metaslab_class_create(spa, msp, B_FALSE);
1691 	spa->spa_log_class = metaslab_class_create(spa, msp, B_TRUE);
1692 	spa->spa_embedded_log_class = metaslab_class_create(spa, msp, B_TRUE);
1693 	spa->spa_special_class = metaslab_class_create(spa, msp, B_FALSE);
1694 	spa->spa_dedup_class = metaslab_class_create(spa, msp, B_FALSE);
1695 
1696 	/* Try to create a covering process */
1697 	mutex_enter(&spa->spa_proc_lock);
1698 	ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1699 	ASSERT(spa->spa_proc == &p0);
1700 	spa->spa_did = 0;
1701 
1702 #ifdef HAVE_SPA_THREAD
1703 	/* Only create a process if we're going to be around a while. */
1704 	if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1705 		if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1706 		    NULL, 0) == 0) {
1707 			spa->spa_proc_state = SPA_PROC_CREATED;
1708 			while (spa->spa_proc_state == SPA_PROC_CREATED) {
1709 				cv_wait(&spa->spa_proc_cv,
1710 				    &spa->spa_proc_lock);
1711 			}
1712 			ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1713 			ASSERT(spa->spa_proc != &p0);
1714 			ASSERT(spa->spa_did != 0);
1715 		} else {
1716 #ifdef _KERNEL
1717 			cmn_err(CE_WARN,
1718 			    "Couldn't create process for zfs pool \"%s\"\n",
1719 			    spa->spa_name);
1720 #endif
1721 		}
1722 	}
1723 #endif /* HAVE_SPA_THREAD */
1724 	mutex_exit(&spa->spa_proc_lock);
1725 
1726 	/* If we didn't create a process, we need to create our taskqs. */
1727 	if (spa->spa_proc == &p0) {
1728 		spa_create_zio_taskqs(spa);
1729 	}
1730 
1731 	for (size_t i = 0; i < TXG_SIZE; i++) {
1732 		spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL,
1733 		    ZIO_FLAG_CANFAIL);
1734 	}
1735 
1736 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1737 	    offsetof(vdev_t, vdev_config_dirty_node));
1738 	list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1739 	    offsetof(objset_t, os_evicting_node));
1740 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1741 	    offsetof(vdev_t, vdev_state_dirty_node));
1742 
1743 	txg_list_create(&spa->spa_vdev_txg_list, spa,
1744 	    offsetof(struct vdev, vdev_txg_node));
1745 
1746 	avl_create(&spa->spa_errlist_scrub,
1747 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1748 	    offsetof(spa_error_entry_t, se_avl));
1749 	avl_create(&spa->spa_errlist_last,
1750 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1751 	    offsetof(spa_error_entry_t, se_avl));
1752 	avl_create(&spa->spa_errlist_healed,
1753 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1754 	    offsetof(spa_error_entry_t, se_avl));
1755 
1756 	spa_activate_os(spa);
1757 
1758 	spa_keystore_init(&spa->spa_keystore);
1759 
1760 	/*
1761 	 * This taskq is used to perform zvol-minor-related tasks
1762 	 * asynchronously. This has several advantages, including easy
1763 	 * resolution of various deadlocks.
1764 	 *
1765 	 * The taskq must be single threaded to ensure tasks are always
1766 	 * processed in the order in which they were dispatched.
1767 	 *
1768 	 * A taskq per pool allows one to keep the pools independent.
1769 	 * This way if one pool is suspended, it will not impact another.
1770 	 *
1771 	 * The preferred location to dispatch a zvol minor task is a sync
1772 	 * task. In this context, there is easy access to the spa_t and minimal
1773 	 * error handling is required because the sync task must succeed.
1774 	 */
1775 	spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri,
1776 	    1, INT_MAX, 0);
1777 
1778 	/*
1779 	 * The taskq to preload metaslabs.
1780 	 */
1781 	spa->spa_metaslab_taskq = taskq_create("z_metaslab",
1782 	    metaslab_preload_pct, maxclsyspri, 1, INT_MAX,
1783 	    TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
1784 
1785 	/*
1786 	 * Taskq dedicated to prefetcher threads: this is used to prevent the
1787 	 * pool traverse code from monopolizing the global (and limited)
1788 	 * system_taskq by inappropriately scheduling long running tasks on it.
1789 	 */
1790 	spa->spa_prefetch_taskq = taskq_create("z_prefetch", 100,
1791 	    defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
1792 
1793 	/*
1794 	 * The taskq to upgrade datasets in this pool. Currently used by
1795 	 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1796 	 */
1797 	spa->spa_upgrade_taskq = taskq_create("z_upgrade", 100,
1798 	    defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
1799 }
1800 
1801 /*
1802  * Opposite of spa_activate().
1803  */
1804 static void
1805 spa_deactivate(spa_t *spa)
1806 {
1807 	ASSERT(spa->spa_sync_on == B_FALSE);
1808 	ASSERT(spa->spa_dsl_pool == NULL);
1809 	ASSERT(spa->spa_root_vdev == NULL);
1810 	ASSERT(spa->spa_async_zio_root == NULL);
1811 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1812 
1813 	spa_evicting_os_wait(spa);
1814 
1815 	if (spa->spa_zvol_taskq) {
1816 		taskq_destroy(spa->spa_zvol_taskq);
1817 		spa->spa_zvol_taskq = NULL;
1818 	}
1819 
1820 	if (spa->spa_metaslab_taskq) {
1821 		taskq_destroy(spa->spa_metaslab_taskq);
1822 		spa->spa_metaslab_taskq = NULL;
1823 	}
1824 
1825 	if (spa->spa_prefetch_taskq) {
1826 		taskq_destroy(spa->spa_prefetch_taskq);
1827 		spa->spa_prefetch_taskq = NULL;
1828 	}
1829 
1830 	if (spa->spa_upgrade_taskq) {
1831 		taskq_destroy(spa->spa_upgrade_taskq);
1832 		spa->spa_upgrade_taskq = NULL;
1833 	}
1834 
1835 	txg_list_destroy(&spa->spa_vdev_txg_list);
1836 
1837 	list_destroy(&spa->spa_config_dirty_list);
1838 	list_destroy(&spa->spa_evicting_os_list);
1839 	list_destroy(&spa->spa_state_dirty_list);
1840 
1841 	taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
1842 
1843 	for (int t = 0; t < ZIO_TYPES; t++) {
1844 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1845 			spa_taskqs_fini(spa, t, q);
1846 		}
1847 	}
1848 
1849 	for (size_t i = 0; i < TXG_SIZE; i++) {
1850 		ASSERT3P(spa->spa_txg_zio[i], !=, NULL);
1851 		VERIFY0(zio_wait(spa->spa_txg_zio[i]));
1852 		spa->spa_txg_zio[i] = NULL;
1853 	}
1854 
1855 	metaslab_class_destroy(spa->spa_normal_class);
1856 	spa->spa_normal_class = NULL;
1857 
1858 	metaslab_class_destroy(spa->spa_log_class);
1859 	spa->spa_log_class = NULL;
1860 
1861 	metaslab_class_destroy(spa->spa_embedded_log_class);
1862 	spa->spa_embedded_log_class = NULL;
1863 
1864 	metaslab_class_destroy(spa->spa_special_class);
1865 	spa->spa_special_class = NULL;
1866 
1867 	metaslab_class_destroy(spa->spa_dedup_class);
1868 	spa->spa_dedup_class = NULL;
1869 
1870 	/*
1871 	 * If this was part of an import or the open otherwise failed, we may
1872 	 * still have errors left in the queues.  Empty them just in case.
1873 	 */
1874 	spa_errlog_drain(spa);
1875 	avl_destroy(&spa->spa_errlist_scrub);
1876 	avl_destroy(&spa->spa_errlist_last);
1877 	avl_destroy(&spa->spa_errlist_healed);
1878 
1879 	spa_keystore_fini(&spa->spa_keystore);
1880 
1881 	spa->spa_state = POOL_STATE_UNINITIALIZED;
1882 
1883 	mutex_enter(&spa->spa_proc_lock);
1884 	if (spa->spa_proc_state != SPA_PROC_NONE) {
1885 		ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1886 		spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1887 		cv_broadcast(&spa->spa_proc_cv);
1888 		while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1889 			ASSERT(spa->spa_proc != &p0);
1890 			cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1891 		}
1892 		ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1893 		spa->spa_proc_state = SPA_PROC_NONE;
1894 	}
1895 	ASSERT(spa->spa_proc == &p0);
1896 	mutex_exit(&spa->spa_proc_lock);
1897 
1898 	/*
1899 	 * We want to make sure spa_thread() has actually exited the ZFS
1900 	 * module, so that the module can't be unloaded out from underneath
1901 	 * it.
1902 	 */
1903 	if (spa->spa_did != 0) {
1904 		thread_join(spa->spa_did);
1905 		spa->spa_did = 0;
1906 	}
1907 
1908 	spa_deactivate_os(spa);
1909 
1910 }
1911 
1912 /*
1913  * Verify a pool configuration, and construct the vdev tree appropriately.  This
1914  * will create all the necessary vdevs in the appropriate layout, with each vdev
1915  * in the CLOSED state.  This will prep the pool before open/creation/import.
1916  * All vdev validation is done by the vdev_alloc() routine.
1917  */
1918 int
1919 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1920     uint_t id, int atype)
1921 {
1922 	nvlist_t **child;
1923 	uint_t children;
1924 	int error;
1925 
1926 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1927 		return (error);
1928 
1929 	if ((*vdp)->vdev_ops->vdev_op_leaf)
1930 		return (0);
1931 
1932 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1933 	    &child, &children);
1934 
1935 	if (error == ENOENT)
1936 		return (0);
1937 
1938 	if (error) {
1939 		vdev_free(*vdp);
1940 		*vdp = NULL;
1941 		return (SET_ERROR(EINVAL));
1942 	}
1943 
1944 	for (int c = 0; c < children; c++) {
1945 		vdev_t *vd;
1946 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1947 		    atype)) != 0) {
1948 			vdev_free(*vdp);
1949 			*vdp = NULL;
1950 			return (error);
1951 		}
1952 	}
1953 
1954 	ASSERT(*vdp != NULL);
1955 
1956 	return (0);
1957 }
1958 
1959 static boolean_t
1960 spa_should_flush_logs_on_unload(spa_t *spa)
1961 {
1962 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
1963 		return (B_FALSE);
1964 
1965 	if (!spa_writeable(spa))
1966 		return (B_FALSE);
1967 
1968 	if (!spa->spa_sync_on)
1969 		return (B_FALSE);
1970 
1971 	if (spa_state(spa) != POOL_STATE_EXPORTED)
1972 		return (B_FALSE);
1973 
1974 	if (zfs_keep_log_spacemaps_at_export)
1975 		return (B_FALSE);
1976 
1977 	return (B_TRUE);
1978 }
1979 
1980 /*
1981  * Opens a transaction that will set the flag that will instruct
1982  * spa_sync to attempt to flush all the metaslabs for that txg.
1983  */
1984 static void
1985 spa_unload_log_sm_flush_all(spa_t *spa)
1986 {
1987 	dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
1988 	VERIFY0(dmu_tx_assign(tx, DMU_TX_WAIT | DMU_TX_SUSPEND));
1989 
1990 	ASSERT3U(spa->spa_log_flushall_txg, ==, 0);
1991 	spa->spa_log_flushall_txg = dmu_tx_get_txg(tx);
1992 
1993 	dmu_tx_commit(tx);
1994 	txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg);
1995 }
1996 
1997 static void
1998 spa_unload_log_sm_metadata(spa_t *spa)
1999 {
2000 	void *cookie = NULL;
2001 	spa_log_sm_t *sls;
2002 	log_summary_entry_t *e;
2003 
2004 	while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg,
2005 	    &cookie)) != NULL) {
2006 		VERIFY0(sls->sls_mscount);
2007 		kmem_free(sls, sizeof (spa_log_sm_t));
2008 	}
2009 
2010 	while ((e = list_remove_head(&spa->spa_log_summary)) != NULL) {
2011 		VERIFY0(e->lse_mscount);
2012 		kmem_free(e, sizeof (log_summary_entry_t));
2013 	}
2014 
2015 	spa->spa_unflushed_stats.sus_nblocks = 0;
2016 	spa->spa_unflushed_stats.sus_memused = 0;
2017 	spa->spa_unflushed_stats.sus_blocklimit = 0;
2018 }
2019 
2020 static void
2021 spa_destroy_aux_threads(spa_t *spa)
2022 {
2023 	if (spa->spa_condense_zthr != NULL) {
2024 		zthr_destroy(spa->spa_condense_zthr);
2025 		spa->spa_condense_zthr = NULL;
2026 	}
2027 	if (spa->spa_checkpoint_discard_zthr != NULL) {
2028 		zthr_destroy(spa->spa_checkpoint_discard_zthr);
2029 		spa->spa_checkpoint_discard_zthr = NULL;
2030 	}
2031 	if (spa->spa_livelist_delete_zthr != NULL) {
2032 		zthr_destroy(spa->spa_livelist_delete_zthr);
2033 		spa->spa_livelist_delete_zthr = NULL;
2034 	}
2035 	if (spa->spa_livelist_condense_zthr != NULL) {
2036 		zthr_destroy(spa->spa_livelist_condense_zthr);
2037 		spa->spa_livelist_condense_zthr = NULL;
2038 	}
2039 	if (spa->spa_raidz_expand_zthr != NULL) {
2040 		zthr_destroy(spa->spa_raidz_expand_zthr);
2041 		spa->spa_raidz_expand_zthr = NULL;
2042 	}
2043 }
2044 
2045 /*
2046  * Opposite of spa_load().
2047  */
2048 static void
2049 spa_unload(spa_t *spa)
2050 {
2051 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
2052 	    spa->spa_export_thread == curthread);
2053 	ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED);
2054 
2055 	spa_import_progress_remove(spa_guid(spa));
2056 	spa_load_note(spa, "UNLOADING");
2057 
2058 	spa_wake_waiters(spa);
2059 
2060 	/*
2061 	 * If we have set the spa_final_txg, we have already performed the
2062 	 * tasks below in spa_export_common(). We should not redo it here since
2063 	 * we delay the final TXGs beyond what spa_final_txg is set at.
2064 	 */
2065 	if (spa->spa_final_txg == UINT64_MAX) {
2066 		/*
2067 		 * If the log space map feature is enabled and the pool is
2068 		 * getting exported (but not destroyed), we want to spend some
2069 		 * time flushing as many metaslabs as we can in an attempt to
2070 		 * destroy log space maps and save import time.
2071 		 */
2072 		if (spa_should_flush_logs_on_unload(spa))
2073 			spa_unload_log_sm_flush_all(spa);
2074 
2075 		/*
2076 		 * Stop async tasks.
2077 		 */
2078 		spa_async_suspend(spa);
2079 
2080 		if (spa->spa_root_vdev) {
2081 			vdev_t *root_vdev = spa->spa_root_vdev;
2082 			vdev_initialize_stop_all(root_vdev,
2083 			    VDEV_INITIALIZE_ACTIVE);
2084 			vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE);
2085 			vdev_autotrim_stop_all(spa);
2086 			vdev_rebuild_stop_all(spa);
2087 			l2arc_spa_rebuild_stop(spa);
2088 		}
2089 
2090 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2091 		spa->spa_final_txg = spa_last_synced_txg(spa) +
2092 		    TXG_DEFER_SIZE + 1;
2093 		spa_config_exit(spa, SCL_ALL, FTAG);
2094 	}
2095 
2096 	/*
2097 	 * Stop syncing.
2098 	 */
2099 	if (spa->spa_sync_on) {
2100 		txg_sync_stop(spa->spa_dsl_pool);
2101 		spa->spa_sync_on = B_FALSE;
2102 	}
2103 
2104 	/*
2105 	 * This ensures that there is no async metaslab prefetching
2106 	 * while we attempt to unload the spa.
2107 	 */
2108 	taskq_wait(spa->spa_metaslab_taskq);
2109 
2110 	if (spa->spa_mmp.mmp_thread)
2111 		mmp_thread_stop(spa);
2112 
2113 	/*
2114 	 * Wait for any outstanding async I/O to complete.
2115 	 */
2116 	if (spa->spa_async_zio_root != NULL) {
2117 		for (int i = 0; i < max_ncpus; i++)
2118 			(void) zio_wait(spa->spa_async_zio_root[i]);
2119 		kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
2120 		spa->spa_async_zio_root = NULL;
2121 	}
2122 
2123 	if (spa->spa_vdev_removal != NULL) {
2124 		spa_vdev_removal_destroy(spa->spa_vdev_removal);
2125 		spa->spa_vdev_removal = NULL;
2126 	}
2127 
2128 	spa_destroy_aux_threads(spa);
2129 
2130 	spa_condense_fini(spa);
2131 
2132 	bpobj_close(&spa->spa_deferred_bpobj);
2133 
2134 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
2135 
2136 	/*
2137 	 * Close all vdevs.
2138 	 */
2139 	if (spa->spa_root_vdev)
2140 		vdev_free(spa->spa_root_vdev);
2141 	ASSERT(spa->spa_root_vdev == NULL);
2142 
2143 	/*
2144 	 * Close the dsl pool.
2145 	 */
2146 	if (spa->spa_dsl_pool) {
2147 		dsl_pool_close(spa->spa_dsl_pool);
2148 		spa->spa_dsl_pool = NULL;
2149 		spa->spa_meta_objset = NULL;
2150 	}
2151 
2152 	ddt_unload(spa);
2153 	brt_unload(spa);
2154 	spa_unload_log_sm_metadata(spa);
2155 
2156 	/*
2157 	 * Drop and purge level 2 cache
2158 	 */
2159 	spa_l2cache_drop(spa);
2160 
2161 	if (spa->spa_spares.sav_vdevs) {
2162 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
2163 			vdev_free(spa->spa_spares.sav_vdevs[i]);
2164 		kmem_free(spa->spa_spares.sav_vdevs,
2165 		    spa->spa_spares.sav_count * sizeof (void *));
2166 		spa->spa_spares.sav_vdevs = NULL;
2167 	}
2168 	if (spa->spa_spares.sav_config) {
2169 		nvlist_free(spa->spa_spares.sav_config);
2170 		spa->spa_spares.sav_config = NULL;
2171 	}
2172 	spa->spa_spares.sav_count = 0;
2173 
2174 	if (spa->spa_l2cache.sav_vdevs) {
2175 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
2176 			vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
2177 			vdev_free(spa->spa_l2cache.sav_vdevs[i]);
2178 		}
2179 		kmem_free(spa->spa_l2cache.sav_vdevs,
2180 		    spa->spa_l2cache.sav_count * sizeof (void *));
2181 		spa->spa_l2cache.sav_vdevs = NULL;
2182 	}
2183 	if (spa->spa_l2cache.sav_config) {
2184 		nvlist_free(spa->spa_l2cache.sav_config);
2185 		spa->spa_l2cache.sav_config = NULL;
2186 	}
2187 	spa->spa_l2cache.sav_count = 0;
2188 
2189 	spa->spa_async_suspended = 0;
2190 
2191 	spa->spa_indirect_vdevs_loaded = B_FALSE;
2192 
2193 	if (spa->spa_comment != NULL) {
2194 		spa_strfree(spa->spa_comment);
2195 		spa->spa_comment = NULL;
2196 	}
2197 	if (spa->spa_compatibility != NULL) {
2198 		spa_strfree(spa->spa_compatibility);
2199 		spa->spa_compatibility = NULL;
2200 	}
2201 
2202 	spa->spa_raidz_expand = NULL;
2203 	spa->spa_checkpoint_txg = 0;
2204 
2205 	spa_config_exit(spa, SCL_ALL, spa);
2206 }
2207 
2208 /*
2209  * Load (or re-load) the current list of vdevs describing the active spares for
2210  * this pool.  When this is called, we have some form of basic information in
2211  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
2212  * then re-generate a more complete list including status information.
2213  */
2214 void
2215 spa_load_spares(spa_t *spa)
2216 {
2217 	nvlist_t **spares;
2218 	uint_t nspares;
2219 	int i;
2220 	vdev_t *vd, *tvd;
2221 
2222 #ifndef _KERNEL
2223 	/*
2224 	 * zdb opens both the current state of the pool and the
2225 	 * checkpointed state (if present), with a different spa_t.
2226 	 *
2227 	 * As spare vdevs are shared among open pools, we skip loading
2228 	 * them when we load the checkpointed state of the pool.
2229 	 */
2230 	if (!spa_writeable(spa))
2231 		return;
2232 #endif
2233 
2234 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2235 
2236 	/*
2237 	 * First, close and free any existing spare vdevs.
2238 	 */
2239 	if (spa->spa_spares.sav_vdevs) {
2240 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
2241 			vd = spa->spa_spares.sav_vdevs[i];
2242 
2243 			/* Undo the call to spa_activate() below */
2244 			if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
2245 			    B_FALSE)) != NULL && tvd->vdev_isspare)
2246 				spa_spare_remove(tvd);
2247 			vdev_close(vd);
2248 			vdev_free(vd);
2249 		}
2250 
2251 		kmem_free(spa->spa_spares.sav_vdevs,
2252 		    spa->spa_spares.sav_count * sizeof (void *));
2253 	}
2254 
2255 	if (spa->spa_spares.sav_config == NULL)
2256 		nspares = 0;
2257 	else
2258 		VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2259 		    ZPOOL_CONFIG_SPARES, &spares, &nspares));
2260 
2261 	spa->spa_spares.sav_count = (int)nspares;
2262 	spa->spa_spares.sav_vdevs = NULL;
2263 
2264 	if (nspares == 0)
2265 		return;
2266 
2267 	/*
2268 	 * Construct the array of vdevs, opening them to get status in the
2269 	 * process.   For each spare, there is potentially two different vdev_t
2270 	 * structures associated with it: one in the list of spares (used only
2271 	 * for basic validation purposes) and one in the active vdev
2272 	 * configuration (if it's spared in).  During this phase we open and
2273 	 * validate each vdev on the spare list.  If the vdev also exists in the
2274 	 * active configuration, then we also mark this vdev as an active spare.
2275 	 */
2276 	spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *),
2277 	    KM_SLEEP);
2278 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
2279 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
2280 		    VDEV_ALLOC_SPARE) == 0);
2281 		ASSERT(vd != NULL);
2282 
2283 		spa->spa_spares.sav_vdevs[i] = vd;
2284 
2285 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
2286 		    B_FALSE)) != NULL) {
2287 			if (!tvd->vdev_isspare)
2288 				spa_spare_add(tvd);
2289 
2290 			/*
2291 			 * We only mark the spare active if we were successfully
2292 			 * able to load the vdev.  Otherwise, importing a pool
2293 			 * with a bad active spare would result in strange
2294 			 * behavior, because multiple pool would think the spare
2295 			 * is actively in use.
2296 			 *
2297 			 * There is a vulnerability here to an equally bizarre
2298 			 * circumstance, where a dead active spare is later
2299 			 * brought back to life (onlined or otherwise).  Given
2300 			 * the rarity of this scenario, and the extra complexity
2301 			 * it adds, we ignore the possibility.
2302 			 */
2303 			if (!vdev_is_dead(tvd))
2304 				spa_spare_activate(tvd);
2305 		}
2306 
2307 		vd->vdev_top = vd;
2308 		vd->vdev_aux = &spa->spa_spares;
2309 
2310 		if (vdev_open(vd) != 0)
2311 			continue;
2312 
2313 		if (vdev_validate_aux(vd) == 0)
2314 			spa_spare_add(vd);
2315 	}
2316 
2317 	/*
2318 	 * Recompute the stashed list of spares, with status information
2319 	 * this time.
2320 	 */
2321 	fnvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES);
2322 
2323 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
2324 	    KM_SLEEP);
2325 	for (i = 0; i < spa->spa_spares.sav_count; i++)
2326 		spares[i] = vdev_config_generate(spa,
2327 		    spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
2328 	fnvlist_add_nvlist_array(spa->spa_spares.sav_config,
2329 	    ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares,
2330 	    spa->spa_spares.sav_count);
2331 	for (i = 0; i < spa->spa_spares.sav_count; i++)
2332 		nvlist_free(spares[i]);
2333 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
2334 }
2335 
2336 /*
2337  * Load (or re-load) the current list of vdevs describing the active l2cache for
2338  * this pool.  When this is called, we have some form of basic information in
2339  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
2340  * then re-generate a more complete list including status information.
2341  * Devices which are already active have their details maintained, and are
2342  * not re-opened.
2343  */
2344 void
2345 spa_load_l2cache(spa_t *spa)
2346 {
2347 	nvlist_t **l2cache = NULL;
2348 	uint_t nl2cache;
2349 	int i, j, oldnvdevs;
2350 	uint64_t guid;
2351 	vdev_t *vd, **oldvdevs, **newvdevs;
2352 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
2353 
2354 #ifndef _KERNEL
2355 	/*
2356 	 * zdb opens both the current state of the pool and the
2357 	 * checkpointed state (if present), with a different spa_t.
2358 	 *
2359 	 * As L2 caches are part of the ARC which is shared among open
2360 	 * pools, we skip loading them when we load the checkpointed
2361 	 * state of the pool.
2362 	 */
2363 	if (!spa_writeable(spa))
2364 		return;
2365 #endif
2366 
2367 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2368 
2369 	oldvdevs = sav->sav_vdevs;
2370 	oldnvdevs = sav->sav_count;
2371 	sav->sav_vdevs = NULL;
2372 	sav->sav_count = 0;
2373 
2374 	if (sav->sav_config == NULL) {
2375 		nl2cache = 0;
2376 		newvdevs = NULL;
2377 		goto out;
2378 	}
2379 
2380 	VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config,
2381 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache));
2382 	newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
2383 
2384 	/*
2385 	 * Process new nvlist of vdevs.
2386 	 */
2387 	for (i = 0; i < nl2cache; i++) {
2388 		guid = fnvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID);
2389 
2390 		newvdevs[i] = NULL;
2391 		for (j = 0; j < oldnvdevs; j++) {
2392 			vd = oldvdevs[j];
2393 			if (vd != NULL && guid == vd->vdev_guid) {
2394 				/*
2395 				 * Retain previous vdev for add/remove ops.
2396 				 */
2397 				newvdevs[i] = vd;
2398 				oldvdevs[j] = NULL;
2399 				break;
2400 			}
2401 		}
2402 
2403 		if (newvdevs[i] == NULL) {
2404 			/*
2405 			 * Create new vdev
2406 			 */
2407 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
2408 			    VDEV_ALLOC_L2CACHE) == 0);
2409 			ASSERT(vd != NULL);
2410 			newvdevs[i] = vd;
2411 
2412 			/*
2413 			 * Commit this vdev as an l2cache device,
2414 			 * even if it fails to open.
2415 			 */
2416 			spa_l2cache_add(vd);
2417 
2418 			vd->vdev_top = vd;
2419 			vd->vdev_aux = sav;
2420 
2421 			spa_l2cache_activate(vd);
2422 
2423 			if (vdev_open(vd) != 0)
2424 				continue;
2425 
2426 			(void) vdev_validate_aux(vd);
2427 
2428 			if (!vdev_is_dead(vd))
2429 				l2arc_add_vdev(spa, vd);
2430 
2431 			/*
2432 			 * Upon cache device addition to a pool or pool
2433 			 * creation with a cache device or if the header
2434 			 * of the device is invalid we issue an async
2435 			 * TRIM command for the whole device which will
2436 			 * execute if l2arc_trim_ahead > 0.
2437 			 */
2438 			spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
2439 		}
2440 	}
2441 
2442 	sav->sav_vdevs = newvdevs;
2443 	sav->sav_count = (int)nl2cache;
2444 
2445 	/*
2446 	 * Recompute the stashed list of l2cache devices, with status
2447 	 * information this time.
2448 	 */
2449 	fnvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE);
2450 
2451 	if (sav->sav_count > 0)
2452 		l2cache = kmem_alloc(sav->sav_count * sizeof (void *),
2453 		    KM_SLEEP);
2454 	for (i = 0; i < sav->sav_count; i++)
2455 		l2cache[i] = vdev_config_generate(spa,
2456 		    sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
2457 	fnvlist_add_nvlist_array(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
2458 	    (const nvlist_t * const *)l2cache, sav->sav_count);
2459 
2460 out:
2461 	/*
2462 	 * Purge vdevs that were dropped
2463 	 */
2464 	if (oldvdevs) {
2465 		for (i = 0; i < oldnvdevs; i++) {
2466 			uint64_t pool;
2467 
2468 			vd = oldvdevs[i];
2469 			if (vd != NULL) {
2470 				ASSERT(vd->vdev_isl2cache);
2471 
2472 				if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2473 				    pool != 0ULL && l2arc_vdev_present(vd))
2474 					l2arc_remove_vdev(vd);
2475 				vdev_clear_stats(vd);
2476 				vdev_free(vd);
2477 			}
2478 		}
2479 
2480 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
2481 	}
2482 
2483 	for (i = 0; i < sav->sav_count; i++)
2484 		nvlist_free(l2cache[i]);
2485 	if (sav->sav_count)
2486 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
2487 }
2488 
2489 static int
2490 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
2491 {
2492 	dmu_buf_t *db;
2493 	char *packed = NULL;
2494 	size_t nvsize = 0;
2495 	int error;
2496 	*value = NULL;
2497 
2498 	error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
2499 	if (error)
2500 		return (error);
2501 
2502 	nvsize = *(uint64_t *)db->db_data;
2503 	dmu_buf_rele(db, FTAG);
2504 
2505 	packed = vmem_alloc(nvsize, KM_SLEEP);
2506 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
2507 	    DMU_READ_PREFETCH);
2508 	if (error == 0)
2509 		error = nvlist_unpack(packed, nvsize, value, 0);
2510 	vmem_free(packed, nvsize);
2511 
2512 	return (error);
2513 }
2514 
2515 /*
2516  * Concrete top-level vdevs that are not missing and are not logs. At every
2517  * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2518  */
2519 static uint64_t
2520 spa_healthy_core_tvds(spa_t *spa)
2521 {
2522 	vdev_t *rvd = spa->spa_root_vdev;
2523 	uint64_t tvds = 0;
2524 
2525 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
2526 		vdev_t *vd = rvd->vdev_child[i];
2527 		if (vd->vdev_islog)
2528 			continue;
2529 		if (vdev_is_concrete(vd) && !vdev_is_dead(vd))
2530 			tvds++;
2531 	}
2532 
2533 	return (tvds);
2534 }
2535 
2536 /*
2537  * Checks to see if the given vdev could not be opened, in which case we post a
2538  * sysevent to notify the autoreplace code that the device has been removed.
2539  */
2540 static void
2541 spa_check_removed(vdev_t *vd)
2542 {
2543 	for (uint64_t c = 0; c < vd->vdev_children; c++)
2544 		spa_check_removed(vd->vdev_child[c]);
2545 
2546 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
2547 	    vdev_is_concrete(vd)) {
2548 		zfs_post_autoreplace(vd->vdev_spa, vd);
2549 		spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK);
2550 	}
2551 }
2552 
2553 static int
2554 spa_check_for_missing_logs(spa_t *spa)
2555 {
2556 	vdev_t *rvd = spa->spa_root_vdev;
2557 
2558 	/*
2559 	 * If we're doing a normal import, then build up any additional
2560 	 * diagnostic information about missing log devices.
2561 	 * We'll pass this up to the user for further processing.
2562 	 */
2563 	if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
2564 		nvlist_t **child, *nv;
2565 		uint64_t idx = 0;
2566 
2567 		child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *),
2568 		    KM_SLEEP);
2569 		nv = fnvlist_alloc();
2570 
2571 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2572 			vdev_t *tvd = rvd->vdev_child[c];
2573 
2574 			/*
2575 			 * We consider a device as missing only if it failed
2576 			 * to open (i.e. offline or faulted is not considered
2577 			 * as missing).
2578 			 */
2579 			if (tvd->vdev_islog &&
2580 			    tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
2581 				child[idx++] = vdev_config_generate(spa, tvd,
2582 				    B_FALSE, VDEV_CONFIG_MISSING);
2583 			}
2584 		}
2585 
2586 		if (idx > 0) {
2587 			fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2588 			    (const nvlist_t * const *)child, idx);
2589 			fnvlist_add_nvlist(spa->spa_load_info,
2590 			    ZPOOL_CONFIG_MISSING_DEVICES, nv);
2591 
2592 			for (uint64_t i = 0; i < idx; i++)
2593 				nvlist_free(child[i]);
2594 		}
2595 		nvlist_free(nv);
2596 		kmem_free(child, rvd->vdev_children * sizeof (char **));
2597 
2598 		if (idx > 0) {
2599 			spa_load_failed(spa, "some log devices are missing");
2600 			vdev_dbgmsg_print_tree(rvd, 2);
2601 			return (SET_ERROR(ENXIO));
2602 		}
2603 	} else {
2604 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2605 			vdev_t *tvd = rvd->vdev_child[c];
2606 
2607 			if (tvd->vdev_islog &&
2608 			    tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
2609 				spa_set_log_state(spa, SPA_LOG_CLEAR);
2610 				spa_load_note(spa, "some log devices are "
2611 				    "missing, ZIL is dropped.");
2612 				vdev_dbgmsg_print_tree(rvd, 2);
2613 				break;
2614 			}
2615 		}
2616 	}
2617 
2618 	return (0);
2619 }
2620 
2621 /*
2622  * Check for missing log devices
2623  */
2624 static boolean_t
2625 spa_check_logs(spa_t *spa)
2626 {
2627 	boolean_t rv = B_FALSE;
2628 	dsl_pool_t *dp = spa_get_dsl(spa);
2629 
2630 	switch (spa->spa_log_state) {
2631 	default:
2632 		break;
2633 	case SPA_LOG_MISSING:
2634 		/* need to recheck in case slog has been restored */
2635 	case SPA_LOG_UNKNOWN:
2636 		rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2637 		    zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
2638 		if (rv)
2639 			spa_set_log_state(spa, SPA_LOG_MISSING);
2640 		break;
2641 	}
2642 	return (rv);
2643 }
2644 
2645 /*
2646  * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2647  */
2648 static boolean_t
2649 spa_passivate_log(spa_t *spa)
2650 {
2651 	vdev_t *rvd = spa->spa_root_vdev;
2652 	boolean_t slog_found = B_FALSE;
2653 
2654 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
2655 
2656 	for (int c = 0; c < rvd->vdev_children; c++) {
2657 		vdev_t *tvd = rvd->vdev_child[c];
2658 
2659 		if (tvd->vdev_islog) {
2660 			ASSERT3P(tvd->vdev_log_mg, ==, NULL);
2661 			metaslab_group_passivate(tvd->vdev_mg);
2662 			slog_found = B_TRUE;
2663 		}
2664 	}
2665 
2666 	return (slog_found);
2667 }
2668 
2669 /*
2670  * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2671  */
2672 static void
2673 spa_activate_log(spa_t *spa)
2674 {
2675 	vdev_t *rvd = spa->spa_root_vdev;
2676 
2677 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
2678 
2679 	for (int c = 0; c < rvd->vdev_children; c++) {
2680 		vdev_t *tvd = rvd->vdev_child[c];
2681 
2682 		if (tvd->vdev_islog) {
2683 			ASSERT3P(tvd->vdev_log_mg, ==, NULL);
2684 			metaslab_group_activate(tvd->vdev_mg);
2685 		}
2686 	}
2687 }
2688 
2689 int
2690 spa_reset_logs(spa_t *spa)
2691 {
2692 	int error;
2693 
2694 	error = dmu_objset_find(spa_name(spa), zil_reset,
2695 	    NULL, DS_FIND_CHILDREN);
2696 	if (error == 0) {
2697 		/*
2698 		 * We successfully offlined the log device, sync out the
2699 		 * current txg so that the "stubby" block can be removed
2700 		 * by zil_sync().
2701 		 */
2702 		txg_wait_synced(spa->spa_dsl_pool, 0);
2703 	}
2704 	return (error);
2705 }
2706 
2707 static void
2708 spa_aux_check_removed(spa_aux_vdev_t *sav)
2709 {
2710 	for (int i = 0; i < sav->sav_count; i++)
2711 		spa_check_removed(sav->sav_vdevs[i]);
2712 }
2713 
2714 void
2715 spa_claim_notify(zio_t *zio)
2716 {
2717 	spa_t *spa = zio->io_spa;
2718 
2719 	if (zio->io_error)
2720 		return;
2721 
2722 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
2723 	if (spa->spa_claim_max_txg < BP_GET_LOGICAL_BIRTH(zio->io_bp))
2724 		spa->spa_claim_max_txg = BP_GET_LOGICAL_BIRTH(zio->io_bp);
2725 	mutex_exit(&spa->spa_props_lock);
2726 }
2727 
2728 typedef struct spa_load_error {
2729 	boolean_t	sle_verify_data;
2730 	uint64_t	sle_meta_count;
2731 	uint64_t	sle_data_count;
2732 } spa_load_error_t;
2733 
2734 static void
2735 spa_load_verify_done(zio_t *zio)
2736 {
2737 	blkptr_t *bp = zio->io_bp;
2738 	spa_load_error_t *sle = zio->io_private;
2739 	dmu_object_type_t type = BP_GET_TYPE(bp);
2740 	int error = zio->io_error;
2741 	spa_t *spa = zio->io_spa;
2742 
2743 	abd_free(zio->io_abd);
2744 	if (error) {
2745 		if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
2746 		    type != DMU_OT_INTENT_LOG)
2747 			atomic_inc_64(&sle->sle_meta_count);
2748 		else
2749 			atomic_inc_64(&sle->sle_data_count);
2750 	}
2751 
2752 	mutex_enter(&spa->spa_scrub_lock);
2753 	spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp);
2754 	cv_broadcast(&spa->spa_scrub_io_cv);
2755 	mutex_exit(&spa->spa_scrub_lock);
2756 }
2757 
2758 /*
2759  * Maximum number of inflight bytes is the log2 fraction of the arc size.
2760  * By default, we set it to 1/16th of the arc.
2761  */
2762 static uint_t spa_load_verify_shift = 4;
2763 static int spa_load_verify_metadata = B_TRUE;
2764 static int spa_load_verify_data = B_TRUE;
2765 
2766 static int
2767 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2768     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2769 {
2770 	zio_t *rio = arg;
2771 	spa_load_error_t *sle = rio->io_private;
2772 
2773 	(void) zilog, (void) dnp;
2774 
2775 	/*
2776 	 * Note: normally this routine will not be called if
2777 	 * spa_load_verify_metadata is not set.  However, it may be useful
2778 	 * to manually set the flag after the traversal has begun.
2779 	 */
2780 	if (!spa_load_verify_metadata)
2781 		return (0);
2782 
2783 	/*
2784 	 * Sanity check the block pointer in order to detect obvious damage
2785 	 * before using the contents in subsequent checks or in zio_read().
2786 	 * When damaged consider it to be a metadata error since we cannot
2787 	 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2788 	 */
2789 	if (zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) {
2790 		atomic_inc_64(&sle->sle_meta_count);
2791 		return (0);
2792 	}
2793 
2794 	if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) ||
2795 	    BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp))
2796 		return (0);
2797 
2798 	if (!BP_IS_METADATA(bp) &&
2799 	    (!spa_load_verify_data || !sle->sle_verify_data))
2800 		return (0);
2801 
2802 	uint64_t maxinflight_bytes =
2803 	    arc_target_bytes() >> spa_load_verify_shift;
2804 	size_t size = BP_GET_PSIZE(bp);
2805 
2806 	mutex_enter(&spa->spa_scrub_lock);
2807 	while (spa->spa_load_verify_bytes >= maxinflight_bytes)
2808 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2809 	spa->spa_load_verify_bytes += size;
2810 	mutex_exit(&spa->spa_scrub_lock);
2811 
2812 	zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size,
2813 	    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
2814 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
2815 	    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
2816 	return (0);
2817 }
2818 
2819 static int
2820 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2821 {
2822 	(void) dp, (void) arg;
2823 
2824 	if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
2825 		return (SET_ERROR(ENAMETOOLONG));
2826 
2827 	return (0);
2828 }
2829 
2830 static int
2831 spa_load_verify(spa_t *spa)
2832 {
2833 	zio_t *rio;
2834 	spa_load_error_t sle = { 0 };
2835 	zpool_load_policy_t policy;
2836 	boolean_t verify_ok = B_FALSE;
2837 	int error = 0;
2838 
2839 	zpool_get_load_policy(spa->spa_config, &policy);
2840 
2841 	if (policy.zlp_rewind & ZPOOL_NEVER_REWIND ||
2842 	    policy.zlp_maxmeta == UINT64_MAX)
2843 		return (0);
2844 
2845 	dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2846 	error = dmu_objset_find_dp(spa->spa_dsl_pool,
2847 	    spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2848 	    DS_FIND_CHILDREN);
2849 	dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2850 	if (error != 0)
2851 		return (error);
2852 
2853 	/*
2854 	 * Verify data only if we are rewinding or error limit was set.
2855 	 * Otherwise nothing except dbgmsg care about it to waste time.
2856 	 */
2857 	sle.sle_verify_data = (policy.zlp_rewind & ZPOOL_REWIND_MASK) ||
2858 	    (policy.zlp_maxdata < UINT64_MAX);
2859 
2860 	rio = zio_root(spa, NULL, &sle,
2861 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
2862 
2863 	if (spa_load_verify_metadata) {
2864 		if (spa->spa_extreme_rewind) {
2865 			spa_load_note(spa, "performing a complete scan of the "
2866 			    "pool since extreme rewind is on. This may take "
2867 			    "a very long time.\n  (spa_load_verify_data=%u, "
2868 			    "spa_load_verify_metadata=%u)",
2869 			    spa_load_verify_data, spa_load_verify_metadata);
2870 		}
2871 
2872 		error = traverse_pool(spa, spa->spa_verify_min_txg,
2873 		    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
2874 		    TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio);
2875 	}
2876 
2877 	(void) zio_wait(rio);
2878 	ASSERT0(spa->spa_load_verify_bytes);
2879 
2880 	spa->spa_load_meta_errors = sle.sle_meta_count;
2881 	spa->spa_load_data_errors = sle.sle_data_count;
2882 
2883 	if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) {
2884 		spa_load_note(spa, "spa_load_verify found %llu metadata errors "
2885 		    "and %llu data errors", (u_longlong_t)sle.sle_meta_count,
2886 		    (u_longlong_t)sle.sle_data_count);
2887 	}
2888 
2889 	if (spa_load_verify_dryrun ||
2890 	    (!error && sle.sle_meta_count <= policy.zlp_maxmeta &&
2891 	    sle.sle_data_count <= policy.zlp_maxdata)) {
2892 		int64_t loss = 0;
2893 
2894 		verify_ok = B_TRUE;
2895 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2896 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2897 
2898 		loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2899 		fnvlist_add_uint64(spa->spa_load_info, ZPOOL_CONFIG_LOAD_TIME,
2900 		    spa->spa_load_txg_ts);
2901 		fnvlist_add_int64(spa->spa_load_info, ZPOOL_CONFIG_REWIND_TIME,
2902 		    loss);
2903 		fnvlist_add_uint64(spa->spa_load_info,
2904 		    ZPOOL_CONFIG_LOAD_META_ERRORS, sle.sle_meta_count);
2905 		fnvlist_add_uint64(spa->spa_load_info,
2906 		    ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count);
2907 	} else {
2908 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2909 	}
2910 
2911 	if (spa_load_verify_dryrun)
2912 		return (0);
2913 
2914 	if (error) {
2915 		if (error != ENXIO && error != EIO)
2916 			error = SET_ERROR(EIO);
2917 		return (error);
2918 	}
2919 
2920 	return (verify_ok ? 0 : EIO);
2921 }
2922 
2923 /*
2924  * Find a value in the pool props object.
2925  */
2926 static void
2927 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2928 {
2929 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2930 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2931 }
2932 
2933 /*
2934  * Find a value in the pool directory object.
2935  */
2936 static int
2937 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent)
2938 {
2939 	int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2940 	    name, sizeof (uint64_t), 1, val);
2941 
2942 	if (error != 0 && (error != ENOENT || log_enoent)) {
2943 		spa_load_failed(spa, "couldn't get '%s' value in MOS directory "
2944 		    "[error=%d]", name, error);
2945 	}
2946 
2947 	return (error);
2948 }
2949 
2950 static int
2951 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2952 {
2953 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2954 	return (SET_ERROR(err));
2955 }
2956 
2957 boolean_t
2958 spa_livelist_delete_check(spa_t *spa)
2959 {
2960 	return (spa->spa_livelists_to_delete != 0);
2961 }
2962 
2963 static boolean_t
2964 spa_livelist_delete_cb_check(void *arg, zthr_t *z)
2965 {
2966 	(void) z;
2967 	spa_t *spa = arg;
2968 	return (spa_livelist_delete_check(spa));
2969 }
2970 
2971 static int
2972 delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2973 {
2974 	spa_t *spa = arg;
2975 	zio_free(spa, tx->tx_txg, bp);
2976 	dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
2977 	    -bp_get_dsize_sync(spa, bp),
2978 	    -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
2979 	return (0);
2980 }
2981 
2982 static int
2983 dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp)
2984 {
2985 	int err;
2986 	zap_cursor_t zc;
2987 	zap_attribute_t *za = zap_attribute_alloc();
2988 	zap_cursor_init(&zc, os, zap_obj);
2989 	err = zap_cursor_retrieve(&zc, za);
2990 	zap_cursor_fini(&zc);
2991 	if (err == 0)
2992 		*llp = za->za_first_integer;
2993 	zap_attribute_free(za);
2994 	return (err);
2995 }
2996 
2997 /*
2998  * Components of livelist deletion that must be performed in syncing
2999  * context: freeing block pointers and updating the pool-wide data
3000  * structures to indicate how much work is left to do
3001  */
3002 typedef struct sublist_delete_arg {
3003 	spa_t *spa;
3004 	dsl_deadlist_t *ll;
3005 	uint64_t key;
3006 	bplist_t *to_free;
3007 } sublist_delete_arg_t;
3008 
3009 static void
3010 sublist_delete_sync(void *arg, dmu_tx_t *tx)
3011 {
3012 	sublist_delete_arg_t *sda = arg;
3013 	spa_t *spa = sda->spa;
3014 	dsl_deadlist_t *ll = sda->ll;
3015 	uint64_t key = sda->key;
3016 	bplist_t *to_free = sda->to_free;
3017 
3018 	bplist_iterate(to_free, delete_blkptr_cb, spa, tx);
3019 	dsl_deadlist_remove_entry(ll, key, tx);
3020 }
3021 
3022 typedef struct livelist_delete_arg {
3023 	spa_t *spa;
3024 	uint64_t ll_obj;
3025 	uint64_t zap_obj;
3026 } livelist_delete_arg_t;
3027 
3028 static void
3029 livelist_delete_sync(void *arg, dmu_tx_t *tx)
3030 {
3031 	livelist_delete_arg_t *lda = arg;
3032 	spa_t *spa = lda->spa;
3033 	uint64_t ll_obj = lda->ll_obj;
3034 	uint64_t zap_obj = lda->zap_obj;
3035 	objset_t *mos = spa->spa_meta_objset;
3036 	uint64_t count;
3037 
3038 	/* free the livelist and decrement the feature count */
3039 	VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx));
3040 	dsl_deadlist_free(mos, ll_obj, tx);
3041 	spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx);
3042 	VERIFY0(zap_count(mos, zap_obj, &count));
3043 	if (count == 0) {
3044 		/* no more livelists to delete */
3045 		VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT,
3046 		    DMU_POOL_DELETED_CLONES, tx));
3047 		VERIFY0(zap_destroy(mos, zap_obj, tx));
3048 		spa->spa_livelists_to_delete = 0;
3049 		spa_notify_waiters(spa);
3050 	}
3051 }
3052 
3053 /*
3054  * Load in the value for the livelist to be removed and open it. Then,
3055  * load its first sublist and determine which block pointers should actually
3056  * be freed. Then, call a synctask which performs the actual frees and updates
3057  * the pool-wide livelist data.
3058  */
3059 static void
3060 spa_livelist_delete_cb(void *arg, zthr_t *z)
3061 {
3062 	spa_t *spa = arg;
3063 	uint64_t ll_obj = 0, count;
3064 	objset_t *mos = spa->spa_meta_objset;
3065 	uint64_t zap_obj = spa->spa_livelists_to_delete;
3066 	/*
3067 	 * Determine the next livelist to delete. This function should only
3068 	 * be called if there is at least one deleted clone.
3069 	 */
3070 	VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj));
3071 	VERIFY0(zap_count(mos, ll_obj, &count));
3072 	if (count > 0) {
3073 		dsl_deadlist_t *ll;
3074 		dsl_deadlist_entry_t *dle;
3075 		bplist_t to_free;
3076 		ll = kmem_zalloc(sizeof (dsl_deadlist_t), KM_SLEEP);
3077 		VERIFY0(dsl_deadlist_open(ll, mos, ll_obj));
3078 		dle = dsl_deadlist_first(ll);
3079 		ASSERT3P(dle, !=, NULL);
3080 		bplist_create(&to_free);
3081 		int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free,
3082 		    z, NULL);
3083 		if (err == 0) {
3084 			sublist_delete_arg_t sync_arg = {
3085 			    .spa = spa,
3086 			    .ll = ll,
3087 			    .key = dle->dle_mintxg,
3088 			    .to_free = &to_free
3089 			};
3090 			zfs_dbgmsg("deleting sublist (id %llu) from"
3091 			    " livelist %llu, %lld remaining",
3092 			    (u_longlong_t)dle->dle_bpobj.bpo_object,
3093 			    (u_longlong_t)ll_obj, (longlong_t)count - 1);
3094 			VERIFY0(dsl_sync_task(spa_name(spa), NULL,
3095 			    sublist_delete_sync, &sync_arg, 0,
3096 			    ZFS_SPACE_CHECK_DESTROY));
3097 		} else {
3098 			VERIFY3U(err, ==, EINTR);
3099 		}
3100 		bplist_clear(&to_free);
3101 		bplist_destroy(&to_free);
3102 		dsl_deadlist_close(ll);
3103 		kmem_free(ll, sizeof (dsl_deadlist_t));
3104 	} else {
3105 		livelist_delete_arg_t sync_arg = {
3106 		    .spa = spa,
3107 		    .ll_obj = ll_obj,
3108 		    .zap_obj = zap_obj
3109 		};
3110 		zfs_dbgmsg("deletion of livelist %llu completed",
3111 		    (u_longlong_t)ll_obj);
3112 		VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync,
3113 		    &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY));
3114 	}
3115 }
3116 
3117 static void
3118 spa_start_livelist_destroy_thread(spa_t *spa)
3119 {
3120 	ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL);
3121 	spa->spa_livelist_delete_zthr =
3122 	    zthr_create("z_livelist_destroy",
3123 	    spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa,
3124 	    minclsyspri);
3125 }
3126 
3127 typedef struct livelist_new_arg {
3128 	bplist_t *allocs;
3129 	bplist_t *frees;
3130 } livelist_new_arg_t;
3131 
3132 static int
3133 livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
3134     dmu_tx_t *tx)
3135 {
3136 	ASSERT(tx == NULL);
3137 	livelist_new_arg_t *lna = arg;
3138 	if (bp_freed) {
3139 		bplist_append(lna->frees, bp);
3140 	} else {
3141 		bplist_append(lna->allocs, bp);
3142 		zfs_livelist_condense_new_alloc++;
3143 	}
3144 	return (0);
3145 }
3146 
3147 typedef struct livelist_condense_arg {
3148 	spa_t *spa;
3149 	bplist_t to_keep;
3150 	uint64_t first_size;
3151 	uint64_t next_size;
3152 } livelist_condense_arg_t;
3153 
3154 static void
3155 spa_livelist_condense_sync(void *arg, dmu_tx_t *tx)
3156 {
3157 	livelist_condense_arg_t *lca = arg;
3158 	spa_t *spa = lca->spa;
3159 	bplist_t new_frees;
3160 	dsl_dataset_t *ds = spa->spa_to_condense.ds;
3161 
3162 	/* Have we been cancelled? */
3163 	if (spa->spa_to_condense.cancelled) {
3164 		zfs_livelist_condense_sync_cancel++;
3165 		goto out;
3166 	}
3167 
3168 	dsl_deadlist_entry_t *first = spa->spa_to_condense.first;
3169 	dsl_deadlist_entry_t *next = spa->spa_to_condense.next;
3170 	dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist;
3171 
3172 	/*
3173 	 * It's possible that the livelist was changed while the zthr was
3174 	 * running. Therefore, we need to check for new blkptrs in the two
3175 	 * entries being condensed and continue to track them in the livelist.
3176 	 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
3177 	 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
3178 	 * we need to sort them into two different bplists.
3179 	 */
3180 	uint64_t first_obj = first->dle_bpobj.bpo_object;
3181 	uint64_t next_obj = next->dle_bpobj.bpo_object;
3182 	uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs;
3183 	uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs;
3184 
3185 	bplist_create(&new_frees);
3186 	livelist_new_arg_t new_bps = {
3187 	    .allocs = &lca->to_keep,
3188 	    .frees = &new_frees,
3189 	};
3190 
3191 	if (cur_first_size > lca->first_size) {
3192 		VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj,
3193 		    livelist_track_new_cb, &new_bps, lca->first_size));
3194 	}
3195 	if (cur_next_size > lca->next_size) {
3196 		VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj,
3197 		    livelist_track_new_cb, &new_bps, lca->next_size));
3198 	}
3199 
3200 	dsl_deadlist_clear_entry(first, ll, tx);
3201 	ASSERT(bpobj_is_empty(&first->dle_bpobj));
3202 	dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx);
3203 
3204 	bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx);
3205 	bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx);
3206 	bplist_destroy(&new_frees);
3207 
3208 	char dsname[ZFS_MAX_DATASET_NAME_LEN];
3209 	dsl_dataset_name(ds, dsname);
3210 	zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
3211 	    "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
3212 	    "(%llu blkptrs)", (u_longlong_t)tx->tx_txg, dsname,
3213 	    (u_longlong_t)ds->ds_object, (u_longlong_t)first_obj,
3214 	    (u_longlong_t)cur_first_size, (u_longlong_t)next_obj,
3215 	    (u_longlong_t)cur_next_size,
3216 	    (u_longlong_t)first->dle_bpobj.bpo_object,
3217 	    (u_longlong_t)first->dle_bpobj.bpo_phys->bpo_num_blkptrs);
3218 out:
3219 	dmu_buf_rele(ds->ds_dbuf, spa);
3220 	spa->spa_to_condense.ds = NULL;
3221 	bplist_clear(&lca->to_keep);
3222 	bplist_destroy(&lca->to_keep);
3223 	kmem_free(lca, sizeof (livelist_condense_arg_t));
3224 	spa->spa_to_condense.syncing = B_FALSE;
3225 }
3226 
3227 static void
3228 spa_livelist_condense_cb(void *arg, zthr_t *t)
3229 {
3230 	while (zfs_livelist_condense_zthr_pause &&
3231 	    !(zthr_has_waiters(t) || zthr_iscancelled(t)))
3232 		delay(1);
3233 
3234 	spa_t *spa = arg;
3235 	dsl_deadlist_entry_t *first = spa->spa_to_condense.first;
3236 	dsl_deadlist_entry_t *next = spa->spa_to_condense.next;
3237 	uint64_t first_size, next_size;
3238 
3239 	livelist_condense_arg_t *lca =
3240 	    kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP);
3241 	bplist_create(&lca->to_keep);
3242 
3243 	/*
3244 	 * Process the livelists (matching FREEs and ALLOCs) in open context
3245 	 * so we have minimal work in syncing context to condense.
3246 	 *
3247 	 * We save bpobj sizes (first_size and next_size) to use later in
3248 	 * syncing context to determine if entries were added to these sublists
3249 	 * while in open context. This is possible because the clone is still
3250 	 * active and open for normal writes and we want to make sure the new,
3251 	 * unprocessed blockpointers are inserted into the livelist normally.
3252 	 *
3253 	 * Note that dsl_process_sub_livelist() both stores the size number of
3254 	 * blockpointers and iterates over them while the bpobj's lock held, so
3255 	 * the sizes returned to us are consistent which what was actually
3256 	 * processed.
3257 	 */
3258 	int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t,
3259 	    &first_size);
3260 	if (err == 0)
3261 		err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep,
3262 		    t, &next_size);
3263 
3264 	if (err == 0) {
3265 		while (zfs_livelist_condense_sync_pause &&
3266 		    !(zthr_has_waiters(t) || zthr_iscancelled(t)))
3267 			delay(1);
3268 
3269 		dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
3270 		dmu_tx_mark_netfree(tx);
3271 		dmu_tx_hold_space(tx, 1);
3272 		err = dmu_tx_assign(tx, DMU_TX_NOWAIT | DMU_TX_NOTHROTTLE);
3273 		if (err == 0) {
3274 			/*
3275 			 * Prevent the condense zthr restarting before
3276 			 * the synctask completes.
3277 			 */
3278 			spa->spa_to_condense.syncing = B_TRUE;
3279 			lca->spa = spa;
3280 			lca->first_size = first_size;
3281 			lca->next_size = next_size;
3282 			dsl_sync_task_nowait(spa_get_dsl(spa),
3283 			    spa_livelist_condense_sync, lca, tx);
3284 			dmu_tx_commit(tx);
3285 			return;
3286 		}
3287 	}
3288 	/*
3289 	 * Condensing can not continue: either it was externally stopped or
3290 	 * we were unable to assign to a tx because the pool has run out of
3291 	 * space. In the second case, we'll just end up trying to condense
3292 	 * again in a later txg.
3293 	 */
3294 	ASSERT(err != 0);
3295 	bplist_clear(&lca->to_keep);
3296 	bplist_destroy(&lca->to_keep);
3297 	kmem_free(lca, sizeof (livelist_condense_arg_t));
3298 	dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa);
3299 	spa->spa_to_condense.ds = NULL;
3300 	if (err == EINTR)
3301 		zfs_livelist_condense_zthr_cancel++;
3302 }
3303 
3304 /*
3305  * Check that there is something to condense but that a condense is not
3306  * already in progress and that condensing has not been cancelled.
3307  */
3308 static boolean_t
3309 spa_livelist_condense_cb_check(void *arg, zthr_t *z)
3310 {
3311 	(void) z;
3312 	spa_t *spa = arg;
3313 	if ((spa->spa_to_condense.ds != NULL) &&
3314 	    (spa->spa_to_condense.syncing == B_FALSE) &&
3315 	    (spa->spa_to_condense.cancelled == B_FALSE)) {
3316 		return (B_TRUE);
3317 	}
3318 	return (B_FALSE);
3319 }
3320 
3321 static void
3322 spa_start_livelist_condensing_thread(spa_t *spa)
3323 {
3324 	spa->spa_to_condense.ds = NULL;
3325 	spa->spa_to_condense.first = NULL;
3326 	spa->spa_to_condense.next = NULL;
3327 	spa->spa_to_condense.syncing = B_FALSE;
3328 	spa->spa_to_condense.cancelled = B_FALSE;
3329 
3330 	ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL);
3331 	spa->spa_livelist_condense_zthr =
3332 	    zthr_create("z_livelist_condense",
3333 	    spa_livelist_condense_cb_check,
3334 	    spa_livelist_condense_cb, spa, minclsyspri);
3335 }
3336 
3337 static void
3338 spa_spawn_aux_threads(spa_t *spa)
3339 {
3340 	ASSERT(spa_writeable(spa));
3341 
3342 	spa_start_raidz_expansion_thread(spa);
3343 	spa_start_indirect_condensing_thread(spa);
3344 	spa_start_livelist_destroy_thread(spa);
3345 	spa_start_livelist_condensing_thread(spa);
3346 
3347 	ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL);
3348 	spa->spa_checkpoint_discard_zthr =
3349 	    zthr_create("z_checkpoint_discard",
3350 	    spa_checkpoint_discard_thread_check,
3351 	    spa_checkpoint_discard_thread, spa, minclsyspri);
3352 }
3353 
3354 /*
3355  * Fix up config after a partly-completed split.  This is done with the
3356  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
3357  * pool have that entry in their config, but only the splitting one contains
3358  * a list of all the guids of the vdevs that are being split off.
3359  *
3360  * This function determines what to do with that list: either rejoin
3361  * all the disks to the pool, or complete the splitting process.  To attempt
3362  * the rejoin, each disk that is offlined is marked online again, and
3363  * we do a reopen() call.  If the vdev label for every disk that was
3364  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
3365  * then we call vdev_split() on each disk, and complete the split.
3366  *
3367  * Otherwise we leave the config alone, with all the vdevs in place in
3368  * the original pool.
3369  */
3370 static void
3371 spa_try_repair(spa_t *spa, nvlist_t *config)
3372 {
3373 	uint_t extracted;
3374 	uint64_t *glist;
3375 	uint_t i, gcount;
3376 	nvlist_t *nvl;
3377 	vdev_t **vd;
3378 	boolean_t attempt_reopen;
3379 
3380 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
3381 		return;
3382 
3383 	/* check that the config is complete */
3384 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
3385 	    &glist, &gcount) != 0)
3386 		return;
3387 
3388 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
3389 
3390 	/* attempt to online all the vdevs & validate */
3391 	attempt_reopen = B_TRUE;
3392 	for (i = 0; i < gcount; i++) {
3393 		if (glist[i] == 0)	/* vdev is hole */
3394 			continue;
3395 
3396 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
3397 		if (vd[i] == NULL) {
3398 			/*
3399 			 * Don't bother attempting to reopen the disks;
3400 			 * just do the split.
3401 			 */
3402 			attempt_reopen = B_FALSE;
3403 		} else {
3404 			/* attempt to re-online it */
3405 			vd[i]->vdev_offline = B_FALSE;
3406 		}
3407 	}
3408 
3409 	if (attempt_reopen) {
3410 		vdev_reopen(spa->spa_root_vdev);
3411 
3412 		/* check each device to see what state it's in */
3413 		for (extracted = 0, i = 0; i < gcount; i++) {
3414 			if (vd[i] != NULL &&
3415 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
3416 				break;
3417 			++extracted;
3418 		}
3419 	}
3420 
3421 	/*
3422 	 * If every disk has been moved to the new pool, or if we never
3423 	 * even attempted to look at them, then we split them off for
3424 	 * good.
3425 	 */
3426 	if (!attempt_reopen || gcount == extracted) {
3427 		for (i = 0; i < gcount; i++)
3428 			if (vd[i] != NULL)
3429 				vdev_split(vd[i]);
3430 		vdev_reopen(spa->spa_root_vdev);
3431 	}
3432 
3433 	kmem_free(vd, gcount * sizeof (vdev_t *));
3434 }
3435 
3436 static int
3437 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type)
3438 {
3439 	const char *ereport = FM_EREPORT_ZFS_POOL;
3440 	int error;
3441 
3442 	spa->spa_load_state = state;
3443 	(void) spa_import_progress_set_state(spa_guid(spa),
3444 	    spa_load_state(spa));
3445 	spa_import_progress_set_notes(spa, "spa_load()");
3446 
3447 	gethrestime(&spa->spa_loaded_ts);
3448 	error = spa_load_impl(spa, type, &ereport);
3449 
3450 	/*
3451 	 * Don't count references from objsets that are already closed
3452 	 * and are making their way through the eviction process.
3453 	 */
3454 	spa_evicting_os_wait(spa);
3455 	spa->spa_minref = zfs_refcount_count(&spa->spa_refcount);
3456 	if (error) {
3457 		if (error != EEXIST) {
3458 			spa->spa_loaded_ts.tv_sec = 0;
3459 			spa->spa_loaded_ts.tv_nsec = 0;
3460 		}
3461 		if (error != EBADF) {
3462 			(void) zfs_ereport_post(ereport, spa,
3463 			    NULL, NULL, NULL, 0);
3464 		}
3465 	}
3466 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
3467 	spa->spa_ena = 0;
3468 
3469 	(void) spa_import_progress_set_state(spa_guid(spa),
3470 	    spa_load_state(spa));
3471 
3472 	return (error);
3473 }
3474 
3475 #ifdef ZFS_DEBUG
3476 /*
3477  * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3478  * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3479  * spa's per-vdev ZAP list.
3480  */
3481 static uint64_t
3482 vdev_count_verify_zaps(vdev_t *vd)
3483 {
3484 	spa_t *spa = vd->vdev_spa;
3485 	uint64_t total = 0;
3486 
3487 	if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2) &&
3488 	    vd->vdev_root_zap != 0) {
3489 		total++;
3490 		ASSERT0(zap_lookup_int(spa->spa_meta_objset,
3491 		    spa->spa_all_vdev_zaps, vd->vdev_root_zap));
3492 	}
3493 	if (vd->vdev_top_zap != 0) {
3494 		total++;
3495 		ASSERT0(zap_lookup_int(spa->spa_meta_objset,
3496 		    spa->spa_all_vdev_zaps, vd->vdev_top_zap));
3497 	}
3498 	if (vd->vdev_leaf_zap != 0) {
3499 		total++;
3500 		ASSERT0(zap_lookup_int(spa->spa_meta_objset,
3501 		    spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
3502 	}
3503 
3504 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
3505 		total += vdev_count_verify_zaps(vd->vdev_child[i]);
3506 	}
3507 
3508 	return (total);
3509 }
3510 #else
3511 #define	vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3512 #endif
3513 
3514 /*
3515  * Determine whether the activity check is required.
3516  */
3517 static boolean_t
3518 spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label,
3519     nvlist_t *config)
3520 {
3521 	uint64_t state = 0;
3522 	uint64_t hostid = 0;
3523 	uint64_t tryconfig_txg = 0;
3524 	uint64_t tryconfig_timestamp = 0;
3525 	uint16_t tryconfig_mmp_seq = 0;
3526 	nvlist_t *nvinfo;
3527 
3528 	if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
3529 		nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3530 		(void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG,
3531 		    &tryconfig_txg);
3532 		(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3533 		    &tryconfig_timestamp);
3534 		(void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ,
3535 		    &tryconfig_mmp_seq);
3536 	}
3537 
3538 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state);
3539 
3540 	/*
3541 	 * Disable the MMP activity check - This is used by zdb which
3542 	 * is intended to be used on potentially active pools.
3543 	 */
3544 	if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP)
3545 		return (B_FALSE);
3546 
3547 	/*
3548 	 * Skip the activity check when the MMP feature is disabled.
3549 	 */
3550 	if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0)
3551 		return (B_FALSE);
3552 
3553 	/*
3554 	 * If the tryconfig_ values are nonzero, they are the results of an
3555 	 * earlier tryimport.  If they all match the uberblock we just found,
3556 	 * then the pool has not changed and we return false so we do not test
3557 	 * a second time.
3558 	 */
3559 	if (tryconfig_txg && tryconfig_txg == ub->ub_txg &&
3560 	    tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp &&
3561 	    tryconfig_mmp_seq && tryconfig_mmp_seq ==
3562 	    (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0))
3563 		return (B_FALSE);
3564 
3565 	/*
3566 	 * Allow the activity check to be skipped when importing the pool
3567 	 * on the same host which last imported it.  Since the hostid from
3568 	 * configuration may be stale use the one read from the label.
3569 	 */
3570 	if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID))
3571 		hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID);
3572 
3573 	if (hostid == spa_get_hostid(spa))
3574 		return (B_FALSE);
3575 
3576 	/*
3577 	 * Skip the activity test when the pool was cleanly exported.
3578 	 */
3579 	if (state != POOL_STATE_ACTIVE)
3580 		return (B_FALSE);
3581 
3582 	return (B_TRUE);
3583 }
3584 
3585 /*
3586  * Nanoseconds the activity check must watch for changes on-disk.
3587  */
3588 static uint64_t
3589 spa_activity_check_duration(spa_t *spa, uberblock_t *ub)
3590 {
3591 	uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1);
3592 	uint64_t multihost_interval = MSEC2NSEC(
3593 	    MMP_INTERVAL_OK(zfs_multihost_interval));
3594 	uint64_t import_delay = MAX(NANOSEC, import_intervals *
3595 	    multihost_interval);
3596 
3597 	/*
3598 	 * Local tunables determine a minimum duration except for the case
3599 	 * where we know when the remote host will suspend the pool if MMP
3600 	 * writes do not land.
3601 	 *
3602 	 * See Big Theory comment at the top of mmp.c for the reasoning behind
3603 	 * these cases and times.
3604 	 */
3605 
3606 	ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100);
3607 
3608 	if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) &&
3609 	    MMP_FAIL_INT(ub) > 0) {
3610 
3611 		/* MMP on remote host will suspend pool after failed writes */
3612 		import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) *
3613 		    MMP_IMPORT_SAFETY_FACTOR / 100;
3614 
3615 		zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3616 		    "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3617 		    "import_intervals=%llu", (u_longlong_t)import_delay,
3618 		    (u_longlong_t)MMP_FAIL_INT(ub),
3619 		    (u_longlong_t)MMP_INTERVAL(ub),
3620 		    (u_longlong_t)import_intervals);
3621 
3622 	} else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) &&
3623 	    MMP_FAIL_INT(ub) == 0) {
3624 
3625 		/* MMP on remote host will never suspend pool */
3626 		import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) +
3627 		    ub->ub_mmp_delay) * import_intervals);
3628 
3629 		zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3630 		    "mmp_interval=%llu ub_mmp_delay=%llu "
3631 		    "import_intervals=%llu", (u_longlong_t)import_delay,
3632 		    (u_longlong_t)MMP_INTERVAL(ub),
3633 		    (u_longlong_t)ub->ub_mmp_delay,
3634 		    (u_longlong_t)import_intervals);
3635 
3636 	} else if (MMP_VALID(ub)) {
3637 		/*
3638 		 * zfs-0.7 compatibility case
3639 		 */
3640 
3641 		import_delay = MAX(import_delay, (multihost_interval +
3642 		    ub->ub_mmp_delay) * import_intervals);
3643 
3644 		zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3645 		    "import_intervals=%llu leaves=%u",
3646 		    (u_longlong_t)import_delay,
3647 		    (u_longlong_t)ub->ub_mmp_delay,
3648 		    (u_longlong_t)import_intervals,
3649 		    vdev_count_leaves(spa));
3650 	} else {
3651 		/* Using local tunings is the only reasonable option */
3652 		zfs_dbgmsg("pool last imported on non-MMP aware "
3653 		    "host using import_delay=%llu multihost_interval=%llu "
3654 		    "import_intervals=%llu", (u_longlong_t)import_delay,
3655 		    (u_longlong_t)multihost_interval,
3656 		    (u_longlong_t)import_intervals);
3657 	}
3658 
3659 	return (import_delay);
3660 }
3661 
3662 /*
3663  * Remote host activity check.
3664  *
3665  * error results:
3666  *          0 - no activity detected
3667  *  EREMOTEIO - remote activity detected
3668  *      EINTR - user canceled the operation
3669  */
3670 static int
3671 spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config,
3672     boolean_t importing)
3673 {
3674 	uint64_t txg = ub->ub_txg;
3675 	uint64_t timestamp = ub->ub_timestamp;
3676 	uint64_t mmp_config = ub->ub_mmp_config;
3677 	uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0;
3678 	uint64_t import_delay;
3679 	hrtime_t import_expire, now;
3680 	nvlist_t *mmp_label = NULL;
3681 	vdev_t *rvd = spa->spa_root_vdev;
3682 	kcondvar_t cv;
3683 	kmutex_t mtx;
3684 	int error = 0;
3685 
3686 	cv_init(&cv, NULL, CV_DEFAULT, NULL);
3687 	mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL);
3688 	mutex_enter(&mtx);
3689 
3690 	/*
3691 	 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3692 	 * during the earlier tryimport.  If the txg recorded there is 0 then
3693 	 * the pool is known to be active on another host.
3694 	 *
3695 	 * Otherwise, the pool might be in use on another host.  Check for
3696 	 * changes in the uberblocks on disk if necessary.
3697 	 */
3698 	if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
3699 		nvlist_t *nvinfo = fnvlist_lookup_nvlist(config,
3700 		    ZPOOL_CONFIG_LOAD_INFO);
3701 
3702 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) &&
3703 		    fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) {
3704 			vdev_uberblock_load(rvd, ub, &mmp_label);
3705 			error = SET_ERROR(EREMOTEIO);
3706 			goto out;
3707 		}
3708 	}
3709 
3710 	import_delay = spa_activity_check_duration(spa, ub);
3711 
3712 	/* Add a small random factor in case of simultaneous imports (0-25%) */
3713 	import_delay += import_delay * random_in_range(250) / 1000;
3714 
3715 	import_expire = gethrtime() + import_delay;
3716 
3717 	if (importing) {
3718 		spa_import_progress_set_notes(spa, "Checking MMP activity, "
3719 		    "waiting %llu ms", (u_longlong_t)NSEC2MSEC(import_delay));
3720 	}
3721 
3722 	int iterations = 0;
3723 	while ((now = gethrtime()) < import_expire) {
3724 		if (importing && iterations++ % 30 == 0) {
3725 			spa_import_progress_set_notes(spa, "Checking MMP "
3726 			    "activity, %llu ms remaining",
3727 			    (u_longlong_t)NSEC2MSEC(import_expire - now));
3728 		}
3729 
3730 		if (importing) {
3731 			(void) spa_import_progress_set_mmp_check(spa_guid(spa),
3732 			    NSEC2SEC(import_expire - gethrtime()));
3733 		}
3734 
3735 		vdev_uberblock_load(rvd, ub, &mmp_label);
3736 
3737 		if (txg != ub->ub_txg || timestamp != ub->ub_timestamp ||
3738 		    mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) {
3739 			zfs_dbgmsg("multihost activity detected "
3740 			    "txg %llu ub_txg  %llu "
3741 			    "timestamp %llu ub_timestamp  %llu "
3742 			    "mmp_config %#llx ub_mmp_config %#llx",
3743 			    (u_longlong_t)txg, (u_longlong_t)ub->ub_txg,
3744 			    (u_longlong_t)timestamp,
3745 			    (u_longlong_t)ub->ub_timestamp,
3746 			    (u_longlong_t)mmp_config,
3747 			    (u_longlong_t)ub->ub_mmp_config);
3748 
3749 			error = SET_ERROR(EREMOTEIO);
3750 			break;
3751 		}
3752 
3753 		if (mmp_label) {
3754 			nvlist_free(mmp_label);
3755 			mmp_label = NULL;
3756 		}
3757 
3758 		error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz);
3759 		if (error != -1) {
3760 			error = SET_ERROR(EINTR);
3761 			break;
3762 		}
3763 		error = 0;
3764 	}
3765 
3766 out:
3767 	mutex_exit(&mtx);
3768 	mutex_destroy(&mtx);
3769 	cv_destroy(&cv);
3770 
3771 	/*
3772 	 * If the pool is determined to be active store the status in the
3773 	 * spa->spa_load_info nvlist.  If the remote hostname or hostid are
3774 	 * available from configuration read from disk store them as well.
3775 	 * This allows 'zpool import' to generate a more useful message.
3776 	 *
3777 	 * ZPOOL_CONFIG_MMP_STATE    - observed pool status (mandatory)
3778 	 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3779 	 * ZPOOL_CONFIG_MMP_HOSTID   - hostid from the active pool
3780 	 */
3781 	if (error == EREMOTEIO) {
3782 		const char *hostname = "<unknown>";
3783 		uint64_t hostid = 0;
3784 
3785 		if (mmp_label) {
3786 			if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) {
3787 				hostname = fnvlist_lookup_string(mmp_label,
3788 				    ZPOOL_CONFIG_HOSTNAME);
3789 				fnvlist_add_string(spa->spa_load_info,
3790 				    ZPOOL_CONFIG_MMP_HOSTNAME, hostname);
3791 			}
3792 
3793 			if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) {
3794 				hostid = fnvlist_lookup_uint64(mmp_label,
3795 				    ZPOOL_CONFIG_HOSTID);
3796 				fnvlist_add_uint64(spa->spa_load_info,
3797 				    ZPOOL_CONFIG_MMP_HOSTID, hostid);
3798 			}
3799 		}
3800 
3801 		fnvlist_add_uint64(spa->spa_load_info,
3802 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE);
3803 		fnvlist_add_uint64(spa->spa_load_info,
3804 		    ZPOOL_CONFIG_MMP_TXG, 0);
3805 
3806 		error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO);
3807 	}
3808 
3809 	if (mmp_label)
3810 		nvlist_free(mmp_label);
3811 
3812 	return (error);
3813 }
3814 
3815 /*
3816  * Called from zfs_ioc_clear for a pool that was suspended
3817  * after failing mmp write checks.
3818  */
3819 boolean_t
3820 spa_mmp_remote_host_activity(spa_t *spa)
3821 {
3822 	ASSERT(spa_multihost(spa) && spa_suspended(spa));
3823 
3824 	nvlist_t *best_label;
3825 	uberblock_t best_ub;
3826 
3827 	/*
3828 	 * Locate the best uberblock on disk
3829 	 */
3830 	vdev_uberblock_load(spa->spa_root_vdev, &best_ub, &best_label);
3831 	if (best_label) {
3832 		/*
3833 		 * confirm that the best hostid matches our hostid
3834 		 */
3835 		if (nvlist_exists(best_label, ZPOOL_CONFIG_HOSTID) &&
3836 		    spa_get_hostid(spa) !=
3837 		    fnvlist_lookup_uint64(best_label, ZPOOL_CONFIG_HOSTID)) {
3838 			nvlist_free(best_label);
3839 			return (B_TRUE);
3840 		}
3841 		nvlist_free(best_label);
3842 	} else {
3843 		return (B_TRUE);
3844 	}
3845 
3846 	if (!MMP_VALID(&best_ub) ||
3847 	    !MMP_FAIL_INT_VALID(&best_ub) ||
3848 	    MMP_FAIL_INT(&best_ub) == 0) {
3849 		return (B_TRUE);
3850 	}
3851 
3852 	if (best_ub.ub_txg != spa->spa_uberblock.ub_txg ||
3853 	    best_ub.ub_timestamp != spa->spa_uberblock.ub_timestamp) {
3854 		zfs_dbgmsg("txg mismatch detected during pool clear "
3855 		    "txg %llu ub_txg %llu timestamp %llu ub_timestamp %llu",
3856 		    (u_longlong_t)spa->spa_uberblock.ub_txg,
3857 		    (u_longlong_t)best_ub.ub_txg,
3858 		    (u_longlong_t)spa->spa_uberblock.ub_timestamp,
3859 		    (u_longlong_t)best_ub.ub_timestamp);
3860 		return (B_TRUE);
3861 	}
3862 
3863 	/*
3864 	 * Perform an activity check looking for any remote writer
3865 	 */
3866 	return (spa_activity_check(spa, &spa->spa_uberblock, spa->spa_config,
3867 	    B_FALSE) != 0);
3868 }
3869 
3870 static int
3871 spa_verify_host(spa_t *spa, nvlist_t *mos_config)
3872 {
3873 	uint64_t hostid;
3874 	const char *hostname;
3875 	uint64_t myhostid = 0;
3876 
3877 	if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config,
3878 	    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
3879 		hostname = fnvlist_lookup_string(mos_config,
3880 		    ZPOOL_CONFIG_HOSTNAME);
3881 
3882 		myhostid = zone_get_hostid(NULL);
3883 
3884 		if (hostid != 0 && myhostid != 0 && hostid != myhostid) {
3885 			cmn_err(CE_WARN, "pool '%s' could not be "
3886 			    "loaded as it was last accessed by "
3887 			    "another system (host: %s hostid: 0x%llx). "
3888 			    "See: https://openzfs.github.io/openzfs-docs/msg/"
3889 			    "ZFS-8000-EY",
3890 			    spa_name(spa), hostname, (u_longlong_t)hostid);
3891 			spa_load_failed(spa, "hostid verification failed: pool "
3892 			    "last accessed by host: %s (hostid: 0x%llx)",
3893 			    hostname, (u_longlong_t)hostid);
3894 			return (SET_ERROR(EBADF));
3895 		}
3896 	}
3897 
3898 	return (0);
3899 }
3900 
3901 static int
3902 spa_ld_parse_config(spa_t *spa, spa_import_type_t type)
3903 {
3904 	int error = 0;
3905 	nvlist_t *nvtree, *nvl, *config = spa->spa_config;
3906 	int parse;
3907 	vdev_t *rvd;
3908 	uint64_t pool_guid;
3909 	const char *comment;
3910 	const char *compatibility;
3911 
3912 	/*
3913 	 * Versioning wasn't explicitly added to the label until later, so if
3914 	 * it's not present treat it as the initial version.
3915 	 */
3916 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3917 	    &spa->spa_ubsync.ub_version) != 0)
3918 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
3919 
3920 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
3921 		spa_load_failed(spa, "invalid config provided: '%s' missing",
3922 		    ZPOOL_CONFIG_POOL_GUID);
3923 		return (SET_ERROR(EINVAL));
3924 	}
3925 
3926 	/*
3927 	 * If we are doing an import, ensure that the pool is not already
3928 	 * imported by checking if its pool guid already exists in the
3929 	 * spa namespace.
3930 	 *
3931 	 * The only case that we allow an already imported pool to be
3932 	 * imported again, is when the pool is checkpointed and we want to
3933 	 * look at its checkpointed state from userland tools like zdb.
3934 	 */
3935 #ifdef _KERNEL
3936 	if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
3937 	    spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
3938 	    spa_guid_exists(pool_guid, 0)) {
3939 #else
3940 	if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
3941 	    spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
3942 	    spa_guid_exists(pool_guid, 0) &&
3943 	    !spa_importing_readonly_checkpoint(spa)) {
3944 #endif
3945 		spa_load_failed(spa, "a pool with guid %llu is already open",
3946 		    (u_longlong_t)pool_guid);
3947 		return (SET_ERROR(EEXIST));
3948 	}
3949 
3950 	spa->spa_config_guid = pool_guid;
3951 
3952 	nvlist_free(spa->spa_load_info);
3953 	spa->spa_load_info = fnvlist_alloc();
3954 
3955 	ASSERT(spa->spa_comment == NULL);
3956 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
3957 		spa->spa_comment = spa_strdup(comment);
3958 
3959 	ASSERT(spa->spa_compatibility == NULL);
3960 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMPATIBILITY,
3961 	    &compatibility) == 0)
3962 		spa->spa_compatibility = spa_strdup(compatibility);
3963 
3964 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
3965 	    &spa->spa_config_txg);
3966 
3967 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0)
3968 		spa->spa_config_splitting = fnvlist_dup(nvl);
3969 
3970 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) {
3971 		spa_load_failed(spa, "invalid config provided: '%s' missing",
3972 		    ZPOOL_CONFIG_VDEV_TREE);
3973 		return (SET_ERROR(EINVAL));
3974 	}
3975 
3976 	/*
3977 	 * Create "The Godfather" zio to hold all async IOs
3978 	 */
3979 	spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3980 	    KM_SLEEP);
3981 	for (int i = 0; i < max_ncpus; i++) {
3982 		spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3983 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3984 		    ZIO_FLAG_GODFATHER);
3985 	}
3986 
3987 	/*
3988 	 * Parse the configuration into a vdev tree.  We explicitly set the
3989 	 * value that will be returned by spa_version() since parsing the
3990 	 * configuration requires knowing the version number.
3991 	 */
3992 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3993 	parse = (type == SPA_IMPORT_EXISTING ?
3994 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
3995 	error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse);
3996 	spa_config_exit(spa, SCL_ALL, FTAG);
3997 
3998 	if (error != 0) {
3999 		spa_load_failed(spa, "unable to parse config [error=%d]",
4000 		    error);
4001 		return (error);
4002 	}
4003 
4004 	ASSERT(spa->spa_root_vdev == rvd);
4005 	ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
4006 	ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
4007 
4008 	if (type != SPA_IMPORT_ASSEMBLE) {
4009 		ASSERT(spa_guid(spa) == pool_guid);
4010 	}
4011 
4012 	return (0);
4013 }
4014 
4015 /*
4016  * Recursively open all vdevs in the vdev tree. This function is called twice:
4017  * first with the untrusted config, then with the trusted config.
4018  */
4019 static int
4020 spa_ld_open_vdevs(spa_t *spa)
4021 {
4022 	int error = 0;
4023 
4024 	/*
4025 	 * spa_missing_tvds_allowed defines how many top-level vdevs can be
4026 	 * missing/unopenable for the root vdev to be still considered openable.
4027 	 */
4028 	if (spa->spa_trust_config) {
4029 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds;
4030 	} else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) {
4031 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile;
4032 	} else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) {
4033 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan;
4034 	} else {
4035 		spa->spa_missing_tvds_allowed = 0;
4036 	}
4037 
4038 	spa->spa_missing_tvds_allowed =
4039 	    MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed);
4040 
4041 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4042 	error = vdev_open(spa->spa_root_vdev);
4043 	spa_config_exit(spa, SCL_ALL, FTAG);
4044 
4045 	if (spa->spa_missing_tvds != 0) {
4046 		spa_load_note(spa, "vdev tree has %lld missing top-level "
4047 		    "vdevs.", (u_longlong_t)spa->spa_missing_tvds);
4048 		if (spa->spa_trust_config && (spa->spa_mode & SPA_MODE_WRITE)) {
4049 			/*
4050 			 * Although theoretically we could allow users to open
4051 			 * incomplete pools in RW mode, we'd need to add a lot
4052 			 * of extra logic (e.g. adjust pool space to account
4053 			 * for missing vdevs).
4054 			 * This limitation also prevents users from accidentally
4055 			 * opening the pool in RW mode during data recovery and
4056 			 * damaging it further.
4057 			 */
4058 			spa_load_note(spa, "pools with missing top-level "
4059 			    "vdevs can only be opened in read-only mode.");
4060 			error = SET_ERROR(ENXIO);
4061 		} else {
4062 			spa_load_note(spa, "current settings allow for maximum "
4063 			    "%lld missing top-level vdevs at this stage.",
4064 			    (u_longlong_t)spa->spa_missing_tvds_allowed);
4065 		}
4066 	}
4067 	if (error != 0) {
4068 		spa_load_failed(spa, "unable to open vdev tree [error=%d]",
4069 		    error);
4070 	}
4071 	if (spa->spa_missing_tvds != 0 || error != 0)
4072 		vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2);
4073 
4074 	return (error);
4075 }
4076 
4077 /*
4078  * We need to validate the vdev labels against the configuration that
4079  * we have in hand. This function is called twice: first with an untrusted
4080  * config, then with a trusted config. The validation is more strict when the
4081  * config is trusted.
4082  */
4083 static int
4084 spa_ld_validate_vdevs(spa_t *spa)
4085 {
4086 	int error = 0;
4087 	vdev_t *rvd = spa->spa_root_vdev;
4088 
4089 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4090 	error = vdev_validate(rvd);
4091 	spa_config_exit(spa, SCL_ALL, FTAG);
4092 
4093 	if (error != 0) {
4094 		spa_load_failed(spa, "vdev_validate failed [error=%d]", error);
4095 		return (error);
4096 	}
4097 
4098 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
4099 		spa_load_failed(spa, "cannot open vdev tree after invalidating "
4100 		    "some vdevs");
4101 		vdev_dbgmsg_print_tree(rvd, 2);
4102 		return (SET_ERROR(ENXIO));
4103 	}
4104 
4105 	return (0);
4106 }
4107 
4108 static void
4109 spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub)
4110 {
4111 	spa->spa_state = POOL_STATE_ACTIVE;
4112 	spa->spa_ubsync = spa->spa_uberblock;
4113 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
4114 	    TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
4115 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
4116 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
4117 	spa->spa_claim_max_txg = spa->spa_first_txg;
4118 	spa->spa_prev_software_version = ub->ub_software_version;
4119 }
4120 
4121 static int
4122 spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type)
4123 {
4124 	vdev_t *rvd = spa->spa_root_vdev;
4125 	nvlist_t *label;
4126 	uberblock_t *ub = &spa->spa_uberblock;
4127 	boolean_t activity_check = B_FALSE;
4128 
4129 	/*
4130 	 * If we are opening the checkpointed state of the pool by
4131 	 * rewinding to it, at this point we will have written the
4132 	 * checkpointed uberblock to the vdev labels, so searching
4133 	 * the labels will find the right uberblock.  However, if
4134 	 * we are opening the checkpointed state read-only, we have
4135 	 * not modified the labels. Therefore, we must ignore the
4136 	 * labels and continue using the spa_uberblock that was set
4137 	 * by spa_ld_checkpoint_rewind.
4138 	 *
4139 	 * Note that it would be fine to ignore the labels when
4140 	 * rewinding (opening writeable) as well. However, if we
4141 	 * crash just after writing the labels, we will end up
4142 	 * searching the labels. Doing so in the common case means
4143 	 * that this code path gets exercised normally, rather than
4144 	 * just in the edge case.
4145 	 */
4146 	if (ub->ub_checkpoint_txg != 0 &&
4147 	    spa_importing_readonly_checkpoint(spa)) {
4148 		spa_ld_select_uberblock_done(spa, ub);
4149 		return (0);
4150 	}
4151 
4152 	/*
4153 	 * Find the best uberblock.
4154 	 */
4155 	vdev_uberblock_load(rvd, ub, &label);
4156 
4157 	/*
4158 	 * If we weren't able to find a single valid uberblock, return failure.
4159 	 */
4160 	if (ub->ub_txg == 0) {
4161 		nvlist_free(label);
4162 		spa_load_failed(spa, "no valid uberblock found");
4163 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
4164 	}
4165 
4166 	if (spa->spa_load_max_txg != UINT64_MAX) {
4167 		(void) spa_import_progress_set_max_txg(spa_guid(spa),
4168 		    (u_longlong_t)spa->spa_load_max_txg);
4169 	}
4170 	spa_load_note(spa, "using uberblock with txg=%llu",
4171 	    (u_longlong_t)ub->ub_txg);
4172 	if (ub->ub_raidz_reflow_info != 0) {
4173 		spa_load_note(spa, "uberblock raidz_reflow_info: "
4174 		    "state=%u offset=%llu",
4175 		    (int)RRSS_GET_STATE(ub),
4176 		    (u_longlong_t)RRSS_GET_OFFSET(ub));
4177 	}
4178 
4179 
4180 	/*
4181 	 * For pools which have the multihost property on determine if the
4182 	 * pool is truly inactive and can be safely imported.  Prevent
4183 	 * hosts which don't have a hostid set from importing the pool.
4184 	 */
4185 	activity_check = spa_activity_check_required(spa, ub, label,
4186 	    spa->spa_config);
4187 	if (activity_check) {
4188 		if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay &&
4189 		    spa_get_hostid(spa) == 0) {
4190 			nvlist_free(label);
4191 			fnvlist_add_uint64(spa->spa_load_info,
4192 			    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
4193 			return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
4194 		}
4195 
4196 		int error =
4197 		    spa_activity_check(spa, ub, spa->spa_config, B_TRUE);
4198 		if (error) {
4199 			nvlist_free(label);
4200 			return (error);
4201 		}
4202 
4203 		fnvlist_add_uint64(spa->spa_load_info,
4204 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE);
4205 		fnvlist_add_uint64(spa->spa_load_info,
4206 		    ZPOOL_CONFIG_MMP_TXG, ub->ub_txg);
4207 		fnvlist_add_uint16(spa->spa_load_info,
4208 		    ZPOOL_CONFIG_MMP_SEQ,
4209 		    (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0));
4210 	}
4211 
4212 	/*
4213 	 * If the pool has an unsupported version we can't open it.
4214 	 */
4215 	if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
4216 		nvlist_free(label);
4217 		spa_load_failed(spa, "version %llu is not supported",
4218 		    (u_longlong_t)ub->ub_version);
4219 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
4220 	}
4221 
4222 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
4223 		nvlist_t *features;
4224 
4225 		/*
4226 		 * If we weren't able to find what's necessary for reading the
4227 		 * MOS in the label, return failure.
4228 		 */
4229 		if (label == NULL) {
4230 			spa_load_failed(spa, "label config unavailable");
4231 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
4232 			    ENXIO));
4233 		}
4234 
4235 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ,
4236 		    &features) != 0) {
4237 			nvlist_free(label);
4238 			spa_load_failed(spa, "invalid label: '%s' missing",
4239 			    ZPOOL_CONFIG_FEATURES_FOR_READ);
4240 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
4241 			    ENXIO));
4242 		}
4243 
4244 		/*
4245 		 * Update our in-core representation with the definitive values
4246 		 * from the label.
4247 		 */
4248 		nvlist_free(spa->spa_label_features);
4249 		spa->spa_label_features = fnvlist_dup(features);
4250 	}
4251 
4252 	nvlist_free(label);
4253 
4254 	/*
4255 	 * Look through entries in the label nvlist's features_for_read. If
4256 	 * there is a feature listed there which we don't understand then we
4257 	 * cannot open a pool.
4258 	 */
4259 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
4260 		nvlist_t *unsup_feat;
4261 
4262 		unsup_feat = fnvlist_alloc();
4263 
4264 		for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
4265 		    NULL); nvp != NULL;
4266 		    nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
4267 			if (!zfeature_is_supported(nvpair_name(nvp))) {
4268 				fnvlist_add_string(unsup_feat,
4269 				    nvpair_name(nvp), "");
4270 			}
4271 		}
4272 
4273 		if (!nvlist_empty(unsup_feat)) {
4274 			fnvlist_add_nvlist(spa->spa_load_info,
4275 			    ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
4276 			nvlist_free(unsup_feat);
4277 			spa_load_failed(spa, "some features are unsupported");
4278 			return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
4279 			    ENOTSUP));
4280 		}
4281 
4282 		nvlist_free(unsup_feat);
4283 	}
4284 
4285 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
4286 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4287 		spa_try_repair(spa, spa->spa_config);
4288 		spa_config_exit(spa, SCL_ALL, FTAG);
4289 		nvlist_free(spa->spa_config_splitting);
4290 		spa->spa_config_splitting = NULL;
4291 	}
4292 
4293 	/*
4294 	 * Initialize internal SPA structures.
4295 	 */
4296 	spa_ld_select_uberblock_done(spa, ub);
4297 
4298 	return (0);
4299 }
4300 
4301 static int
4302 spa_ld_open_rootbp(spa_t *spa)
4303 {
4304 	int error = 0;
4305 	vdev_t *rvd = spa->spa_root_vdev;
4306 
4307 	error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
4308 	if (error != 0) {
4309 		spa_load_failed(spa, "unable to open rootbp in dsl_pool_init "
4310 		    "[error=%d]", error);
4311 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4312 	}
4313 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
4314 
4315 	return (0);
4316 }
4317 
4318 static int
4319 spa_ld_trusted_config(spa_t *spa, spa_import_type_t type,
4320     boolean_t reloading)
4321 {
4322 	vdev_t *mrvd, *rvd = spa->spa_root_vdev;
4323 	nvlist_t *nv, *mos_config, *policy;
4324 	int error = 0, copy_error;
4325 	uint64_t healthy_tvds, healthy_tvds_mos;
4326 	uint64_t mos_config_txg;
4327 
4328 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE)
4329 	    != 0)
4330 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4331 
4332 	/*
4333 	 * If we're assembling a pool from a split, the config provided is
4334 	 * already trusted so there is nothing to do.
4335 	 */
4336 	if (type == SPA_IMPORT_ASSEMBLE)
4337 		return (0);
4338 
4339 	healthy_tvds = spa_healthy_core_tvds(spa);
4340 
4341 	if (load_nvlist(spa, spa->spa_config_object, &mos_config)
4342 	    != 0) {
4343 		spa_load_failed(spa, "unable to retrieve MOS config");
4344 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4345 	}
4346 
4347 	/*
4348 	 * If we are doing an open, pool owner wasn't verified yet, thus do
4349 	 * the verification here.
4350 	 */
4351 	if (spa->spa_load_state == SPA_LOAD_OPEN) {
4352 		error = spa_verify_host(spa, mos_config);
4353 		if (error != 0) {
4354 			nvlist_free(mos_config);
4355 			return (error);
4356 		}
4357 	}
4358 
4359 	nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE);
4360 
4361 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4362 
4363 	/*
4364 	 * Build a new vdev tree from the trusted config
4365 	 */
4366 	error = spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD);
4367 	if (error != 0) {
4368 		nvlist_free(mos_config);
4369 		spa_config_exit(spa, SCL_ALL, FTAG);
4370 		spa_load_failed(spa, "spa_config_parse failed [error=%d]",
4371 		    error);
4372 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4373 	}
4374 
4375 	/*
4376 	 * Vdev paths in the MOS may be obsolete. If the untrusted config was
4377 	 * obtained by scanning /dev/dsk, then it will have the right vdev
4378 	 * paths. We update the trusted MOS config with this information.
4379 	 * We first try to copy the paths with vdev_copy_path_strict, which
4380 	 * succeeds only when both configs have exactly the same vdev tree.
4381 	 * If that fails, we fall back to a more flexible method that has a
4382 	 * best effort policy.
4383 	 */
4384 	copy_error = vdev_copy_path_strict(rvd, mrvd);
4385 	if (copy_error != 0 || spa_load_print_vdev_tree) {
4386 		spa_load_note(spa, "provided vdev tree:");
4387 		vdev_dbgmsg_print_tree(rvd, 2);
4388 		spa_load_note(spa, "MOS vdev tree:");
4389 		vdev_dbgmsg_print_tree(mrvd, 2);
4390 	}
4391 	if (copy_error != 0) {
4392 		spa_load_note(spa, "vdev_copy_path_strict failed, falling "
4393 		    "back to vdev_copy_path_relaxed");
4394 		vdev_copy_path_relaxed(rvd, mrvd);
4395 	}
4396 
4397 	vdev_close(rvd);
4398 	vdev_free(rvd);
4399 	spa->spa_root_vdev = mrvd;
4400 	rvd = mrvd;
4401 	spa_config_exit(spa, SCL_ALL, FTAG);
4402 
4403 	/*
4404 	 * If 'zpool import' used a cached config, then the on-disk hostid and
4405 	 * hostname may be different to the cached config in ways that should
4406 	 * prevent import.  Userspace can't discover this without a scan, but
4407 	 * we know, so we add these values to LOAD_INFO so the caller can know
4408 	 * the difference.
4409 	 *
4410 	 * Note that we have to do this before the config is regenerated,
4411 	 * because the new config will have the hostid and hostname for this
4412 	 * host, in readiness for import.
4413 	 */
4414 	if (nvlist_exists(mos_config, ZPOOL_CONFIG_HOSTID))
4415 		fnvlist_add_uint64(spa->spa_load_info, ZPOOL_CONFIG_HOSTID,
4416 		    fnvlist_lookup_uint64(mos_config, ZPOOL_CONFIG_HOSTID));
4417 	if (nvlist_exists(mos_config, ZPOOL_CONFIG_HOSTNAME))
4418 		fnvlist_add_string(spa->spa_load_info, ZPOOL_CONFIG_HOSTNAME,
4419 		    fnvlist_lookup_string(mos_config, ZPOOL_CONFIG_HOSTNAME));
4420 
4421 	/*
4422 	 * We will use spa_config if we decide to reload the spa or if spa_load
4423 	 * fails and we rewind. We must thus regenerate the config using the
4424 	 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
4425 	 * pass settings on how to load the pool and is not stored in the MOS.
4426 	 * We copy it over to our new, trusted config.
4427 	 */
4428 	mos_config_txg = fnvlist_lookup_uint64(mos_config,
4429 	    ZPOOL_CONFIG_POOL_TXG);
4430 	nvlist_free(mos_config);
4431 	mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE);
4432 	if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY,
4433 	    &policy) == 0)
4434 		fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy);
4435 	spa_config_set(spa, mos_config);
4436 	spa->spa_config_source = SPA_CONFIG_SRC_MOS;
4437 
4438 	/*
4439 	 * Now that we got the config from the MOS, we should be more strict
4440 	 * in checking blkptrs and can make assumptions about the consistency
4441 	 * of the vdev tree. spa_trust_config must be set to true before opening
4442 	 * vdevs in order for them to be writeable.
4443 	 */
4444 	spa->spa_trust_config = B_TRUE;
4445 
4446 	/*
4447 	 * Open and validate the new vdev tree
4448 	 */
4449 	error = spa_ld_open_vdevs(spa);
4450 	if (error != 0)
4451 		return (error);
4452 
4453 	error = spa_ld_validate_vdevs(spa);
4454 	if (error != 0)
4455 		return (error);
4456 
4457 	if (copy_error != 0 || spa_load_print_vdev_tree) {
4458 		spa_load_note(spa, "final vdev tree:");
4459 		vdev_dbgmsg_print_tree(rvd, 2);
4460 	}
4461 
4462 	if (spa->spa_load_state != SPA_LOAD_TRYIMPORT &&
4463 	    !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) {
4464 		/*
4465 		 * Sanity check to make sure that we are indeed loading the
4466 		 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
4467 		 * in the config provided and they happened to be the only ones
4468 		 * to have the latest uberblock, we could involuntarily perform
4469 		 * an extreme rewind.
4470 		 */
4471 		healthy_tvds_mos = spa_healthy_core_tvds(spa);
4472 		if (healthy_tvds_mos - healthy_tvds >=
4473 		    SPA_SYNC_MIN_VDEVS) {
4474 			spa_load_note(spa, "config provided misses too many "
4475 			    "top-level vdevs compared to MOS (%lld vs %lld). ",
4476 			    (u_longlong_t)healthy_tvds,
4477 			    (u_longlong_t)healthy_tvds_mos);
4478 			spa_load_note(spa, "vdev tree:");
4479 			vdev_dbgmsg_print_tree(rvd, 2);
4480 			if (reloading) {
4481 				spa_load_failed(spa, "config was already "
4482 				    "provided from MOS. Aborting.");
4483 				return (spa_vdev_err(rvd,
4484 				    VDEV_AUX_CORRUPT_DATA, EIO));
4485 			}
4486 			spa_load_note(spa, "spa must be reloaded using MOS "
4487 			    "config");
4488 			return (SET_ERROR(EAGAIN));
4489 		}
4490 	}
4491 
4492 	error = spa_check_for_missing_logs(spa);
4493 	if (error != 0)
4494 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
4495 
4496 	if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) {
4497 		spa_load_failed(spa, "uberblock guid sum doesn't match MOS "
4498 		    "guid sum (%llu != %llu)",
4499 		    (u_longlong_t)spa->spa_uberblock.ub_guid_sum,
4500 		    (u_longlong_t)rvd->vdev_guid_sum);
4501 		return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
4502 		    ENXIO));
4503 	}
4504 
4505 	return (0);
4506 }
4507 
4508 static int
4509 spa_ld_open_indirect_vdev_metadata(spa_t *spa)
4510 {
4511 	int error = 0;
4512 	vdev_t *rvd = spa->spa_root_vdev;
4513 
4514 	/*
4515 	 * Everything that we read before spa_remove_init() must be stored
4516 	 * on concreted vdevs.  Therefore we do this as early as possible.
4517 	 */
4518 	error = spa_remove_init(spa);
4519 	if (error != 0) {
4520 		spa_load_failed(spa, "spa_remove_init failed [error=%d]",
4521 		    error);
4522 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4523 	}
4524 
4525 	/*
4526 	 * Retrieve information needed to condense indirect vdev mappings.
4527 	 */
4528 	error = spa_condense_init(spa);
4529 	if (error != 0) {
4530 		spa_load_failed(spa, "spa_condense_init failed [error=%d]",
4531 		    error);
4532 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4533 	}
4534 
4535 	return (0);
4536 }
4537 
4538 static int
4539 spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep)
4540 {
4541 	int error = 0;
4542 	vdev_t *rvd = spa->spa_root_vdev;
4543 
4544 	if (spa_version(spa) >= SPA_VERSION_FEATURES) {
4545 		boolean_t missing_feat_read = B_FALSE;
4546 		nvlist_t *unsup_feat, *enabled_feat;
4547 
4548 		if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
4549 		    &spa->spa_feat_for_read_obj, B_TRUE) != 0) {
4550 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4551 		}
4552 
4553 		if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
4554 		    &spa->spa_feat_for_write_obj, B_TRUE) != 0) {
4555 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4556 		}
4557 
4558 		if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
4559 		    &spa->spa_feat_desc_obj, B_TRUE) != 0) {
4560 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4561 		}
4562 
4563 		enabled_feat = fnvlist_alloc();
4564 		unsup_feat = fnvlist_alloc();
4565 
4566 		if (!spa_features_check(spa, B_FALSE,
4567 		    unsup_feat, enabled_feat))
4568 			missing_feat_read = B_TRUE;
4569 
4570 		if (spa_writeable(spa) ||
4571 		    spa->spa_load_state == SPA_LOAD_TRYIMPORT) {
4572 			if (!spa_features_check(spa, B_TRUE,
4573 			    unsup_feat, enabled_feat)) {
4574 				*missing_feat_writep = B_TRUE;
4575 			}
4576 		}
4577 
4578 		fnvlist_add_nvlist(spa->spa_load_info,
4579 		    ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
4580 
4581 		if (!nvlist_empty(unsup_feat)) {
4582 			fnvlist_add_nvlist(spa->spa_load_info,
4583 			    ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
4584 		}
4585 
4586 		fnvlist_free(enabled_feat);
4587 		fnvlist_free(unsup_feat);
4588 
4589 		if (!missing_feat_read) {
4590 			fnvlist_add_boolean(spa->spa_load_info,
4591 			    ZPOOL_CONFIG_CAN_RDONLY);
4592 		}
4593 
4594 		/*
4595 		 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4596 		 * twofold: to determine whether the pool is available for
4597 		 * import in read-write mode and (if it is not) whether the
4598 		 * pool is available for import in read-only mode. If the pool
4599 		 * is available for import in read-write mode, it is displayed
4600 		 * as available in userland; if it is not available for import
4601 		 * in read-only mode, it is displayed as unavailable in
4602 		 * userland. If the pool is available for import in read-only
4603 		 * mode but not read-write mode, it is displayed as unavailable
4604 		 * in userland with a special note that the pool is actually
4605 		 * available for open in read-only mode.
4606 		 *
4607 		 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4608 		 * missing a feature for write, we must first determine whether
4609 		 * the pool can be opened read-only before returning to
4610 		 * userland in order to know whether to display the
4611 		 * abovementioned note.
4612 		 */
4613 		if (missing_feat_read || (*missing_feat_writep &&
4614 		    spa_writeable(spa))) {
4615 			spa_load_failed(spa, "pool uses unsupported features");
4616 			return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
4617 			    ENOTSUP));
4618 		}
4619 
4620 		/*
4621 		 * Load refcounts for ZFS features from disk into an in-memory
4622 		 * cache during SPA initialization.
4623 		 */
4624 		for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
4625 			uint64_t refcount;
4626 
4627 			error = feature_get_refcount_from_disk(spa,
4628 			    &spa_feature_table[i], &refcount);
4629 			if (error == 0) {
4630 				spa->spa_feat_refcount_cache[i] = refcount;
4631 			} else if (error == ENOTSUP) {
4632 				spa->spa_feat_refcount_cache[i] =
4633 				    SPA_FEATURE_DISABLED;
4634 			} else {
4635 				spa_load_failed(spa, "error getting refcount "
4636 				    "for feature %s [error=%d]",
4637 				    spa_feature_table[i].fi_guid, error);
4638 				return (spa_vdev_err(rvd,
4639 				    VDEV_AUX_CORRUPT_DATA, EIO));
4640 			}
4641 		}
4642 	}
4643 
4644 	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
4645 		if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
4646 		    &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0)
4647 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4648 	}
4649 
4650 	/*
4651 	 * Encryption was added before bookmark_v2, even though bookmark_v2
4652 	 * is now a dependency. If this pool has encryption enabled without
4653 	 * bookmark_v2, trigger an errata message.
4654 	 */
4655 	if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) &&
4656 	    !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) {
4657 		spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION;
4658 	}
4659 
4660 	return (0);
4661 }
4662 
4663 static int
4664 spa_ld_load_special_directories(spa_t *spa)
4665 {
4666 	int error = 0;
4667 	vdev_t *rvd = spa->spa_root_vdev;
4668 
4669 	spa->spa_is_initializing = B_TRUE;
4670 	error = dsl_pool_open(spa->spa_dsl_pool);
4671 	spa->spa_is_initializing = B_FALSE;
4672 	if (error != 0) {
4673 		spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error);
4674 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4675 	}
4676 
4677 	return (0);
4678 }
4679 
4680 static int
4681 spa_ld_get_props(spa_t *spa)
4682 {
4683 	int error = 0;
4684 	uint64_t obj;
4685 	vdev_t *rvd = spa->spa_root_vdev;
4686 
4687 	/* Grab the checksum salt from the MOS. */
4688 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4689 	    DMU_POOL_CHECKSUM_SALT, 1,
4690 	    sizeof (spa->spa_cksum_salt.zcs_bytes),
4691 	    spa->spa_cksum_salt.zcs_bytes);
4692 	if (error == ENOENT) {
4693 		/* Generate a new salt for subsequent use */
4694 		(void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
4695 		    sizeof (spa->spa_cksum_salt.zcs_bytes));
4696 	} else if (error != 0) {
4697 		spa_load_failed(spa, "unable to retrieve checksum salt from "
4698 		    "MOS [error=%d]", error);
4699 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4700 	}
4701 
4702 	if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0)
4703 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4704 	error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
4705 	if (error != 0) {
4706 		spa_load_failed(spa, "error opening deferred-frees bpobj "
4707 		    "[error=%d]", error);
4708 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4709 	}
4710 
4711 	/*
4712 	 * Load the bit that tells us to use the new accounting function
4713 	 * (raid-z deflation).  If we have an older pool, this will not
4714 	 * be present.
4715 	 */
4716 	error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE);
4717 	if (error != 0 && error != ENOENT)
4718 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4719 
4720 	error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
4721 	    &spa->spa_creation_version, B_FALSE);
4722 	if (error != 0 && error != ENOENT)
4723 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4724 
4725 	/*
4726 	 * Load the persistent error log.  If we have an older pool, this will
4727 	 * not be present.
4728 	 */
4729 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last,
4730 	    B_FALSE);
4731 	if (error != 0 && error != ENOENT)
4732 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4733 
4734 	error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
4735 	    &spa->spa_errlog_scrub, B_FALSE);
4736 	if (error != 0 && error != ENOENT)
4737 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4738 
4739 	/* Load the last scrubbed txg. */
4740 	error = spa_dir_prop(spa, DMU_POOL_LAST_SCRUBBED_TXG,
4741 	    &spa->spa_scrubbed_last_txg, B_FALSE);
4742 	if (error != 0 && error != ENOENT)
4743 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4744 
4745 	/*
4746 	 * Load the livelist deletion field. If a livelist is queued for
4747 	 * deletion, indicate that in the spa
4748 	 */
4749 	error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES,
4750 	    &spa->spa_livelists_to_delete, B_FALSE);
4751 	if (error != 0 && error != ENOENT)
4752 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4753 
4754 	/*
4755 	 * Load the history object.  If we have an older pool, this
4756 	 * will not be present.
4757 	 */
4758 	error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE);
4759 	if (error != 0 && error != ENOENT)
4760 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4761 
4762 	/*
4763 	 * Load the per-vdev ZAP map. If we have an older pool, this will not
4764 	 * be present; in this case, defer its creation to a later time to
4765 	 * avoid dirtying the MOS this early / out of sync context. See
4766 	 * spa_sync_config_object.
4767 	 */
4768 
4769 	/* The sentinel is only available in the MOS config. */
4770 	nvlist_t *mos_config;
4771 	if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) {
4772 		spa_load_failed(spa, "unable to retrieve MOS config");
4773 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4774 	}
4775 
4776 	error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP,
4777 	    &spa->spa_all_vdev_zaps, B_FALSE);
4778 
4779 	if (error == ENOENT) {
4780 		VERIFY(!nvlist_exists(mos_config,
4781 		    ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
4782 		spa->spa_avz_action = AVZ_ACTION_INITIALIZE;
4783 		ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
4784 	} else if (error != 0) {
4785 		nvlist_free(mos_config);
4786 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4787 	} else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) {
4788 		/*
4789 		 * An older version of ZFS overwrote the sentinel value, so
4790 		 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4791 		 * destruction to later; see spa_sync_config_object.
4792 		 */
4793 		spa->spa_avz_action = AVZ_ACTION_DESTROY;
4794 		/*
4795 		 * We're assuming that no vdevs have had their ZAPs created
4796 		 * before this. Better be sure of it.
4797 		 */
4798 		ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
4799 	}
4800 	nvlist_free(mos_config);
4801 
4802 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
4803 
4804 	error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object,
4805 	    B_FALSE);
4806 	if (error && error != ENOENT)
4807 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4808 
4809 	if (error == 0) {
4810 		uint64_t autoreplace = 0;
4811 
4812 		spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
4813 		spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
4814 		spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
4815 		spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
4816 		spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
4817 		spa_prop_find(spa, ZPOOL_PROP_DEDUP_TABLE_QUOTA,
4818 		    &spa->spa_dedup_table_quota);
4819 		spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost);
4820 		spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim);
4821 		spa->spa_autoreplace = (autoreplace != 0);
4822 	}
4823 
4824 	/*
4825 	 * If we are importing a pool with missing top-level vdevs,
4826 	 * we enforce that the pool doesn't panic or get suspended on
4827 	 * error since the likelihood of missing data is extremely high.
4828 	 */
4829 	if (spa->spa_missing_tvds > 0 &&
4830 	    spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE &&
4831 	    spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
4832 		spa_load_note(spa, "forcing failmode to 'continue' "
4833 		    "as some top level vdevs are missing");
4834 		spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE;
4835 	}
4836 
4837 	return (0);
4838 }
4839 
4840 static int
4841 spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type)
4842 {
4843 	int error = 0;
4844 	vdev_t *rvd = spa->spa_root_vdev;
4845 
4846 	/*
4847 	 * If we're assembling the pool from the split-off vdevs of
4848 	 * an existing pool, we don't want to attach the spares & cache
4849 	 * devices.
4850 	 */
4851 
4852 	/*
4853 	 * Load any hot spares for this pool.
4854 	 */
4855 	error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object,
4856 	    B_FALSE);
4857 	if (error != 0 && error != ENOENT)
4858 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4859 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
4860 		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
4861 		if (load_nvlist(spa, spa->spa_spares.sav_object,
4862 		    &spa->spa_spares.sav_config) != 0) {
4863 			spa_load_failed(spa, "error loading spares nvlist");
4864 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4865 		}
4866 
4867 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4868 		spa_load_spares(spa);
4869 		spa_config_exit(spa, SCL_ALL, FTAG);
4870 	} else if (error == 0) {
4871 		spa->spa_spares.sav_sync = B_TRUE;
4872 	}
4873 
4874 	/*
4875 	 * Load any level 2 ARC devices for this pool.
4876 	 */
4877 	error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
4878 	    &spa->spa_l2cache.sav_object, B_FALSE);
4879 	if (error != 0 && error != ENOENT)
4880 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4881 	if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
4882 		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
4883 		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
4884 		    &spa->spa_l2cache.sav_config) != 0) {
4885 			spa_load_failed(spa, "error loading l2cache nvlist");
4886 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4887 		}
4888 
4889 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4890 		spa_load_l2cache(spa);
4891 		spa_config_exit(spa, SCL_ALL, FTAG);
4892 	} else if (error == 0) {
4893 		spa->spa_l2cache.sav_sync = B_TRUE;
4894 	}
4895 
4896 	return (0);
4897 }
4898 
4899 static int
4900 spa_ld_load_vdev_metadata(spa_t *spa)
4901 {
4902 	int error = 0;
4903 	vdev_t *rvd = spa->spa_root_vdev;
4904 
4905 	/*
4906 	 * If the 'multihost' property is set, then never allow a pool to
4907 	 * be imported when the system hostid is zero.  The exception to
4908 	 * this rule is zdb which is always allowed to access pools.
4909 	 */
4910 	if (spa_multihost(spa) && spa_get_hostid(spa) == 0 &&
4911 	    (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) {
4912 		fnvlist_add_uint64(spa->spa_load_info,
4913 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
4914 		return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
4915 	}
4916 
4917 	/*
4918 	 * If the 'autoreplace' property is set, then post a resource notifying
4919 	 * the ZFS DE that it should not issue any faults for unopenable
4920 	 * devices.  We also iterate over the vdevs, and post a sysevent for any
4921 	 * unopenable vdevs so that the normal autoreplace handler can take
4922 	 * over.
4923 	 */
4924 	if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
4925 		spa_check_removed(spa->spa_root_vdev);
4926 		/*
4927 		 * For the import case, this is done in spa_import(), because
4928 		 * at this point we're using the spare definitions from
4929 		 * the MOS config, not necessarily from the userland config.
4930 		 */
4931 		if (spa->spa_load_state != SPA_LOAD_IMPORT) {
4932 			spa_aux_check_removed(&spa->spa_spares);
4933 			spa_aux_check_removed(&spa->spa_l2cache);
4934 		}
4935 	}
4936 
4937 	/*
4938 	 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4939 	 */
4940 	error = vdev_load(rvd);
4941 	if (error != 0) {
4942 		spa_load_failed(spa, "vdev_load failed [error=%d]", error);
4943 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4944 	}
4945 
4946 	error = spa_ld_log_spacemaps(spa);
4947 	if (error != 0) {
4948 		spa_load_failed(spa, "spa_ld_log_spacemaps failed [error=%d]",
4949 		    error);
4950 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4951 	}
4952 
4953 	/*
4954 	 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4955 	 */
4956 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4957 	vdev_dtl_reassess(rvd, 0, 0, B_FALSE, B_FALSE);
4958 	spa_config_exit(spa, SCL_ALL, FTAG);
4959 
4960 	return (0);
4961 }
4962 
4963 static int
4964 spa_ld_load_dedup_tables(spa_t *spa)
4965 {
4966 	int error = 0;
4967 	vdev_t *rvd = spa->spa_root_vdev;
4968 
4969 	error = ddt_load(spa);
4970 	if (error != 0) {
4971 		spa_load_failed(spa, "ddt_load failed [error=%d]", error);
4972 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4973 	}
4974 
4975 	return (0);
4976 }
4977 
4978 static int
4979 spa_ld_load_brt(spa_t *spa)
4980 {
4981 	int error = 0;
4982 	vdev_t *rvd = spa->spa_root_vdev;
4983 
4984 	error = brt_load(spa);
4985 	if (error != 0) {
4986 		spa_load_failed(spa, "brt_load failed [error=%d]", error);
4987 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4988 	}
4989 
4990 	return (0);
4991 }
4992 
4993 static int
4994 spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, const char **ereport)
4995 {
4996 	vdev_t *rvd = spa->spa_root_vdev;
4997 
4998 	if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) {
4999 		boolean_t missing = spa_check_logs(spa);
5000 		if (missing) {
5001 			if (spa->spa_missing_tvds != 0) {
5002 				spa_load_note(spa, "spa_check_logs failed "
5003 				    "so dropping the logs");
5004 			} else {
5005 				*ereport = FM_EREPORT_ZFS_LOG_REPLAY;
5006 				spa_load_failed(spa, "spa_check_logs failed");
5007 				return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG,
5008 				    ENXIO));
5009 			}
5010 		}
5011 	}
5012 
5013 	return (0);
5014 }
5015 
5016 static int
5017 spa_ld_verify_pool_data(spa_t *spa)
5018 {
5019 	int error = 0;
5020 	vdev_t *rvd = spa->spa_root_vdev;
5021 
5022 	/*
5023 	 * We've successfully opened the pool, verify that we're ready
5024 	 * to start pushing transactions.
5025 	 */
5026 	if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
5027 		error = spa_load_verify(spa);
5028 		if (error != 0) {
5029 			spa_load_failed(spa, "spa_load_verify failed "
5030 			    "[error=%d]", error);
5031 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
5032 			    error));
5033 		}
5034 	}
5035 
5036 	return (0);
5037 }
5038 
5039 static void
5040 spa_ld_claim_log_blocks(spa_t *spa)
5041 {
5042 	dmu_tx_t *tx;
5043 	dsl_pool_t *dp = spa_get_dsl(spa);
5044 
5045 	/*
5046 	 * Claim log blocks that haven't been committed yet.
5047 	 * This must all happen in a single txg.
5048 	 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
5049 	 * invoked from zil_claim_log_block()'s i/o done callback.
5050 	 * Price of rollback is that we abandon the log.
5051 	 */
5052 	spa->spa_claiming = B_TRUE;
5053 
5054 	tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
5055 	(void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
5056 	    zil_claim, tx, DS_FIND_CHILDREN);
5057 	dmu_tx_commit(tx);
5058 
5059 	spa->spa_claiming = B_FALSE;
5060 
5061 	spa_set_log_state(spa, SPA_LOG_GOOD);
5062 }
5063 
5064 static void
5065 spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg,
5066     boolean_t update_config_cache)
5067 {
5068 	vdev_t *rvd = spa->spa_root_vdev;
5069 	int need_update = B_FALSE;
5070 
5071 	/*
5072 	 * If the config cache is stale, or we have uninitialized
5073 	 * metaslabs (see spa_vdev_add()), then update the config.
5074 	 *
5075 	 * If this is a verbatim import, trust the current
5076 	 * in-core spa_config and update the disk labels.
5077 	 */
5078 	if (update_config_cache || config_cache_txg != spa->spa_config_txg ||
5079 	    spa->spa_load_state == SPA_LOAD_IMPORT ||
5080 	    spa->spa_load_state == SPA_LOAD_RECOVER ||
5081 	    (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
5082 		need_update = B_TRUE;
5083 
5084 	for (int c = 0; c < rvd->vdev_children; c++)
5085 		if (rvd->vdev_child[c]->vdev_ms_array == 0)
5086 			need_update = B_TRUE;
5087 
5088 	/*
5089 	 * Update the config cache asynchronously in case we're the
5090 	 * root pool, in which case the config cache isn't writable yet.
5091 	 */
5092 	if (need_update)
5093 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
5094 }
5095 
5096 static void
5097 spa_ld_prepare_for_reload(spa_t *spa)
5098 {
5099 	spa_mode_t mode = spa->spa_mode;
5100 	int async_suspended = spa->spa_async_suspended;
5101 
5102 	spa_unload(spa);
5103 	spa_deactivate(spa);
5104 	spa_activate(spa, mode);
5105 
5106 	/*
5107 	 * We save the value of spa_async_suspended as it gets reset to 0 by
5108 	 * spa_unload(). We want to restore it back to the original value before
5109 	 * returning as we might be calling spa_async_resume() later.
5110 	 */
5111 	spa->spa_async_suspended = async_suspended;
5112 }
5113 
5114 static int
5115 spa_ld_read_checkpoint_txg(spa_t *spa)
5116 {
5117 	uberblock_t checkpoint;
5118 	int error = 0;
5119 
5120 	ASSERT0(spa->spa_checkpoint_txg);
5121 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
5122 	    spa->spa_load_thread == curthread);
5123 
5124 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
5125 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
5126 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
5127 
5128 	if (error == ENOENT)
5129 		return (0);
5130 
5131 	if (error != 0)
5132 		return (error);
5133 
5134 	ASSERT3U(checkpoint.ub_txg, !=, 0);
5135 	ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0);
5136 	ASSERT3U(checkpoint.ub_timestamp, !=, 0);
5137 	spa->spa_checkpoint_txg = checkpoint.ub_txg;
5138 	spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp;
5139 
5140 	return (0);
5141 }
5142 
5143 static int
5144 spa_ld_mos_init(spa_t *spa, spa_import_type_t type)
5145 {
5146 	int error = 0;
5147 
5148 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
5149 	ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE);
5150 
5151 	/*
5152 	 * Never trust the config that is provided unless we are assembling
5153 	 * a pool following a split.
5154 	 * This means don't trust blkptrs and the vdev tree in general. This
5155 	 * also effectively puts the spa in read-only mode since
5156 	 * spa_writeable() checks for spa_trust_config to be true.
5157 	 * We will later load a trusted config from the MOS.
5158 	 */
5159 	if (type != SPA_IMPORT_ASSEMBLE)
5160 		spa->spa_trust_config = B_FALSE;
5161 
5162 	/*
5163 	 * Parse the config provided to create a vdev tree.
5164 	 */
5165 	error = spa_ld_parse_config(spa, type);
5166 	if (error != 0)
5167 		return (error);
5168 
5169 	spa_import_progress_add(spa);
5170 
5171 	/*
5172 	 * Now that we have the vdev tree, try to open each vdev. This involves
5173 	 * opening the underlying physical device, retrieving its geometry and
5174 	 * probing the vdev with a dummy I/O. The state of each vdev will be set
5175 	 * based on the success of those operations. After this we'll be ready
5176 	 * to read from the vdevs.
5177 	 */
5178 	error = spa_ld_open_vdevs(spa);
5179 	if (error != 0)
5180 		return (error);
5181 
5182 	/*
5183 	 * Read the label of each vdev and make sure that the GUIDs stored
5184 	 * there match the GUIDs in the config provided.
5185 	 * If we're assembling a new pool that's been split off from an
5186 	 * existing pool, the labels haven't yet been updated so we skip
5187 	 * validation for now.
5188 	 */
5189 	if (type != SPA_IMPORT_ASSEMBLE) {
5190 		error = spa_ld_validate_vdevs(spa);
5191 		if (error != 0)
5192 			return (error);
5193 	}
5194 
5195 	/*
5196 	 * Read all vdev labels to find the best uberblock (i.e. latest,
5197 	 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
5198 	 * get the list of features required to read blkptrs in the MOS from
5199 	 * the vdev label with the best uberblock and verify that our version
5200 	 * of zfs supports them all.
5201 	 */
5202 	error = spa_ld_select_uberblock(spa, type);
5203 	if (error != 0)
5204 		return (error);
5205 
5206 	/*
5207 	 * Pass that uberblock to the dsl_pool layer which will open the root
5208 	 * blkptr. This blkptr points to the latest version of the MOS and will
5209 	 * allow us to read its contents.
5210 	 */
5211 	error = spa_ld_open_rootbp(spa);
5212 	if (error != 0)
5213 		return (error);
5214 
5215 	return (0);
5216 }
5217 
5218 static int
5219 spa_ld_checkpoint_rewind(spa_t *spa)
5220 {
5221 	uberblock_t checkpoint;
5222 	int error = 0;
5223 
5224 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
5225 	ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
5226 
5227 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
5228 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
5229 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
5230 
5231 	if (error != 0) {
5232 		spa_load_failed(spa, "unable to retrieve checkpointed "
5233 		    "uberblock from the MOS config [error=%d]", error);
5234 
5235 		if (error == ENOENT)
5236 			error = ZFS_ERR_NO_CHECKPOINT;
5237 
5238 		return (error);
5239 	}
5240 
5241 	ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg);
5242 	ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg);
5243 
5244 	/*
5245 	 * We need to update the txg and timestamp of the checkpointed
5246 	 * uberblock to be higher than the latest one. This ensures that
5247 	 * the checkpointed uberblock is selected if we were to close and
5248 	 * reopen the pool right after we've written it in the vdev labels.
5249 	 * (also see block comment in vdev_uberblock_compare)
5250 	 */
5251 	checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1;
5252 	checkpoint.ub_timestamp = gethrestime_sec();
5253 
5254 	/*
5255 	 * Set current uberblock to be the checkpointed uberblock.
5256 	 */
5257 	spa->spa_uberblock = checkpoint;
5258 
5259 	/*
5260 	 * If we are doing a normal rewind, then the pool is open for
5261 	 * writing and we sync the "updated" checkpointed uberblock to
5262 	 * disk. Once this is done, we've basically rewound the whole
5263 	 * pool and there is no way back.
5264 	 *
5265 	 * There are cases when we don't want to attempt and sync the
5266 	 * checkpointed uberblock to disk because we are opening a
5267 	 * pool as read-only. Specifically, verifying the checkpointed
5268 	 * state with zdb, and importing the checkpointed state to get
5269 	 * a "preview" of its content.
5270 	 */
5271 	if (spa_writeable(spa)) {
5272 		vdev_t *rvd = spa->spa_root_vdev;
5273 
5274 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5275 		vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL };
5276 		int svdcount = 0;
5277 		int children = rvd->vdev_children;
5278 		int c0 = random_in_range(children);
5279 
5280 		for (int c = 0; c < children; c++) {
5281 			vdev_t *vd = rvd->vdev_child[(c0 + c) % children];
5282 
5283 			/* Stop when revisiting the first vdev */
5284 			if (c > 0 && svd[0] == vd)
5285 				break;
5286 
5287 			if (vd->vdev_ms_array == 0 || vd->vdev_islog ||
5288 			    !vdev_is_concrete(vd))
5289 				continue;
5290 
5291 			svd[svdcount++] = vd;
5292 			if (svdcount == SPA_SYNC_MIN_VDEVS)
5293 				break;
5294 		}
5295 		error = vdev_config_sync(svd, svdcount, spa->spa_first_txg);
5296 		if (error == 0)
5297 			spa->spa_last_synced_guid = rvd->vdev_guid;
5298 		spa_config_exit(spa, SCL_ALL, FTAG);
5299 
5300 		if (error != 0) {
5301 			spa_load_failed(spa, "failed to write checkpointed "
5302 			    "uberblock to the vdev labels [error=%d]", error);
5303 			return (error);
5304 		}
5305 	}
5306 
5307 	return (0);
5308 }
5309 
5310 static int
5311 spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type,
5312     boolean_t *update_config_cache)
5313 {
5314 	int error;
5315 
5316 	/*
5317 	 * Parse the config for pool, open and validate vdevs,
5318 	 * select an uberblock, and use that uberblock to open
5319 	 * the MOS.
5320 	 */
5321 	error = spa_ld_mos_init(spa, type);
5322 	if (error != 0)
5323 		return (error);
5324 
5325 	/*
5326 	 * Retrieve the trusted config stored in the MOS and use it to create
5327 	 * a new, exact version of the vdev tree, then reopen all vdevs.
5328 	 */
5329 	error = spa_ld_trusted_config(spa, type, B_FALSE);
5330 	if (error == EAGAIN) {
5331 		if (update_config_cache != NULL)
5332 			*update_config_cache = B_TRUE;
5333 
5334 		/*
5335 		 * Redo the loading process with the trusted config if it is
5336 		 * too different from the untrusted config.
5337 		 */
5338 		spa_ld_prepare_for_reload(spa);
5339 		spa_load_note(spa, "RELOADING");
5340 		error = spa_ld_mos_init(spa, type);
5341 		if (error != 0)
5342 			return (error);
5343 
5344 		error = spa_ld_trusted_config(spa, type, B_TRUE);
5345 		if (error != 0)
5346 			return (error);
5347 
5348 	} else if (error != 0) {
5349 		return (error);
5350 	}
5351 
5352 	return (0);
5353 }
5354 
5355 /*
5356  * Load an existing storage pool, using the config provided. This config
5357  * describes which vdevs are part of the pool and is later validated against
5358  * partial configs present in each vdev's label and an entire copy of the
5359  * config stored in the MOS.
5360  */
5361 static int
5362 spa_load_impl(spa_t *spa, spa_import_type_t type, const char **ereport)
5363 {
5364 	int error = 0;
5365 	boolean_t missing_feat_write = B_FALSE;
5366 	boolean_t checkpoint_rewind =
5367 	    (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
5368 	boolean_t update_config_cache = B_FALSE;
5369 	hrtime_t load_start = gethrtime();
5370 
5371 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
5372 	ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE);
5373 
5374 	spa_load_note(spa, "LOADING");
5375 
5376 	error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache);
5377 	if (error != 0)
5378 		return (error);
5379 
5380 	/*
5381 	 * If we are rewinding to the checkpoint then we need to repeat
5382 	 * everything we've done so far in this function but this time
5383 	 * selecting the checkpointed uberblock and using that to open
5384 	 * the MOS.
5385 	 */
5386 	if (checkpoint_rewind) {
5387 		/*
5388 		 * If we are rewinding to the checkpoint update config cache
5389 		 * anyway.
5390 		 */
5391 		update_config_cache = B_TRUE;
5392 
5393 		/*
5394 		 * Extract the checkpointed uberblock from the current MOS
5395 		 * and use this as the pool's uberblock from now on. If the
5396 		 * pool is imported as writeable we also write the checkpoint
5397 		 * uberblock to the labels, making the rewind permanent.
5398 		 */
5399 		error = spa_ld_checkpoint_rewind(spa);
5400 		if (error != 0)
5401 			return (error);
5402 
5403 		/*
5404 		 * Redo the loading process again with the
5405 		 * checkpointed uberblock.
5406 		 */
5407 		spa_ld_prepare_for_reload(spa);
5408 		spa_load_note(spa, "LOADING checkpointed uberblock");
5409 		error = spa_ld_mos_with_trusted_config(spa, type, NULL);
5410 		if (error != 0)
5411 			return (error);
5412 	}
5413 
5414 	/*
5415 	 * Drop the namespace lock for the rest of the function.
5416 	 */
5417 	spa->spa_load_thread = curthread;
5418 	mutex_exit(&spa_namespace_lock);
5419 
5420 	/*
5421 	 * Retrieve the checkpoint txg if the pool has a checkpoint.
5422 	 */
5423 	spa_import_progress_set_notes(spa, "Loading checkpoint txg");
5424 	error = spa_ld_read_checkpoint_txg(spa);
5425 	if (error != 0)
5426 		goto fail;
5427 
5428 	/*
5429 	 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
5430 	 * from the pool and their contents were re-mapped to other vdevs. Note
5431 	 * that everything that we read before this step must have been
5432 	 * rewritten on concrete vdevs after the last device removal was
5433 	 * initiated. Otherwise we could be reading from indirect vdevs before
5434 	 * we have loaded their mappings.
5435 	 */
5436 	spa_import_progress_set_notes(spa, "Loading indirect vdev metadata");
5437 	error = spa_ld_open_indirect_vdev_metadata(spa);
5438 	if (error != 0)
5439 		goto fail;
5440 
5441 	/*
5442 	 * Retrieve the full list of active features from the MOS and check if
5443 	 * they are all supported.
5444 	 */
5445 	spa_import_progress_set_notes(spa, "Checking feature flags");
5446 	error = spa_ld_check_features(spa, &missing_feat_write);
5447 	if (error != 0)
5448 		goto fail;
5449 
5450 	/*
5451 	 * Load several special directories from the MOS needed by the dsl_pool
5452 	 * layer.
5453 	 */
5454 	spa_import_progress_set_notes(spa, "Loading special MOS directories");
5455 	error = spa_ld_load_special_directories(spa);
5456 	if (error != 0)
5457 		goto fail;
5458 
5459 	/*
5460 	 * Retrieve pool properties from the MOS.
5461 	 */
5462 	spa_import_progress_set_notes(spa, "Loading properties");
5463 	error = spa_ld_get_props(spa);
5464 	if (error != 0)
5465 		goto fail;
5466 
5467 	/*
5468 	 * Retrieve the list of auxiliary devices - cache devices and spares -
5469 	 * and open them.
5470 	 */
5471 	spa_import_progress_set_notes(spa, "Loading AUX vdevs");
5472 	error = spa_ld_open_aux_vdevs(spa, type);
5473 	if (error != 0)
5474 		goto fail;
5475 
5476 	/*
5477 	 * Load the metadata for all vdevs. Also check if unopenable devices
5478 	 * should be autoreplaced.
5479 	 */
5480 	spa_import_progress_set_notes(spa, "Loading vdev metadata");
5481 	error = spa_ld_load_vdev_metadata(spa);
5482 	if (error != 0)
5483 		goto fail;
5484 
5485 	spa_import_progress_set_notes(spa, "Loading dedup tables");
5486 	error = spa_ld_load_dedup_tables(spa);
5487 	if (error != 0)
5488 		goto fail;
5489 
5490 	spa_import_progress_set_notes(spa, "Loading BRT");
5491 	error = spa_ld_load_brt(spa);
5492 	if (error != 0)
5493 		goto fail;
5494 
5495 	/*
5496 	 * Verify the logs now to make sure we don't have any unexpected errors
5497 	 * when we claim log blocks later.
5498 	 */
5499 	spa_import_progress_set_notes(spa, "Verifying Log Devices");
5500 	error = spa_ld_verify_logs(spa, type, ereport);
5501 	if (error != 0)
5502 		goto fail;
5503 
5504 	if (missing_feat_write) {
5505 		ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT);
5506 
5507 		/*
5508 		 * At this point, we know that we can open the pool in
5509 		 * read-only mode but not read-write mode. We now have enough
5510 		 * information and can return to userland.
5511 		 */
5512 		error = spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT,
5513 		    ENOTSUP);
5514 		goto fail;
5515 	}
5516 
5517 	/*
5518 	 * Traverse the last txgs to make sure the pool was left off in a safe
5519 	 * state. When performing an extreme rewind, we verify the whole pool,
5520 	 * which can take a very long time.
5521 	 */
5522 	spa_import_progress_set_notes(spa, "Verifying pool data");
5523 	error = spa_ld_verify_pool_data(spa);
5524 	if (error != 0)
5525 		goto fail;
5526 
5527 	/*
5528 	 * Calculate the deflated space for the pool. This must be done before
5529 	 * we write anything to the pool because we'd need to update the space
5530 	 * accounting using the deflated sizes.
5531 	 */
5532 	spa_import_progress_set_notes(spa, "Calculating deflated space");
5533 	spa_update_dspace(spa);
5534 
5535 	/*
5536 	 * We have now retrieved all the information we needed to open the
5537 	 * pool. If we are importing the pool in read-write mode, a few
5538 	 * additional steps must be performed to finish the import.
5539 	 */
5540 	spa_import_progress_set_notes(spa, "Starting import");
5541 	if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER ||
5542 	    spa->spa_load_max_txg == UINT64_MAX)) {
5543 		uint64_t config_cache_txg = spa->spa_config_txg;
5544 
5545 		ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT);
5546 
5547 		/*
5548 		 * Before we do any zio_write's, complete the raidz expansion
5549 		 * scratch space copying, if necessary.
5550 		 */
5551 		if (RRSS_GET_STATE(&spa->spa_uberblock) == RRSS_SCRATCH_VALID)
5552 			vdev_raidz_reflow_copy_scratch(spa);
5553 
5554 		/*
5555 		 * In case of a checkpoint rewind, log the original txg
5556 		 * of the checkpointed uberblock.
5557 		 */
5558 		if (checkpoint_rewind) {
5559 			spa_history_log_internal(spa, "checkpoint rewind",
5560 			    NULL, "rewound state to txg=%llu",
5561 			    (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg);
5562 		}
5563 
5564 		spa_import_progress_set_notes(spa, "Claiming ZIL blocks");
5565 		/*
5566 		 * Traverse the ZIL and claim all blocks.
5567 		 */
5568 		spa_ld_claim_log_blocks(spa);
5569 
5570 		/*
5571 		 * Kick-off the syncing thread.
5572 		 */
5573 		spa->spa_sync_on = B_TRUE;
5574 		txg_sync_start(spa->spa_dsl_pool);
5575 		mmp_thread_start(spa);
5576 
5577 		/*
5578 		 * Wait for all claims to sync.  We sync up to the highest
5579 		 * claimed log block birth time so that claimed log blocks
5580 		 * don't appear to be from the future.  spa_claim_max_txg
5581 		 * will have been set for us by ZIL traversal operations
5582 		 * performed above.
5583 		 */
5584 		spa_import_progress_set_notes(spa, "Syncing ZIL claims");
5585 		txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
5586 
5587 		/*
5588 		 * Check if we need to request an update of the config. On the
5589 		 * next sync, we would update the config stored in vdev labels
5590 		 * and the cachefile (by default /etc/zfs/zpool.cache).
5591 		 */
5592 		spa_import_progress_set_notes(spa, "Updating configs");
5593 		spa_ld_check_for_config_update(spa, config_cache_txg,
5594 		    update_config_cache);
5595 
5596 		/*
5597 		 * Check if a rebuild was in progress and if so resume it.
5598 		 * Then check all DTLs to see if anything needs resilvering.
5599 		 * The resilver will be deferred if a rebuild was started.
5600 		 */
5601 		spa_import_progress_set_notes(spa, "Starting resilvers");
5602 		if (vdev_rebuild_active(spa->spa_root_vdev)) {
5603 			vdev_rebuild_restart(spa);
5604 		} else if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
5605 		    vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5606 			spa_async_request(spa, SPA_ASYNC_RESILVER);
5607 		}
5608 
5609 		/*
5610 		 * Log the fact that we booted up (so that we can detect if
5611 		 * we rebooted in the middle of an operation).
5612 		 */
5613 		spa_history_log_version(spa, "open", NULL);
5614 
5615 		spa_import_progress_set_notes(spa,
5616 		    "Restarting device removals");
5617 		spa_restart_removal(spa);
5618 		spa_spawn_aux_threads(spa);
5619 
5620 		/*
5621 		 * Delete any inconsistent datasets.
5622 		 *
5623 		 * Note:
5624 		 * Since we may be issuing deletes for clones here,
5625 		 * we make sure to do so after we've spawned all the
5626 		 * auxiliary threads above (from which the livelist
5627 		 * deletion zthr is part of).
5628 		 */
5629 		spa_import_progress_set_notes(spa,
5630 		    "Cleaning up inconsistent objsets");
5631 		(void) dmu_objset_find(spa_name(spa),
5632 		    dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
5633 
5634 		/*
5635 		 * Clean up any stale temporary dataset userrefs.
5636 		 */
5637 		spa_import_progress_set_notes(spa,
5638 		    "Cleaning up temporary userrefs");
5639 		dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
5640 
5641 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5642 		spa_import_progress_set_notes(spa, "Restarting initialize");
5643 		vdev_initialize_restart(spa->spa_root_vdev);
5644 		spa_import_progress_set_notes(spa, "Restarting TRIM");
5645 		vdev_trim_restart(spa->spa_root_vdev);
5646 		vdev_autotrim_restart(spa);
5647 		spa_config_exit(spa, SCL_CONFIG, FTAG);
5648 		spa_import_progress_set_notes(spa, "Finished importing");
5649 	}
5650 	zio_handle_import_delay(spa, gethrtime() - load_start);
5651 
5652 	spa_import_progress_remove(spa_guid(spa));
5653 	spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
5654 
5655 	spa_load_note(spa, "LOADED");
5656 fail:
5657 	mutex_enter(&spa_namespace_lock);
5658 	spa->spa_load_thread = NULL;
5659 	cv_broadcast(&spa_namespace_cv);
5660 
5661 	return (error);
5662 
5663 }
5664 
5665 static int
5666 spa_load_retry(spa_t *spa, spa_load_state_t state)
5667 {
5668 	spa_mode_t mode = spa->spa_mode;
5669 
5670 	spa_unload(spa);
5671 	spa_deactivate(spa);
5672 
5673 	spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
5674 
5675 	spa_activate(spa, mode);
5676 	spa_async_suspend(spa);
5677 
5678 	spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu",
5679 	    (u_longlong_t)spa->spa_load_max_txg);
5680 
5681 	return (spa_load(spa, state, SPA_IMPORT_EXISTING));
5682 }
5683 
5684 /*
5685  * If spa_load() fails this function will try loading prior txg's. If
5686  * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5687  * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5688  * function will not rewind the pool and will return the same error as
5689  * spa_load().
5690  */
5691 static int
5692 spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request,
5693     int rewind_flags)
5694 {
5695 	nvlist_t *loadinfo = NULL;
5696 	nvlist_t *config = NULL;
5697 	int load_error, rewind_error;
5698 	uint64_t safe_rewind_txg;
5699 	uint64_t min_txg;
5700 
5701 	if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
5702 		spa->spa_load_max_txg = spa->spa_load_txg;
5703 		spa_set_log_state(spa, SPA_LOG_CLEAR);
5704 	} else {
5705 		spa->spa_load_max_txg = max_request;
5706 		if (max_request != UINT64_MAX)
5707 			spa->spa_extreme_rewind = B_TRUE;
5708 	}
5709 
5710 	load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING);
5711 	if (load_error == 0)
5712 		return (0);
5713 	if (load_error == ZFS_ERR_NO_CHECKPOINT) {
5714 		/*
5715 		 * When attempting checkpoint-rewind on a pool with no
5716 		 * checkpoint, we should not attempt to load uberblocks
5717 		 * from previous txgs when spa_load fails.
5718 		 */
5719 		ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
5720 		spa_import_progress_remove(spa_guid(spa));
5721 		return (load_error);
5722 	}
5723 
5724 	if (spa->spa_root_vdev != NULL)
5725 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
5726 
5727 	spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
5728 	spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
5729 
5730 	if (rewind_flags & ZPOOL_NEVER_REWIND) {
5731 		nvlist_free(config);
5732 		spa_import_progress_remove(spa_guid(spa));
5733 		return (load_error);
5734 	}
5735 
5736 	if (state == SPA_LOAD_RECOVER) {
5737 		/* Price of rolling back is discarding txgs, including log */
5738 		spa_set_log_state(spa, SPA_LOG_CLEAR);
5739 	} else {
5740 		/*
5741 		 * If we aren't rolling back save the load info from our first
5742 		 * import attempt so that we can restore it after attempting
5743 		 * to rewind.
5744 		 */
5745 		loadinfo = spa->spa_load_info;
5746 		spa->spa_load_info = fnvlist_alloc();
5747 	}
5748 
5749 	spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
5750 	safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
5751 	min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
5752 	    TXG_INITIAL : safe_rewind_txg;
5753 
5754 	/*
5755 	 * Continue as long as we're finding errors, we're still within
5756 	 * the acceptable rewind range, and we're still finding uberblocks
5757 	 */
5758 	while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
5759 	    spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
5760 		if (spa->spa_load_max_txg < safe_rewind_txg)
5761 			spa->spa_extreme_rewind = B_TRUE;
5762 		rewind_error = spa_load_retry(spa, state);
5763 	}
5764 
5765 	spa->spa_extreme_rewind = B_FALSE;
5766 	spa->spa_load_max_txg = UINT64_MAX;
5767 
5768 	if (config && (rewind_error || state != SPA_LOAD_RECOVER))
5769 		spa_config_set(spa, config);
5770 	else
5771 		nvlist_free(config);
5772 
5773 	if (state == SPA_LOAD_RECOVER) {
5774 		ASSERT3P(loadinfo, ==, NULL);
5775 		spa_import_progress_remove(spa_guid(spa));
5776 		return (rewind_error);
5777 	} else {
5778 		/* Store the rewind info as part of the initial load info */
5779 		fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
5780 		    spa->spa_load_info);
5781 
5782 		/* Restore the initial load info */
5783 		fnvlist_free(spa->spa_load_info);
5784 		spa->spa_load_info = loadinfo;
5785 
5786 		spa_import_progress_remove(spa_guid(spa));
5787 		return (load_error);
5788 	}
5789 }
5790 
5791 /*
5792  * Pool Open/Import
5793  *
5794  * The import case is identical to an open except that the configuration is sent
5795  * down from userland, instead of grabbed from the configuration cache.  For the
5796  * case of an open, the pool configuration will exist in the
5797  * POOL_STATE_UNINITIALIZED state.
5798  *
5799  * The stats information (gen/count/ustats) is used to gather vdev statistics at
5800  * the same time open the pool, without having to keep around the spa_t in some
5801  * ambiguous state.
5802  */
5803 static int
5804 spa_open_common(const char *pool, spa_t **spapp, const void *tag,
5805     nvlist_t *nvpolicy, nvlist_t **config)
5806 {
5807 	spa_t *spa;
5808 	spa_load_state_t state = SPA_LOAD_OPEN;
5809 	int error;
5810 	int locked = B_FALSE;
5811 	int firstopen = B_FALSE;
5812 
5813 	*spapp = NULL;
5814 
5815 	/*
5816 	 * As disgusting as this is, we need to support recursive calls to this
5817 	 * function because dsl_dir_open() is called during spa_load(), and ends
5818 	 * up calling spa_open() again.  The real fix is to figure out how to
5819 	 * avoid dsl_dir_open() calling this in the first place.
5820 	 */
5821 	if (MUTEX_NOT_HELD(&spa_namespace_lock)) {
5822 		mutex_enter(&spa_namespace_lock);
5823 		locked = B_TRUE;
5824 	}
5825 
5826 	if ((spa = spa_lookup(pool)) == NULL) {
5827 		if (locked)
5828 			mutex_exit(&spa_namespace_lock);
5829 		return (SET_ERROR(ENOENT));
5830 	}
5831 
5832 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
5833 		zpool_load_policy_t policy;
5834 
5835 		firstopen = B_TRUE;
5836 
5837 		zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config,
5838 		    &policy);
5839 		if (policy.zlp_rewind & ZPOOL_DO_REWIND)
5840 			state = SPA_LOAD_RECOVER;
5841 
5842 		spa_activate(spa, spa_mode_global);
5843 
5844 		if (state != SPA_LOAD_RECOVER)
5845 			spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
5846 		spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE;
5847 
5848 		zfs_dbgmsg("spa_open_common: opening %s", pool);
5849 		error = spa_load_best(spa, state, policy.zlp_txg,
5850 		    policy.zlp_rewind);
5851 
5852 		if (error == EBADF) {
5853 			/*
5854 			 * If vdev_validate() returns failure (indicated by
5855 			 * EBADF), it indicates that one of the vdevs indicates
5856 			 * that the pool has been exported or destroyed.  If
5857 			 * this is the case, the config cache is out of sync and
5858 			 * we should remove the pool from the namespace.
5859 			 */
5860 			spa_unload(spa);
5861 			spa_deactivate(spa);
5862 			spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE);
5863 			spa_remove(spa);
5864 			if (locked)
5865 				mutex_exit(&spa_namespace_lock);
5866 			return (SET_ERROR(ENOENT));
5867 		}
5868 
5869 		if (error) {
5870 			/*
5871 			 * We can't open the pool, but we still have useful
5872 			 * information: the state of each vdev after the
5873 			 * attempted vdev_open().  Return this to the user.
5874 			 */
5875 			if (config != NULL && spa->spa_config) {
5876 				*config = fnvlist_dup(spa->spa_config);
5877 				fnvlist_add_nvlist(*config,
5878 				    ZPOOL_CONFIG_LOAD_INFO,
5879 				    spa->spa_load_info);
5880 			}
5881 			spa_unload(spa);
5882 			spa_deactivate(spa);
5883 			spa->spa_last_open_failed = error;
5884 			if (locked)
5885 				mutex_exit(&spa_namespace_lock);
5886 			*spapp = NULL;
5887 			return (error);
5888 		}
5889 	}
5890 
5891 	spa_open_ref(spa, tag);
5892 
5893 	if (config != NULL)
5894 		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
5895 
5896 	/*
5897 	 * If we've recovered the pool, pass back any information we
5898 	 * gathered while doing the load.
5899 	 */
5900 	if (state == SPA_LOAD_RECOVER && config != NULL) {
5901 		fnvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
5902 		    spa->spa_load_info);
5903 	}
5904 
5905 	if (locked) {
5906 		spa->spa_last_open_failed = 0;
5907 		spa->spa_last_ubsync_txg = 0;
5908 		spa->spa_load_txg = 0;
5909 		mutex_exit(&spa_namespace_lock);
5910 	}
5911 
5912 	if (firstopen)
5913 		zvol_create_minors_recursive(spa_name(spa));
5914 
5915 	*spapp = spa;
5916 
5917 	return (0);
5918 }
5919 
5920 int
5921 spa_open_rewind(const char *name, spa_t **spapp, const void *tag,
5922     nvlist_t *policy, nvlist_t **config)
5923 {
5924 	return (spa_open_common(name, spapp, tag, policy, config));
5925 }
5926 
5927 int
5928 spa_open(const char *name, spa_t **spapp, const void *tag)
5929 {
5930 	return (spa_open_common(name, spapp, tag, NULL, NULL));
5931 }
5932 
5933 /*
5934  * Lookup the given spa_t, incrementing the inject count in the process,
5935  * preventing it from being exported or destroyed.
5936  */
5937 spa_t *
5938 spa_inject_addref(char *name)
5939 {
5940 	spa_t *spa;
5941 
5942 	mutex_enter(&spa_namespace_lock);
5943 	if ((spa = spa_lookup(name)) == NULL) {
5944 		mutex_exit(&spa_namespace_lock);
5945 		return (NULL);
5946 	}
5947 	spa->spa_inject_ref++;
5948 	mutex_exit(&spa_namespace_lock);
5949 
5950 	return (spa);
5951 }
5952 
5953 void
5954 spa_inject_delref(spa_t *spa)
5955 {
5956 	mutex_enter(&spa_namespace_lock);
5957 	spa->spa_inject_ref--;
5958 	mutex_exit(&spa_namespace_lock);
5959 }
5960 
5961 /*
5962  * Add spares device information to the nvlist.
5963  */
5964 static void
5965 spa_add_spares(spa_t *spa, nvlist_t *config)
5966 {
5967 	nvlist_t **spares;
5968 	uint_t i, nspares;
5969 	nvlist_t *nvroot;
5970 	uint64_t guid;
5971 	vdev_stat_t *vs;
5972 	uint_t vsc;
5973 	uint64_t pool;
5974 
5975 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
5976 
5977 	if (spa->spa_spares.sav_count == 0)
5978 		return;
5979 
5980 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
5981 	VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5982 	    ZPOOL_CONFIG_SPARES, &spares, &nspares));
5983 	if (nspares != 0) {
5984 		fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
5985 		    (const nvlist_t * const *)spares, nspares);
5986 		VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
5987 		    &spares, &nspares));
5988 
5989 		/*
5990 		 * Go through and find any spares which have since been
5991 		 * repurposed as an active spare.  If this is the case, update
5992 		 * their status appropriately.
5993 		 */
5994 		for (i = 0; i < nspares; i++) {
5995 			guid = fnvlist_lookup_uint64(spares[i],
5996 			    ZPOOL_CONFIG_GUID);
5997 			VERIFY0(nvlist_lookup_uint64_array(spares[i],
5998 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc));
5999 			if (spa_spare_exists(guid, &pool, NULL) &&
6000 			    pool != 0ULL) {
6001 				vs->vs_state = VDEV_STATE_CANT_OPEN;
6002 				vs->vs_aux = VDEV_AUX_SPARED;
6003 			} else {
6004 				vs->vs_state =
6005 				    spa->spa_spares.sav_vdevs[i]->vdev_state;
6006 			}
6007 		}
6008 	}
6009 }
6010 
6011 /*
6012  * Add l2cache device information to the nvlist, including vdev stats.
6013  */
6014 static void
6015 spa_add_l2cache(spa_t *spa, nvlist_t *config)
6016 {
6017 	nvlist_t **l2cache;
6018 	uint_t i, j, nl2cache;
6019 	nvlist_t *nvroot;
6020 	uint64_t guid;
6021 	vdev_t *vd;
6022 	vdev_stat_t *vs;
6023 	uint_t vsc;
6024 
6025 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
6026 
6027 	if (spa->spa_l2cache.sav_count == 0)
6028 		return;
6029 
6030 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6031 	VERIFY0(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
6032 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache));
6033 	if (nl2cache != 0) {
6034 		fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6035 		    (const nvlist_t * const *)l2cache, nl2cache);
6036 		VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6037 		    &l2cache, &nl2cache));
6038 
6039 		/*
6040 		 * Update level 2 cache device stats.
6041 		 */
6042 
6043 		for (i = 0; i < nl2cache; i++) {
6044 			guid = fnvlist_lookup_uint64(l2cache[i],
6045 			    ZPOOL_CONFIG_GUID);
6046 
6047 			vd = NULL;
6048 			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
6049 				if (guid ==
6050 				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
6051 					vd = spa->spa_l2cache.sav_vdevs[j];
6052 					break;
6053 				}
6054 			}
6055 			ASSERT(vd != NULL);
6056 
6057 			VERIFY0(nvlist_lookup_uint64_array(l2cache[i],
6058 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc));
6059 			vdev_get_stats(vd, vs);
6060 			vdev_config_generate_stats(vd, l2cache[i]);
6061 
6062 		}
6063 	}
6064 }
6065 
6066 static void
6067 spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features)
6068 {
6069 	zap_cursor_t zc;
6070 	zap_attribute_t *za = zap_attribute_alloc();
6071 
6072 	if (spa->spa_feat_for_read_obj != 0) {
6073 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
6074 		    spa->spa_feat_for_read_obj);
6075 		    zap_cursor_retrieve(&zc, za) == 0;
6076 		    zap_cursor_advance(&zc)) {
6077 			ASSERT(za->za_integer_length == sizeof (uint64_t) &&
6078 			    za->za_num_integers == 1);
6079 			VERIFY0(nvlist_add_uint64(features, za->za_name,
6080 			    za->za_first_integer));
6081 		}
6082 		zap_cursor_fini(&zc);
6083 	}
6084 
6085 	if (spa->spa_feat_for_write_obj != 0) {
6086 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
6087 		    spa->spa_feat_for_write_obj);
6088 		    zap_cursor_retrieve(&zc, za) == 0;
6089 		    zap_cursor_advance(&zc)) {
6090 			ASSERT(za->za_integer_length == sizeof (uint64_t) &&
6091 			    za->za_num_integers == 1);
6092 			VERIFY0(nvlist_add_uint64(features, za->za_name,
6093 			    za->za_first_integer));
6094 		}
6095 		zap_cursor_fini(&zc);
6096 	}
6097 	zap_attribute_free(za);
6098 }
6099 
6100 static void
6101 spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features)
6102 {
6103 	int i;
6104 
6105 	for (i = 0; i < SPA_FEATURES; i++) {
6106 		zfeature_info_t feature = spa_feature_table[i];
6107 		uint64_t refcount;
6108 
6109 		if (feature_get_refcount(spa, &feature, &refcount) != 0)
6110 			continue;
6111 
6112 		VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount));
6113 	}
6114 }
6115 
6116 /*
6117  * Store a list of pool features and their reference counts in the
6118  * config.
6119  *
6120  * The first time this is called on a spa, allocate a new nvlist, fetch
6121  * the pool features and reference counts from disk, then save the list
6122  * in the spa. In subsequent calls on the same spa use the saved nvlist
6123  * and refresh its values from the cached reference counts.  This
6124  * ensures we don't block here on I/O on a suspended pool so 'zpool
6125  * clear' can resume the pool.
6126  */
6127 static void
6128 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
6129 {
6130 	nvlist_t *features;
6131 
6132 	ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
6133 
6134 	mutex_enter(&spa->spa_feat_stats_lock);
6135 	features = spa->spa_feat_stats;
6136 
6137 	if (features != NULL) {
6138 		spa_feature_stats_from_cache(spa, features);
6139 	} else {
6140 		VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP));
6141 		spa->spa_feat_stats = features;
6142 		spa_feature_stats_from_disk(spa, features);
6143 	}
6144 
6145 	VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
6146 	    features));
6147 
6148 	mutex_exit(&spa->spa_feat_stats_lock);
6149 }
6150 
6151 int
6152 spa_get_stats(const char *name, nvlist_t **config,
6153     char *altroot, size_t buflen)
6154 {
6155 	int error;
6156 	spa_t *spa;
6157 
6158 	*config = NULL;
6159 	error = spa_open_common(name, &spa, FTAG, NULL, config);
6160 
6161 	if (spa != NULL) {
6162 		/*
6163 		 * This still leaves a window of inconsistency where the spares
6164 		 * or l2cache devices could change and the config would be
6165 		 * self-inconsistent.
6166 		 */
6167 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6168 
6169 		if (*config != NULL) {
6170 			uint64_t loadtimes[2];
6171 
6172 			loadtimes[0] = spa->spa_loaded_ts.tv_sec;
6173 			loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
6174 			fnvlist_add_uint64_array(*config,
6175 			    ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2);
6176 
6177 			fnvlist_add_uint64(*config,
6178 			    ZPOOL_CONFIG_ERRCOUNT,
6179 			    spa_approx_errlog_size(spa));
6180 
6181 			if (spa_suspended(spa)) {
6182 				fnvlist_add_uint64(*config,
6183 				    ZPOOL_CONFIG_SUSPENDED,
6184 				    spa->spa_failmode);
6185 				fnvlist_add_uint64(*config,
6186 				    ZPOOL_CONFIG_SUSPENDED_REASON,
6187 				    spa->spa_suspended);
6188 			}
6189 
6190 			spa_add_spares(spa, *config);
6191 			spa_add_l2cache(spa, *config);
6192 			spa_add_feature_stats(spa, *config);
6193 		}
6194 	}
6195 
6196 	/*
6197 	 * We want to get the alternate root even for faulted pools, so we cheat
6198 	 * and call spa_lookup() directly.
6199 	 */
6200 	if (altroot) {
6201 		if (spa == NULL) {
6202 			mutex_enter(&spa_namespace_lock);
6203 			spa = spa_lookup(name);
6204 			if (spa)
6205 				spa_altroot(spa, altroot, buflen);
6206 			else
6207 				altroot[0] = '\0';
6208 			spa = NULL;
6209 			mutex_exit(&spa_namespace_lock);
6210 		} else {
6211 			spa_altroot(spa, altroot, buflen);
6212 		}
6213 	}
6214 
6215 	if (spa != NULL) {
6216 		spa_config_exit(spa, SCL_CONFIG, FTAG);
6217 		spa_close(spa, FTAG);
6218 	}
6219 
6220 	return (error);
6221 }
6222 
6223 /*
6224  * Validate that the auxiliary device array is well formed.  We must have an
6225  * array of nvlists, each which describes a valid leaf vdev.  If this is an
6226  * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
6227  * specified, as long as they are well-formed.
6228  */
6229 static int
6230 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
6231     spa_aux_vdev_t *sav, const char *config, uint64_t version,
6232     vdev_labeltype_t label)
6233 {
6234 	nvlist_t **dev;
6235 	uint_t i, ndev;
6236 	vdev_t *vd;
6237 	int error;
6238 
6239 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
6240 
6241 	/*
6242 	 * It's acceptable to have no devs specified.
6243 	 */
6244 	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
6245 		return (0);
6246 
6247 	if (ndev == 0)
6248 		return (SET_ERROR(EINVAL));
6249 
6250 	/*
6251 	 * Make sure the pool is formatted with a version that supports this
6252 	 * device type.
6253 	 */
6254 	if (spa_version(spa) < version)
6255 		return (SET_ERROR(ENOTSUP));
6256 
6257 	/*
6258 	 * Set the pending device list so we correctly handle device in-use
6259 	 * checking.
6260 	 */
6261 	sav->sav_pending = dev;
6262 	sav->sav_npending = ndev;
6263 
6264 	for (i = 0; i < ndev; i++) {
6265 		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
6266 		    mode)) != 0)
6267 			goto out;
6268 
6269 		if (!vd->vdev_ops->vdev_op_leaf) {
6270 			vdev_free(vd);
6271 			error = SET_ERROR(EINVAL);
6272 			goto out;
6273 		}
6274 
6275 		vd->vdev_top = vd;
6276 
6277 		if ((error = vdev_open(vd)) == 0 &&
6278 		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
6279 			fnvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
6280 			    vd->vdev_guid);
6281 		}
6282 
6283 		vdev_free(vd);
6284 
6285 		if (error &&
6286 		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
6287 			goto out;
6288 		else
6289 			error = 0;
6290 	}
6291 
6292 out:
6293 	sav->sav_pending = NULL;
6294 	sav->sav_npending = 0;
6295 	return (error);
6296 }
6297 
6298 static int
6299 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
6300 {
6301 	int error;
6302 
6303 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
6304 
6305 	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
6306 	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
6307 	    VDEV_LABEL_SPARE)) != 0) {
6308 		return (error);
6309 	}
6310 
6311 	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
6312 	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
6313 	    VDEV_LABEL_L2CACHE));
6314 }
6315 
6316 static void
6317 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
6318     const char *config)
6319 {
6320 	int i;
6321 
6322 	if (sav->sav_config != NULL) {
6323 		nvlist_t **olddevs;
6324 		uint_t oldndevs;
6325 		nvlist_t **newdevs;
6326 
6327 		/*
6328 		 * Generate new dev list by concatenating with the
6329 		 * current dev list.
6330 		 */
6331 		VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, config,
6332 		    &olddevs, &oldndevs));
6333 
6334 		newdevs = kmem_alloc(sizeof (void *) *
6335 		    (ndevs + oldndevs), KM_SLEEP);
6336 		for (i = 0; i < oldndevs; i++)
6337 			newdevs[i] = fnvlist_dup(olddevs[i]);
6338 		for (i = 0; i < ndevs; i++)
6339 			newdevs[i + oldndevs] = fnvlist_dup(devs[i]);
6340 
6341 		fnvlist_remove(sav->sav_config, config);
6342 
6343 		fnvlist_add_nvlist_array(sav->sav_config, config,
6344 		    (const nvlist_t * const *)newdevs, ndevs + oldndevs);
6345 		for (i = 0; i < oldndevs + ndevs; i++)
6346 			nvlist_free(newdevs[i]);
6347 		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
6348 	} else {
6349 		/*
6350 		 * Generate a new dev list.
6351 		 */
6352 		sav->sav_config = fnvlist_alloc();
6353 		fnvlist_add_nvlist_array(sav->sav_config, config,
6354 		    (const nvlist_t * const *)devs, ndevs);
6355 	}
6356 }
6357 
6358 /*
6359  * Stop and drop level 2 ARC devices
6360  */
6361 void
6362 spa_l2cache_drop(spa_t *spa)
6363 {
6364 	vdev_t *vd;
6365 	int i;
6366 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
6367 
6368 	for (i = 0; i < sav->sav_count; i++) {
6369 		uint64_t pool;
6370 
6371 		vd = sav->sav_vdevs[i];
6372 		ASSERT(vd != NULL);
6373 
6374 		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
6375 		    pool != 0ULL && l2arc_vdev_present(vd))
6376 			l2arc_remove_vdev(vd);
6377 	}
6378 }
6379 
6380 /*
6381  * Verify encryption parameters for spa creation. If we are encrypting, we must
6382  * have the encryption feature flag enabled.
6383  */
6384 static int
6385 spa_create_check_encryption_params(dsl_crypto_params_t *dcp,
6386     boolean_t has_encryption)
6387 {
6388 	if (dcp->cp_crypt != ZIO_CRYPT_OFF &&
6389 	    dcp->cp_crypt != ZIO_CRYPT_INHERIT &&
6390 	    !has_encryption)
6391 		return (SET_ERROR(ENOTSUP));
6392 
6393 	return (dmu_objset_create_crypt_check(NULL, dcp, NULL));
6394 }
6395 
6396 /*
6397  * Pool Creation
6398  */
6399 int
6400 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
6401     nvlist_t *zplprops, dsl_crypto_params_t *dcp)
6402 {
6403 	spa_t *spa;
6404 	const char *altroot = NULL;
6405 	vdev_t *rvd;
6406 	dsl_pool_t *dp;
6407 	dmu_tx_t *tx;
6408 	int error = 0;
6409 	uint64_t txg = TXG_INITIAL;
6410 	nvlist_t **spares, **l2cache;
6411 	uint_t nspares, nl2cache;
6412 	uint64_t version, obj, ndraid = 0;
6413 	boolean_t has_features;
6414 	boolean_t has_encryption;
6415 	boolean_t has_allocclass;
6416 	spa_feature_t feat;
6417 	const char *feat_name;
6418 	const char *poolname;
6419 	nvlist_t *nvl;
6420 
6421 	if (props == NULL ||
6422 	    nvlist_lookup_string(props,
6423 	    zpool_prop_to_name(ZPOOL_PROP_TNAME), &poolname) != 0)
6424 		poolname = (char *)pool;
6425 
6426 	/*
6427 	 * If this pool already exists, return failure.
6428 	 */
6429 	mutex_enter(&spa_namespace_lock);
6430 	if (spa_lookup(poolname) != NULL) {
6431 		mutex_exit(&spa_namespace_lock);
6432 		return (SET_ERROR(EEXIST));
6433 	}
6434 
6435 	/*
6436 	 * Allocate a new spa_t structure.
6437 	 */
6438 	nvl = fnvlist_alloc();
6439 	fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool);
6440 	(void) nvlist_lookup_string(props,
6441 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
6442 	spa = spa_add(poolname, nvl, altroot);
6443 	fnvlist_free(nvl);
6444 	spa_activate(spa, spa_mode_global);
6445 
6446 	if (props && (error = spa_prop_validate(spa, props))) {
6447 		spa_deactivate(spa);
6448 		spa_remove(spa);
6449 		mutex_exit(&spa_namespace_lock);
6450 		return (error);
6451 	}
6452 
6453 	/*
6454 	 * Temporary pool names should never be written to disk.
6455 	 */
6456 	if (poolname != pool)
6457 		spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME;
6458 
6459 	has_features = B_FALSE;
6460 	has_encryption = B_FALSE;
6461 	has_allocclass = B_FALSE;
6462 	for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
6463 	    elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
6464 		if (zpool_prop_feature(nvpair_name(elem))) {
6465 			has_features = B_TRUE;
6466 
6467 			feat_name = strchr(nvpair_name(elem), '@') + 1;
6468 			VERIFY0(zfeature_lookup_name(feat_name, &feat));
6469 			if (feat == SPA_FEATURE_ENCRYPTION)
6470 				has_encryption = B_TRUE;
6471 			if (feat == SPA_FEATURE_ALLOCATION_CLASSES)
6472 				has_allocclass = B_TRUE;
6473 		}
6474 	}
6475 
6476 	/* verify encryption params, if they were provided */
6477 	if (dcp != NULL) {
6478 		error = spa_create_check_encryption_params(dcp, has_encryption);
6479 		if (error != 0) {
6480 			spa_deactivate(spa);
6481 			spa_remove(spa);
6482 			mutex_exit(&spa_namespace_lock);
6483 			return (error);
6484 		}
6485 	}
6486 	if (!has_allocclass && zfs_special_devs(nvroot, NULL)) {
6487 		spa_deactivate(spa);
6488 		spa_remove(spa);
6489 		mutex_exit(&spa_namespace_lock);
6490 		return (ENOTSUP);
6491 	}
6492 
6493 	if (has_features || nvlist_lookup_uint64(props,
6494 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
6495 		version = SPA_VERSION;
6496 	}
6497 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6498 
6499 	spa->spa_first_txg = txg;
6500 	spa->spa_uberblock.ub_txg = txg - 1;
6501 	spa->spa_uberblock.ub_version = version;
6502 	spa->spa_ubsync = spa->spa_uberblock;
6503 	spa->spa_load_state = SPA_LOAD_CREATE;
6504 	spa->spa_removing_phys.sr_state = DSS_NONE;
6505 	spa->spa_removing_phys.sr_removing_vdev = -1;
6506 	spa->spa_removing_phys.sr_prev_indirect_vdev = -1;
6507 	spa->spa_indirect_vdevs_loaded = B_TRUE;
6508 
6509 	/*
6510 	 * Create "The Godfather" zio to hold all async IOs
6511 	 */
6512 	spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
6513 	    KM_SLEEP);
6514 	for (int i = 0; i < max_ncpus; i++) {
6515 		spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
6516 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
6517 		    ZIO_FLAG_GODFATHER);
6518 	}
6519 
6520 	/*
6521 	 * Create the root vdev.
6522 	 */
6523 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6524 
6525 	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
6526 
6527 	ASSERT(error != 0 || rvd != NULL);
6528 	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
6529 
6530 	if (error == 0 && !zfs_allocatable_devs(nvroot))
6531 		error = SET_ERROR(EINVAL);
6532 
6533 	if (error == 0 &&
6534 	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
6535 	    (error = vdev_draid_spare_create(nvroot, rvd, &ndraid, 0)) == 0 &&
6536 	    (error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) == 0) {
6537 		/*
6538 		 * instantiate the metaslab groups (this will dirty the vdevs)
6539 		 * we can no longer error exit past this point
6540 		 */
6541 		for (int c = 0; error == 0 && c < rvd->vdev_children; c++) {
6542 			vdev_t *vd = rvd->vdev_child[c];
6543 
6544 			vdev_metaslab_set_size(vd);
6545 			vdev_expand(vd, txg);
6546 		}
6547 	}
6548 
6549 	spa_config_exit(spa, SCL_ALL, FTAG);
6550 
6551 	if (error != 0) {
6552 		spa_unload(spa);
6553 		spa_deactivate(spa);
6554 		spa_remove(spa);
6555 		mutex_exit(&spa_namespace_lock);
6556 		return (error);
6557 	}
6558 
6559 	/*
6560 	 * Get the list of spares, if specified.
6561 	 */
6562 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6563 	    &spares, &nspares) == 0) {
6564 		spa->spa_spares.sav_config = fnvlist_alloc();
6565 		fnvlist_add_nvlist_array(spa->spa_spares.sav_config,
6566 		    ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares,
6567 		    nspares);
6568 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6569 		spa_load_spares(spa);
6570 		spa_config_exit(spa, SCL_ALL, FTAG);
6571 		spa->spa_spares.sav_sync = B_TRUE;
6572 	}
6573 
6574 	/*
6575 	 * Get the list of level 2 cache devices, if specified.
6576 	 */
6577 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6578 	    &l2cache, &nl2cache) == 0) {
6579 		VERIFY0(nvlist_alloc(&spa->spa_l2cache.sav_config,
6580 		    NV_UNIQUE_NAME, KM_SLEEP));
6581 		fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
6582 		    ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache,
6583 		    nl2cache);
6584 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6585 		spa_load_l2cache(spa);
6586 		spa_config_exit(spa, SCL_ALL, FTAG);
6587 		spa->spa_l2cache.sav_sync = B_TRUE;
6588 	}
6589 
6590 	spa->spa_is_initializing = B_TRUE;
6591 	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg);
6592 	spa->spa_is_initializing = B_FALSE;
6593 
6594 	/*
6595 	 * Create DDTs (dedup tables).
6596 	 */
6597 	ddt_create(spa);
6598 	/*
6599 	 * Create BRT table and BRT table object.
6600 	 */
6601 	brt_create(spa);
6602 
6603 	spa_update_dspace(spa);
6604 
6605 	tx = dmu_tx_create_assigned(dp, txg);
6606 
6607 	/*
6608 	 * Create the pool's history object.
6609 	 */
6610 	if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history)
6611 		spa_history_create_obj(spa, tx);
6612 
6613 	spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE);
6614 	spa_history_log_version(spa, "create", tx);
6615 
6616 	/*
6617 	 * Create the pool config object.
6618 	 */
6619 	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
6620 	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
6621 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
6622 
6623 	if (zap_add(spa->spa_meta_objset,
6624 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
6625 	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
6626 		cmn_err(CE_PANIC, "failed to add pool config");
6627 	}
6628 
6629 	if (zap_add(spa->spa_meta_objset,
6630 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
6631 	    sizeof (uint64_t), 1, &version, tx) != 0) {
6632 		cmn_err(CE_PANIC, "failed to add pool version");
6633 	}
6634 
6635 	/* Newly created pools with the right version are always deflated. */
6636 	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
6637 		spa->spa_deflate = TRUE;
6638 		if (zap_add(spa->spa_meta_objset,
6639 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6640 		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
6641 			cmn_err(CE_PANIC, "failed to add deflate");
6642 		}
6643 	}
6644 
6645 	/*
6646 	 * Create the deferred-free bpobj.  Turn off compression
6647 	 * because sync-to-convergence takes longer if the blocksize
6648 	 * keeps changing.
6649 	 */
6650 	obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
6651 	dmu_object_set_compress(spa->spa_meta_objset, obj,
6652 	    ZIO_COMPRESS_OFF, tx);
6653 	if (zap_add(spa->spa_meta_objset,
6654 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
6655 	    sizeof (uint64_t), 1, &obj, tx) != 0) {
6656 		cmn_err(CE_PANIC, "failed to add bpobj");
6657 	}
6658 	VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
6659 	    spa->spa_meta_objset, obj));
6660 
6661 	/*
6662 	 * Generate some random noise for salted checksums to operate on.
6663 	 */
6664 	(void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
6665 	    sizeof (spa->spa_cksum_salt.zcs_bytes));
6666 
6667 	/*
6668 	 * Set pool properties.
6669 	 */
6670 	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
6671 	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
6672 	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
6673 	spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
6674 	spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST);
6675 	spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM);
6676 	spa->spa_dedup_table_quota =
6677 	    zpool_prop_default_numeric(ZPOOL_PROP_DEDUP_TABLE_QUOTA);
6678 
6679 	if (props != NULL) {
6680 		spa_configfile_set(spa, props, B_FALSE);
6681 		spa_sync_props(props, tx);
6682 	}
6683 
6684 	for (int i = 0; i < ndraid; i++)
6685 		spa_feature_incr(spa, SPA_FEATURE_DRAID, tx);
6686 
6687 	dmu_tx_commit(tx);
6688 
6689 	spa->spa_sync_on = B_TRUE;
6690 	txg_sync_start(dp);
6691 	mmp_thread_start(spa);
6692 	txg_wait_synced(dp, txg);
6693 
6694 	spa_spawn_aux_threads(spa);
6695 
6696 	spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE);
6697 
6698 	/*
6699 	 * Don't count references from objsets that are already closed
6700 	 * and are making their way through the eviction process.
6701 	 */
6702 	spa_evicting_os_wait(spa);
6703 	spa->spa_minref = zfs_refcount_count(&spa->spa_refcount);
6704 	spa->spa_load_state = SPA_LOAD_NONE;
6705 
6706 	spa_import_os(spa);
6707 
6708 	mutex_exit(&spa_namespace_lock);
6709 
6710 	return (0);
6711 }
6712 
6713 /*
6714  * Import a non-root pool into the system.
6715  */
6716 int
6717 spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
6718 {
6719 	spa_t *spa;
6720 	const char *altroot = NULL;
6721 	spa_load_state_t state = SPA_LOAD_IMPORT;
6722 	zpool_load_policy_t policy;
6723 	spa_mode_t mode = spa_mode_global;
6724 	uint64_t readonly = B_FALSE;
6725 	int error;
6726 	nvlist_t *nvroot;
6727 	nvlist_t **spares, **l2cache;
6728 	uint_t nspares, nl2cache;
6729 
6730 	/*
6731 	 * If a pool with this name exists, return failure.
6732 	 */
6733 	mutex_enter(&spa_namespace_lock);
6734 	if (spa_lookup(pool) != NULL) {
6735 		mutex_exit(&spa_namespace_lock);
6736 		return (SET_ERROR(EEXIST));
6737 	}
6738 
6739 	/*
6740 	 * Create and initialize the spa structure.
6741 	 */
6742 	(void) nvlist_lookup_string(props,
6743 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
6744 	(void) nvlist_lookup_uint64(props,
6745 	    zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
6746 	if (readonly)
6747 		mode = SPA_MODE_READ;
6748 	spa = spa_add(pool, config, altroot);
6749 	spa->spa_import_flags = flags;
6750 
6751 	/*
6752 	 * Verbatim import - Take a pool and insert it into the namespace
6753 	 * as if it had been loaded at boot.
6754 	 */
6755 	if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
6756 		if (props != NULL)
6757 			spa_configfile_set(spa, props, B_FALSE);
6758 
6759 		spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
6760 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
6761 		zfs_dbgmsg("spa_import: verbatim import of %s", pool);
6762 		mutex_exit(&spa_namespace_lock);
6763 		return (0);
6764 	}
6765 
6766 	spa_activate(spa, mode);
6767 
6768 	/*
6769 	 * Don't start async tasks until we know everything is healthy.
6770 	 */
6771 	spa_async_suspend(spa);
6772 
6773 	zpool_get_load_policy(config, &policy);
6774 	if (policy.zlp_rewind & ZPOOL_DO_REWIND)
6775 		state = SPA_LOAD_RECOVER;
6776 
6777 	spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT;
6778 
6779 	if (state != SPA_LOAD_RECOVER) {
6780 		spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
6781 		zfs_dbgmsg("spa_import: importing %s", pool);
6782 	} else {
6783 		zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6784 		    "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg);
6785 	}
6786 	error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind);
6787 
6788 	/*
6789 	 * Propagate anything learned while loading the pool and pass it
6790 	 * back to caller (i.e. rewind info, missing devices, etc).
6791 	 */
6792 	fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, spa->spa_load_info);
6793 
6794 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6795 	/*
6796 	 * Toss any existing sparelist, as it doesn't have any validity
6797 	 * anymore, and conflicts with spa_has_spare().
6798 	 */
6799 	if (spa->spa_spares.sav_config) {
6800 		nvlist_free(spa->spa_spares.sav_config);
6801 		spa->spa_spares.sav_config = NULL;
6802 		spa_load_spares(spa);
6803 	}
6804 	if (spa->spa_l2cache.sav_config) {
6805 		nvlist_free(spa->spa_l2cache.sav_config);
6806 		spa->spa_l2cache.sav_config = NULL;
6807 		spa_load_l2cache(spa);
6808 	}
6809 
6810 	nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
6811 	spa_config_exit(spa, SCL_ALL, FTAG);
6812 
6813 	if (props != NULL)
6814 		spa_configfile_set(spa, props, B_FALSE);
6815 
6816 	if (error != 0 || (props && spa_writeable(spa) &&
6817 	    (error = spa_prop_set(spa, props)))) {
6818 		spa_unload(spa);
6819 		spa_deactivate(spa);
6820 		spa_remove(spa);
6821 		mutex_exit(&spa_namespace_lock);
6822 		return (error);
6823 	}
6824 
6825 	spa_async_resume(spa);
6826 
6827 	/*
6828 	 * Override any spares and level 2 cache devices as specified by
6829 	 * the user, as these may have correct device names/devids, etc.
6830 	 */
6831 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6832 	    &spares, &nspares) == 0) {
6833 		if (spa->spa_spares.sav_config)
6834 			fnvlist_remove(spa->spa_spares.sav_config,
6835 			    ZPOOL_CONFIG_SPARES);
6836 		else
6837 			spa->spa_spares.sav_config = fnvlist_alloc();
6838 		fnvlist_add_nvlist_array(spa->spa_spares.sav_config,
6839 		    ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares,
6840 		    nspares);
6841 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6842 		spa_load_spares(spa);
6843 		spa_config_exit(spa, SCL_ALL, FTAG);
6844 		spa->spa_spares.sav_sync = B_TRUE;
6845 		spa->spa_spares.sav_label_sync = B_TRUE;
6846 	}
6847 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6848 	    &l2cache, &nl2cache) == 0) {
6849 		if (spa->spa_l2cache.sav_config)
6850 			fnvlist_remove(spa->spa_l2cache.sav_config,
6851 			    ZPOOL_CONFIG_L2CACHE);
6852 		else
6853 			spa->spa_l2cache.sav_config = fnvlist_alloc();
6854 		fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
6855 		    ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache,
6856 		    nl2cache);
6857 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6858 		spa_load_l2cache(spa);
6859 		spa_config_exit(spa, SCL_ALL, FTAG);
6860 		spa->spa_l2cache.sav_sync = B_TRUE;
6861 		spa->spa_l2cache.sav_label_sync = B_TRUE;
6862 	}
6863 
6864 	/*
6865 	 * Check for any removed devices.
6866 	 */
6867 	if (spa->spa_autoreplace) {
6868 		spa_aux_check_removed(&spa->spa_spares);
6869 		spa_aux_check_removed(&spa->spa_l2cache);
6870 	}
6871 
6872 	if (spa_writeable(spa)) {
6873 		/*
6874 		 * Update the config cache to include the newly-imported pool.
6875 		 */
6876 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
6877 	}
6878 
6879 	/*
6880 	 * It's possible that the pool was expanded while it was exported.
6881 	 * We kick off an async task to handle this for us.
6882 	 */
6883 	spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
6884 
6885 	spa_history_log_version(spa, "import", NULL);
6886 
6887 	spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
6888 
6889 	mutex_exit(&spa_namespace_lock);
6890 
6891 	zvol_create_minors_recursive(pool);
6892 
6893 	spa_import_os(spa);
6894 
6895 	return (0);
6896 }
6897 
6898 nvlist_t *
6899 spa_tryimport(nvlist_t *tryconfig)
6900 {
6901 	nvlist_t *config = NULL;
6902 	const char *poolname, *cachefile;
6903 	spa_t *spa;
6904 	uint64_t state;
6905 	int error;
6906 	zpool_load_policy_t policy;
6907 
6908 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
6909 		return (NULL);
6910 
6911 	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
6912 		return (NULL);
6913 
6914 	/*
6915 	 * Create and initialize the spa structure.
6916 	 */
6917 	char *name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6918 	(void) snprintf(name, MAXPATHLEN, "%s-%llx-%s",
6919 	    TRYIMPORT_NAME, (u_longlong_t)(uintptr_t)curthread, poolname);
6920 
6921 	mutex_enter(&spa_namespace_lock);
6922 	spa = spa_add(name, tryconfig, NULL);
6923 	spa_activate(spa, SPA_MODE_READ);
6924 	kmem_free(name, MAXPATHLEN);
6925 
6926 	/*
6927 	 * Rewind pool if a max txg was provided.
6928 	 */
6929 	zpool_get_load_policy(spa->spa_config, &policy);
6930 	if (policy.zlp_txg != UINT64_MAX) {
6931 		spa->spa_load_max_txg = policy.zlp_txg;
6932 		spa->spa_extreme_rewind = B_TRUE;
6933 		zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6934 		    poolname, (longlong_t)policy.zlp_txg);
6935 	} else {
6936 		zfs_dbgmsg("spa_tryimport: importing %s", poolname);
6937 	}
6938 
6939 	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile)
6940 	    == 0) {
6941 		zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile);
6942 		spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE;
6943 	} else {
6944 		spa->spa_config_source = SPA_CONFIG_SRC_SCAN;
6945 	}
6946 
6947 	/*
6948 	 * spa_import() relies on a pool config fetched by spa_try_import()
6949 	 * for spare/cache devices. Import flags are not passed to
6950 	 * spa_tryimport(), which makes it return early due to a missing log
6951 	 * device and missing retrieving the cache device and spare eventually.
6952 	 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6953 	 * the correct configuration regardless of the missing log device.
6954 	 */
6955 	spa->spa_import_flags |= ZFS_IMPORT_MISSING_LOG;
6956 
6957 	error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING);
6958 
6959 	/*
6960 	 * If 'tryconfig' was at least parsable, return the current config.
6961 	 */
6962 	if (spa->spa_root_vdev != NULL) {
6963 		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
6964 		fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, poolname);
6965 		fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, state);
6966 		fnvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
6967 		    spa->spa_uberblock.ub_timestamp);
6968 		fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
6969 		    spa->spa_load_info);
6970 		fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA,
6971 		    spa->spa_errata);
6972 
6973 		/*
6974 		 * If the bootfs property exists on this pool then we
6975 		 * copy it out so that external consumers can tell which
6976 		 * pools are bootable.
6977 		 */
6978 		if ((!error || error == EEXIST) && spa->spa_bootfs) {
6979 			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6980 
6981 			/*
6982 			 * We have to play games with the name since the
6983 			 * pool was opened as TRYIMPORT_NAME.
6984 			 */
6985 			if (dsl_dsobj_to_dsname(spa_name(spa),
6986 			    spa->spa_bootfs, tmpname) == 0) {
6987 				char *cp;
6988 				char *dsname;
6989 
6990 				dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6991 
6992 				cp = strchr(tmpname, '/');
6993 				if (cp == NULL) {
6994 					(void) strlcpy(dsname, tmpname,
6995 					    MAXPATHLEN);
6996 				} else {
6997 					(void) snprintf(dsname, MAXPATHLEN,
6998 					    "%s/%s", poolname, ++cp);
6999 				}
7000 				fnvlist_add_string(config, ZPOOL_CONFIG_BOOTFS,
7001 				    dsname);
7002 				kmem_free(dsname, MAXPATHLEN);
7003 			}
7004 			kmem_free(tmpname, MAXPATHLEN);
7005 		}
7006 
7007 		/*
7008 		 * Add the list of hot spares and level 2 cache devices.
7009 		 */
7010 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
7011 		spa_add_spares(spa, config);
7012 		spa_add_l2cache(spa, config);
7013 		spa_config_exit(spa, SCL_CONFIG, FTAG);
7014 	}
7015 
7016 	spa_unload(spa);
7017 	spa_deactivate(spa);
7018 	spa_remove(spa);
7019 	mutex_exit(&spa_namespace_lock);
7020 
7021 	return (config);
7022 }
7023 
7024 /*
7025  * Pool export/destroy
7026  *
7027  * The act of destroying or exporting a pool is very simple.  We make sure there
7028  * is no more pending I/O and any references to the pool are gone.  Then, we
7029  * update the pool state and sync all the labels to disk, removing the
7030  * configuration from the cache afterwards. If the 'hardforce' flag is set, then
7031  * we don't sync the labels or remove the configuration cache.
7032  */
7033 static int
7034 spa_export_common(const char *pool, int new_state, nvlist_t **oldconfig,
7035     boolean_t force, boolean_t hardforce)
7036 {
7037 	int error = 0;
7038 	spa_t *spa;
7039 	hrtime_t export_start = gethrtime();
7040 
7041 	if (oldconfig)
7042 		*oldconfig = NULL;
7043 
7044 	if (!(spa_mode_global & SPA_MODE_WRITE))
7045 		return (SET_ERROR(EROFS));
7046 
7047 	mutex_enter(&spa_namespace_lock);
7048 	if ((spa = spa_lookup(pool)) == NULL) {
7049 		mutex_exit(&spa_namespace_lock);
7050 		return (SET_ERROR(ENOENT));
7051 	}
7052 
7053 	if (spa->spa_is_exporting) {
7054 		/* the pool is being exported by another thread */
7055 		mutex_exit(&spa_namespace_lock);
7056 		return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS));
7057 	}
7058 	spa->spa_is_exporting = B_TRUE;
7059 
7060 	/*
7061 	 * Put a hold on the pool, drop the namespace lock, stop async tasks
7062 	 * and see if we can export.
7063 	 */
7064 	spa_open_ref(spa, FTAG);
7065 	mutex_exit(&spa_namespace_lock);
7066 	spa_async_suspend(spa);
7067 	if (spa->spa_zvol_taskq) {
7068 		zvol_remove_minors(spa, spa_name(spa), B_TRUE);
7069 		taskq_wait(spa->spa_zvol_taskq);
7070 	}
7071 	mutex_enter(&spa_namespace_lock);
7072 	spa->spa_export_thread = curthread;
7073 	spa_close(spa, FTAG);
7074 
7075 	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
7076 		mutex_exit(&spa_namespace_lock);
7077 		goto export_spa;
7078 	}
7079 
7080 	/*
7081 	 * The pool will be in core if it's openable, in which case we can
7082 	 * modify its state.  Objsets may be open only because they're dirty,
7083 	 * so we have to force it to sync before checking spa_refcnt.
7084 	 */
7085 	if (spa->spa_sync_on) {
7086 		txg_wait_synced(spa->spa_dsl_pool, 0);
7087 		spa_evicting_os_wait(spa);
7088 	}
7089 
7090 	/*
7091 	 * A pool cannot be exported or destroyed if there are active
7092 	 * references.  If we are resetting a pool, allow references by
7093 	 * fault injection handlers.
7094 	 */
7095 	if (!spa_refcount_zero(spa) || (spa->spa_inject_ref != 0)) {
7096 		error = SET_ERROR(EBUSY);
7097 		goto fail;
7098 	}
7099 
7100 	mutex_exit(&spa_namespace_lock);
7101 	/*
7102 	 * At this point we no longer hold the spa_namespace_lock and
7103 	 * there were no references on the spa. Future spa_lookups will
7104 	 * notice the spa->spa_export_thread and wait until we signal
7105 	 * that we are finshed.
7106 	 */
7107 
7108 	if (spa->spa_sync_on) {
7109 		vdev_t *rvd = spa->spa_root_vdev;
7110 		/*
7111 		 * A pool cannot be exported if it has an active shared spare.
7112 		 * This is to prevent other pools stealing the active spare
7113 		 * from an exported pool. At user's own will, such pool can
7114 		 * be forcedly exported.
7115 		 */
7116 		if (!force && new_state == POOL_STATE_EXPORTED &&
7117 		    spa_has_active_shared_spare(spa)) {
7118 			error = SET_ERROR(EXDEV);
7119 			mutex_enter(&spa_namespace_lock);
7120 			goto fail;
7121 		}
7122 
7123 		/*
7124 		 * We're about to export or destroy this pool. Make sure
7125 		 * we stop all initialization and trim activity here before
7126 		 * we set the spa_final_txg. This will ensure that all
7127 		 * dirty data resulting from the initialization is
7128 		 * committed to disk before we unload the pool.
7129 		 */
7130 		vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE);
7131 		vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE);
7132 		vdev_autotrim_stop_all(spa);
7133 		vdev_rebuild_stop_all(spa);
7134 		l2arc_spa_rebuild_stop(spa);
7135 
7136 		/*
7137 		 * We want this to be reflected on every label,
7138 		 * so mark them all dirty.  spa_unload() will do the
7139 		 * final sync that pushes these changes out.
7140 		 */
7141 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
7142 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7143 			spa->spa_state = new_state;
7144 			vdev_config_dirty(rvd);
7145 			spa_config_exit(spa, SCL_ALL, FTAG);
7146 		}
7147 
7148 		/*
7149 		 * If the log space map feature is enabled and the pool is
7150 		 * getting exported (but not destroyed), we want to spend some
7151 		 * time flushing as many metaslabs as we can in an attempt to
7152 		 * destroy log space maps and save import time. This has to be
7153 		 * done before we set the spa_final_txg, otherwise
7154 		 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
7155 		 * spa_should_flush_logs_on_unload() should be called after
7156 		 * spa_state has been set to the new_state.
7157 		 */
7158 		if (spa_should_flush_logs_on_unload(spa))
7159 			spa_unload_log_sm_flush_all(spa);
7160 
7161 		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
7162 			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7163 			spa->spa_final_txg = spa_last_synced_txg(spa) +
7164 			    TXG_DEFER_SIZE + 1;
7165 			spa_config_exit(spa, SCL_ALL, FTAG);
7166 		}
7167 	}
7168 
7169 export_spa:
7170 	spa_export_os(spa);
7171 
7172 	if (new_state == POOL_STATE_DESTROYED)
7173 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY);
7174 	else if (new_state == POOL_STATE_EXPORTED)
7175 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT);
7176 
7177 	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
7178 		spa_unload(spa);
7179 		spa_deactivate(spa);
7180 	}
7181 
7182 	if (oldconfig && spa->spa_config)
7183 		*oldconfig = fnvlist_dup(spa->spa_config);
7184 
7185 	if (new_state == POOL_STATE_EXPORTED)
7186 		zio_handle_export_delay(spa, gethrtime() - export_start);
7187 
7188 	/*
7189 	 * Take the namespace lock for the actual spa_t removal
7190 	 */
7191 	mutex_enter(&spa_namespace_lock);
7192 	if (new_state != POOL_STATE_UNINITIALIZED) {
7193 		if (!hardforce)
7194 			spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE);
7195 		spa_remove(spa);
7196 	} else {
7197 		/*
7198 		 * If spa_remove() is not called for this spa_t and
7199 		 * there is any possibility that it can be reused,
7200 		 * we make sure to reset the exporting flag.
7201 		 */
7202 		spa->spa_is_exporting = B_FALSE;
7203 		spa->spa_export_thread = NULL;
7204 	}
7205 
7206 	/*
7207 	 * Wake up any waiters in spa_lookup()
7208 	 */
7209 	cv_broadcast(&spa_namespace_cv);
7210 	mutex_exit(&spa_namespace_lock);
7211 	return (0);
7212 
7213 fail:
7214 	spa->spa_is_exporting = B_FALSE;
7215 	spa->spa_export_thread = NULL;
7216 
7217 	spa_async_resume(spa);
7218 	/*
7219 	 * Wake up any waiters in spa_lookup()
7220 	 */
7221 	cv_broadcast(&spa_namespace_cv);
7222 	mutex_exit(&spa_namespace_lock);
7223 	return (error);
7224 }
7225 
7226 /*
7227  * Destroy a storage pool.
7228  */
7229 int
7230 spa_destroy(const char *pool)
7231 {
7232 	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
7233 	    B_FALSE, B_FALSE));
7234 }
7235 
7236 /*
7237  * Export a storage pool.
7238  */
7239 int
7240 spa_export(const char *pool, nvlist_t **oldconfig, boolean_t force,
7241     boolean_t hardforce)
7242 {
7243 	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
7244 	    force, hardforce));
7245 }
7246 
7247 /*
7248  * Similar to spa_export(), this unloads the spa_t without actually removing it
7249  * from the namespace in any way.
7250  */
7251 int
7252 spa_reset(const char *pool)
7253 {
7254 	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
7255 	    B_FALSE, B_FALSE));
7256 }
7257 
7258 /*
7259  * ==========================================================================
7260  * Device manipulation
7261  * ==========================================================================
7262  */
7263 
7264 /*
7265  * This is called as a synctask to increment the draid feature flag
7266  */
7267 static void
7268 spa_draid_feature_incr(void *arg, dmu_tx_t *tx)
7269 {
7270 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7271 	int draid = (int)(uintptr_t)arg;
7272 
7273 	for (int c = 0; c < draid; c++)
7274 		spa_feature_incr(spa, SPA_FEATURE_DRAID, tx);
7275 }
7276 
7277 /*
7278  * Add a device to a storage pool.
7279  */
7280 int
7281 spa_vdev_add(spa_t *spa, nvlist_t *nvroot, boolean_t check_ashift)
7282 {
7283 	uint64_t txg, ndraid = 0;
7284 	int error;
7285 	vdev_t *rvd = spa->spa_root_vdev;
7286 	vdev_t *vd, *tvd;
7287 	nvlist_t **spares, **l2cache;
7288 	uint_t nspares, nl2cache;
7289 
7290 	ASSERT(spa_writeable(spa));
7291 
7292 	txg = spa_vdev_enter(spa);
7293 
7294 	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
7295 	    VDEV_ALLOC_ADD)) != 0)
7296 		return (spa_vdev_exit(spa, NULL, txg, error));
7297 
7298 	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
7299 
7300 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
7301 	    &nspares) != 0)
7302 		nspares = 0;
7303 
7304 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
7305 	    &nl2cache) != 0)
7306 		nl2cache = 0;
7307 
7308 	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
7309 		return (spa_vdev_exit(spa, vd, txg, EINVAL));
7310 
7311 	if (vd->vdev_children != 0 &&
7312 	    (error = vdev_create(vd, txg, B_FALSE)) != 0) {
7313 		return (spa_vdev_exit(spa, vd, txg, error));
7314 	}
7315 
7316 	/*
7317 	 * The virtual dRAID spares must be added after vdev tree is created
7318 	 * and the vdev guids are generated.  The guid of their associated
7319 	 * dRAID is stored in the config and used when opening the spare.
7320 	 */
7321 	if ((error = vdev_draid_spare_create(nvroot, vd, &ndraid,
7322 	    rvd->vdev_children)) == 0) {
7323 		if (ndraid > 0 && nvlist_lookup_nvlist_array(nvroot,
7324 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) != 0)
7325 			nspares = 0;
7326 	} else {
7327 		return (spa_vdev_exit(spa, vd, txg, error));
7328 	}
7329 
7330 	/*
7331 	 * We must validate the spares and l2cache devices after checking the
7332 	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
7333 	 */
7334 	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
7335 		return (spa_vdev_exit(spa, vd, txg, error));
7336 
7337 	/*
7338 	 * If we are in the middle of a device removal, we can only add
7339 	 * devices which match the existing devices in the pool.
7340 	 * If we are in the middle of a removal, or have some indirect
7341 	 * vdevs, we can not add raidz or dRAID top levels.
7342 	 */
7343 	if (spa->spa_vdev_removal != NULL ||
7344 	    spa->spa_removing_phys.sr_prev_indirect_vdev != -1) {
7345 		for (int c = 0; c < vd->vdev_children; c++) {
7346 			tvd = vd->vdev_child[c];
7347 			if (spa->spa_vdev_removal != NULL &&
7348 			    tvd->vdev_ashift != spa->spa_max_ashift) {
7349 				return (spa_vdev_exit(spa, vd, txg, EINVAL));
7350 			}
7351 			/* Fail if top level vdev is raidz or a dRAID */
7352 			if (vdev_get_nparity(tvd) != 0)
7353 				return (spa_vdev_exit(spa, vd, txg, EINVAL));
7354 
7355 			/*
7356 			 * Need the top level mirror to be
7357 			 * a mirror of leaf vdevs only
7358 			 */
7359 			if (tvd->vdev_ops == &vdev_mirror_ops) {
7360 				for (uint64_t cid = 0;
7361 				    cid < tvd->vdev_children; cid++) {
7362 					vdev_t *cvd = tvd->vdev_child[cid];
7363 					if (!cvd->vdev_ops->vdev_op_leaf) {
7364 						return (spa_vdev_exit(spa, vd,
7365 						    txg, EINVAL));
7366 					}
7367 				}
7368 			}
7369 		}
7370 	}
7371 
7372 	if (check_ashift && spa->spa_max_ashift == spa->spa_min_ashift) {
7373 		for (int c = 0; c < vd->vdev_children; c++) {
7374 			tvd = vd->vdev_child[c];
7375 			if (tvd->vdev_ashift != spa->spa_max_ashift) {
7376 				return (spa_vdev_exit(spa, vd, txg,
7377 				    ZFS_ERR_ASHIFT_MISMATCH));
7378 			}
7379 		}
7380 	}
7381 
7382 	for (int c = 0; c < vd->vdev_children; c++) {
7383 		tvd = vd->vdev_child[c];
7384 		vdev_remove_child(vd, tvd);
7385 		tvd->vdev_id = rvd->vdev_children;
7386 		vdev_add_child(rvd, tvd);
7387 		vdev_config_dirty(tvd);
7388 	}
7389 
7390 	if (nspares != 0) {
7391 		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
7392 		    ZPOOL_CONFIG_SPARES);
7393 		spa_load_spares(spa);
7394 		spa->spa_spares.sav_sync = B_TRUE;
7395 	}
7396 
7397 	if (nl2cache != 0) {
7398 		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
7399 		    ZPOOL_CONFIG_L2CACHE);
7400 		spa_load_l2cache(spa);
7401 		spa->spa_l2cache.sav_sync = B_TRUE;
7402 	}
7403 
7404 	/*
7405 	 * We can't increment a feature while holding spa_vdev so we
7406 	 * have to do it in a synctask.
7407 	 */
7408 	if (ndraid != 0) {
7409 		dmu_tx_t *tx;
7410 
7411 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
7412 		dsl_sync_task_nowait(spa->spa_dsl_pool, spa_draid_feature_incr,
7413 		    (void *)(uintptr_t)ndraid, tx);
7414 		dmu_tx_commit(tx);
7415 	}
7416 
7417 	/*
7418 	 * We have to be careful when adding new vdevs to an existing pool.
7419 	 * If other threads start allocating from these vdevs before we
7420 	 * sync the config cache, and we lose power, then upon reboot we may
7421 	 * fail to open the pool because there are DVAs that the config cache
7422 	 * can't translate.  Therefore, we first add the vdevs without
7423 	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
7424 	 * and then let spa_config_update() initialize the new metaslabs.
7425 	 *
7426 	 * spa_load() checks for added-but-not-initialized vdevs, so that
7427 	 * if we lose power at any point in this sequence, the remaining
7428 	 * steps will be completed the next time we load the pool.
7429 	 */
7430 	(void) spa_vdev_exit(spa, vd, txg, 0);
7431 
7432 	mutex_enter(&spa_namespace_lock);
7433 	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
7434 	spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD);
7435 	mutex_exit(&spa_namespace_lock);
7436 
7437 	return (0);
7438 }
7439 
7440 /*
7441  * Given a vdev to be replaced and its parent, check for a possible
7442  * "double spare" condition if a vdev is to be replaced by a spare.  When this
7443  * happens, you can get two spares assigned to one failed vdev.
7444  *
7445  * To trigger a double spare condition:
7446  *
7447  * 1. disk1 fails
7448  * 2. 1st spare is kicked in for disk1 and it resilvers
7449  * 3. Someone replaces disk1 with a new blank disk
7450  * 4. New blank disk starts resilvering
7451  * 5. While resilvering, new blank disk has IO errors and faults
7452  * 6. 2nd spare is kicked in for new blank disk
7453  * 7. At this point two spares are kicked in for the original disk1.
7454  *
7455  * It looks like this:
7456  *
7457  * NAME                                            STATE     READ WRITE CKSUM
7458  * tank2                                           DEGRADED     0     0     0
7459  *   draid2:6d:10c:2s-0                            DEGRADED     0     0     0
7460  *     scsi-0QEMU_QEMU_HARDDISK_d1                 ONLINE       0     0     0
7461  *     scsi-0QEMU_QEMU_HARDDISK_d2                 ONLINE       0     0     0
7462  *     scsi-0QEMU_QEMU_HARDDISK_d3                 ONLINE       0     0     0
7463  *     scsi-0QEMU_QEMU_HARDDISK_d4                 ONLINE       0     0     0
7464  *     scsi-0QEMU_QEMU_HARDDISK_d5                 ONLINE       0     0     0
7465  *     scsi-0QEMU_QEMU_HARDDISK_d6                 ONLINE       0     0     0
7466  *     scsi-0QEMU_QEMU_HARDDISK_d7                 ONLINE       0     0     0
7467  *     scsi-0QEMU_QEMU_HARDDISK_d8                 ONLINE       0     0     0
7468  *     scsi-0QEMU_QEMU_HARDDISK_d9                 ONLINE       0     0     0
7469  *     spare-9                                     DEGRADED     0     0     0
7470  *       replacing-0                               DEGRADED     0    93     0
7471  *         scsi-0QEMU_QEMU_HARDDISK_d10-part1/old  UNAVAIL      0     0     0
7472  *         spare-1                                 DEGRADED     0     0     0
7473  *           scsi-0QEMU_QEMU_HARDDISK_d10          REMOVED      0     0     0
7474  *           draid2-0-0                            ONLINE       0     0     0
7475  *       draid2-0-1                                ONLINE       0     0     0
7476  * spares
7477  *   draid2-0-0                                    INUSE     currently in use
7478  *   draid2-0-1                                    INUSE     currently in use
7479  *
7480  * ARGS:
7481  *
7482  * newvd:  New spare disk
7483  * pvd:    Parent vdev_t the spare should attach to
7484  *
7485  * This function returns B_TRUE if adding the new vdev would create a double
7486  * spare condition, B_FALSE otherwise.
7487  */
7488 static boolean_t
7489 spa_vdev_new_spare_would_cause_double_spares(vdev_t *newvd, vdev_t *pvd)
7490 {
7491 	vdev_t *ppvd;
7492 
7493 	ppvd = pvd->vdev_parent;
7494 	if (ppvd == NULL)
7495 		return (B_FALSE);
7496 
7497 	/*
7498 	 * To determine if this configuration would cause a double spare, we
7499 	 * look at the vdev_op of the parent vdev, and of the parent's parent
7500 	 * vdev.  We also look at vdev_isspare on the new disk.  A double spare
7501 	 * condition looks like this:
7502 	 *
7503 	 * 1. parent of parent's op is a spare or draid spare
7504 	 * 2. parent's op is replacing
7505 	 * 3. new disk is a spare
7506 	 */
7507 	if ((ppvd->vdev_ops == &vdev_spare_ops) ||
7508 	    (ppvd->vdev_ops == &vdev_draid_spare_ops))
7509 		if (pvd->vdev_ops == &vdev_replacing_ops)
7510 			if (newvd->vdev_isspare)
7511 				return (B_TRUE);
7512 
7513 	return (B_FALSE);
7514 }
7515 
7516 /*
7517  * Attach a device to a vdev specified by its guid.  The vdev type can be
7518  * a mirror, a raidz, or a leaf device that is also a top-level (e.g. a
7519  * single device). When the vdev is a single device, a mirror vdev will be
7520  * automatically inserted.
7521  *
7522  * If 'replacing' is specified, the new device is intended to replace the
7523  * existing device; in this case the two devices are made into their own
7524  * mirror using the 'replacing' vdev, which is functionally identical to
7525  * the mirror vdev (it actually reuses all the same ops) but has a few
7526  * extra rules: you can't attach to it after it's been created, and upon
7527  * completion of resilvering, the first disk (the one being replaced)
7528  * is automatically detached.
7529  *
7530  * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
7531  * should be performed instead of traditional healing reconstruction.  From
7532  * an administrators perspective these are both resilver operations.
7533  */
7534 int
7535 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing,
7536     int rebuild)
7537 {
7538 	uint64_t txg, dtl_max_txg;
7539 	vdev_t *rvd = spa->spa_root_vdev;
7540 	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
7541 	vdev_ops_t *pvops;
7542 	char *oldvdpath, *newvdpath;
7543 	int newvd_isspare = B_FALSE;
7544 	int error;
7545 
7546 	ASSERT(spa_writeable(spa));
7547 
7548 	txg = spa_vdev_enter(spa);
7549 
7550 	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
7551 
7552 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
7553 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
7554 		error = (spa_has_checkpoint(spa)) ?
7555 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
7556 		return (spa_vdev_exit(spa, NULL, txg, error));
7557 	}
7558 
7559 	if (rebuild) {
7560 		if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD))
7561 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7562 
7563 		if (dsl_scan_resilvering(spa_get_dsl(spa)) ||
7564 		    dsl_scan_resilver_scheduled(spa_get_dsl(spa))) {
7565 			return (spa_vdev_exit(spa, NULL, txg,
7566 			    ZFS_ERR_RESILVER_IN_PROGRESS));
7567 		}
7568 	} else {
7569 		if (vdev_rebuild_active(rvd))
7570 			return (spa_vdev_exit(spa, NULL, txg,
7571 			    ZFS_ERR_REBUILD_IN_PROGRESS));
7572 	}
7573 
7574 	if (spa->spa_vdev_removal != NULL) {
7575 		return (spa_vdev_exit(spa, NULL, txg,
7576 		    ZFS_ERR_DEVRM_IN_PROGRESS));
7577 	}
7578 
7579 	if (oldvd == NULL)
7580 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
7581 
7582 	boolean_t raidz = oldvd->vdev_ops == &vdev_raidz_ops;
7583 
7584 	if (raidz) {
7585 		if (!spa_feature_is_enabled(spa, SPA_FEATURE_RAIDZ_EXPANSION))
7586 			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7587 
7588 		/*
7589 		 * Can't expand a raidz while prior expand is in progress.
7590 		 */
7591 		if (spa->spa_raidz_expand != NULL) {
7592 			return (spa_vdev_exit(spa, NULL, txg,
7593 			    ZFS_ERR_RAIDZ_EXPAND_IN_PROGRESS));
7594 		}
7595 	} else if (!oldvd->vdev_ops->vdev_op_leaf) {
7596 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7597 	}
7598 
7599 	if (raidz)
7600 		pvd = oldvd;
7601 	else
7602 		pvd = oldvd->vdev_parent;
7603 
7604 	if (spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
7605 	    VDEV_ALLOC_ATTACH) != 0)
7606 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
7607 
7608 	if (newrootvd->vdev_children != 1)
7609 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
7610 
7611 	newvd = newrootvd->vdev_child[0];
7612 
7613 	if (!newvd->vdev_ops->vdev_op_leaf)
7614 		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
7615 
7616 	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
7617 		return (spa_vdev_exit(spa, newrootvd, txg, error));
7618 
7619 	/*
7620 	 * log, dedup and special vdevs should not be replaced by spares.
7621 	 */
7622 	if ((oldvd->vdev_top->vdev_alloc_bias != VDEV_BIAS_NONE ||
7623 	    oldvd->vdev_top->vdev_islog) && newvd->vdev_isspare) {
7624 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7625 	}
7626 
7627 	/*
7628 	 * A dRAID spare can only replace a child of its parent dRAID vdev.
7629 	 */
7630 	if (newvd->vdev_ops == &vdev_draid_spare_ops &&
7631 	    oldvd->vdev_top != vdev_draid_spare_get_parent(newvd)) {
7632 		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7633 	}
7634 
7635 	if (rebuild) {
7636 		/*
7637 		 * For rebuilds, the top vdev must support reconstruction
7638 		 * using only space maps.  This means the only allowable
7639 		 * vdevs types are the root vdev, a mirror, or dRAID.
7640 		 */
7641 		tvd = pvd;
7642 		if (pvd->vdev_top != NULL)
7643 			tvd = pvd->vdev_top;
7644 
7645 		if (tvd->vdev_ops != &vdev_mirror_ops &&
7646 		    tvd->vdev_ops != &vdev_root_ops &&
7647 		    tvd->vdev_ops != &vdev_draid_ops) {
7648 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7649 		}
7650 	}
7651 
7652 	if (!replacing) {
7653 		/*
7654 		 * For attach, the only allowable parent is a mirror or
7655 		 * the root vdev. A raidz vdev can be attached to, but
7656 		 * you cannot attach to a raidz child.
7657 		 */
7658 		if (pvd->vdev_ops != &vdev_mirror_ops &&
7659 		    pvd->vdev_ops != &vdev_root_ops &&
7660 		    !raidz)
7661 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7662 
7663 		pvops = &vdev_mirror_ops;
7664 	} else {
7665 		/*
7666 		 * Active hot spares can only be replaced by inactive hot
7667 		 * spares.
7668 		 */
7669 		if (pvd->vdev_ops == &vdev_spare_ops &&
7670 		    oldvd->vdev_isspare &&
7671 		    !spa_has_spare(spa, newvd->vdev_guid))
7672 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7673 
7674 		/*
7675 		 * If the source is a hot spare, and the parent isn't already a
7676 		 * spare, then we want to create a new hot spare.  Otherwise, we
7677 		 * want to create a replacing vdev.  The user is not allowed to
7678 		 * attach to a spared vdev child unless the 'isspare' state is
7679 		 * the same (spare replaces spare, non-spare replaces
7680 		 * non-spare).
7681 		 */
7682 		if (pvd->vdev_ops == &vdev_replacing_ops &&
7683 		    spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
7684 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7685 		} else if (pvd->vdev_ops == &vdev_spare_ops &&
7686 		    newvd->vdev_isspare != oldvd->vdev_isspare) {
7687 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7688 		}
7689 
7690 		if (spa_vdev_new_spare_would_cause_double_spares(newvd, pvd)) {
7691 			vdev_dbgmsg(newvd,
7692 			    "disk would create double spares, ignore.");
7693 			return (spa_vdev_exit(spa, newrootvd, txg, EEXIST));
7694 		}
7695 
7696 		if (newvd->vdev_isspare)
7697 			pvops = &vdev_spare_ops;
7698 		else
7699 			pvops = &vdev_replacing_ops;
7700 	}
7701 
7702 	/*
7703 	 * Make sure the new device is big enough.
7704 	 */
7705 	vdev_t *min_vdev = raidz ? oldvd->vdev_child[0] : oldvd;
7706 	if (newvd->vdev_asize < vdev_get_min_asize(min_vdev))
7707 		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
7708 
7709 	/*
7710 	 * The new device cannot have a higher alignment requirement
7711 	 * than the top-level vdev.
7712 	 */
7713 	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) {
7714 		return (spa_vdev_exit(spa, newrootvd, txg,
7715 		    ZFS_ERR_ASHIFT_MISMATCH));
7716 	}
7717 
7718 	/*
7719 	 * RAIDZ-expansion-specific checks.
7720 	 */
7721 	if (raidz) {
7722 		if (vdev_raidz_attach_check(newvd) != 0)
7723 			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7724 
7725 		/*
7726 		 * Fail early if a child is not healthy or being replaced
7727 		 */
7728 		for (int i = 0; i < oldvd->vdev_children; i++) {
7729 			if (vdev_is_dead(oldvd->vdev_child[i]) ||
7730 			    !oldvd->vdev_child[i]->vdev_ops->vdev_op_leaf) {
7731 				return (spa_vdev_exit(spa, newrootvd, txg,
7732 				    ENXIO));
7733 			}
7734 			/* Also fail if reserved boot area is in-use */
7735 			if (vdev_check_boot_reserve(spa, oldvd->vdev_child[i])
7736 			    != 0) {
7737 				return (spa_vdev_exit(spa, newrootvd, txg,
7738 				    EADDRINUSE));
7739 			}
7740 		}
7741 	}
7742 
7743 	if (raidz) {
7744 		/*
7745 		 * Note: oldvdpath is freed by spa_strfree(),  but
7746 		 * kmem_asprintf() is freed by kmem_strfree(), so we have to
7747 		 * move it to a spa_strdup-ed string.
7748 		 */
7749 		char *tmp = kmem_asprintf("raidz%u-%u",
7750 		    (uint_t)vdev_get_nparity(oldvd), (uint_t)oldvd->vdev_id);
7751 		oldvdpath = spa_strdup(tmp);
7752 		kmem_strfree(tmp);
7753 	} else {
7754 		oldvdpath = spa_strdup(oldvd->vdev_path);
7755 	}
7756 	newvdpath = spa_strdup(newvd->vdev_path);
7757 
7758 	/*
7759 	 * If this is an in-place replacement, update oldvd's path and devid
7760 	 * to make it distinguishable from newvd, and unopenable from now on.
7761 	 */
7762 	if (strcmp(oldvdpath, newvdpath) == 0) {
7763 		spa_strfree(oldvd->vdev_path);
7764 		oldvd->vdev_path = kmem_alloc(strlen(newvdpath) + 5,
7765 		    KM_SLEEP);
7766 		(void) sprintf(oldvd->vdev_path, "%s/old",
7767 		    newvdpath);
7768 		if (oldvd->vdev_devid != NULL) {
7769 			spa_strfree(oldvd->vdev_devid);
7770 			oldvd->vdev_devid = NULL;
7771 		}
7772 		spa_strfree(oldvdpath);
7773 		oldvdpath = spa_strdup(oldvd->vdev_path);
7774 	}
7775 
7776 	/*
7777 	 * If the parent is not a mirror, or if we're replacing, insert the new
7778 	 * mirror/replacing/spare vdev above oldvd.
7779 	 */
7780 	if (!raidz && pvd->vdev_ops != pvops) {
7781 		pvd = vdev_add_parent(oldvd, pvops);
7782 		ASSERT(pvd->vdev_ops == pvops);
7783 		ASSERT(oldvd->vdev_parent == pvd);
7784 	}
7785 
7786 	ASSERT(pvd->vdev_top->vdev_parent == rvd);
7787 
7788 	/*
7789 	 * Extract the new device from its root and add it to pvd.
7790 	 */
7791 	vdev_remove_child(newrootvd, newvd);
7792 	newvd->vdev_id = pvd->vdev_children;
7793 	newvd->vdev_crtxg = oldvd->vdev_crtxg;
7794 	vdev_add_child(pvd, newvd);
7795 
7796 	/*
7797 	 * Reevaluate the parent vdev state.
7798 	 */
7799 	vdev_propagate_state(pvd);
7800 
7801 	tvd = newvd->vdev_top;
7802 	ASSERT(pvd->vdev_top == tvd);
7803 	ASSERT(tvd->vdev_parent == rvd);
7804 
7805 	vdev_config_dirty(tvd);
7806 
7807 	/*
7808 	 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7809 	 * for any dmu_sync-ed blocks.  It will propagate upward when
7810 	 * spa_vdev_exit() calls vdev_dtl_reassess().
7811 	 */
7812 	dtl_max_txg = txg + TXG_CONCURRENT_STATES;
7813 
7814 	if (raidz) {
7815 		/*
7816 		 * Wait for the youngest allocations and frees to sync,
7817 		 * and then wait for the deferral of those frees to finish.
7818 		 */
7819 		spa_vdev_config_exit(spa, NULL,
7820 		    txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
7821 
7822 		vdev_initialize_stop_all(tvd, VDEV_INITIALIZE_ACTIVE);
7823 		vdev_trim_stop_all(tvd, VDEV_TRIM_ACTIVE);
7824 		vdev_autotrim_stop_wait(tvd);
7825 
7826 		dtl_max_txg = spa_vdev_config_enter(spa);
7827 
7828 		tvd->vdev_rz_expanding = B_TRUE;
7829 
7830 		vdev_dirty_leaves(tvd, VDD_DTL, dtl_max_txg);
7831 		vdev_config_dirty(tvd);
7832 
7833 		dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool,
7834 		    dtl_max_txg);
7835 		dsl_sync_task_nowait(spa->spa_dsl_pool, vdev_raidz_attach_sync,
7836 		    newvd, tx);
7837 		dmu_tx_commit(tx);
7838 	} else {
7839 		vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
7840 		    dtl_max_txg - TXG_INITIAL);
7841 
7842 		if (newvd->vdev_isspare) {
7843 			spa_spare_activate(newvd);
7844 			spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE);
7845 		}
7846 
7847 		newvd_isspare = newvd->vdev_isspare;
7848 
7849 		/*
7850 		 * Mark newvd's DTL dirty in this txg.
7851 		 */
7852 		vdev_dirty(tvd, VDD_DTL, newvd, txg);
7853 
7854 		/*
7855 		 * Schedule the resilver or rebuild to restart in the future.
7856 		 * We do this to ensure that dmu_sync-ed blocks have been
7857 		 * stitched into the respective datasets.
7858 		 */
7859 		if (rebuild) {
7860 			newvd->vdev_rebuild_txg = txg;
7861 
7862 			vdev_rebuild(tvd);
7863 		} else {
7864 			newvd->vdev_resilver_txg = txg;
7865 
7866 			if (dsl_scan_resilvering(spa_get_dsl(spa)) &&
7867 			    spa_feature_is_enabled(spa,
7868 			    SPA_FEATURE_RESILVER_DEFER)) {
7869 				vdev_defer_resilver(newvd);
7870 			} else {
7871 				dsl_scan_restart_resilver(spa->spa_dsl_pool,
7872 				    dtl_max_txg);
7873 			}
7874 		}
7875 	}
7876 
7877 	if (spa->spa_bootfs)
7878 		spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH);
7879 
7880 	spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH);
7881 
7882 	/*
7883 	 * Commit the config
7884 	 */
7885 	(void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
7886 
7887 	spa_history_log_internal(spa, "vdev attach", NULL,
7888 	    "%s vdev=%s %s vdev=%s",
7889 	    replacing && newvd_isspare ? "spare in" :
7890 	    replacing ? "replace" : "attach", newvdpath,
7891 	    replacing ? "for" : "to", oldvdpath);
7892 
7893 	spa_strfree(oldvdpath);
7894 	spa_strfree(newvdpath);
7895 
7896 	return (0);
7897 }
7898 
7899 /*
7900  * Detach a device from a mirror or replacing vdev.
7901  *
7902  * If 'replace_done' is specified, only detach if the parent
7903  * is a replacing or a spare vdev.
7904  */
7905 int
7906 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
7907 {
7908 	uint64_t txg;
7909 	int error;
7910 	vdev_t *rvd __maybe_unused = spa->spa_root_vdev;
7911 	vdev_t *vd, *pvd, *cvd, *tvd;
7912 	boolean_t unspare = B_FALSE;
7913 	uint64_t unspare_guid = 0;
7914 	char *vdpath;
7915 
7916 	ASSERT(spa_writeable(spa));
7917 
7918 	txg = spa_vdev_detach_enter(spa, guid);
7919 
7920 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
7921 
7922 	/*
7923 	 * Besides being called directly from the userland through the
7924 	 * ioctl interface, spa_vdev_detach() can be potentially called
7925 	 * at the end of spa_vdev_resilver_done().
7926 	 *
7927 	 * In the regular case, when we have a checkpoint this shouldn't
7928 	 * happen as we never empty the DTLs of a vdev during the scrub
7929 	 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7930 	 * should never get here when we have a checkpoint.
7931 	 *
7932 	 * That said, even in a case when we checkpoint the pool exactly
7933 	 * as spa_vdev_resilver_done() calls this function everything
7934 	 * should be fine as the resilver will return right away.
7935 	 */
7936 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
7937 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
7938 		error = (spa_has_checkpoint(spa)) ?
7939 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
7940 		return (spa_vdev_exit(spa, NULL, txg, error));
7941 	}
7942 
7943 	if (vd == NULL)
7944 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
7945 
7946 	if (!vd->vdev_ops->vdev_op_leaf)
7947 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7948 
7949 	pvd = vd->vdev_parent;
7950 
7951 	/*
7952 	 * If the parent/child relationship is not as expected, don't do it.
7953 	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7954 	 * vdev that's replacing B with C.  The user's intent in replacing
7955 	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
7956 	 * the replace by detaching C, the expected behavior is to end up
7957 	 * M(A,B).  But suppose that right after deciding to detach C,
7958 	 * the replacement of B completes.  We would have M(A,C), and then
7959 	 * ask to detach C, which would leave us with just A -- not what
7960 	 * the user wanted.  To prevent this, we make sure that the
7961 	 * parent/child relationship hasn't changed -- in this example,
7962 	 * that C's parent is still the replacing vdev R.
7963 	 */
7964 	if (pvd->vdev_guid != pguid && pguid != 0)
7965 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
7966 
7967 	/*
7968 	 * Only 'replacing' or 'spare' vdevs can be replaced.
7969 	 */
7970 	if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
7971 	    pvd->vdev_ops != &vdev_spare_ops)
7972 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7973 
7974 	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
7975 	    spa_version(spa) >= SPA_VERSION_SPARES);
7976 
7977 	/*
7978 	 * Only mirror, replacing, and spare vdevs support detach.
7979 	 */
7980 	if (pvd->vdev_ops != &vdev_replacing_ops &&
7981 	    pvd->vdev_ops != &vdev_mirror_ops &&
7982 	    pvd->vdev_ops != &vdev_spare_ops)
7983 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7984 
7985 	/*
7986 	 * If this device has the only valid copy of some data,
7987 	 * we cannot safely detach it.
7988 	 */
7989 	if (vdev_dtl_required(vd))
7990 		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
7991 
7992 	ASSERT(pvd->vdev_children >= 2);
7993 
7994 	/*
7995 	 * If we are detaching the second disk from a replacing vdev, then
7996 	 * check to see if we changed the original vdev's path to have "/old"
7997 	 * at the end in spa_vdev_attach().  If so, undo that change now.
7998 	 */
7999 	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
8000 	    vd->vdev_path != NULL) {
8001 		size_t len = strlen(vd->vdev_path);
8002 
8003 		for (int c = 0; c < pvd->vdev_children; c++) {
8004 			cvd = pvd->vdev_child[c];
8005 
8006 			if (cvd == vd || cvd->vdev_path == NULL)
8007 				continue;
8008 
8009 			if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
8010 			    strcmp(cvd->vdev_path + len, "/old") == 0) {
8011 				spa_strfree(cvd->vdev_path);
8012 				cvd->vdev_path = spa_strdup(vd->vdev_path);
8013 				break;
8014 			}
8015 		}
8016 	}
8017 
8018 	/*
8019 	 * If we are detaching the original disk from a normal spare, then it
8020 	 * implies that the spare should become a real disk, and be removed
8021 	 * from the active spare list for the pool.  dRAID spares on the
8022 	 * other hand are coupled to the pool and thus should never be removed
8023 	 * from the spares list.
8024 	 */
8025 	if (pvd->vdev_ops == &vdev_spare_ops && vd->vdev_id == 0) {
8026 		vdev_t *last_cvd = pvd->vdev_child[pvd->vdev_children - 1];
8027 
8028 		if (last_cvd->vdev_isspare &&
8029 		    last_cvd->vdev_ops != &vdev_draid_spare_ops) {
8030 			unspare = B_TRUE;
8031 		}
8032 	}
8033 
8034 	/*
8035 	 * Erase the disk labels so the disk can be used for other things.
8036 	 * This must be done after all other error cases are handled,
8037 	 * but before we disembowel vd (so we can still do I/O to it).
8038 	 * But if we can't do it, don't treat the error as fatal --
8039 	 * it may be that the unwritability of the disk is the reason
8040 	 * it's being detached!
8041 	 */
8042 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
8043 
8044 	/*
8045 	 * Remove vd from its parent and compact the parent's children.
8046 	 */
8047 	vdev_remove_child(pvd, vd);
8048 	vdev_compact_children(pvd);
8049 
8050 	/*
8051 	 * Remember one of the remaining children so we can get tvd below.
8052 	 */
8053 	cvd = pvd->vdev_child[pvd->vdev_children - 1];
8054 
8055 	/*
8056 	 * If we need to remove the remaining child from the list of hot spares,
8057 	 * do it now, marking the vdev as no longer a spare in the process.
8058 	 * We must do this before vdev_remove_parent(), because that can
8059 	 * change the GUID if it creates a new toplevel GUID.  For a similar
8060 	 * reason, we must remove the spare now, in the same txg as the detach;
8061 	 * otherwise someone could attach a new sibling, change the GUID, and
8062 	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
8063 	 */
8064 	if (unspare) {
8065 		ASSERT(cvd->vdev_isspare);
8066 		spa_spare_remove(cvd);
8067 		unspare_guid = cvd->vdev_guid;
8068 		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
8069 		cvd->vdev_unspare = B_TRUE;
8070 	}
8071 
8072 	/*
8073 	 * If the parent mirror/replacing vdev only has one child,
8074 	 * the parent is no longer needed.  Remove it from the tree.
8075 	 */
8076 	if (pvd->vdev_children == 1) {
8077 		if (pvd->vdev_ops == &vdev_spare_ops)
8078 			cvd->vdev_unspare = B_FALSE;
8079 		vdev_remove_parent(cvd);
8080 	}
8081 
8082 	/*
8083 	 * We don't set tvd until now because the parent we just removed
8084 	 * may have been the previous top-level vdev.
8085 	 */
8086 	tvd = cvd->vdev_top;
8087 	ASSERT(tvd->vdev_parent == rvd);
8088 
8089 	/*
8090 	 * Reevaluate the parent vdev state.
8091 	 */
8092 	vdev_propagate_state(cvd);
8093 
8094 	/*
8095 	 * If the 'autoexpand' property is set on the pool then automatically
8096 	 * try to expand the size of the pool. For example if the device we
8097 	 * just detached was smaller than the others, it may be possible to
8098 	 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
8099 	 * first so that we can obtain the updated sizes of the leaf vdevs.
8100 	 */
8101 	if (spa->spa_autoexpand) {
8102 		vdev_reopen(tvd);
8103 		vdev_expand(tvd, txg);
8104 	}
8105 
8106 	vdev_config_dirty(tvd);
8107 
8108 	/*
8109 	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
8110 	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
8111 	 * But first make sure we're not on any *other* txg's DTL list, to
8112 	 * prevent vd from being accessed after it's freed.
8113 	 */
8114 	vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none");
8115 	for (int t = 0; t < TXG_SIZE; t++)
8116 		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
8117 	vd->vdev_detached = B_TRUE;
8118 	vdev_dirty(tvd, VDD_DTL, vd, txg);
8119 
8120 	spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE);
8121 	spa_notify_waiters(spa);
8122 
8123 	/* hang on to the spa before we release the lock */
8124 	spa_open_ref(spa, FTAG);
8125 
8126 	error = spa_vdev_exit(spa, vd, txg, 0);
8127 
8128 	spa_history_log_internal(spa, "detach", NULL,
8129 	    "vdev=%s", vdpath);
8130 	spa_strfree(vdpath);
8131 
8132 	/*
8133 	 * If this was the removal of the original device in a hot spare vdev,
8134 	 * then we want to go through and remove the device from the hot spare
8135 	 * list of every other pool.
8136 	 */
8137 	if (unspare) {
8138 		spa_t *altspa = NULL;
8139 
8140 		mutex_enter(&spa_namespace_lock);
8141 		while ((altspa = spa_next(altspa)) != NULL) {
8142 			if (altspa->spa_state != POOL_STATE_ACTIVE ||
8143 			    altspa == spa)
8144 				continue;
8145 
8146 			spa_open_ref(altspa, FTAG);
8147 			mutex_exit(&spa_namespace_lock);
8148 			(void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
8149 			mutex_enter(&spa_namespace_lock);
8150 			spa_close(altspa, FTAG);
8151 		}
8152 		mutex_exit(&spa_namespace_lock);
8153 
8154 		/* search the rest of the vdevs for spares to remove */
8155 		spa_vdev_resilver_done(spa);
8156 	}
8157 
8158 	/* all done with the spa; OK to release */
8159 	mutex_enter(&spa_namespace_lock);
8160 	spa_close(spa, FTAG);
8161 	mutex_exit(&spa_namespace_lock);
8162 
8163 	return (error);
8164 }
8165 
8166 static int
8167 spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type,
8168     list_t *vd_list)
8169 {
8170 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8171 
8172 	spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
8173 
8174 	/* Look up vdev and ensure it's a leaf. */
8175 	vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
8176 	if (vd == NULL || vd->vdev_detached) {
8177 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8178 		return (SET_ERROR(ENODEV));
8179 	} else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) {
8180 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8181 		return (SET_ERROR(EINVAL));
8182 	} else if (!vdev_writeable(vd)) {
8183 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8184 		return (SET_ERROR(EROFS));
8185 	}
8186 	mutex_enter(&vd->vdev_initialize_lock);
8187 	spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8188 
8189 	/*
8190 	 * When we activate an initialize action we check to see
8191 	 * if the vdev_initialize_thread is NULL. We do this instead
8192 	 * of using the vdev_initialize_state since there might be
8193 	 * a previous initialization process which has completed but
8194 	 * the thread is not exited.
8195 	 */
8196 	if (cmd_type == POOL_INITIALIZE_START &&
8197 	    (vd->vdev_initialize_thread != NULL ||
8198 	    vd->vdev_top->vdev_removing || vd->vdev_top->vdev_rz_expanding)) {
8199 		mutex_exit(&vd->vdev_initialize_lock);
8200 		return (SET_ERROR(EBUSY));
8201 	} else if (cmd_type == POOL_INITIALIZE_CANCEL &&
8202 	    (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE &&
8203 	    vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) {
8204 		mutex_exit(&vd->vdev_initialize_lock);
8205 		return (SET_ERROR(ESRCH));
8206 	} else if (cmd_type == POOL_INITIALIZE_SUSPEND &&
8207 	    vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) {
8208 		mutex_exit(&vd->vdev_initialize_lock);
8209 		return (SET_ERROR(ESRCH));
8210 	} else if (cmd_type == POOL_INITIALIZE_UNINIT &&
8211 	    vd->vdev_initialize_thread != NULL) {
8212 		mutex_exit(&vd->vdev_initialize_lock);
8213 		return (SET_ERROR(EBUSY));
8214 	}
8215 
8216 	switch (cmd_type) {
8217 	case POOL_INITIALIZE_START:
8218 		vdev_initialize(vd);
8219 		break;
8220 	case POOL_INITIALIZE_CANCEL:
8221 		vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list);
8222 		break;
8223 	case POOL_INITIALIZE_SUSPEND:
8224 		vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list);
8225 		break;
8226 	case POOL_INITIALIZE_UNINIT:
8227 		vdev_uninitialize(vd);
8228 		break;
8229 	default:
8230 		panic("invalid cmd_type %llu", (unsigned long long)cmd_type);
8231 	}
8232 	mutex_exit(&vd->vdev_initialize_lock);
8233 
8234 	return (0);
8235 }
8236 
8237 int
8238 spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type,
8239     nvlist_t *vdev_errlist)
8240 {
8241 	int total_errors = 0;
8242 	list_t vd_list;
8243 
8244 	list_create(&vd_list, sizeof (vdev_t),
8245 	    offsetof(vdev_t, vdev_initialize_node));
8246 
8247 	/*
8248 	 * We hold the namespace lock through the whole function
8249 	 * to prevent any changes to the pool while we're starting or
8250 	 * stopping initialization. The config and state locks are held so that
8251 	 * we can properly assess the vdev state before we commit to
8252 	 * the initializing operation.
8253 	 */
8254 	mutex_enter(&spa_namespace_lock);
8255 
8256 	for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
8257 	    pair != NULL; pair = nvlist_next_nvpair(nv, pair)) {
8258 		uint64_t vdev_guid = fnvpair_value_uint64(pair);
8259 
8260 		int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type,
8261 		    &vd_list);
8262 		if (error != 0) {
8263 			char guid_as_str[MAXNAMELEN];
8264 
8265 			(void) snprintf(guid_as_str, sizeof (guid_as_str),
8266 			    "%llu", (unsigned long long)vdev_guid);
8267 			fnvlist_add_int64(vdev_errlist, guid_as_str, error);
8268 			total_errors++;
8269 		}
8270 	}
8271 
8272 	/* Wait for all initialize threads to stop. */
8273 	vdev_initialize_stop_wait(spa, &vd_list);
8274 
8275 	/* Sync out the initializing state */
8276 	txg_wait_synced(spa->spa_dsl_pool, 0);
8277 	mutex_exit(&spa_namespace_lock);
8278 
8279 	list_destroy(&vd_list);
8280 
8281 	return (total_errors);
8282 }
8283 
8284 static int
8285 spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type,
8286     uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list)
8287 {
8288 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8289 
8290 	spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
8291 
8292 	/* Look up vdev and ensure it's a leaf. */
8293 	vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
8294 	if (vd == NULL || vd->vdev_detached) {
8295 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8296 		return (SET_ERROR(ENODEV));
8297 	} else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) {
8298 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8299 		return (SET_ERROR(EINVAL));
8300 	} else if (!vdev_writeable(vd)) {
8301 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8302 		return (SET_ERROR(EROFS));
8303 	} else if (!vd->vdev_has_trim) {
8304 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8305 		return (SET_ERROR(EOPNOTSUPP));
8306 	} else if (secure && !vd->vdev_has_securetrim) {
8307 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8308 		return (SET_ERROR(EOPNOTSUPP));
8309 	}
8310 	mutex_enter(&vd->vdev_trim_lock);
8311 	spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8312 
8313 	/*
8314 	 * When we activate a TRIM action we check to see if the
8315 	 * vdev_trim_thread is NULL. We do this instead of using the
8316 	 * vdev_trim_state since there might be a previous TRIM process
8317 	 * which has completed but the thread is not exited.
8318 	 */
8319 	if (cmd_type == POOL_TRIM_START &&
8320 	    (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing ||
8321 	    vd->vdev_top->vdev_rz_expanding)) {
8322 		mutex_exit(&vd->vdev_trim_lock);
8323 		return (SET_ERROR(EBUSY));
8324 	} else if (cmd_type == POOL_TRIM_CANCEL &&
8325 	    (vd->vdev_trim_state != VDEV_TRIM_ACTIVE &&
8326 	    vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) {
8327 		mutex_exit(&vd->vdev_trim_lock);
8328 		return (SET_ERROR(ESRCH));
8329 	} else if (cmd_type == POOL_TRIM_SUSPEND &&
8330 	    vd->vdev_trim_state != VDEV_TRIM_ACTIVE) {
8331 		mutex_exit(&vd->vdev_trim_lock);
8332 		return (SET_ERROR(ESRCH));
8333 	}
8334 
8335 	switch (cmd_type) {
8336 	case POOL_TRIM_START:
8337 		vdev_trim(vd, rate, partial, secure);
8338 		break;
8339 	case POOL_TRIM_CANCEL:
8340 		vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list);
8341 		break;
8342 	case POOL_TRIM_SUSPEND:
8343 		vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list);
8344 		break;
8345 	default:
8346 		panic("invalid cmd_type %llu", (unsigned long long)cmd_type);
8347 	}
8348 	mutex_exit(&vd->vdev_trim_lock);
8349 
8350 	return (0);
8351 }
8352 
8353 /*
8354  * Initiates a manual TRIM for the requested vdevs. This kicks off individual
8355  * TRIM threads for each child vdev.  These threads pass over all of the free
8356  * space in the vdev's metaslabs and issues TRIM commands for that space.
8357  */
8358 int
8359 spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate,
8360     boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist)
8361 {
8362 	int total_errors = 0;
8363 	list_t vd_list;
8364 
8365 	list_create(&vd_list, sizeof (vdev_t),
8366 	    offsetof(vdev_t, vdev_trim_node));
8367 
8368 	/*
8369 	 * We hold the namespace lock through the whole function
8370 	 * to prevent any changes to the pool while we're starting or
8371 	 * stopping TRIM. The config and state locks are held so that
8372 	 * we can properly assess the vdev state before we commit to
8373 	 * the TRIM operation.
8374 	 */
8375 	mutex_enter(&spa_namespace_lock);
8376 
8377 	for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
8378 	    pair != NULL; pair = nvlist_next_nvpair(nv, pair)) {
8379 		uint64_t vdev_guid = fnvpair_value_uint64(pair);
8380 
8381 		int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type,
8382 		    rate, partial, secure, &vd_list);
8383 		if (error != 0) {
8384 			char guid_as_str[MAXNAMELEN];
8385 
8386 			(void) snprintf(guid_as_str, sizeof (guid_as_str),
8387 			    "%llu", (unsigned long long)vdev_guid);
8388 			fnvlist_add_int64(vdev_errlist, guid_as_str, error);
8389 			total_errors++;
8390 		}
8391 	}
8392 
8393 	/* Wait for all TRIM threads to stop. */
8394 	vdev_trim_stop_wait(spa, &vd_list);
8395 
8396 	/* Sync out the TRIM state */
8397 	txg_wait_synced(spa->spa_dsl_pool, 0);
8398 	mutex_exit(&spa_namespace_lock);
8399 
8400 	list_destroy(&vd_list);
8401 
8402 	return (total_errors);
8403 }
8404 
8405 /*
8406  * Split a set of devices from their mirrors, and create a new pool from them.
8407  */
8408 int
8409 spa_vdev_split_mirror(spa_t *spa, const char *newname, nvlist_t *config,
8410     nvlist_t *props, boolean_t exp)
8411 {
8412 	int error = 0;
8413 	uint64_t txg, *glist;
8414 	spa_t *newspa;
8415 	uint_t c, children, lastlog;
8416 	nvlist_t **child, *nvl, *tmp;
8417 	dmu_tx_t *tx;
8418 	const char *altroot = NULL;
8419 	vdev_t *rvd, **vml = NULL;			/* vdev modify list */
8420 	boolean_t activate_slog;
8421 
8422 	ASSERT(spa_writeable(spa));
8423 
8424 	txg = spa_vdev_enter(spa);
8425 
8426 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8427 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
8428 		error = (spa_has_checkpoint(spa)) ?
8429 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
8430 		return (spa_vdev_exit(spa, NULL, txg, error));
8431 	}
8432 
8433 	/* clear the log and flush everything up to now */
8434 	activate_slog = spa_passivate_log(spa);
8435 	(void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
8436 	error = spa_reset_logs(spa);
8437 	txg = spa_vdev_config_enter(spa);
8438 
8439 	if (activate_slog)
8440 		spa_activate_log(spa);
8441 
8442 	if (error != 0)
8443 		return (spa_vdev_exit(spa, NULL, txg, error));
8444 
8445 	/* check new spa name before going any further */
8446 	if (spa_lookup(newname) != NULL)
8447 		return (spa_vdev_exit(spa, NULL, txg, EEXIST));
8448 
8449 	/*
8450 	 * scan through all the children to ensure they're all mirrors
8451 	 */
8452 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
8453 	    nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
8454 	    &children) != 0)
8455 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
8456 
8457 	/* first, check to ensure we've got the right child count */
8458 	rvd = spa->spa_root_vdev;
8459 	lastlog = 0;
8460 	for (c = 0; c < rvd->vdev_children; c++) {
8461 		vdev_t *vd = rvd->vdev_child[c];
8462 
8463 		/* don't count the holes & logs as children */
8464 		if (vd->vdev_islog || (vd->vdev_ops != &vdev_indirect_ops &&
8465 		    !vdev_is_concrete(vd))) {
8466 			if (lastlog == 0)
8467 				lastlog = c;
8468 			continue;
8469 		}
8470 
8471 		lastlog = 0;
8472 	}
8473 	if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
8474 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
8475 
8476 	/* next, ensure no spare or cache devices are part of the split */
8477 	if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
8478 	    nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
8479 		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
8480 
8481 	vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
8482 	glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
8483 
8484 	/* then, loop over each vdev and validate it */
8485 	for (c = 0; c < children; c++) {
8486 		uint64_t is_hole = 0;
8487 
8488 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
8489 		    &is_hole);
8490 
8491 		if (is_hole != 0) {
8492 			if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
8493 			    spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
8494 				continue;
8495 			} else {
8496 				error = SET_ERROR(EINVAL);
8497 				break;
8498 			}
8499 		}
8500 
8501 		/* deal with indirect vdevs */
8502 		if (spa->spa_root_vdev->vdev_child[c]->vdev_ops ==
8503 		    &vdev_indirect_ops)
8504 			continue;
8505 
8506 		/* which disk is going to be split? */
8507 		if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
8508 		    &glist[c]) != 0) {
8509 			error = SET_ERROR(EINVAL);
8510 			break;
8511 		}
8512 
8513 		/* look it up in the spa */
8514 		vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
8515 		if (vml[c] == NULL) {
8516 			error = SET_ERROR(ENODEV);
8517 			break;
8518 		}
8519 
8520 		/* make sure there's nothing stopping the split */
8521 		if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
8522 		    vml[c]->vdev_islog ||
8523 		    !vdev_is_concrete(vml[c]) ||
8524 		    vml[c]->vdev_isspare ||
8525 		    vml[c]->vdev_isl2cache ||
8526 		    !vdev_writeable(vml[c]) ||
8527 		    vml[c]->vdev_children != 0 ||
8528 		    vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
8529 		    c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
8530 			error = SET_ERROR(EINVAL);
8531 			break;
8532 		}
8533 
8534 		if (vdev_dtl_required(vml[c]) ||
8535 		    vdev_resilver_needed(vml[c], NULL, NULL)) {
8536 			error = SET_ERROR(EBUSY);
8537 			break;
8538 		}
8539 
8540 		/* we need certain info from the top level */
8541 		fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
8542 		    vml[c]->vdev_top->vdev_ms_array);
8543 		fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
8544 		    vml[c]->vdev_top->vdev_ms_shift);
8545 		fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
8546 		    vml[c]->vdev_top->vdev_asize);
8547 		fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
8548 		    vml[c]->vdev_top->vdev_ashift);
8549 
8550 		/* transfer per-vdev ZAPs */
8551 		ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0);
8552 		VERIFY0(nvlist_add_uint64(child[c],
8553 		    ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap));
8554 
8555 		ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0);
8556 		VERIFY0(nvlist_add_uint64(child[c],
8557 		    ZPOOL_CONFIG_VDEV_TOP_ZAP,
8558 		    vml[c]->vdev_parent->vdev_top_zap));
8559 	}
8560 
8561 	if (error != 0) {
8562 		kmem_free(vml, children * sizeof (vdev_t *));
8563 		kmem_free(glist, children * sizeof (uint64_t));
8564 		return (spa_vdev_exit(spa, NULL, txg, error));
8565 	}
8566 
8567 	/* stop writers from using the disks */
8568 	for (c = 0; c < children; c++) {
8569 		if (vml[c] != NULL)
8570 			vml[c]->vdev_offline = B_TRUE;
8571 	}
8572 	vdev_reopen(spa->spa_root_vdev);
8573 
8574 	/*
8575 	 * Temporarily record the splitting vdevs in the spa config.  This
8576 	 * will disappear once the config is regenerated.
8577 	 */
8578 	nvl = fnvlist_alloc();
8579 	fnvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, glist, children);
8580 	kmem_free(glist, children * sizeof (uint64_t));
8581 
8582 	mutex_enter(&spa->spa_props_lock);
8583 	fnvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, nvl);
8584 	mutex_exit(&spa->spa_props_lock);
8585 	spa->spa_config_splitting = nvl;
8586 	vdev_config_dirty(spa->spa_root_vdev);
8587 
8588 	/* configure and create the new pool */
8589 	fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname);
8590 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
8591 	    exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE);
8592 	fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa));
8593 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, spa->spa_config_txg);
8594 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
8595 	    spa_generate_guid(NULL));
8596 	VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
8597 	(void) nvlist_lookup_string(props,
8598 	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
8599 
8600 	/* add the new pool to the namespace */
8601 	newspa = spa_add(newname, config, altroot);
8602 	newspa->spa_avz_action = AVZ_ACTION_REBUILD;
8603 	newspa->spa_config_txg = spa->spa_config_txg;
8604 	spa_set_log_state(newspa, SPA_LOG_CLEAR);
8605 
8606 	/* release the spa config lock, retaining the namespace lock */
8607 	spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
8608 
8609 	if (zio_injection_enabled)
8610 		zio_handle_panic_injection(spa, FTAG, 1);
8611 
8612 	spa_activate(newspa, spa_mode_global);
8613 	spa_async_suspend(newspa);
8614 
8615 	/*
8616 	 * Temporarily stop the initializing and TRIM activity.  We set the
8617 	 * state to ACTIVE so that we know to resume initializing or TRIM
8618 	 * once the split has completed.
8619 	 */
8620 	list_t vd_initialize_list;
8621 	list_create(&vd_initialize_list, sizeof (vdev_t),
8622 	    offsetof(vdev_t, vdev_initialize_node));
8623 
8624 	list_t vd_trim_list;
8625 	list_create(&vd_trim_list, sizeof (vdev_t),
8626 	    offsetof(vdev_t, vdev_trim_node));
8627 
8628 	for (c = 0; c < children; c++) {
8629 		if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) {
8630 			mutex_enter(&vml[c]->vdev_initialize_lock);
8631 			vdev_initialize_stop(vml[c],
8632 			    VDEV_INITIALIZE_ACTIVE, &vd_initialize_list);
8633 			mutex_exit(&vml[c]->vdev_initialize_lock);
8634 
8635 			mutex_enter(&vml[c]->vdev_trim_lock);
8636 			vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list);
8637 			mutex_exit(&vml[c]->vdev_trim_lock);
8638 		}
8639 	}
8640 
8641 	vdev_initialize_stop_wait(spa, &vd_initialize_list);
8642 	vdev_trim_stop_wait(spa, &vd_trim_list);
8643 
8644 	list_destroy(&vd_initialize_list);
8645 	list_destroy(&vd_trim_list);
8646 
8647 	newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT;
8648 	newspa->spa_is_splitting = B_TRUE;
8649 
8650 	/* create the new pool from the disks of the original pool */
8651 	error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE);
8652 	if (error)
8653 		goto out;
8654 
8655 	/* if that worked, generate a real config for the new pool */
8656 	if (newspa->spa_root_vdev != NULL) {
8657 		newspa->spa_config_splitting = fnvlist_alloc();
8658 		fnvlist_add_uint64(newspa->spa_config_splitting,
8659 		    ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa));
8660 		spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
8661 		    B_TRUE));
8662 	}
8663 
8664 	/* set the props */
8665 	if (props != NULL) {
8666 		spa_configfile_set(newspa, props, B_FALSE);
8667 		error = spa_prop_set(newspa, props);
8668 		if (error)
8669 			goto out;
8670 	}
8671 
8672 	/* flush everything */
8673 	txg = spa_vdev_config_enter(newspa);
8674 	vdev_config_dirty(newspa->spa_root_vdev);
8675 	(void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
8676 
8677 	if (zio_injection_enabled)
8678 		zio_handle_panic_injection(spa, FTAG, 2);
8679 
8680 	spa_async_resume(newspa);
8681 
8682 	/* finally, update the original pool's config */
8683 	txg = spa_vdev_config_enter(spa);
8684 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
8685 	error = dmu_tx_assign(tx, DMU_TX_WAIT);
8686 	if (error != 0)
8687 		dmu_tx_abort(tx);
8688 	for (c = 0; c < children; c++) {
8689 		if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) {
8690 			vdev_t *tvd = vml[c]->vdev_top;
8691 
8692 			/*
8693 			 * Need to be sure the detachable VDEV is not
8694 			 * on any *other* txg's DTL list to prevent it
8695 			 * from being accessed after it's freed.
8696 			 */
8697 			for (int t = 0; t < TXG_SIZE; t++) {
8698 				(void) txg_list_remove_this(
8699 				    &tvd->vdev_dtl_list, vml[c], t);
8700 			}
8701 
8702 			vdev_split(vml[c]);
8703 			if (error == 0)
8704 				spa_history_log_internal(spa, "detach", tx,
8705 				    "vdev=%s", vml[c]->vdev_path);
8706 
8707 			vdev_free(vml[c]);
8708 		}
8709 	}
8710 	spa->spa_avz_action = AVZ_ACTION_REBUILD;
8711 	vdev_config_dirty(spa->spa_root_vdev);
8712 	spa->spa_config_splitting = NULL;
8713 	nvlist_free(nvl);
8714 	if (error == 0)
8715 		dmu_tx_commit(tx);
8716 	(void) spa_vdev_exit(spa, NULL, txg, 0);
8717 
8718 	if (zio_injection_enabled)
8719 		zio_handle_panic_injection(spa, FTAG, 3);
8720 
8721 	/* split is complete; log a history record */
8722 	spa_history_log_internal(newspa, "split", NULL,
8723 	    "from pool %s", spa_name(spa));
8724 
8725 	newspa->spa_is_splitting = B_FALSE;
8726 	kmem_free(vml, children * sizeof (vdev_t *));
8727 
8728 	/* if we're not going to mount the filesystems in userland, export */
8729 	if (exp)
8730 		error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
8731 		    B_FALSE, B_FALSE);
8732 
8733 	return (error);
8734 
8735 out:
8736 	spa_unload(newspa);
8737 	spa_deactivate(newspa);
8738 	spa_remove(newspa);
8739 
8740 	txg = spa_vdev_config_enter(spa);
8741 
8742 	/* re-online all offlined disks */
8743 	for (c = 0; c < children; c++) {
8744 		if (vml[c] != NULL)
8745 			vml[c]->vdev_offline = B_FALSE;
8746 	}
8747 
8748 	/* restart initializing or trimming disks as necessary */
8749 	spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART);
8750 	spa_async_request(spa, SPA_ASYNC_TRIM_RESTART);
8751 	spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART);
8752 
8753 	vdev_reopen(spa->spa_root_vdev);
8754 
8755 	nvlist_free(spa->spa_config_splitting);
8756 	spa->spa_config_splitting = NULL;
8757 	(void) spa_vdev_exit(spa, NULL, txg, error);
8758 
8759 	kmem_free(vml, children * sizeof (vdev_t *));
8760 	return (error);
8761 }
8762 
8763 /*
8764  * Find any device that's done replacing, or a vdev marked 'unspare' that's
8765  * currently spared, so we can detach it.
8766  */
8767 static vdev_t *
8768 spa_vdev_resilver_done_hunt(vdev_t *vd)
8769 {
8770 	vdev_t *newvd, *oldvd;
8771 
8772 	for (int c = 0; c < vd->vdev_children; c++) {
8773 		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
8774 		if (oldvd != NULL)
8775 			return (oldvd);
8776 	}
8777 
8778 	/*
8779 	 * Check for a completed replacement.  We always consider the first
8780 	 * vdev in the list to be the oldest vdev, and the last one to be
8781 	 * the newest (see spa_vdev_attach() for how that works).  In
8782 	 * the case where the newest vdev is faulted, we will not automatically
8783 	 * remove it after a resilver completes.  This is OK as it will require
8784 	 * user intervention to determine which disk the admin wishes to keep.
8785 	 */
8786 	if (vd->vdev_ops == &vdev_replacing_ops) {
8787 		ASSERT(vd->vdev_children > 1);
8788 
8789 		newvd = vd->vdev_child[vd->vdev_children - 1];
8790 		oldvd = vd->vdev_child[0];
8791 
8792 		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
8793 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
8794 		    !vdev_dtl_required(oldvd))
8795 			return (oldvd);
8796 	}
8797 
8798 	/*
8799 	 * Check for a completed resilver with the 'unspare' flag set.
8800 	 * Also potentially update faulted state.
8801 	 */
8802 	if (vd->vdev_ops == &vdev_spare_ops) {
8803 		vdev_t *first = vd->vdev_child[0];
8804 		vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
8805 
8806 		if (last->vdev_unspare) {
8807 			oldvd = first;
8808 			newvd = last;
8809 		} else if (first->vdev_unspare) {
8810 			oldvd = last;
8811 			newvd = first;
8812 		} else {
8813 			oldvd = NULL;
8814 		}
8815 
8816 		if (oldvd != NULL &&
8817 		    vdev_dtl_empty(newvd, DTL_MISSING) &&
8818 		    vdev_dtl_empty(newvd, DTL_OUTAGE) &&
8819 		    !vdev_dtl_required(oldvd))
8820 			return (oldvd);
8821 
8822 		vdev_propagate_state(vd);
8823 
8824 		/*
8825 		 * If there are more than two spares attached to a disk,
8826 		 * and those spares are not required, then we want to
8827 		 * attempt to free them up now so that they can be used
8828 		 * by other pools.  Once we're back down to a single
8829 		 * disk+spare, we stop removing them.
8830 		 */
8831 		if (vd->vdev_children > 2) {
8832 			newvd = vd->vdev_child[1];
8833 
8834 			if (newvd->vdev_isspare && last->vdev_isspare &&
8835 			    vdev_dtl_empty(last, DTL_MISSING) &&
8836 			    vdev_dtl_empty(last, DTL_OUTAGE) &&
8837 			    !vdev_dtl_required(newvd))
8838 				return (newvd);
8839 		}
8840 	}
8841 
8842 	return (NULL);
8843 }
8844 
8845 static void
8846 spa_vdev_resilver_done(spa_t *spa)
8847 {
8848 	vdev_t *vd, *pvd, *ppvd;
8849 	uint64_t guid, sguid, pguid, ppguid;
8850 
8851 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
8852 
8853 	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
8854 		pvd = vd->vdev_parent;
8855 		ppvd = pvd->vdev_parent;
8856 		guid = vd->vdev_guid;
8857 		pguid = pvd->vdev_guid;
8858 		ppguid = ppvd->vdev_guid;
8859 		sguid = 0;
8860 		/*
8861 		 * If we have just finished replacing a hot spared device, then
8862 		 * we need to detach the parent's first child (the original hot
8863 		 * spare) as well.
8864 		 */
8865 		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
8866 		    ppvd->vdev_children == 2) {
8867 			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
8868 			sguid = ppvd->vdev_child[1]->vdev_guid;
8869 		}
8870 		ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
8871 
8872 		spa_config_exit(spa, SCL_ALL, FTAG);
8873 		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
8874 			return;
8875 		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
8876 			return;
8877 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
8878 	}
8879 
8880 	spa_config_exit(spa, SCL_ALL, FTAG);
8881 
8882 	/*
8883 	 * If a detach was not performed above replace waiters will not have
8884 	 * been notified.  In which case we must do so now.
8885 	 */
8886 	spa_notify_waiters(spa);
8887 }
8888 
8889 /*
8890  * Update the stored path or FRU for this vdev.
8891  */
8892 static int
8893 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
8894     boolean_t ispath)
8895 {
8896 	vdev_t *vd;
8897 	boolean_t sync = B_FALSE;
8898 
8899 	ASSERT(spa_writeable(spa));
8900 
8901 	spa_vdev_state_enter(spa, SCL_ALL);
8902 
8903 	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
8904 		return (spa_vdev_state_exit(spa, NULL, ENOENT));
8905 
8906 	if (!vd->vdev_ops->vdev_op_leaf)
8907 		return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
8908 
8909 	if (ispath) {
8910 		if (strcmp(value, vd->vdev_path) != 0) {
8911 			spa_strfree(vd->vdev_path);
8912 			vd->vdev_path = spa_strdup(value);
8913 			sync = B_TRUE;
8914 		}
8915 	} else {
8916 		if (vd->vdev_fru == NULL) {
8917 			vd->vdev_fru = spa_strdup(value);
8918 			sync = B_TRUE;
8919 		} else if (strcmp(value, vd->vdev_fru) != 0) {
8920 			spa_strfree(vd->vdev_fru);
8921 			vd->vdev_fru = spa_strdup(value);
8922 			sync = B_TRUE;
8923 		}
8924 	}
8925 
8926 	return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
8927 }
8928 
8929 int
8930 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
8931 {
8932 	return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
8933 }
8934 
8935 int
8936 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
8937 {
8938 	return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
8939 }
8940 
8941 /*
8942  * ==========================================================================
8943  * SPA Scanning
8944  * ==========================================================================
8945  */
8946 int
8947 spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd)
8948 {
8949 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
8950 
8951 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
8952 		return (SET_ERROR(EBUSY));
8953 
8954 	return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd));
8955 }
8956 
8957 int
8958 spa_scan_stop(spa_t *spa)
8959 {
8960 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
8961 	if (dsl_scan_resilvering(spa->spa_dsl_pool))
8962 		return (SET_ERROR(EBUSY));
8963 
8964 	return (dsl_scan_cancel(spa->spa_dsl_pool));
8965 }
8966 
8967 int
8968 spa_scan(spa_t *spa, pool_scan_func_t func)
8969 {
8970 	return (spa_scan_range(spa, func, 0, 0));
8971 }
8972 
8973 int
8974 spa_scan_range(spa_t *spa, pool_scan_func_t func, uint64_t txgstart,
8975     uint64_t txgend)
8976 {
8977 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
8978 
8979 	if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
8980 		return (SET_ERROR(ENOTSUP));
8981 
8982 	if (func == POOL_SCAN_RESILVER &&
8983 	    !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER))
8984 		return (SET_ERROR(ENOTSUP));
8985 
8986 	if (func != POOL_SCAN_SCRUB && (txgstart != 0 || txgend != 0))
8987 		return (SET_ERROR(ENOTSUP));
8988 
8989 	/*
8990 	 * If a resilver was requested, but there is no DTL on a
8991 	 * writeable leaf device, we have nothing to do.
8992 	 */
8993 	if (func == POOL_SCAN_RESILVER &&
8994 	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
8995 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
8996 		return (0);
8997 	}
8998 
8999 	if (func == POOL_SCAN_ERRORSCRUB &&
9000 	    !spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG))
9001 		return (SET_ERROR(ENOTSUP));
9002 
9003 	return (dsl_scan(spa->spa_dsl_pool, func, txgstart, txgend));
9004 }
9005 
9006 /*
9007  * ==========================================================================
9008  * SPA async task processing
9009  * ==========================================================================
9010  */
9011 
9012 static void
9013 spa_async_remove(spa_t *spa, vdev_t *vd, boolean_t by_kernel)
9014 {
9015 	if (vd->vdev_remove_wanted) {
9016 		vd->vdev_remove_wanted = B_FALSE;
9017 		vd->vdev_delayed_close = B_FALSE;
9018 		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
9019 
9020 		/*
9021 		 * We want to clear the stats, but we don't want to do a full
9022 		 * vdev_clear() as that will cause us to throw away
9023 		 * degraded/faulted state as well as attempt to reopen the
9024 		 * device, all of which is a waste.
9025 		 */
9026 		vd->vdev_stat.vs_read_errors = 0;
9027 		vd->vdev_stat.vs_write_errors = 0;
9028 		vd->vdev_stat.vs_checksum_errors = 0;
9029 
9030 		vdev_state_dirty(vd->vdev_top);
9031 
9032 		/* Tell userspace that the vdev is gone. */
9033 		zfs_post_remove(spa, vd, by_kernel);
9034 	}
9035 
9036 	for (int c = 0; c < vd->vdev_children; c++)
9037 		spa_async_remove(spa, vd->vdev_child[c], by_kernel);
9038 }
9039 
9040 static void
9041 spa_async_fault_vdev(vdev_t *vd, boolean_t *suspend)
9042 {
9043 	if (vd->vdev_fault_wanted) {
9044 		vdev_state_t newstate = VDEV_STATE_FAULTED;
9045 		vd->vdev_fault_wanted = B_FALSE;
9046 
9047 		/*
9048 		 * If this device has the only valid copy of the data, then
9049 		 * back off and simply mark the vdev as degraded instead.
9050 		 */
9051 		if (!vd->vdev_top->vdev_islog && vd->vdev_aux == NULL &&
9052 		    vdev_dtl_required(vd)) {
9053 			newstate = VDEV_STATE_DEGRADED;
9054 			/* A required disk is missing so suspend the pool */
9055 			*suspend = B_TRUE;
9056 		}
9057 		vdev_set_state(vd, B_TRUE, newstate, VDEV_AUX_ERR_EXCEEDED);
9058 	}
9059 	for (int c = 0; c < vd->vdev_children; c++)
9060 		spa_async_fault_vdev(vd->vdev_child[c], suspend);
9061 }
9062 
9063 static void
9064 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
9065 {
9066 	if (!spa->spa_autoexpand)
9067 		return;
9068 
9069 	for (int c = 0; c < vd->vdev_children; c++) {
9070 		vdev_t *cvd = vd->vdev_child[c];
9071 		spa_async_autoexpand(spa, cvd);
9072 	}
9073 
9074 	if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
9075 		return;
9076 
9077 	spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND);
9078 }
9079 
9080 static __attribute__((noreturn)) void
9081 spa_async_thread(void *arg)
9082 {
9083 	spa_t *spa = (spa_t *)arg;
9084 	dsl_pool_t *dp = spa->spa_dsl_pool;
9085 	int tasks;
9086 
9087 	ASSERT(spa->spa_sync_on);
9088 
9089 	mutex_enter(&spa->spa_async_lock);
9090 	tasks = spa->spa_async_tasks;
9091 	spa->spa_async_tasks = 0;
9092 	mutex_exit(&spa->spa_async_lock);
9093 
9094 	/*
9095 	 * See if the config needs to be updated.
9096 	 */
9097 	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
9098 		uint64_t old_space, new_space;
9099 
9100 		mutex_enter(&spa_namespace_lock);
9101 		old_space = metaslab_class_get_space(spa_normal_class(spa));
9102 		old_space += metaslab_class_get_space(spa_special_class(spa));
9103 		old_space += metaslab_class_get_space(spa_dedup_class(spa));
9104 		old_space += metaslab_class_get_space(
9105 		    spa_embedded_log_class(spa));
9106 
9107 		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
9108 
9109 		new_space = metaslab_class_get_space(spa_normal_class(spa));
9110 		new_space += metaslab_class_get_space(spa_special_class(spa));
9111 		new_space += metaslab_class_get_space(spa_dedup_class(spa));
9112 		new_space += metaslab_class_get_space(
9113 		    spa_embedded_log_class(spa));
9114 		mutex_exit(&spa_namespace_lock);
9115 
9116 		/*
9117 		 * If the pool grew as a result of the config update,
9118 		 * then log an internal history event.
9119 		 */
9120 		if (new_space != old_space) {
9121 			spa_history_log_internal(spa, "vdev online", NULL,
9122 			    "pool '%s' size: %llu(+%llu)",
9123 			    spa_name(spa), (u_longlong_t)new_space,
9124 			    (u_longlong_t)(new_space - old_space));
9125 		}
9126 	}
9127 
9128 	/*
9129 	 * See if any devices need to be marked REMOVED.
9130 	 */
9131 	if (tasks & (SPA_ASYNC_REMOVE | SPA_ASYNC_REMOVE_BY_USER)) {
9132 		boolean_t by_kernel = B_TRUE;
9133 		if (tasks & SPA_ASYNC_REMOVE_BY_USER)
9134 			by_kernel = B_FALSE;
9135 		spa_vdev_state_enter(spa, SCL_NONE);
9136 		spa_async_remove(spa, spa->spa_root_vdev, by_kernel);
9137 		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
9138 			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i],
9139 			    by_kernel);
9140 		for (int i = 0; i < spa->spa_spares.sav_count; i++)
9141 			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i],
9142 			    by_kernel);
9143 		(void) spa_vdev_state_exit(spa, NULL, 0);
9144 	}
9145 
9146 	if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
9147 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
9148 		spa_async_autoexpand(spa, spa->spa_root_vdev);
9149 		spa_config_exit(spa, SCL_CONFIG, FTAG);
9150 	}
9151 
9152 	/*
9153 	 * See if any devices need to be marked faulted.
9154 	 */
9155 	if (tasks & SPA_ASYNC_FAULT_VDEV) {
9156 		spa_vdev_state_enter(spa, SCL_NONE);
9157 		boolean_t suspend = B_FALSE;
9158 		spa_async_fault_vdev(spa->spa_root_vdev, &suspend);
9159 		(void) spa_vdev_state_exit(spa, NULL, 0);
9160 		if (suspend)
9161 			zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR);
9162 	}
9163 
9164 	/*
9165 	 * If any devices are done replacing, detach them.
9166 	 */
9167 	if (tasks & SPA_ASYNC_RESILVER_DONE ||
9168 	    tasks & SPA_ASYNC_REBUILD_DONE ||
9169 	    tasks & SPA_ASYNC_DETACH_SPARE) {
9170 		spa_vdev_resilver_done(spa);
9171 	}
9172 
9173 	/*
9174 	 * Kick off a resilver.
9175 	 */
9176 	if (tasks & SPA_ASYNC_RESILVER &&
9177 	    !vdev_rebuild_active(spa->spa_root_vdev) &&
9178 	    (!dsl_scan_resilvering(dp) ||
9179 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER)))
9180 		dsl_scan_restart_resilver(dp, 0);
9181 
9182 	if (tasks & SPA_ASYNC_INITIALIZE_RESTART) {
9183 		mutex_enter(&spa_namespace_lock);
9184 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
9185 		vdev_initialize_restart(spa->spa_root_vdev);
9186 		spa_config_exit(spa, SCL_CONFIG, FTAG);
9187 		mutex_exit(&spa_namespace_lock);
9188 	}
9189 
9190 	if (tasks & SPA_ASYNC_TRIM_RESTART) {
9191 		mutex_enter(&spa_namespace_lock);
9192 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
9193 		vdev_trim_restart(spa->spa_root_vdev);
9194 		spa_config_exit(spa, SCL_CONFIG, FTAG);
9195 		mutex_exit(&spa_namespace_lock);
9196 	}
9197 
9198 	if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) {
9199 		mutex_enter(&spa_namespace_lock);
9200 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
9201 		vdev_autotrim_restart(spa);
9202 		spa_config_exit(spa, SCL_CONFIG, FTAG);
9203 		mutex_exit(&spa_namespace_lock);
9204 	}
9205 
9206 	/*
9207 	 * Kick off L2 cache whole device TRIM.
9208 	 */
9209 	if (tasks & SPA_ASYNC_L2CACHE_TRIM) {
9210 		mutex_enter(&spa_namespace_lock);
9211 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
9212 		vdev_trim_l2arc(spa);
9213 		spa_config_exit(spa, SCL_CONFIG, FTAG);
9214 		mutex_exit(&spa_namespace_lock);
9215 	}
9216 
9217 	/*
9218 	 * Kick off L2 cache rebuilding.
9219 	 */
9220 	if (tasks & SPA_ASYNC_L2CACHE_REBUILD) {
9221 		mutex_enter(&spa_namespace_lock);
9222 		spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER);
9223 		l2arc_spa_rebuild_start(spa);
9224 		spa_config_exit(spa, SCL_L2ARC, FTAG);
9225 		mutex_exit(&spa_namespace_lock);
9226 	}
9227 
9228 	/*
9229 	 * Let the world know that we're done.
9230 	 */
9231 	mutex_enter(&spa->spa_async_lock);
9232 	spa->spa_async_thread = NULL;
9233 	cv_broadcast(&spa->spa_async_cv);
9234 	mutex_exit(&spa->spa_async_lock);
9235 	thread_exit();
9236 }
9237 
9238 void
9239 spa_async_suspend(spa_t *spa)
9240 {
9241 	mutex_enter(&spa->spa_async_lock);
9242 	spa->spa_async_suspended++;
9243 	while (spa->spa_async_thread != NULL)
9244 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
9245 	mutex_exit(&spa->spa_async_lock);
9246 
9247 	spa_vdev_remove_suspend(spa);
9248 
9249 	zthr_t *condense_thread = spa->spa_condense_zthr;
9250 	if (condense_thread != NULL)
9251 		zthr_cancel(condense_thread);
9252 
9253 	zthr_t *raidz_expand_thread = spa->spa_raidz_expand_zthr;
9254 	if (raidz_expand_thread != NULL)
9255 		zthr_cancel(raidz_expand_thread);
9256 
9257 	zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr;
9258 	if (discard_thread != NULL)
9259 		zthr_cancel(discard_thread);
9260 
9261 	zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr;
9262 	if (ll_delete_thread != NULL)
9263 		zthr_cancel(ll_delete_thread);
9264 
9265 	zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;
9266 	if (ll_condense_thread != NULL)
9267 		zthr_cancel(ll_condense_thread);
9268 }
9269 
9270 void
9271 spa_async_resume(spa_t *spa)
9272 {
9273 	mutex_enter(&spa->spa_async_lock);
9274 	ASSERT(spa->spa_async_suspended != 0);
9275 	spa->spa_async_suspended--;
9276 	mutex_exit(&spa->spa_async_lock);
9277 	spa_restart_removal(spa);
9278 
9279 	zthr_t *condense_thread = spa->spa_condense_zthr;
9280 	if (condense_thread != NULL)
9281 		zthr_resume(condense_thread);
9282 
9283 	zthr_t *raidz_expand_thread = spa->spa_raidz_expand_zthr;
9284 	if (raidz_expand_thread != NULL)
9285 		zthr_resume(raidz_expand_thread);
9286 
9287 	zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr;
9288 	if (discard_thread != NULL)
9289 		zthr_resume(discard_thread);
9290 
9291 	zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr;
9292 	if (ll_delete_thread != NULL)
9293 		zthr_resume(ll_delete_thread);
9294 
9295 	zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;
9296 	if (ll_condense_thread != NULL)
9297 		zthr_resume(ll_condense_thread);
9298 }
9299 
9300 static boolean_t
9301 spa_async_tasks_pending(spa_t *spa)
9302 {
9303 	uint_t non_config_tasks;
9304 	uint_t config_task;
9305 	boolean_t config_task_suspended;
9306 
9307 	non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
9308 	config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
9309 	if (spa->spa_ccw_fail_time == 0) {
9310 		config_task_suspended = B_FALSE;
9311 	} else {
9312 		config_task_suspended =
9313 		    (gethrtime() - spa->spa_ccw_fail_time) <
9314 		    ((hrtime_t)zfs_ccw_retry_interval * NANOSEC);
9315 	}
9316 
9317 	return (non_config_tasks || (config_task && !config_task_suspended));
9318 }
9319 
9320 static void
9321 spa_async_dispatch(spa_t *spa)
9322 {
9323 	mutex_enter(&spa->spa_async_lock);
9324 	if (spa_async_tasks_pending(spa) &&
9325 	    !spa->spa_async_suspended &&
9326 	    spa->spa_async_thread == NULL)
9327 		spa->spa_async_thread = thread_create(NULL, 0,
9328 		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
9329 	mutex_exit(&spa->spa_async_lock);
9330 }
9331 
9332 void
9333 spa_async_request(spa_t *spa, int task)
9334 {
9335 	zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
9336 	mutex_enter(&spa->spa_async_lock);
9337 	spa->spa_async_tasks |= task;
9338 	mutex_exit(&spa->spa_async_lock);
9339 }
9340 
9341 int
9342 spa_async_tasks(spa_t *spa)
9343 {
9344 	return (spa->spa_async_tasks);
9345 }
9346 
9347 /*
9348  * ==========================================================================
9349  * SPA syncing routines
9350  * ==========================================================================
9351  */
9352 
9353 
9354 static int
9355 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
9356     dmu_tx_t *tx)
9357 {
9358 	bpobj_t *bpo = arg;
9359 	bpobj_enqueue(bpo, bp, bp_freed, tx);
9360 	return (0);
9361 }
9362 
9363 int
9364 bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
9365 {
9366 	return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx));
9367 }
9368 
9369 int
9370 bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
9371 {
9372 	return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx));
9373 }
9374 
9375 static int
9376 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
9377 {
9378 	zio_t *pio = arg;
9379 
9380 	zio_nowait(zio_free_sync(pio, pio->io_spa, dmu_tx_get_txg(tx), bp,
9381 	    pio->io_flags));
9382 	return (0);
9383 }
9384 
9385 static int
9386 bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
9387     dmu_tx_t *tx)
9388 {
9389 	ASSERT(!bp_freed);
9390 	return (spa_free_sync_cb(arg, bp, tx));
9391 }
9392 
9393 /*
9394  * Note: this simple function is not inlined to make it easier to dtrace the
9395  * amount of time spent syncing frees.
9396  */
9397 static void
9398 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
9399 {
9400 	zio_t *zio = zio_root(spa, NULL, NULL, 0);
9401 	bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
9402 	VERIFY(zio_wait(zio) == 0);
9403 }
9404 
9405 /*
9406  * Note: this simple function is not inlined to make it easier to dtrace the
9407  * amount of time spent syncing deferred frees.
9408  */
9409 static void
9410 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
9411 {
9412 	if (spa_sync_pass(spa) != 1)
9413 		return;
9414 
9415 	/*
9416 	 * Note:
9417 	 * If the log space map feature is active, we stop deferring
9418 	 * frees to the next TXG and therefore running this function
9419 	 * would be considered a no-op as spa_deferred_bpobj should
9420 	 * not have any entries.
9421 	 *
9422 	 * That said we run this function anyway (instead of returning
9423 	 * immediately) for the edge-case scenario where we just
9424 	 * activated the log space map feature in this TXG but we have
9425 	 * deferred frees from the previous TXG.
9426 	 */
9427 	zio_t *zio = zio_root(spa, NULL, NULL, 0);
9428 	VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
9429 	    bpobj_spa_free_sync_cb, zio, tx), ==, 0);
9430 	VERIFY0(zio_wait(zio));
9431 }
9432 
9433 static void
9434 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
9435 {
9436 	char *packed = NULL;
9437 	size_t bufsize;
9438 	size_t nvsize = 0;
9439 	dmu_buf_t *db;
9440 
9441 	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
9442 
9443 	/*
9444 	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
9445 	 * information.  This avoids the dmu_buf_will_dirty() path and
9446 	 * saves us a pre-read to get data we don't actually care about.
9447 	 */
9448 	bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
9449 	packed = vmem_alloc(bufsize, KM_SLEEP);
9450 
9451 	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
9452 	    KM_SLEEP) == 0);
9453 	memset(packed + nvsize, 0, bufsize - nvsize);
9454 
9455 	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
9456 
9457 	vmem_free(packed, bufsize);
9458 
9459 	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9460 	dmu_buf_will_dirty(db, tx);
9461 	*(uint64_t *)db->db_data = nvsize;
9462 	dmu_buf_rele(db, FTAG);
9463 }
9464 
9465 static void
9466 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
9467     const char *config, const char *entry)
9468 {
9469 	nvlist_t *nvroot;
9470 	nvlist_t **list;
9471 	int i;
9472 
9473 	if (!sav->sav_sync)
9474 		return;
9475 
9476 	/*
9477 	 * Update the MOS nvlist describing the list of available devices.
9478 	 * spa_validate_aux() will have already made sure this nvlist is
9479 	 * valid and the vdevs are labeled appropriately.
9480 	 */
9481 	if (sav->sav_object == 0) {
9482 		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
9483 		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
9484 		    sizeof (uint64_t), tx);
9485 		VERIFY(zap_update(spa->spa_meta_objset,
9486 		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
9487 		    &sav->sav_object, tx) == 0);
9488 	}
9489 
9490 	nvroot = fnvlist_alloc();
9491 	if (sav->sav_count == 0) {
9492 		fnvlist_add_nvlist_array(nvroot, config,
9493 		    (const nvlist_t * const *)NULL, 0);
9494 	} else {
9495 		list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP);
9496 		for (i = 0; i < sav->sav_count; i++)
9497 			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
9498 			    B_FALSE, VDEV_CONFIG_L2CACHE);
9499 		fnvlist_add_nvlist_array(nvroot, config,
9500 		    (const nvlist_t * const *)list, sav->sav_count);
9501 		for (i = 0; i < sav->sav_count; i++)
9502 			nvlist_free(list[i]);
9503 		kmem_free(list, sav->sav_count * sizeof (void *));
9504 	}
9505 
9506 	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
9507 	nvlist_free(nvroot);
9508 
9509 	sav->sav_sync = B_FALSE;
9510 }
9511 
9512 /*
9513  * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
9514  * The all-vdev ZAP must be empty.
9515  */
9516 static void
9517 spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx)
9518 {
9519 	spa_t *spa = vd->vdev_spa;
9520 
9521 	if (vd->vdev_root_zap != 0 &&
9522 	    spa_feature_is_active(spa, SPA_FEATURE_AVZ_V2)) {
9523 		VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
9524 		    vd->vdev_root_zap, tx));
9525 	}
9526 	if (vd->vdev_top_zap != 0) {
9527 		VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
9528 		    vd->vdev_top_zap, tx));
9529 	}
9530 	if (vd->vdev_leaf_zap != 0) {
9531 		VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
9532 		    vd->vdev_leaf_zap, tx));
9533 	}
9534 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
9535 		spa_avz_build(vd->vdev_child[i], avz, tx);
9536 	}
9537 }
9538 
9539 static void
9540 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
9541 {
9542 	nvlist_t *config;
9543 
9544 	/*
9545 	 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
9546 	 * its config may not be dirty but we still need to build per-vdev ZAPs.
9547 	 * Similarly, if the pool is being assembled (e.g. after a split), we
9548 	 * need to rebuild the AVZ although the config may not be dirty.
9549 	 */
9550 	if (list_is_empty(&spa->spa_config_dirty_list) &&
9551 	    spa->spa_avz_action == AVZ_ACTION_NONE)
9552 		return;
9553 
9554 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
9555 
9556 	ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE ||
9557 	    spa->spa_avz_action == AVZ_ACTION_INITIALIZE ||
9558 	    spa->spa_all_vdev_zaps != 0);
9559 
9560 	if (spa->spa_avz_action == AVZ_ACTION_REBUILD) {
9561 		/* Make and build the new AVZ */
9562 		uint64_t new_avz = zap_create(spa->spa_meta_objset,
9563 		    DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
9564 		spa_avz_build(spa->spa_root_vdev, new_avz, tx);
9565 
9566 		/* Diff old AVZ with new one */
9567 		zap_cursor_t zc;
9568 		zap_attribute_t *za = zap_attribute_alloc();
9569 
9570 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
9571 		    spa->spa_all_vdev_zaps);
9572 		    zap_cursor_retrieve(&zc, za) == 0;
9573 		    zap_cursor_advance(&zc)) {
9574 			uint64_t vdzap = za->za_first_integer;
9575 			if (zap_lookup_int(spa->spa_meta_objset, new_avz,
9576 			    vdzap) == ENOENT) {
9577 				/*
9578 				 * ZAP is listed in old AVZ but not in new one;
9579 				 * destroy it
9580 				 */
9581 				VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap,
9582 				    tx));
9583 			}
9584 		}
9585 
9586 		zap_cursor_fini(&zc);
9587 		zap_attribute_free(za);
9588 
9589 		/* Destroy the old AVZ */
9590 		VERIFY0(zap_destroy(spa->spa_meta_objset,
9591 		    spa->spa_all_vdev_zaps, tx));
9592 
9593 		/* Replace the old AVZ in the dir obj with the new one */
9594 		VERIFY0(zap_update(spa->spa_meta_objset,
9595 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP,
9596 		    sizeof (new_avz), 1, &new_avz, tx));
9597 
9598 		spa->spa_all_vdev_zaps = new_avz;
9599 	} else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) {
9600 		zap_cursor_t zc;
9601 		zap_attribute_t *za = zap_attribute_alloc();
9602 
9603 		/* Walk through the AVZ and destroy all listed ZAPs */
9604 		for (zap_cursor_init(&zc, spa->spa_meta_objset,
9605 		    spa->spa_all_vdev_zaps);
9606 		    zap_cursor_retrieve(&zc, za) == 0;
9607 		    zap_cursor_advance(&zc)) {
9608 			uint64_t zap = za->za_first_integer;
9609 			VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx));
9610 		}
9611 
9612 		zap_cursor_fini(&zc);
9613 		zap_attribute_free(za);
9614 
9615 		/* Destroy and unlink the AVZ itself */
9616 		VERIFY0(zap_destroy(spa->spa_meta_objset,
9617 		    spa->spa_all_vdev_zaps, tx));
9618 		VERIFY0(zap_remove(spa->spa_meta_objset,
9619 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx));
9620 		spa->spa_all_vdev_zaps = 0;
9621 	}
9622 
9623 	if (spa->spa_all_vdev_zaps == 0) {
9624 		spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset,
9625 		    DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
9626 		    DMU_POOL_VDEV_ZAP_MAP, tx);
9627 	}
9628 	spa->spa_avz_action = AVZ_ACTION_NONE;
9629 
9630 	/* Create ZAPs for vdevs that don't have them. */
9631 	vdev_construct_zaps(spa->spa_root_vdev, tx);
9632 
9633 	config = spa_config_generate(spa, spa->spa_root_vdev,
9634 	    dmu_tx_get_txg(tx), B_FALSE);
9635 
9636 	/*
9637 	 * If we're upgrading the spa version then make sure that
9638 	 * the config object gets updated with the correct version.
9639 	 */
9640 	if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
9641 		fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
9642 		    spa->spa_uberblock.ub_version);
9643 
9644 	spa_config_exit(spa, SCL_STATE, FTAG);
9645 
9646 	nvlist_free(spa->spa_config_syncing);
9647 	spa->spa_config_syncing = config;
9648 
9649 	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
9650 }
9651 
9652 static void
9653 spa_sync_version(void *arg, dmu_tx_t *tx)
9654 {
9655 	uint64_t *versionp = arg;
9656 	uint64_t version = *versionp;
9657 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
9658 
9659 	/*
9660 	 * Setting the version is special cased when first creating the pool.
9661 	 */
9662 	ASSERT(tx->tx_txg != TXG_INITIAL);
9663 
9664 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
9665 	ASSERT(version >= spa_version(spa));
9666 
9667 	spa->spa_uberblock.ub_version = version;
9668 	vdev_config_dirty(spa->spa_root_vdev);
9669 	spa_history_log_internal(spa, "set", tx, "version=%lld",
9670 	    (longlong_t)version);
9671 }
9672 
9673 /*
9674  * Set zpool properties.
9675  */
9676 static void
9677 spa_sync_props(void *arg, dmu_tx_t *tx)
9678 {
9679 	nvlist_t *nvp = arg;
9680 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
9681 	objset_t *mos = spa->spa_meta_objset;
9682 	nvpair_t *elem = NULL;
9683 
9684 	mutex_enter(&spa->spa_props_lock);
9685 
9686 	while ((elem = nvlist_next_nvpair(nvp, elem))) {
9687 		uint64_t intval;
9688 		const char *strval, *fname;
9689 		zpool_prop_t prop;
9690 		const char *propname;
9691 		const char *elemname = nvpair_name(elem);
9692 		zprop_type_t proptype;
9693 		spa_feature_t fid;
9694 
9695 		switch (prop = zpool_name_to_prop(elemname)) {
9696 		case ZPOOL_PROP_VERSION:
9697 			intval = fnvpair_value_uint64(elem);
9698 			/*
9699 			 * The version is synced separately before other
9700 			 * properties and should be correct by now.
9701 			 */
9702 			ASSERT3U(spa_version(spa), >=, intval);
9703 			break;
9704 
9705 		case ZPOOL_PROP_ALTROOT:
9706 			/*
9707 			 * 'altroot' is a non-persistent property. It should
9708 			 * have been set temporarily at creation or import time.
9709 			 */
9710 			ASSERT(spa->spa_root != NULL);
9711 			break;
9712 
9713 		case ZPOOL_PROP_READONLY:
9714 		case ZPOOL_PROP_CACHEFILE:
9715 			/*
9716 			 * 'readonly' and 'cachefile' are also non-persistent
9717 			 * properties.
9718 			 */
9719 			break;
9720 		case ZPOOL_PROP_COMMENT:
9721 			strval = fnvpair_value_string(elem);
9722 			if (spa->spa_comment != NULL)
9723 				spa_strfree(spa->spa_comment);
9724 			spa->spa_comment = spa_strdup(strval);
9725 			/*
9726 			 * We need to dirty the configuration on all the vdevs
9727 			 * so that their labels get updated.  We also need to
9728 			 * update the cache file to keep it in sync with the
9729 			 * MOS version. It's unnecessary to do this for pool
9730 			 * creation since the vdev's configuration has already
9731 			 * been dirtied.
9732 			 */
9733 			if (tx->tx_txg != TXG_INITIAL) {
9734 				vdev_config_dirty(spa->spa_root_vdev);
9735 				spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
9736 			}
9737 			spa_history_log_internal(spa, "set", tx,
9738 			    "%s=%s", elemname, strval);
9739 			break;
9740 		case ZPOOL_PROP_COMPATIBILITY:
9741 			strval = fnvpair_value_string(elem);
9742 			if (spa->spa_compatibility != NULL)
9743 				spa_strfree(spa->spa_compatibility);
9744 			spa->spa_compatibility = spa_strdup(strval);
9745 			/*
9746 			 * Dirty the configuration on vdevs as above.
9747 			 */
9748 			if (tx->tx_txg != TXG_INITIAL) {
9749 				vdev_config_dirty(spa->spa_root_vdev);
9750 				spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
9751 			}
9752 
9753 			spa_history_log_internal(spa, "set", tx,
9754 			    "%s=%s", nvpair_name(elem), strval);
9755 			break;
9756 
9757 		case ZPOOL_PROP_INVAL:
9758 			if (zpool_prop_feature(elemname)) {
9759 				fname = strchr(elemname, '@') + 1;
9760 				VERIFY0(zfeature_lookup_name(fname, &fid));
9761 
9762 				spa_feature_enable(spa, fid, tx);
9763 				spa_history_log_internal(spa, "set", tx,
9764 				    "%s=enabled", elemname);
9765 				break;
9766 			} else if (!zfs_prop_user(elemname)) {
9767 				ASSERT(zpool_prop_feature(elemname));
9768 				break;
9769 			}
9770 			zfs_fallthrough;
9771 		default:
9772 			/*
9773 			 * Set pool property values in the poolprops mos object.
9774 			 */
9775 			if (spa->spa_pool_props_object == 0) {
9776 				spa->spa_pool_props_object =
9777 				    zap_create_link(mos, DMU_OT_POOL_PROPS,
9778 				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
9779 				    tx);
9780 			}
9781 
9782 			/* normalize the property name */
9783 			if (prop == ZPOOL_PROP_INVAL) {
9784 				propname = elemname;
9785 				proptype = PROP_TYPE_STRING;
9786 			} else {
9787 				propname = zpool_prop_to_name(prop);
9788 				proptype = zpool_prop_get_type(prop);
9789 			}
9790 
9791 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
9792 				ASSERT(proptype == PROP_TYPE_STRING);
9793 				strval = fnvpair_value_string(elem);
9794 				if (strlen(strval) == 0) {
9795 					/* remove the property if value == "" */
9796 					(void) zap_remove(mos,
9797 					    spa->spa_pool_props_object,
9798 					    propname, tx);
9799 				} else {
9800 					VERIFY0(zap_update(mos,
9801 					    spa->spa_pool_props_object,
9802 					    propname, 1, strlen(strval) + 1,
9803 					    strval, tx));
9804 				}
9805 				spa_history_log_internal(spa, "set", tx,
9806 				    "%s=%s", elemname, strval);
9807 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
9808 				intval = fnvpair_value_uint64(elem);
9809 
9810 				if (proptype == PROP_TYPE_INDEX) {
9811 					const char *unused;
9812 					VERIFY0(zpool_prop_index_to_string(
9813 					    prop, intval, &unused));
9814 				}
9815 				VERIFY0(zap_update(mos,
9816 				    spa->spa_pool_props_object, propname,
9817 				    8, 1, &intval, tx));
9818 				spa_history_log_internal(spa, "set", tx,
9819 				    "%s=%lld", elemname,
9820 				    (longlong_t)intval);
9821 
9822 				switch (prop) {
9823 				case ZPOOL_PROP_DELEGATION:
9824 					spa->spa_delegation = intval;
9825 					break;
9826 				case ZPOOL_PROP_BOOTFS:
9827 					spa->spa_bootfs = intval;
9828 					break;
9829 				case ZPOOL_PROP_FAILUREMODE:
9830 					spa->spa_failmode = intval;
9831 					break;
9832 				case ZPOOL_PROP_AUTOTRIM:
9833 					spa->spa_autotrim = intval;
9834 					spa_async_request(spa,
9835 					    SPA_ASYNC_AUTOTRIM_RESTART);
9836 					break;
9837 				case ZPOOL_PROP_AUTOEXPAND:
9838 					spa->spa_autoexpand = intval;
9839 					if (tx->tx_txg != TXG_INITIAL)
9840 						spa_async_request(spa,
9841 						    SPA_ASYNC_AUTOEXPAND);
9842 					break;
9843 				case ZPOOL_PROP_MULTIHOST:
9844 					spa->spa_multihost = intval;
9845 					break;
9846 				case ZPOOL_PROP_DEDUP_TABLE_QUOTA:
9847 					spa->spa_dedup_table_quota = intval;
9848 					break;
9849 				default:
9850 					break;
9851 				}
9852 			} else {
9853 				ASSERT(0); /* not allowed */
9854 			}
9855 		}
9856 
9857 	}
9858 
9859 	mutex_exit(&spa->spa_props_lock);
9860 }
9861 
9862 /*
9863  * Perform one-time upgrade on-disk changes.  spa_version() does not
9864  * reflect the new version this txg, so there must be no changes this
9865  * txg to anything that the upgrade code depends on after it executes.
9866  * Therefore this must be called after dsl_pool_sync() does the sync
9867  * tasks.
9868  */
9869 static void
9870 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
9871 {
9872 	if (spa_sync_pass(spa) != 1)
9873 		return;
9874 
9875 	dsl_pool_t *dp = spa->spa_dsl_pool;
9876 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
9877 
9878 	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
9879 	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
9880 		dsl_pool_create_origin(dp, tx);
9881 
9882 		/* Keeping the origin open increases spa_minref */
9883 		spa->spa_minref += 3;
9884 	}
9885 
9886 	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
9887 	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
9888 		dsl_pool_upgrade_clones(dp, tx);
9889 	}
9890 
9891 	if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
9892 	    spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
9893 		dsl_pool_upgrade_dir_clones(dp, tx);
9894 
9895 		/* Keeping the freedir open increases spa_minref */
9896 		spa->spa_minref += 3;
9897 	}
9898 
9899 	if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
9900 	    spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
9901 		spa_feature_create_zap_objects(spa, tx);
9902 	}
9903 
9904 	/*
9905 	 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9906 	 * when possibility to use lz4 compression for metadata was added
9907 	 * Old pools that have this feature enabled must be upgraded to have
9908 	 * this feature active
9909 	 */
9910 	if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
9911 		boolean_t lz4_en = spa_feature_is_enabled(spa,
9912 		    SPA_FEATURE_LZ4_COMPRESS);
9913 		boolean_t lz4_ac = spa_feature_is_active(spa,
9914 		    SPA_FEATURE_LZ4_COMPRESS);
9915 
9916 		if (lz4_en && !lz4_ac)
9917 			spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
9918 	}
9919 
9920 	/*
9921 	 * If we haven't written the salt, do so now.  Note that the
9922 	 * feature may not be activated yet, but that's fine since
9923 	 * the presence of this ZAP entry is backwards compatible.
9924 	 */
9925 	if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
9926 	    DMU_POOL_CHECKSUM_SALT) == ENOENT) {
9927 		VERIFY0(zap_add(spa->spa_meta_objset,
9928 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
9929 		    sizeof (spa->spa_cksum_salt.zcs_bytes),
9930 		    spa->spa_cksum_salt.zcs_bytes, tx));
9931 	}
9932 
9933 	rrw_exit(&dp->dp_config_rwlock, FTAG);
9934 }
9935 
9936 static void
9937 vdev_indirect_state_sync_verify(vdev_t *vd)
9938 {
9939 	vdev_indirect_mapping_t *vim __maybe_unused = vd->vdev_indirect_mapping;
9940 	vdev_indirect_births_t *vib __maybe_unused = vd->vdev_indirect_births;
9941 
9942 	if (vd->vdev_ops == &vdev_indirect_ops) {
9943 		ASSERT(vim != NULL);
9944 		ASSERT(vib != NULL);
9945 	}
9946 
9947 	uint64_t obsolete_sm_object = 0;
9948 	ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
9949 	if (obsolete_sm_object != 0) {
9950 		ASSERT(vd->vdev_obsolete_sm != NULL);
9951 		ASSERT(vd->vdev_removing ||
9952 		    vd->vdev_ops == &vdev_indirect_ops);
9953 		ASSERT(vdev_indirect_mapping_num_entries(vim) > 0);
9954 		ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0);
9955 		ASSERT3U(obsolete_sm_object, ==,
9956 		    space_map_object(vd->vdev_obsolete_sm));
9957 		ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=,
9958 		    space_map_allocated(vd->vdev_obsolete_sm));
9959 	}
9960 	ASSERT(vd->vdev_obsolete_segments != NULL);
9961 
9962 	/*
9963 	 * Since frees / remaps to an indirect vdev can only
9964 	 * happen in syncing context, the obsolete segments
9965 	 * tree must be empty when we start syncing.
9966 	 */
9967 	ASSERT0(zfs_range_tree_space(vd->vdev_obsolete_segments));
9968 }
9969 
9970 /*
9971  * Set the top-level vdev's max queue depth. Evaluate each top-level's
9972  * async write queue depth in case it changed. The max queue depth will
9973  * not change in the middle of syncing out this txg.
9974  */
9975 static void
9976 spa_sync_adjust_vdev_max_queue_depth(spa_t *spa)
9977 {
9978 	ASSERT(spa_writeable(spa));
9979 
9980 	metaslab_class_balance(spa_normal_class(spa), B_TRUE);
9981 	metaslab_class_balance(spa_special_class(spa), B_TRUE);
9982 	metaslab_class_balance(spa_dedup_class(spa), B_TRUE);
9983 }
9984 
9985 static void
9986 spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx)
9987 {
9988 	ASSERT(spa_writeable(spa));
9989 
9990 	vdev_t *rvd = spa->spa_root_vdev;
9991 	for (int c = 0; c < rvd->vdev_children; c++) {
9992 		vdev_t *vd = rvd->vdev_child[c];
9993 		vdev_indirect_state_sync_verify(vd);
9994 
9995 		if (vdev_indirect_should_condense(vd)) {
9996 			spa_condense_indirect_start_sync(vd, tx);
9997 			break;
9998 		}
9999 	}
10000 }
10001 
10002 static void
10003 spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx)
10004 {
10005 	objset_t *mos = spa->spa_meta_objset;
10006 	dsl_pool_t *dp = spa->spa_dsl_pool;
10007 	uint64_t txg = tx->tx_txg;
10008 	bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
10009 
10010 	do {
10011 		int pass = ++spa->spa_sync_pass;
10012 
10013 		spa_sync_config_object(spa, tx);
10014 		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
10015 		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
10016 		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
10017 		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
10018 		spa_errlog_sync(spa, txg);
10019 		dsl_pool_sync(dp, txg);
10020 
10021 		if (pass < zfs_sync_pass_deferred_free ||
10022 		    spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) {
10023 			/*
10024 			 * If the log space map feature is active we don't
10025 			 * care about deferred frees and the deferred bpobj
10026 			 * as the log space map should effectively have the
10027 			 * same results (i.e. appending only to one object).
10028 			 */
10029 			spa_sync_frees(spa, free_bpl, tx);
10030 		} else {
10031 			/*
10032 			 * We can not defer frees in pass 1, because
10033 			 * we sync the deferred frees later in pass 1.
10034 			 */
10035 			ASSERT3U(pass, >, 1);
10036 			bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb,
10037 			    &spa->spa_deferred_bpobj, tx);
10038 		}
10039 
10040 		brt_sync(spa, txg);
10041 		ddt_sync(spa, txg);
10042 		dsl_scan_sync(dp, tx);
10043 		dsl_errorscrub_sync(dp, tx);
10044 		svr_sync(spa, tx);
10045 		spa_sync_upgrades(spa, tx);
10046 
10047 		spa_flush_metaslabs(spa, tx);
10048 
10049 		vdev_t *vd = NULL;
10050 		while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
10051 		    != NULL)
10052 			vdev_sync(vd, txg);
10053 
10054 		if (pass == 1) {
10055 			/*
10056 			 * dsl_pool_sync() -> dp_sync_tasks may have dirtied
10057 			 * the config. If that happens, this txg should not
10058 			 * be a no-op. So we must sync the config to the MOS
10059 			 * before checking for no-op.
10060 			 *
10061 			 * Note that when the config is dirty, it will
10062 			 * be written to the MOS (i.e. the MOS will be
10063 			 * dirtied) every time we call spa_sync_config_object()
10064 			 * in this txg.  Therefore we can't call this after
10065 			 * dsl_pool_sync() every pass, because it would
10066 			 * prevent us from converging, since we'd dirty
10067 			 * the MOS every pass.
10068 			 *
10069 			 * Sync tasks can only be processed in pass 1, so
10070 			 * there's no need to do this in later passes.
10071 			 */
10072 			spa_sync_config_object(spa, tx);
10073 		}
10074 
10075 		/*
10076 		 * Note: We need to check if the MOS is dirty because we could
10077 		 * have marked the MOS dirty without updating the uberblock
10078 		 * (e.g. if we have sync tasks but no dirty user data). We need
10079 		 * to check the uberblock's rootbp because it is updated if we
10080 		 * have synced out dirty data (though in this case the MOS will
10081 		 * most likely also be dirty due to second order effects, we
10082 		 * don't want to rely on that here).
10083 		 */
10084 		if (pass == 1 &&
10085 		    BP_GET_LOGICAL_BIRTH(&spa->spa_uberblock.ub_rootbp) < txg &&
10086 		    !dmu_objset_is_dirty(mos, txg)) {
10087 			/*
10088 			 * Nothing changed on the first pass, therefore this
10089 			 * TXG is a no-op. Avoid syncing deferred frees, so
10090 			 * that we can keep this TXG as a no-op.
10091 			 */
10092 			ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
10093 			ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
10094 			ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
10095 			ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg));
10096 			break;
10097 		}
10098 
10099 		spa_sync_deferred_frees(spa, tx);
10100 	} while (dmu_objset_is_dirty(mos, txg));
10101 }
10102 
10103 /*
10104  * Rewrite the vdev configuration (which includes the uberblock) to
10105  * commit the transaction group.
10106  *
10107  * If there are no dirty vdevs, we sync the uberblock to a few random
10108  * top-level vdevs that are known to be visible in the config cache
10109  * (see spa_vdev_add() for a complete description). If there *are* dirty
10110  * vdevs, sync the uberblock to all vdevs.
10111  */
10112 static void
10113 spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx)
10114 {
10115 	vdev_t *rvd = spa->spa_root_vdev;
10116 	uint64_t txg = tx->tx_txg;
10117 
10118 	for (;;) {
10119 		int error = 0;
10120 
10121 		/*
10122 		 * We hold SCL_STATE to prevent vdev open/close/etc.
10123 		 * while we're attempting to write the vdev labels.
10124 		 */
10125 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
10126 
10127 		if (list_is_empty(&spa->spa_config_dirty_list)) {
10128 			vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL };
10129 			int svdcount = 0;
10130 			int children = rvd->vdev_children;
10131 			int c0 = random_in_range(children);
10132 
10133 			for (int c = 0; c < children; c++) {
10134 				vdev_t *vd =
10135 				    rvd->vdev_child[(c0 + c) % children];
10136 
10137 				/* Stop when revisiting the first vdev */
10138 				if (c > 0 && svd[0] == vd)
10139 					break;
10140 
10141 				if (vd->vdev_ms_array == 0 ||
10142 				    vd->vdev_islog ||
10143 				    !vdev_is_concrete(vd))
10144 					continue;
10145 
10146 				svd[svdcount++] = vd;
10147 				if (svdcount == SPA_SYNC_MIN_VDEVS)
10148 					break;
10149 			}
10150 			error = vdev_config_sync(svd, svdcount, txg);
10151 		} else {
10152 			error = vdev_config_sync(rvd->vdev_child,
10153 			    rvd->vdev_children, txg);
10154 		}
10155 
10156 		if (error == 0)
10157 			spa->spa_last_synced_guid = rvd->vdev_guid;
10158 
10159 		spa_config_exit(spa, SCL_STATE, FTAG);
10160 
10161 		if (error == 0)
10162 			break;
10163 		zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR);
10164 		zio_resume_wait(spa);
10165 	}
10166 }
10167 
10168 /*
10169  * Sync the specified transaction group.  New blocks may be dirtied as
10170  * part of the process, so we iterate until it converges.
10171  */
10172 void
10173 spa_sync(spa_t *spa, uint64_t txg)
10174 {
10175 	vdev_t *vd = NULL;
10176 
10177 	VERIFY(spa_writeable(spa));
10178 
10179 	/*
10180 	 * Wait for i/os issued in open context that need to complete
10181 	 * before this txg syncs.
10182 	 */
10183 	(void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]);
10184 	spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL,
10185 	    ZIO_FLAG_CANFAIL);
10186 
10187 	/*
10188 	 * Now that there can be no more cloning in this transaction group,
10189 	 * but we are still before issuing frees, we can process pending BRT
10190 	 * updates.
10191 	 */
10192 	brt_pending_apply(spa, txg);
10193 
10194 	/*
10195 	 * Lock out configuration changes.
10196 	 */
10197 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
10198 
10199 	spa->spa_syncing_txg = txg;
10200 	spa->spa_sync_pass = 0;
10201 
10202 	/*
10203 	 * If there are any pending vdev state changes, convert them
10204 	 * into config changes that go out with this transaction group.
10205 	 */
10206 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
10207 	while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
10208 		/* Avoid holding the write lock unless actually necessary */
10209 		if (vd->vdev_aux == NULL) {
10210 			vdev_state_clean(vd);
10211 			vdev_config_dirty(vd);
10212 			continue;
10213 		}
10214 		/*
10215 		 * We need the write lock here because, for aux vdevs,
10216 		 * calling vdev_config_dirty() modifies sav_config.
10217 		 * This is ugly and will become unnecessary when we
10218 		 * eliminate the aux vdev wart by integrating all vdevs
10219 		 * into the root vdev tree.
10220 		 */
10221 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10222 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
10223 		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
10224 			vdev_state_clean(vd);
10225 			vdev_config_dirty(vd);
10226 		}
10227 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10228 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
10229 	}
10230 	spa_config_exit(spa, SCL_STATE, FTAG);
10231 
10232 	dsl_pool_t *dp = spa->spa_dsl_pool;
10233 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
10234 
10235 	spa->spa_sync_starttime = gethrtime();
10236 	taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
10237 	spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
10238 	    spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
10239 	    NSEC_TO_TICK(spa->spa_deadman_synctime));
10240 
10241 	/*
10242 	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
10243 	 * set spa_deflate if we have no raid-z vdevs.
10244 	 */
10245 	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
10246 	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
10247 		vdev_t *rvd = spa->spa_root_vdev;
10248 
10249 		int i;
10250 		for (i = 0; i < rvd->vdev_children; i++) {
10251 			vd = rvd->vdev_child[i];
10252 			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
10253 				break;
10254 		}
10255 		if (i == rvd->vdev_children) {
10256 			spa->spa_deflate = TRUE;
10257 			VERIFY0(zap_add(spa->spa_meta_objset,
10258 			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
10259 			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
10260 		}
10261 	}
10262 
10263 	spa_sync_adjust_vdev_max_queue_depth(spa);
10264 
10265 	spa_sync_condense_indirect(spa, tx);
10266 
10267 	spa_sync_iterate_to_convergence(spa, tx);
10268 
10269 #ifdef ZFS_DEBUG
10270 	if (!list_is_empty(&spa->spa_config_dirty_list)) {
10271 	/*
10272 	 * Make sure that the number of ZAPs for all the vdevs matches
10273 	 * the number of ZAPs in the per-vdev ZAP list. This only gets
10274 	 * called if the config is dirty; otherwise there may be
10275 	 * outstanding AVZ operations that weren't completed in
10276 	 * spa_sync_config_object.
10277 	 */
10278 		uint64_t all_vdev_zap_entry_count;
10279 		ASSERT0(zap_count(spa->spa_meta_objset,
10280 		    spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count));
10281 		ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==,
10282 		    all_vdev_zap_entry_count);
10283 	}
10284 #endif
10285 
10286 	if (spa->spa_vdev_removal != NULL) {
10287 		ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]);
10288 	}
10289 
10290 	spa_sync_rewrite_vdev_config(spa, tx);
10291 	dmu_tx_commit(tx);
10292 
10293 	taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
10294 	spa->spa_deadman_tqid = 0;
10295 
10296 	/*
10297 	 * Clear the dirty config list.
10298 	 */
10299 	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
10300 		vdev_config_clean(vd);
10301 
10302 	/*
10303 	 * Now that the new config has synced transactionally,
10304 	 * let it become visible to the config cache.
10305 	 */
10306 	if (spa->spa_config_syncing != NULL) {
10307 		spa_config_set(spa, spa->spa_config_syncing);
10308 		spa->spa_config_txg = txg;
10309 		spa->spa_config_syncing = NULL;
10310 	}
10311 
10312 	dsl_pool_sync_done(dp, txg);
10313 
10314 	/*
10315 	 * Update usable space statistics.
10316 	 */
10317 	while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
10318 	    != NULL)
10319 		vdev_sync_done(vd, txg);
10320 
10321 	metaslab_class_evict_old(spa->spa_normal_class, txg);
10322 	metaslab_class_evict_old(spa->spa_log_class, txg);
10323 	/* spa_embedded_log_class has only one metaslab per vdev. */
10324 	metaslab_class_evict_old(spa->spa_special_class, txg);
10325 	metaslab_class_evict_old(spa->spa_dedup_class, txg);
10326 
10327 	spa_sync_close_syncing_log_sm(spa);
10328 
10329 	spa_update_dspace(spa);
10330 
10331 	if (spa_get_autotrim(spa) == SPA_AUTOTRIM_ON)
10332 		vdev_autotrim_kick(spa);
10333 
10334 	/*
10335 	 * It had better be the case that we didn't dirty anything
10336 	 * since vdev_config_sync().
10337 	 */
10338 	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
10339 	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
10340 	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
10341 
10342 	while (zfs_pause_spa_sync)
10343 		delay(1);
10344 
10345 	spa->spa_sync_pass = 0;
10346 
10347 	/*
10348 	 * Update the last synced uberblock here. We want to do this at
10349 	 * the end of spa_sync() so that consumers of spa_last_synced_txg()
10350 	 * will be guaranteed that all the processing associated with
10351 	 * that txg has been completed.
10352 	 */
10353 	spa->spa_ubsync = spa->spa_uberblock;
10354 	spa_config_exit(spa, SCL_CONFIG, FTAG);
10355 
10356 	spa_handle_ignored_writes(spa);
10357 
10358 	/*
10359 	 * If any async tasks have been requested, kick them off.
10360 	 */
10361 	spa_async_dispatch(spa);
10362 }
10363 
10364 /*
10365  * Sync all pools.  We don't want to hold the namespace lock across these
10366  * operations, so we take a reference on the spa_t and drop the lock during the
10367  * sync.
10368  */
10369 void
10370 spa_sync_allpools(void)
10371 {
10372 	spa_t *spa = NULL;
10373 	mutex_enter(&spa_namespace_lock);
10374 	while ((spa = spa_next(spa)) != NULL) {
10375 		if (spa_state(spa) != POOL_STATE_ACTIVE ||
10376 		    !spa_writeable(spa) || spa_suspended(spa))
10377 			continue;
10378 		spa_open_ref(spa, FTAG);
10379 		mutex_exit(&spa_namespace_lock);
10380 		txg_wait_synced(spa_get_dsl(spa), 0);
10381 		mutex_enter(&spa_namespace_lock);
10382 		spa_close(spa, FTAG);
10383 	}
10384 	mutex_exit(&spa_namespace_lock);
10385 }
10386 
10387 taskq_t *
10388 spa_sync_tq_create(spa_t *spa, const char *name)
10389 {
10390 	kthread_t **kthreads;
10391 
10392 	ASSERT(spa->spa_sync_tq == NULL);
10393 	ASSERT3S(spa->spa_alloc_count, <=, boot_ncpus);
10394 
10395 	/*
10396 	 * - do not allow more allocators than cpus.
10397 	 * - there may be more cpus than allocators.
10398 	 * - do not allow more sync taskq threads than allocators or cpus.
10399 	 */
10400 	int nthreads = spa->spa_alloc_count;
10401 	spa->spa_syncthreads = kmem_zalloc(sizeof (spa_syncthread_info_t) *
10402 	    nthreads, KM_SLEEP);
10403 
10404 	spa->spa_sync_tq = taskq_create_synced(name, nthreads, minclsyspri,
10405 	    nthreads, INT_MAX, TASKQ_PREPOPULATE, &kthreads);
10406 	VERIFY(spa->spa_sync_tq != NULL);
10407 	VERIFY(kthreads != NULL);
10408 
10409 	spa_syncthread_info_t *ti = spa->spa_syncthreads;
10410 	for (int i = 0; i < nthreads; i++, ti++) {
10411 		ti->sti_thread = kthreads[i];
10412 		ti->sti_allocator = i;
10413 	}
10414 
10415 	kmem_free(kthreads, sizeof (*kthreads) * nthreads);
10416 	return (spa->spa_sync_tq);
10417 }
10418 
10419 void
10420 spa_sync_tq_destroy(spa_t *spa)
10421 {
10422 	ASSERT(spa->spa_sync_tq != NULL);
10423 
10424 	taskq_wait(spa->spa_sync_tq);
10425 	taskq_destroy(spa->spa_sync_tq);
10426 	kmem_free(spa->spa_syncthreads,
10427 	    sizeof (spa_syncthread_info_t) * spa->spa_alloc_count);
10428 	spa->spa_sync_tq = NULL;
10429 }
10430 
10431 uint_t
10432 spa_acq_allocator(spa_t *spa)
10433 {
10434 	int i;
10435 
10436 	if (spa->spa_alloc_count == 1)
10437 		return (0);
10438 
10439 	mutex_enter(&spa->spa_allocs_use->sau_lock);
10440 	uint_t r = spa->spa_allocs_use->sau_rotor;
10441 	do {
10442 		if (++r == spa->spa_alloc_count)
10443 			r = 0;
10444 	} while (spa->spa_allocs_use->sau_inuse[r]);
10445 	spa->spa_allocs_use->sau_inuse[r] = B_TRUE;
10446 	spa->spa_allocs_use->sau_rotor = r;
10447 	mutex_exit(&spa->spa_allocs_use->sau_lock);
10448 
10449 	spa_syncthread_info_t *ti = spa->spa_syncthreads;
10450 	for (i = 0; i < spa->spa_alloc_count; i++, ti++) {
10451 		if (ti->sti_thread == curthread) {
10452 			ti->sti_allocator = r;
10453 			break;
10454 		}
10455 	}
10456 	ASSERT3S(i, <, spa->spa_alloc_count);
10457 	return (r);
10458 }
10459 
10460 void
10461 spa_rel_allocator(spa_t *spa, uint_t allocator)
10462 {
10463 	if (spa->spa_alloc_count > 1)
10464 		spa->spa_allocs_use->sau_inuse[allocator] = B_FALSE;
10465 }
10466 
10467 void
10468 spa_select_allocator(zio_t *zio)
10469 {
10470 	zbookmark_phys_t *bm = &zio->io_bookmark;
10471 	spa_t *spa = zio->io_spa;
10472 
10473 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
10474 
10475 	/*
10476 	 * A gang block (for example) may have inherited its parent's
10477 	 * allocator, in which case there is nothing further to do here.
10478 	 */
10479 	if (ZIO_HAS_ALLOCATOR(zio))
10480 		return;
10481 
10482 	ASSERT(spa != NULL);
10483 	ASSERT(bm != NULL);
10484 
10485 	/*
10486 	 * First try to use an allocator assigned to the syncthread, and set
10487 	 * the corresponding write issue taskq for the allocator.
10488 	 * Note, we must have an open pool to do this.
10489 	 */
10490 	if (spa->spa_sync_tq != NULL) {
10491 		spa_syncthread_info_t *ti = spa->spa_syncthreads;
10492 		for (int i = 0; i < spa->spa_alloc_count; i++, ti++) {
10493 			if (ti->sti_thread == curthread) {
10494 				zio->io_allocator = ti->sti_allocator;
10495 				return;
10496 			}
10497 		}
10498 	}
10499 
10500 	/*
10501 	 * We want to try to use as many allocators as possible to help improve
10502 	 * performance, but we also want logically adjacent IOs to be physically
10503 	 * adjacent to improve sequential read performance. We chunk each object
10504 	 * into 2^20 block regions, and then hash based on the objset, object,
10505 	 * level, and region to accomplish both of these goals.
10506 	 */
10507 	uint64_t hv = cityhash4(bm->zb_objset, bm->zb_object, bm->zb_level,
10508 	    bm->zb_blkid >> 20);
10509 
10510 	zio->io_allocator = (uint_t)hv % spa->spa_alloc_count;
10511 }
10512 
10513 /*
10514  * ==========================================================================
10515  * Miscellaneous routines
10516  * ==========================================================================
10517  */
10518 
10519 /*
10520  * Remove all pools in the system.
10521  */
10522 void
10523 spa_evict_all(void)
10524 {
10525 	spa_t *spa;
10526 
10527 	/*
10528 	 * Remove all cached state.  All pools should be closed now,
10529 	 * so every spa in the AVL tree should be unreferenced.
10530 	 */
10531 	mutex_enter(&spa_namespace_lock);
10532 	while ((spa = spa_next(NULL)) != NULL) {
10533 		/*
10534 		 * Stop async tasks.  The async thread may need to detach
10535 		 * a device that's been replaced, which requires grabbing
10536 		 * spa_namespace_lock, so we must drop it here.
10537 		 */
10538 		spa_open_ref(spa, FTAG);
10539 		mutex_exit(&spa_namespace_lock);
10540 		spa_async_suspend(spa);
10541 		mutex_enter(&spa_namespace_lock);
10542 		spa_close(spa, FTAG);
10543 
10544 		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
10545 			spa_unload(spa);
10546 			spa_deactivate(spa);
10547 		}
10548 		spa_remove(spa);
10549 	}
10550 	mutex_exit(&spa_namespace_lock);
10551 }
10552 
10553 vdev_t *
10554 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
10555 {
10556 	vdev_t *vd;
10557 	int i;
10558 
10559 	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
10560 		return (vd);
10561 
10562 	if (aux) {
10563 		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
10564 			vd = spa->spa_l2cache.sav_vdevs[i];
10565 			if (vd->vdev_guid == guid)
10566 				return (vd);
10567 		}
10568 
10569 		for (i = 0; i < spa->spa_spares.sav_count; i++) {
10570 			vd = spa->spa_spares.sav_vdevs[i];
10571 			if (vd->vdev_guid == guid)
10572 				return (vd);
10573 		}
10574 	}
10575 
10576 	return (NULL);
10577 }
10578 
10579 void
10580 spa_upgrade(spa_t *spa, uint64_t version)
10581 {
10582 	ASSERT(spa_writeable(spa));
10583 
10584 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
10585 
10586 	/*
10587 	 * This should only be called for a non-faulted pool, and since a
10588 	 * future version would result in an unopenable pool, this shouldn't be
10589 	 * possible.
10590 	 */
10591 	ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
10592 	ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
10593 
10594 	spa->spa_uberblock.ub_version = version;
10595 	vdev_config_dirty(spa->spa_root_vdev);
10596 
10597 	spa_config_exit(spa, SCL_ALL, FTAG);
10598 
10599 	txg_wait_synced(spa_get_dsl(spa), 0);
10600 }
10601 
10602 static boolean_t
10603 spa_has_aux_vdev(spa_t *spa, uint64_t guid, spa_aux_vdev_t *sav)
10604 {
10605 	(void) spa;
10606 	int i;
10607 	uint64_t vdev_guid;
10608 
10609 	for (i = 0; i < sav->sav_count; i++)
10610 		if (sav->sav_vdevs[i]->vdev_guid == guid)
10611 			return (B_TRUE);
10612 
10613 	for (i = 0; i < sav->sav_npending; i++) {
10614 		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
10615 		    &vdev_guid) == 0 && vdev_guid == guid)
10616 			return (B_TRUE);
10617 	}
10618 
10619 	return (B_FALSE);
10620 }
10621 
10622 boolean_t
10623 spa_has_l2cache(spa_t *spa, uint64_t guid)
10624 {
10625 	return (spa_has_aux_vdev(spa, guid, &spa->spa_l2cache));
10626 }
10627 
10628 boolean_t
10629 spa_has_spare(spa_t *spa, uint64_t guid)
10630 {
10631 	return (spa_has_aux_vdev(spa, guid, &spa->spa_spares));
10632 }
10633 
10634 /*
10635  * Check if a pool has an active shared spare device.
10636  * Note: reference count of an active spare is 2, as a spare and as a replace
10637  */
10638 static boolean_t
10639 spa_has_active_shared_spare(spa_t *spa)
10640 {
10641 	int i, refcnt;
10642 	uint64_t pool;
10643 	spa_aux_vdev_t *sav = &spa->spa_spares;
10644 
10645 	for (i = 0; i < sav->sav_count; i++) {
10646 		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
10647 		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
10648 		    refcnt > 2)
10649 			return (B_TRUE);
10650 	}
10651 
10652 	return (B_FALSE);
10653 }
10654 
10655 uint64_t
10656 spa_total_metaslabs(spa_t *spa)
10657 {
10658 	vdev_t *rvd = spa->spa_root_vdev;
10659 
10660 	uint64_t m = 0;
10661 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
10662 		vdev_t *vd = rvd->vdev_child[c];
10663 		if (!vdev_is_concrete(vd))
10664 			continue;
10665 		m += vd->vdev_ms_count;
10666 	}
10667 	return (m);
10668 }
10669 
10670 /*
10671  * Notify any waiting threads that some activity has switched from being in-
10672  * progress to not-in-progress so that the thread can wake up and determine
10673  * whether it is finished waiting.
10674  */
10675 void
10676 spa_notify_waiters(spa_t *spa)
10677 {
10678 	/*
10679 	 * Acquiring spa_activities_lock here prevents the cv_broadcast from
10680 	 * happening between the waiting thread's check and cv_wait.
10681 	 */
10682 	mutex_enter(&spa->spa_activities_lock);
10683 	cv_broadcast(&spa->spa_activities_cv);
10684 	mutex_exit(&spa->spa_activities_lock);
10685 }
10686 
10687 /*
10688  * Notify any waiting threads that the pool is exporting, and then block until
10689  * they are finished using the spa_t.
10690  */
10691 void
10692 spa_wake_waiters(spa_t *spa)
10693 {
10694 	mutex_enter(&spa->spa_activities_lock);
10695 	spa->spa_waiters_cancel = B_TRUE;
10696 	cv_broadcast(&spa->spa_activities_cv);
10697 	while (spa->spa_waiters != 0)
10698 		cv_wait(&spa->spa_waiters_cv, &spa->spa_activities_lock);
10699 	spa->spa_waiters_cancel = B_FALSE;
10700 	mutex_exit(&spa->spa_activities_lock);
10701 }
10702 
10703 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
10704 static boolean_t
10705 spa_vdev_activity_in_progress_impl(vdev_t *vd, zpool_wait_activity_t activity)
10706 {
10707 	spa_t *spa = vd->vdev_spa;
10708 
10709 	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER));
10710 	ASSERT(MUTEX_HELD(&spa->spa_activities_lock));
10711 	ASSERT(activity == ZPOOL_WAIT_INITIALIZE ||
10712 	    activity == ZPOOL_WAIT_TRIM);
10713 
10714 	kmutex_t *lock = activity == ZPOOL_WAIT_INITIALIZE ?
10715 	    &vd->vdev_initialize_lock : &vd->vdev_trim_lock;
10716 
10717 	mutex_exit(&spa->spa_activities_lock);
10718 	mutex_enter(lock);
10719 	mutex_enter(&spa->spa_activities_lock);
10720 
10721 	boolean_t in_progress = (activity == ZPOOL_WAIT_INITIALIZE) ?
10722 	    (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) :
10723 	    (vd->vdev_trim_state == VDEV_TRIM_ACTIVE);
10724 	mutex_exit(lock);
10725 
10726 	if (in_progress)
10727 		return (B_TRUE);
10728 
10729 	for (int i = 0; i < vd->vdev_children; i++) {
10730 		if (spa_vdev_activity_in_progress_impl(vd->vdev_child[i],
10731 		    activity))
10732 			return (B_TRUE);
10733 	}
10734 
10735 	return (B_FALSE);
10736 }
10737 
10738 /*
10739  * If use_guid is true, this checks whether the vdev specified by guid is
10740  * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
10741  * is being initialized/trimmed. The caller must hold the config lock and
10742  * spa_activities_lock.
10743  */
10744 static int
10745 spa_vdev_activity_in_progress(spa_t *spa, boolean_t use_guid, uint64_t guid,
10746     zpool_wait_activity_t activity, boolean_t *in_progress)
10747 {
10748 	mutex_exit(&spa->spa_activities_lock);
10749 	spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
10750 	mutex_enter(&spa->spa_activities_lock);
10751 
10752 	vdev_t *vd;
10753 	if (use_guid) {
10754 		vd = spa_lookup_by_guid(spa, guid, B_FALSE);
10755 		if (vd == NULL || !vd->vdev_ops->vdev_op_leaf) {
10756 			spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10757 			return (EINVAL);
10758 		}
10759 	} else {
10760 		vd = spa->spa_root_vdev;
10761 	}
10762 
10763 	*in_progress = spa_vdev_activity_in_progress_impl(vd, activity);
10764 
10765 	spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10766 	return (0);
10767 }
10768 
10769 /*
10770  * Locking for waiting threads
10771  * ---------------------------
10772  *
10773  * Waiting threads need a way to check whether a given activity is in progress,
10774  * and then, if it is, wait for it to complete. Each activity will have some
10775  * in-memory representation of the relevant on-disk state which can be used to
10776  * determine whether or not the activity is in progress. The in-memory state and
10777  * the locking used to protect it will be different for each activity, and may
10778  * not be suitable for use with a cvar (e.g., some state is protected by the
10779  * config lock). To allow waiting threads to wait without any races, another
10780  * lock, spa_activities_lock, is used.
10781  *
10782  * When the state is checked, both the activity-specific lock (if there is one)
10783  * and spa_activities_lock are held. In some cases, the activity-specific lock
10784  * is acquired explicitly (e.g. the config lock). In others, the locking is
10785  * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
10786  * thread releases the activity-specific lock and, if the activity is in
10787  * progress, then cv_waits using spa_activities_lock.
10788  *
10789  * The waiting thread is woken when another thread, one completing some
10790  * activity, updates the state of the activity and then calls
10791  * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
10792  * needs to hold its activity-specific lock when updating the state, and this
10793  * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
10794  *
10795  * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
10796  * and because it is held when the waiting thread checks the state of the
10797  * activity, it can never be the case that the completing thread both updates
10798  * the activity state and cv_broadcasts in between the waiting thread's check
10799  * and cv_wait. Thus, a waiting thread can never miss a wakeup.
10800  *
10801  * In order to prevent deadlock, when the waiting thread does its check, in some
10802  * cases it will temporarily drop spa_activities_lock in order to acquire the
10803  * activity-specific lock. The order in which spa_activities_lock and the
10804  * activity specific lock are acquired in the waiting thread is determined by
10805  * the order in which they are acquired in the completing thread; if the
10806  * completing thread calls spa_notify_waiters with the activity-specific lock
10807  * held, then the waiting thread must also acquire the activity-specific lock
10808  * first.
10809  */
10810 
10811 static int
10812 spa_activity_in_progress(spa_t *spa, zpool_wait_activity_t activity,
10813     boolean_t use_tag, uint64_t tag, boolean_t *in_progress)
10814 {
10815 	int error = 0;
10816 
10817 	ASSERT(MUTEX_HELD(&spa->spa_activities_lock));
10818 
10819 	switch (activity) {
10820 	case ZPOOL_WAIT_CKPT_DISCARD:
10821 		*in_progress =
10822 		    (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT) &&
10823 		    zap_contains(spa_meta_objset(spa),
10824 		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ZPOOL_CHECKPOINT) ==
10825 		    ENOENT);
10826 		break;
10827 	case ZPOOL_WAIT_FREE:
10828 		*in_progress = ((spa_version(spa) >= SPA_VERSION_DEADLISTS &&
10829 		    !bpobj_is_empty(&spa->spa_dsl_pool->dp_free_bpobj)) ||
10830 		    spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY) ||
10831 		    spa_livelist_delete_check(spa));
10832 		break;
10833 	case ZPOOL_WAIT_INITIALIZE:
10834 	case ZPOOL_WAIT_TRIM:
10835 		error = spa_vdev_activity_in_progress(spa, use_tag, tag,
10836 		    activity, in_progress);
10837 		break;
10838 	case ZPOOL_WAIT_REPLACE:
10839 		mutex_exit(&spa->spa_activities_lock);
10840 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
10841 		mutex_enter(&spa->spa_activities_lock);
10842 
10843 		*in_progress = vdev_replace_in_progress(spa->spa_root_vdev);
10844 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10845 		break;
10846 	case ZPOOL_WAIT_REMOVE:
10847 		*in_progress = (spa->spa_removing_phys.sr_state ==
10848 		    DSS_SCANNING);
10849 		break;
10850 	case ZPOOL_WAIT_RESILVER:
10851 		*in_progress = vdev_rebuild_active(spa->spa_root_vdev);
10852 		if (*in_progress)
10853 			break;
10854 		zfs_fallthrough;
10855 	case ZPOOL_WAIT_SCRUB:
10856 	{
10857 		boolean_t scanning, paused, is_scrub;
10858 		dsl_scan_t *scn =  spa->spa_dsl_pool->dp_scan;
10859 
10860 		is_scrub = (scn->scn_phys.scn_func == POOL_SCAN_SCRUB);
10861 		scanning = (scn->scn_phys.scn_state == DSS_SCANNING);
10862 		paused = dsl_scan_is_paused_scrub(scn);
10863 		*in_progress = (scanning && !paused &&
10864 		    is_scrub == (activity == ZPOOL_WAIT_SCRUB));
10865 		break;
10866 	}
10867 	case ZPOOL_WAIT_RAIDZ_EXPAND:
10868 	{
10869 		vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
10870 		*in_progress = (vre != NULL && vre->vre_state == DSS_SCANNING);
10871 		break;
10872 	}
10873 	default:
10874 		panic("unrecognized value for activity %d", activity);
10875 	}
10876 
10877 	return (error);
10878 }
10879 
10880 static int
10881 spa_wait_common(const char *pool, zpool_wait_activity_t activity,
10882     boolean_t use_tag, uint64_t tag, boolean_t *waited)
10883 {
10884 	/*
10885 	 * The tag is used to distinguish between instances of an activity.
10886 	 * 'initialize' and 'trim' are the only activities that we use this for.
10887 	 * The other activities can only have a single instance in progress in a
10888 	 * pool at one time, making the tag unnecessary.
10889 	 *
10890 	 * There can be multiple devices being replaced at once, but since they
10891 	 * all finish once resilvering finishes, we don't bother keeping track
10892 	 * of them individually, we just wait for them all to finish.
10893 	 */
10894 	if (use_tag && activity != ZPOOL_WAIT_INITIALIZE &&
10895 	    activity != ZPOOL_WAIT_TRIM)
10896 		return (EINVAL);
10897 
10898 	if (activity < 0 || activity >= ZPOOL_WAIT_NUM_ACTIVITIES)
10899 		return (EINVAL);
10900 
10901 	spa_t *spa;
10902 	int error = spa_open(pool, &spa, FTAG);
10903 	if (error != 0)
10904 		return (error);
10905 
10906 	/*
10907 	 * Increment the spa's waiter count so that we can call spa_close and
10908 	 * still ensure that the spa_t doesn't get freed before this thread is
10909 	 * finished with it when the pool is exported. We want to call spa_close
10910 	 * before we start waiting because otherwise the additional ref would
10911 	 * prevent the pool from being exported or destroyed throughout the
10912 	 * potentially long wait.
10913 	 */
10914 	mutex_enter(&spa->spa_activities_lock);
10915 	spa->spa_waiters++;
10916 	spa_close(spa, FTAG);
10917 
10918 	*waited = B_FALSE;
10919 	for (;;) {
10920 		boolean_t in_progress;
10921 		error = spa_activity_in_progress(spa, activity, use_tag, tag,
10922 		    &in_progress);
10923 
10924 		if (error || !in_progress || spa->spa_waiters_cancel)
10925 			break;
10926 
10927 		*waited = B_TRUE;
10928 
10929 		if (cv_wait_sig(&spa->spa_activities_cv,
10930 		    &spa->spa_activities_lock) == 0) {
10931 			error = EINTR;
10932 			break;
10933 		}
10934 	}
10935 
10936 	spa->spa_waiters--;
10937 	cv_signal(&spa->spa_waiters_cv);
10938 	mutex_exit(&spa->spa_activities_lock);
10939 
10940 	return (error);
10941 }
10942 
10943 /*
10944  * Wait for a particular instance of the specified activity to complete, where
10945  * the instance is identified by 'tag'
10946  */
10947 int
10948 spa_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag,
10949     boolean_t *waited)
10950 {
10951 	return (spa_wait_common(pool, activity, B_TRUE, tag, waited));
10952 }
10953 
10954 /*
10955  * Wait for all instances of the specified activity complete
10956  */
10957 int
10958 spa_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited)
10959 {
10960 
10961 	return (spa_wait_common(pool, activity, B_FALSE, 0, waited));
10962 }
10963 
10964 sysevent_t *
10965 spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
10966 {
10967 	sysevent_t *ev = NULL;
10968 #ifdef _KERNEL
10969 	nvlist_t *resource;
10970 
10971 	resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl);
10972 	if (resource) {
10973 		ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP);
10974 		ev->resource = resource;
10975 	}
10976 #else
10977 	(void) spa, (void) vd, (void) hist_nvl, (void) name;
10978 #endif
10979 	return (ev);
10980 }
10981 
10982 void
10983 spa_event_post(sysevent_t *ev)
10984 {
10985 #ifdef _KERNEL
10986 	if (ev) {
10987 		zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb);
10988 		kmem_free(ev, sizeof (*ev));
10989 	}
10990 #else
10991 	(void) ev;
10992 #endif
10993 }
10994 
10995 /*
10996  * Post a zevent corresponding to the given sysevent.   The 'name' must be one
10997  * of the event definitions in sys/sysevent/eventdefs.h.  The payload will be
10998  * filled in from the spa and (optionally) the vdev.  This doesn't do anything
10999  * in the userland libzpool, as we don't want consumers to misinterpret ztest
11000  * or zdb as real changes.
11001  */
11002 void
11003 spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
11004 {
11005 	spa_event_post(spa_event_create(spa, vd, hist_nvl, name));
11006 }
11007 
11008 /* state manipulation functions */
11009 EXPORT_SYMBOL(spa_open);
11010 EXPORT_SYMBOL(spa_open_rewind);
11011 EXPORT_SYMBOL(spa_get_stats);
11012 EXPORT_SYMBOL(spa_create);
11013 EXPORT_SYMBOL(spa_import);
11014 EXPORT_SYMBOL(spa_tryimport);
11015 EXPORT_SYMBOL(spa_destroy);
11016 EXPORT_SYMBOL(spa_export);
11017 EXPORT_SYMBOL(spa_reset);
11018 EXPORT_SYMBOL(spa_async_request);
11019 EXPORT_SYMBOL(spa_async_suspend);
11020 EXPORT_SYMBOL(spa_async_resume);
11021 EXPORT_SYMBOL(spa_inject_addref);
11022 EXPORT_SYMBOL(spa_inject_delref);
11023 EXPORT_SYMBOL(spa_scan_stat_init);
11024 EXPORT_SYMBOL(spa_scan_get_stats);
11025 
11026 /* device manipulation */
11027 EXPORT_SYMBOL(spa_vdev_add);
11028 EXPORT_SYMBOL(spa_vdev_attach);
11029 EXPORT_SYMBOL(spa_vdev_detach);
11030 EXPORT_SYMBOL(spa_vdev_setpath);
11031 EXPORT_SYMBOL(spa_vdev_setfru);
11032 EXPORT_SYMBOL(spa_vdev_split_mirror);
11033 
11034 /* spare statech is global across all pools) */
11035 EXPORT_SYMBOL(spa_spare_add);
11036 EXPORT_SYMBOL(spa_spare_remove);
11037 EXPORT_SYMBOL(spa_spare_exists);
11038 EXPORT_SYMBOL(spa_spare_activate);
11039 
11040 /* L2ARC statech is global across all pools) */
11041 EXPORT_SYMBOL(spa_l2cache_add);
11042 EXPORT_SYMBOL(spa_l2cache_remove);
11043 EXPORT_SYMBOL(spa_l2cache_exists);
11044 EXPORT_SYMBOL(spa_l2cache_activate);
11045 EXPORT_SYMBOL(spa_l2cache_drop);
11046 
11047 /* scanning */
11048 EXPORT_SYMBOL(spa_scan);
11049 EXPORT_SYMBOL(spa_scan_range);
11050 EXPORT_SYMBOL(spa_scan_stop);
11051 
11052 /* spa syncing */
11053 EXPORT_SYMBOL(spa_sync); /* only for DMU use */
11054 EXPORT_SYMBOL(spa_sync_allpools);
11055 
11056 /* properties */
11057 EXPORT_SYMBOL(spa_prop_set);
11058 EXPORT_SYMBOL(spa_prop_get);
11059 EXPORT_SYMBOL(spa_prop_clear_bootfs);
11060 
11061 /* asynchronous event notification */
11062 EXPORT_SYMBOL(spa_event_notify);
11063 
11064 ZFS_MODULE_PARAM(zfs_metaslab, metaslab_, preload_pct, UINT, ZMOD_RW,
11065 	"Percentage of CPUs to run a metaslab preload taskq");
11066 
11067 ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_shift, UINT, ZMOD_RW,
11068 	"log2 fraction of arc that can be used by inflight I/Os when "
11069 	"verifying pool during import");
11070 
11071 ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_metadata, INT, ZMOD_RW,
11072 	"Set to traverse metadata on pool import");
11073 
11074 ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_data, INT, ZMOD_RW,
11075 	"Set to traverse data on pool import");
11076 
11077 ZFS_MODULE_PARAM(zfs_spa, spa_, load_print_vdev_tree, INT, ZMOD_RW,
11078 	"Print vdev tree to zfs_dbgmsg during pool import");
11079 
11080 ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_pct, UINT, ZMOD_RW,
11081 	"Percentage of CPUs to run an IO worker thread");
11082 
11083 ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_tpq, UINT, ZMOD_RW,
11084 	"Number of threads per IO worker taskqueue");
11085 
11086 ZFS_MODULE_PARAM(zfs, zfs_, max_missing_tvds, U64, ZMOD_RW,
11087 	"Allow importing pool with up to this number of missing top-level "
11088 	"vdevs (in read-only mode)");
11089 
11090 ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_pause, INT,
11091 	ZMOD_RW, "Set the livelist condense zthr to pause");
11092 
11093 ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_pause, INT,
11094 	ZMOD_RW, "Set the livelist condense synctask to pause");
11095 
11096 ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_cancel,
11097 	INT, ZMOD_RW,
11098 	"Whether livelist condensing was canceled in the synctask");
11099 
11100 ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_cancel,
11101 	INT, ZMOD_RW,
11102 	"Whether livelist condensing was canceled in the zthr function");
11103 
11104 ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT,
11105 	ZMOD_RW,
11106 	"Whether extra ALLOC blkptrs were added to a livelist entry while it "
11107 	"was being condensed");
11108 
11109 #ifdef _KERNEL
11110 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio, zio_, taskq_read,
11111 	spa_taskq_read_param_set, spa_taskq_read_param_get, ZMOD_RW,
11112 	"Configure IO queues for read IO");
11113 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio, zio_, taskq_write,
11114 	spa_taskq_write_param_set, spa_taskq_write_param_get, ZMOD_RW,
11115 	"Configure IO queues for write IO");
11116 #endif
11117 
11118 ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_write_tpq, UINT, ZMOD_RW,
11119 	"Number of CPUs per write issue taskq");
11120