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