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