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