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