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