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