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