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