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