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