xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa.c (revision f3a2bc1e)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
25  * Copyright (c) 2015, Nexenta Systems, Inc.  All rights reserved.
26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27  * Copyright 2013 Saso Kiselkov. All rights reserved.
28  * Copyright (c) 2014 Integros [integros.com]
29  * Copyright 2016 Toomas Soome <tsoome@me.com>
30  * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
31  * Copyright 2019 Joyent, Inc.
32  * Copyright (c) 2017, Intel Corporation.
33  * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
34  * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
35  */
36 
37 /*
38  * SPA: Storage Pool Allocator
39  *
40  * This file contains all the routines used when modifying on-disk SPA state.
41  * This includes opening, importing, destroying, exporting a pool, and syncing a
42  * pool.
43  */
44 
45 #include <sys/zfs_context.h>
46 #include <sys/fm/fs/zfs.h>
47 #include <sys/spa_impl.h>
48 #include <sys/zio.h>
49 #include <sys/zio_checksum.h>
50 #include <sys/dmu.h>
51 #include <sys/dmu_tx.h>
52 #include <sys/zap.h>
53 #include <sys/zil.h>
54 #include <sys/ddt.h>
55 #include <sys/vdev_impl.h>
56 #include <sys/vdev_removal.h>
57 #include <sys/vdev_indirect_mapping.h>
58 #include <sys/vdev_indirect_births.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_trim.h>
61 #include <sys/metaslab.h>
62 #include <sys/metaslab_impl.h>
63 #include <sys/mmp.h>
64 #include <sys/uberblock_impl.h>
65 #include <sys/txg.h>
66 #include <sys/avl.h>
67 #include <sys/bpobj.h>
68 #include <sys/dmu_traverse.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/unique.h>
71 #include <sys/dsl_pool.h>
72 #include <sys/dsl_dataset.h>
73 #include <sys/dsl_dir.h>
74 #include <sys/dsl_prop.h>
75 #include <sys/dsl_synctask.h>
76 #include <sys/fs/zfs.h>
77 #include <sys/arc.h>
78 #include <sys/callb.h>
79 #include <sys/systeminfo.h>
80 #include <sys/spa_boot.h>
81 #include <sys/zfs_ioctl.h>
82 #include <sys/dsl_scan.h>
83 #include <sys/zfeature.h>
84 #include <sys/dsl_destroy.h>
85 #include <sys/abd.h>
86 
87 #ifdef	_KERNEL
88 #include <sys/bootprops.h>
89 #include <sys/callb.h>
90 #include <sys/cpupart.h>
91 #include <sys/pool.h>
92 #include <sys/sysdc.h>
93 #include <sys/zone.h>
94 #endif	/* _KERNEL */
95 
96 #include "zfs_prop.h"
97 #include "zfs_comutil.h"
98 
99 /*
100  * The interval, in seconds, at which failed configuration cache file writes
101  * should be retried.
102  */
103 int zfs_ccw_retry_interval = 300;
104 
105 typedef enum zti_modes {
106 	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
107 	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
108 	ZTI_MODE_NULL,			/* don't create a taskq */
109 	ZTI_NMODES
110 } zti_modes_t;
111 
112 #define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
113 #define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
114 #define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
115 
116 #define	ZTI_N(n)	ZTI_P(n, 1)
117 #define	ZTI_ONE		ZTI_N(1)
118 
119 typedef struct zio_taskq_info {
120 	zti_modes_t zti_mode;
121 	uint_t zti_value;
122 	uint_t zti_count;
123 } zio_taskq_info_t;
124 
125 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
126 	"issue", "issue_high", "intr", "intr_high"
127 };
128 
129 /*
130  * This table defines the taskq settings for each ZFS I/O type. When
131  * initializing a pool, we use this table to create an appropriately sized
132  * taskq. Some operations are low volume and therefore have a small, static
133  * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
134  * macros. Other operations process a large amount of data; the ZTI_BATCH
135  * macro causes us to create a taskq oriented for throughput. Some operations
136  * are so high frequency and short-lived that the taskq itself can become a
137  * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
138  * additional degree of parallelism specified by the number of threads per-
139  * taskq and the number of taskqs; when dispatching an event in this case, the
140  * particular taskq is chosen at random.
141  *
142  * The different taskq priorities are to handle the different contexts (issue
143  * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
144  * need to be handled with minimum delay.
145  */
146 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
147 	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
148 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
149 	{ ZTI_N(8),	ZTI_NULL,	ZTI_P(12, 8),	ZTI_NULL }, /* READ */
150 	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
151 	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
152 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
153 	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
154 	{ ZTI_N(4),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* TRIM */
155 };
156 
157 static void spa_sync_version(void *arg, dmu_tx_t *tx);
158 static void spa_sync_props(void *arg, dmu_tx_t *tx);
159 static boolean_t spa_has_active_shared_spare(spa_t *spa);
160 static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport);
161 static void spa_vdev_resilver_done(spa_t *spa);
162 
163 uint_t		zio_taskq_batch_pct = 75;	/* 1 thread per cpu in pset */
164 id_t		zio_taskq_psrset_bind = PS_NONE;
165 boolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
166 uint_t		zio_taskq_basedc = 80;		/* base duty cycle */
167 
168 boolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
169 extern int	zfs_sync_pass_deferred_free;
170 
171 /*
172  * Report any spa_load_verify errors found, but do not fail spa_load.
173  * This is used by zdb to analyze non-idle pools.
174  */
175 boolean_t	spa_load_verify_dryrun = B_FALSE;
176 
177 /*
178  * This (illegal) pool name is used when temporarily importing a spa_t in order
179  * to get the vdev stats associated with the imported devices.
180  */
181 #define	TRYIMPORT_NAME	"$import"
182 
183 /*
184  * For debugging purposes: print out vdev tree during pool import.
185  */
186 boolean_t	spa_load_print_vdev_tree = B_FALSE;
187 
188 /*
189  * A non-zero value for zfs_max_missing_tvds means that we allow importing
190  * pools with missing top-level vdevs. This is strictly intended for advanced
191  * pool recovery cases since missing data is almost inevitable. Pools with
192  * missing devices can only be imported read-only for safety reasons, and their
193  * fail-mode will be automatically set to "continue".
194  *
195  * With 1 missing vdev we should be able to import the pool and mount all
196  * datasets. User data that was not modified after the missing device has been
197  * added should be recoverable. This means that snapshots created prior to the
198  * addition of that device should be completely intact.
199  *
200  * With 2 missing vdevs, some datasets may fail to mount since there are
201  * dataset statistics that are stored as regular metadata. Some data might be
202  * recoverable if those vdevs were added recently.
203  *
204  * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
205  * may be missing entirely. Chances of data recovery are very low. Note that
206  * there are also risks of performing an inadvertent rewind as we might be
207  * missing all the vdevs with the latest uberblocks.
208  */
209 uint64_t	zfs_max_missing_tvds = 0;
210 
211 /*
212  * The parameters below are similar to zfs_max_missing_tvds but are only
213  * intended for a preliminary open of the pool with an untrusted config which
214  * might be incomplete or out-dated.
215  *
216  * We are more tolerant for pools opened from a cachefile since we could have
217  * an out-dated cachefile where a device removal was not registered.
218  * We could have set the limit arbitrarily high but in the case where devices
219  * are really missing we would want to return the proper error codes; we chose
220  * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
221  * and we get a chance to retrieve the trusted config.
222  */
223 uint64_t	zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1;
224 
225 /*
226  * In the case where config was assembled by scanning device paths (/dev/dsks
227  * by default) we are less tolerant since all the existing devices should have
228  * been detected and we want spa_load to return the right error codes.
229  */
230 uint64_t	zfs_max_missing_tvds_scan = 0;
231 
232 /*
233  * Interval in seconds at which to poll spare vdevs for health.
234  * Setting this to zero disables spare polling.
235  * Set to three hours by default.
236  */
237 uint_t		spa_spare_poll_interval_seconds = 60 * 60 * 3;
238 
239 /*
240  * Debugging aid that pauses spa_sync() towards the end.
241  */
242 boolean_t	zfs_pause_spa_sync = B_FALSE;
243 
244 /*
245  * ==========================================================================
246  * SPA properties routines
247  * ==========================================================================
248  */
249 
250 /*
251  * Add a (source=src, propname=propval) list to an nvlist.
252  */
253 static void
spa_prop_add_list(nvlist_t * nvl,zpool_prop_t prop,char * strval,uint64_t intval,zprop_source_t src)254 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
255     uint64_t intval, zprop_source_t src)
256 {
257 	const char *propname = zpool_prop_to_name(prop);
258 	nvlist_t *propval;
259 
260 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
261 	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
262 
263 	if (strval != NULL)
264 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
265 	else
266 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
267 
268 	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
269 	nvlist_free(propval);
270 }
271 
272 /*
273  * Get property values from the spa configuration.
274  */
275 static void
spa_prop_get_config(spa_t * spa,nvlist_t ** nvp)276 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
277 {
278 	vdev_t *rvd = spa->spa_root_vdev;
279 	dsl_pool_t *pool = spa->spa_dsl_pool;
280 	uint64_t size, alloc, cap, version;
281 	zprop_source_t src = ZPROP_SRC_NONE;
282 	spa_config_dirent_t *dp;
283 	metaslab_class_t *mc = spa_normal_class(spa);
284 
285 	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
286 
287 	if (rvd != NULL) {
288 		alloc = metaslab_class_get_alloc(mc);
289 		alloc += metaslab_class_get_alloc(spa_special_class(spa));
290 		alloc += metaslab_class_get_alloc(spa_dedup_class(spa));
291 
292 		size = metaslab_class_get_space(mc);
293 		size += metaslab_class_get_space(spa_special_class(spa));
294 		size += metaslab_class_get_space(spa_dedup_class(spa));
295 
296 		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
297 		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
298 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
299 		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
300 		    size - alloc, src);
301 		spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL,
302 		    spa->spa_checkpoint_info.sci_dspace, src);
303 
304 		spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
305 		    metaslab_class_fragmentation(mc), src);
306 		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
307 		    metaslab_class_expandable_space(mc), src);
308 		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
309 		    (spa_mode(spa) == FREAD), src);
310 
311 		cap = (size == 0) ? 0 : (alloc * 100 / size);
312 		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
313 
314 		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
315 		    ddt_get_pool_dedup_ratio(spa), src);
316 
317 		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
318 		    rvd->vdev_state, src);
319 
320 		version = spa_version(spa);
321 		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
322 			src = ZPROP_SRC_DEFAULT;
323 		else
324 			src = ZPROP_SRC_LOCAL;
325 		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
326 	}
327 
328 	if (pool != NULL) {
329 		/*
330 		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
331 		 * when opening pools before this version freedir will be NULL.
332 		 */
333 		if (pool->dp_free_dir != NULL) {
334 			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
335 			    dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
336 			    src);
337 		} else {
338 			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
339 			    NULL, 0, src);
340 		}
341 
342 		if (pool->dp_leak_dir != NULL) {
343 			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
344 			    dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
345 			    src);
346 		} else {
347 			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
348 			    NULL, 0, src);
349 		}
350 	}
351 
352 	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
353 
354 	if (spa->spa_comment != NULL) {
355 		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
356 		    0, ZPROP_SRC_LOCAL);
357 	}
358 
359 	if (spa->spa_root != NULL)
360 		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
361 		    0, ZPROP_SRC_LOCAL);
362 
363 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
364 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
365 		    MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
366 	} else {
367 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
368 		    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
369 	}
370 
371 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) {
372 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
373 		    DNODE_MAX_SIZE, ZPROP_SRC_NONE);
374 	} else {
375 		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
376 		    DNODE_MIN_SIZE, ZPROP_SRC_NONE);
377 	}
378 
379 	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
380 		if (dp->scd_path == NULL) {
381 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
382 			    "none", 0, ZPROP_SRC_LOCAL);
383 		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
384 			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
385 			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
386 		}
387 	}
388 }
389 
390 /*
391  * Get zpool property values.
392  */
393 int
spa_prop_get(spa_t * spa,nvlist_t ** nvp)394 spa_prop_get(spa_t *spa, nvlist_t **nvp)
395 {
396 	objset_t *mos = spa->spa_meta_objset;
397 	zap_cursor_t zc;
398 	zap_attribute_t za;
399 	int err;
400 
401 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
402 
403 	mutex_enter(&spa->spa_props_lock);
404 
405 	/*
406 	 * Get properties from the spa config.
407 	 */
408 	spa_prop_get_config(spa, nvp);
409 
410 	/* If no pool property object, no more prop to get. */
411 	if (mos == NULL || spa->spa_pool_props_object == 0) {
412 		mutex_exit(&spa->spa_props_lock);
413 		return (0);
414 	}
415 
416 	/*
417 	 * Get properties from the MOS pool property object.
418 	 */
419 	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
420 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
421 	    zap_cursor_advance(&zc)) {
422 		uint64_t intval = 0;
423 		char *strval = NULL;
424 		zprop_source_t src = ZPROP_SRC_DEFAULT;
425 		zpool_prop_t prop;
426 
427 		if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL)
428 			continue;
429 
430 		switch (za.za_integer_length) {
431 		case 8:
432 			/* integer property */
433 			if (za.za_first_integer !=
434 			    zpool_prop_default_numeric(prop))
435 				src = ZPROP_SRC_LOCAL;
436 
437 			if (prop == ZPOOL_PROP_BOOTFS) {
438 				dsl_pool_t *dp;
439 				dsl_dataset_t *ds = NULL;
440 
441 				dp = spa_get_dsl(spa);
442 				dsl_pool_config_enter(dp, FTAG);
443 				err = dsl_dataset_hold_obj(dp,
444 				    za.za_first_integer, FTAG, &ds);
445 				if (err != 0) {
446 					dsl_pool_config_exit(dp, FTAG);
447 					break;
448 				}
449 
450 				strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
451 				    KM_SLEEP);
452 				dsl_dataset_name(ds, strval);
453 				dsl_dataset_rele(ds, FTAG);
454 				dsl_pool_config_exit(dp, FTAG);
455 			} else {
456 				strval = NULL;
457 				intval = za.za_first_integer;
458 			}
459 
460 			spa_prop_add_list(*nvp, prop, strval, intval, src);
461 
462 			if (strval != NULL)
463 				kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
464 
465 			break;
466 
467 		case 1:
468 			/* string property */
469 			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
470 			err = zap_lookup(mos, spa->spa_pool_props_object,
471 			    za.za_name, 1, za.za_num_integers, strval);
472 			if (err) {
473 				kmem_free(strval, za.za_num_integers);
474 				break;
475 			}
476 			spa_prop_add_list(*nvp, prop, strval, 0, src);
477 			kmem_free(strval, za.za_num_integers);
478 			break;
479 
480 		default:
481 			break;
482 		}
483 	}
484 	zap_cursor_fini(&zc);
485 	mutex_exit(&spa->spa_props_lock);
486 out:
487 	if (err && err != ENOENT) {
488 		nvlist_free(*nvp);
489 		*nvp = NULL;
490 		return (err);
491 	}
492 
493 	return (0);
494 }
495 
496 /*
497  * Validate the given pool properties nvlist and modify the list
498  * for the property values to be set.
499  */
500 static int
spa_prop_validate(spa_t * spa,nvlist_t * props)501 spa_prop_validate(spa_t *spa, nvlist_t *props)
502 {
503 	nvpair_t *elem;
504 	int error = 0, reset_bootfs = 0;
505 	uint64_t objnum = 0;
506 	boolean_t has_feature = B_FALSE;
507 
508 	elem = NULL;
509 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
510 		uint64_t intval;
511 		char *strval, *slash, *check, *fname;
512 		const char *propname = nvpair_name(elem);
513 		zpool_prop_t prop = zpool_name_to_prop(propname);
514 
515 		switch (prop) {
516 		case ZPOOL_PROP_INVAL:
517 			if (!zpool_prop_feature(propname)) {
518 				error = SET_ERROR(EINVAL);
519 				break;
520 			}
521 
522 			/*
523 			 * Sanitize the input.
524 			 */
525 			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
526 				error = SET_ERROR(EINVAL);
527 				break;
528 			}
529 
530 			if (nvpair_value_uint64(elem, &intval) != 0) {
531 				error = SET_ERROR(EINVAL);
532 				break;
533 			}
534 
535 			if (intval != 0) {
536 				error = SET_ERROR(EINVAL);
537 				break;
538 			}
539 
540 			fname = strchr(propname, '@') + 1;
541 			if (zfeature_lookup_name(fname, NULL) != 0) {
542 				error = SET_ERROR(EINVAL);
543 				break;
544 			}
545 
546 			has_feature = B_TRUE;
547 			break;
548 
549 		case ZPOOL_PROP_VERSION:
550 			error = nvpair_value_uint64(elem, &intval);
551 			if (!error &&
552 			    (intval < spa_version(spa) ||
553 			    intval > SPA_VERSION_BEFORE_FEATURES ||
554 			    has_feature))
555 				error = SET_ERROR(EINVAL);
556 			break;
557 
558 		case ZPOOL_PROP_DELEGATION:
559 		case ZPOOL_PROP_AUTOREPLACE:
560 		case ZPOOL_PROP_LISTSNAPS:
561 		case ZPOOL_PROP_AUTOEXPAND:
562 		case ZPOOL_PROP_AUTOTRIM:
563 			error = nvpair_value_uint64(elem, &intval);
564 			if (!error && intval > 1)
565 				error = SET_ERROR(EINVAL);
566 			break;
567 
568 		case ZPOOL_PROP_MULTIHOST:
569 			error = nvpair_value_uint64(elem, &intval);
570 			if (!error && intval > 1)
571 				error = SET_ERROR(EINVAL);
572 
573 			if (!error && !spa_get_hostid())
574 				error = SET_ERROR(ENOTSUP);
575 
576 			break;
577 
578 		case ZPOOL_PROP_BOOTFS:
579 			/*
580 			 * If the pool version is less than SPA_VERSION_BOOTFS,
581 			 * or the pool is still being created (version == 0),
582 			 * the bootfs property cannot be set.
583 			 */
584 			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
585 				error = SET_ERROR(ENOTSUP);
586 				break;
587 			}
588 
589 			/*
590 			 * Make sure the vdev config is bootable
591 			 */
592 			if (!vdev_is_bootable(spa->spa_root_vdev)) {
593 				error = SET_ERROR(ENOTSUP);
594 				break;
595 			}
596 
597 			reset_bootfs = 1;
598 
599 			error = nvpair_value_string(elem, &strval);
600 
601 			if (!error) {
602 				objset_t *os;
603 				uint64_t propval;
604 
605 				if (strval == NULL || strval[0] == '\0') {
606 					objnum = zpool_prop_default_numeric(
607 					    ZPOOL_PROP_BOOTFS);
608 					break;
609 				}
610 
611 				error = dmu_objset_hold(strval, FTAG, &os);
612 				if (error != 0)
613 					break;
614 
615 				/*
616 				 * Must be ZPL, and its property settings
617 				 * must be supported.
618 				 */
619 
620 				if (dmu_objset_type(os) != DMU_OST_ZFS) {
621 					error = SET_ERROR(ENOTSUP);
622 				} else if ((error =
623 				    dsl_prop_get_int_ds(dmu_objset_ds(os),
624 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
625 				    &propval)) == 0 &&
626 				    !BOOTFS_COMPRESS_VALID(propval)) {
627 					error = SET_ERROR(ENOTSUP);
628 				} else {
629 					objnum = dmu_objset_id(os);
630 				}
631 				dmu_objset_rele(os, FTAG);
632 			}
633 			break;
634 
635 		case ZPOOL_PROP_FAILUREMODE:
636 			error = nvpair_value_uint64(elem, &intval);
637 			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
638 			    intval > ZIO_FAILURE_MODE_PANIC))
639 				error = SET_ERROR(EINVAL);
640 
641 			/*
642 			 * This is a special case which only occurs when
643 			 * the pool has completely failed. This allows
644 			 * the user to change the in-core failmode property
645 			 * without syncing it out to disk (I/Os might
646 			 * currently be blocked). We do this by returning
647 			 * EIO to the caller (spa_prop_set) to trick it
648 			 * into thinking we encountered a property validation
649 			 * error.
650 			 */
651 			if (!error && spa_suspended(spa)) {
652 				spa->spa_failmode = intval;
653 				error = SET_ERROR(EIO);
654 			}
655 			break;
656 
657 		case ZPOOL_PROP_CACHEFILE:
658 			if ((error = nvpair_value_string(elem, &strval)) != 0)
659 				break;
660 
661 			if (strval[0] == '\0')
662 				break;
663 
664 			if (strcmp(strval, "none") == 0)
665 				break;
666 
667 			if (strval[0] != '/') {
668 				error = SET_ERROR(EINVAL);
669 				break;
670 			}
671 
672 			slash = strrchr(strval, '/');
673 			ASSERT(slash != NULL);
674 
675 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
676 			    strcmp(slash, "/..") == 0)
677 				error = SET_ERROR(EINVAL);
678 			break;
679 
680 		case ZPOOL_PROP_COMMENT:
681 			if ((error = nvpair_value_string(elem, &strval)) != 0)
682 				break;
683 			for (check = strval; *check != '\0'; check++) {
684 				/*
685 				 * The kernel doesn't have an easy isprint()
686 				 * check.  For this kernel check, we merely
687 				 * check ASCII apart from DEL.  Fix this if
688 				 * there is an easy-to-use kernel isprint().
689 				 */
690 				if (*check >= 0x7f) {
691 					error = SET_ERROR(EINVAL);
692 					break;
693 				}
694 			}
695 			if (strlen(strval) > ZPROP_MAX_COMMENT)
696 				error = E2BIG;
697 			break;
698 
699 		case ZPOOL_PROP_DEDUPDITTO:
700 			if (spa_version(spa) < SPA_VERSION_DEDUP)
701 				error = SET_ERROR(ENOTSUP);
702 			else
703 				error = nvpair_value_uint64(elem, &intval);
704 			if (error == 0 &&
705 			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
706 				error = SET_ERROR(EINVAL);
707 			break;
708 		}
709 
710 		if (error)
711 			break;
712 	}
713 
714 	if (!error && reset_bootfs) {
715 		error = nvlist_remove(props,
716 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
717 
718 		if (!error) {
719 			error = nvlist_add_uint64(props,
720 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
721 		}
722 	}
723 
724 	return (error);
725 }
726 
727 void
spa_configfile_set(spa_t * spa,nvlist_t * nvp,boolean_t need_sync)728 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
729 {
730 	char *cachefile;
731 	spa_config_dirent_t *dp;
732 
733 	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
734 	    &cachefile) != 0)
735 		return;
736 
737 	dp = kmem_alloc(sizeof (spa_config_dirent_t),
738 	    KM_SLEEP);
739 
740 	if (cachefile[0] == '\0')
741 		dp->scd_path = spa_strdup(spa_config_path);
742 	else if (strcmp(cachefile, "none") == 0)
743 		dp->scd_path = NULL;
744 	else
745 		dp->scd_path = spa_strdup(cachefile);
746 
747 	list_insert_head(&spa->spa_config_list, dp);
748 	if (need_sync)
749 		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
750 }
751 
752 int
spa_prop_set(spa_t * spa,nvlist_t * nvp)753 spa_prop_set(spa_t *spa, nvlist_t *nvp)
754 {
755 	int error;
756 	nvpair_t *elem = NULL;
757 	boolean_t need_sync = B_FALSE;
758 
759 	if ((error = spa_prop_validate(spa, nvp)) != 0)
760 		return (error);
761 
762 	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
763 		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
764 
765 		if (prop == ZPOOL_PROP_CACHEFILE ||
766 		    prop == ZPOOL_PROP_ALTROOT ||
767 		    prop == ZPOOL_PROP_READONLY)
768 			continue;
769 
770 		if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) {
771 			uint64_t ver;
772 
773 			if (prop == ZPOOL_PROP_VERSION) {
774 				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
775 			} else {
776 				ASSERT(zpool_prop_feature(nvpair_name(elem)));
777 				ver = SPA_VERSION_FEATURES;
778 				need_sync = B_TRUE;
779 			}
780 
781 			/* Save time if the version is already set. */
782 			if (ver == spa_version(spa))
783 				continue;
784 
785 			/*
786 			 * In addition to the pool directory object, we might
787 			 * create the pool properties object, the features for
788 			 * read object, the features for write object, or the
789 			 * feature descriptions object.
790 			 */
791 			error = dsl_sync_task(spa->spa_name, NULL,
792 			    spa_sync_version, &ver,
793 			    6, ZFS_SPACE_CHECK_RESERVED);
794 			if (error)
795 				return (error);
796 			continue;
797 		}
798 
799 		need_sync = B_TRUE;
800 		break;
801 	}
802 
803 	if (need_sync) {
804 		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
805 		    nvp, 6, ZFS_SPACE_CHECK_RESERVED));
806 	}
807 
808 	return (0);
809 }
810 
811 /*
812  * If the bootfs property value is dsobj, clear it.
813  */
814 void
spa_prop_clear_bootfs(spa_t * spa,uint64_t dsobj,dmu_tx_t * tx)815 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
816 {
817 	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
818 		VERIFY(zap_remove(spa->spa_meta_objset,
819 		    spa->spa_pool_props_object,
820 		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
821 		spa->spa_bootfs = 0;
822 	}
823 }
824 
825 /*ARGSUSED*/
826 static int
spa_change_guid_check(void * arg,dmu_tx_t * tx)827 spa_change_guid_check(void *arg, dmu_tx_t *tx)
828 {
829 	uint64_t *newguid = arg;
830 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
831 	vdev_t *rvd = spa->spa_root_vdev;
832 	uint64_t vdev_state;
833 
834 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
835 		int error = (spa_has_checkpoint(spa)) ?
836 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
837 		return (SET_ERROR(error));
838 	}
839 
840 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
841 	vdev_state = rvd->vdev_state;
842 	spa_config_exit(spa, SCL_STATE, FTAG);
843 
844 	if (vdev_state != VDEV_STATE_HEALTHY)
845 		return (SET_ERROR(ENXIO));
846 
847 	ASSERT3U(spa_guid(spa), !=, *newguid);
848 
849 	return (0);
850 }
851 
852 static void
spa_change_guid_sync(void * arg,dmu_tx_t * tx)853 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
854 {
855 	uint64_t *newguid = arg;
856 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
857 	uint64_t oldguid;
858 	vdev_t *rvd = spa->spa_root_vdev;
859 
860 	oldguid = spa_guid(spa);
861 
862 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
863 	rvd->vdev_guid = *newguid;
864 	rvd->vdev_guid_sum += (*newguid - oldguid);
865 	vdev_config_dirty(rvd);
866 	spa_config_exit(spa, SCL_STATE, FTAG);
867 
868 	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
869 	    oldguid, *newguid);
870 }
871 
872 /*
873  * Change the GUID for the pool.  This is done so that we can later
874  * re-import a pool built from a clone of our own vdevs.  We will modify
875  * the root vdev's guid, our own pool guid, and then mark all of our
876  * vdevs dirty.  Note that we must make sure that all our vdevs are
877  * online when we do this, or else any vdevs that weren't present
878  * would be orphaned from our pool.  We are also going to issue a
879  * sysevent to update any watchers.
880  */
881 int
spa_change_guid(spa_t * spa)882 spa_change_guid(spa_t *spa)
883 {
884 	int error;
885 	uint64_t guid;
886 
887 	mutex_enter(&spa->spa_vdev_top_lock);
888 	mutex_enter(&spa_namespace_lock);
889 	guid = spa_generate_guid(NULL);
890 
891 	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
892 	    spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
893 
894 	if (error == 0) {
895 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
896 		spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID);
897 	}
898 
899 	mutex_exit(&spa_namespace_lock);
900 	mutex_exit(&spa->spa_vdev_top_lock);
901 
902 	return (error);
903 }
904 
905 /*
906  * ==========================================================================
907  * SPA state manipulation (open/create/destroy/import/export)
908  * ==========================================================================
909  */
910 
911 static int
spa_error_entry_compare(const void * a,const void * b)912 spa_error_entry_compare(const void *a, const void *b)
913 {
914 	const spa_error_entry_t *sa = (const spa_error_entry_t *)a;
915 	const spa_error_entry_t *sb = (const spa_error_entry_t *)b;
916 	int ret;
917 
918 	ret = memcmp(&sa->se_bookmark, &sb->se_bookmark,
919 	    sizeof (zbookmark_phys_t));
920 
921 	return (TREE_ISIGN(ret));
922 }
923 
924 /*
925  * Utility function which retrieves copies of the current logs and
926  * re-initializes them in the process.
927  */
928 void
spa_get_errlists(spa_t * spa,avl_tree_t * last,avl_tree_t * scrub)929 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
930 {
931 	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
932 
933 	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
934 	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
935 
936 	avl_create(&spa->spa_errlist_scrub,
937 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
938 	    offsetof(spa_error_entry_t, se_avl));
939 	avl_create(&spa->spa_errlist_last,
940 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
941 	    offsetof(spa_error_entry_t, se_avl));
942 }
943 
944 static void
spa_taskqs_init(spa_t * spa,zio_type_t t,zio_taskq_type_t q)945 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
946 {
947 	const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
948 	enum zti_modes mode = ztip->zti_mode;
949 	uint_t value = ztip->zti_value;
950 	uint_t count = ztip->zti_count;
951 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
952 	char name[32];
953 	uint_t flags = 0;
954 	boolean_t batch = B_FALSE;
955 
956 	if (mode == ZTI_MODE_NULL) {
957 		tqs->stqs_count = 0;
958 		tqs->stqs_taskq = NULL;
959 		return;
960 	}
961 
962 	ASSERT3U(count, >, 0);
963 
964 	tqs->stqs_count = count;
965 	tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
966 
967 	switch (mode) {
968 	case ZTI_MODE_FIXED:
969 		ASSERT3U(value, >=, 1);
970 		value = MAX(value, 1);
971 		break;
972 
973 	case ZTI_MODE_BATCH:
974 		batch = B_TRUE;
975 		flags |= TASKQ_THREADS_CPU_PCT;
976 		value = zio_taskq_batch_pct;
977 		break;
978 
979 	default:
980 		panic("unrecognized mode for %s_%s taskq (%u:%u) in "
981 		    "spa_activate()",
982 		    zio_type_name[t], zio_taskq_types[q], mode, value);
983 		break;
984 	}
985 
986 	for (uint_t i = 0; i < count; i++) {
987 		taskq_t *tq;
988 
989 		if (count > 1) {
990 			(void) snprintf(name, sizeof (name), "%s_%s_%u",
991 			    zio_type_name[t], zio_taskq_types[q], i);
992 		} else {
993 			(void) snprintf(name, sizeof (name), "%s_%s",
994 			    zio_type_name[t], zio_taskq_types[q]);
995 		}
996 
997 		if (zio_taskq_sysdc && spa->spa_proc != &p0) {
998 			if (batch)
999 				flags |= TASKQ_DC_BATCH;
1000 
1001 			tq = taskq_create_sysdc(name, value, 50, INT_MAX,
1002 			    spa->spa_proc, zio_taskq_basedc, flags);
1003 		} else {
1004 			pri_t pri = maxclsyspri;
1005 			/*
1006 			 * The write issue taskq can be extremely CPU
1007 			 * intensive.  Run it at slightly lower priority
1008 			 * than the other taskqs.
1009 			 */
1010 			if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
1011 				pri--;
1012 
1013 			tq = taskq_create_proc(name, value, pri, 50,
1014 			    INT_MAX, spa->spa_proc, flags);
1015 		}
1016 
1017 		tqs->stqs_taskq[i] = tq;
1018 	}
1019 }
1020 
1021 static void
spa_taskqs_fini(spa_t * spa,zio_type_t t,zio_taskq_type_t q)1022 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
1023 {
1024 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1025 
1026 	if (tqs->stqs_taskq == NULL) {
1027 		ASSERT0(tqs->stqs_count);
1028 		return;
1029 	}
1030 
1031 	for (uint_t i = 0; i < tqs->stqs_count; i++) {
1032 		ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
1033 		taskq_destroy(tqs->stqs_taskq[i]);
1034 	}
1035 
1036 	kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
1037 	tqs->stqs_taskq = NULL;
1038 }
1039 
1040 /*
1041  * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1042  * Note that a type may have multiple discrete taskqs to avoid lock contention
1043  * on the taskq itself. In that case we choose which taskq at random by using
1044  * the low bits of gethrtime().
1045  */
1046 void
spa_taskq_dispatch_ent(spa_t * spa,zio_type_t t,zio_taskq_type_t q,task_func_t * func,void * arg,uint_t flags,taskq_ent_t * ent)1047 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
1048     task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
1049 {
1050 	spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1051 	taskq_t *tq;
1052 
1053 	ASSERT3P(tqs->stqs_taskq, !=, NULL);
1054 	ASSERT3U(tqs->stqs_count, !=, 0);
1055 
1056 	if (tqs->stqs_count == 1) {
1057 		tq = tqs->stqs_taskq[0];
1058 	} else {
1059 		tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
1060 	}
1061 
1062 	taskq_dispatch_ent(tq, func, arg, flags, ent);
1063 }
1064 
1065 static void
spa_create_zio_taskqs(spa_t * spa)1066 spa_create_zio_taskqs(spa_t *spa)
1067 {
1068 	for (int t = 0; t < ZIO_TYPES; t++) {
1069 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1070 			spa_taskqs_init(spa, t, q);
1071 		}
1072 	}
1073 }
1074 
1075 #ifdef _KERNEL
1076 static void
spa_thread(void * arg)1077 spa_thread(void *arg)
1078 {
1079 	callb_cpr_t cprinfo;
1080 
1081 	spa_t *spa = arg;
1082 	user_t *pu = PTOU(curproc);
1083 
1084 	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1085 	    spa->spa_name);
1086 
1087 	ASSERT(curproc != &p0);
1088 	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1089 	    "zpool-%s", spa->spa_name);
1090 	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1091 
1092 	/* bind this thread to the requested psrset */
1093 	if (zio_taskq_psrset_bind != PS_NONE) {
1094 		pool_lock();
1095 		mutex_enter(&cpu_lock);
1096 		mutex_enter(&pidlock);
1097 		mutex_enter(&curproc->p_lock);
1098 
1099 		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1100 		    0, NULL, NULL) == 0)  {
1101 			curthread->t_bind_pset = zio_taskq_psrset_bind;
1102 		} else {
1103 			cmn_err(CE_WARN,
1104 			    "Couldn't bind process for zfs pool \"%s\" to "
1105 			    "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1106 		}
1107 
1108 		mutex_exit(&curproc->p_lock);
1109 		mutex_exit(&pidlock);
1110 		mutex_exit(&cpu_lock);
1111 		pool_unlock();
1112 	}
1113 
1114 	if (zio_taskq_sysdc) {
1115 		sysdc_thread_enter(curthread, 100, 0);
1116 	}
1117 
1118 	spa->spa_proc = curproc;
1119 	spa->spa_did = curthread->t_did;
1120 
1121 	spa_create_zio_taskqs(spa);
1122 
1123 	mutex_enter(&spa->spa_proc_lock);
1124 	ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1125 
1126 	spa->spa_proc_state = SPA_PROC_ACTIVE;
1127 	cv_broadcast(&spa->spa_proc_cv);
1128 
1129 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
1130 	while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1131 		cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1132 	CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1133 
1134 	ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1135 	spa->spa_proc_state = SPA_PROC_GONE;
1136 	spa->spa_proc = &p0;
1137 	cv_broadcast(&spa->spa_proc_cv);
1138 	CALLB_CPR_EXIT(&cprinfo);	/* drops spa_proc_lock */
1139 
1140 	mutex_enter(&curproc->p_lock);
1141 	lwp_exit();
1142 }
1143 #endif
1144 
1145 /*
1146  * Activate an uninitialized pool.
1147  */
1148 static void
spa_activate(spa_t * spa,int mode)1149 spa_activate(spa_t *spa, int mode)
1150 {
1151 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1152 
1153 	spa->spa_state = POOL_STATE_ACTIVE;
1154 	spa->spa_mode = mode;
1155 
1156 	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1157 	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1158 	spa->spa_special_class = metaslab_class_create(spa, zfs_metaslab_ops);
1159 	spa->spa_dedup_class = metaslab_class_create(spa, zfs_metaslab_ops);
1160 
1161 	/* Try to create a covering process */
1162 	mutex_enter(&spa->spa_proc_lock);
1163 	ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1164 	ASSERT(spa->spa_proc == &p0);
1165 	spa->spa_did = 0;
1166 
1167 	/* Only create a process if we're going to be around a while. */
1168 	if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1169 		if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1170 		    NULL, 0) == 0) {
1171 			spa->spa_proc_state = SPA_PROC_CREATED;
1172 			while (spa->spa_proc_state == SPA_PROC_CREATED) {
1173 				cv_wait(&spa->spa_proc_cv,
1174 				    &spa->spa_proc_lock);
1175 			}
1176 			ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1177 			ASSERT(spa->spa_proc != &p0);
1178 			ASSERT(spa->spa_did != 0);
1179 		} else {
1180 #ifdef _KERNEL
1181 			cmn_err(CE_WARN,
1182 			    "Couldn't create process for zfs pool \"%s\"\n",
1183 			    spa->spa_name);
1184 #endif
1185 		}
1186 	}
1187 	mutex_exit(&spa->spa_proc_lock);
1188 
1189 	/* If we didn't create a process, we need to create our taskqs. */
1190 	if (spa->spa_proc == &p0) {
1191 		spa_create_zio_taskqs(spa);
1192 	}
1193 
1194 	for (size_t i = 0; i < TXG_SIZE; i++) {
1195 		spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL,
1196 		    ZIO_FLAG_CANFAIL);
1197 	}
1198 
1199 	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1200 	    offsetof(vdev_t, vdev_config_dirty_node));
1201 	list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1202 	    offsetof(objset_t, os_evicting_node));
1203 	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1204 	    offsetof(vdev_t, vdev_state_dirty_node));
1205 
1206 	txg_list_create(&spa->spa_vdev_txg_list, spa,
1207 	    offsetof(struct vdev, vdev_txg_node));
1208 
1209 	avl_create(&spa->spa_errlist_scrub,
1210 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1211 	    offsetof(spa_error_entry_t, se_avl));
1212 	avl_create(&spa->spa_errlist_last,
1213 	    spa_error_entry_compare, sizeof (spa_error_entry_t),
1214 	    offsetof(spa_error_entry_t, se_avl));
1215 
1216 	spa_keystore_init(&spa->spa_keystore);
1217 
1218 	/*
1219 	 * The taskq to upgrade datasets in this pool. Currently used by
1220 	 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1221 	 */
1222 	spa->spa_upgrade_taskq = taskq_create("z_upgrade", boot_ncpus,
1223 	    minclsyspri, 1, INT_MAX, TASKQ_DYNAMIC);
1224 }
1225 
1226 /*
1227  * Opposite of spa_activate().
1228  */
1229 static void
spa_deactivate(spa_t * spa)1230 spa_deactivate(spa_t *spa)
1231 {
1232 	ASSERT(spa->spa_sync_on == B_FALSE);
1233 	ASSERT(spa->spa_dsl_pool == NULL);
1234 	ASSERT(spa->spa_root_vdev == NULL);
1235 	ASSERT(spa->spa_async_zio_root == NULL);
1236 	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1237 
1238 	spa_evicting_os_wait(spa);
1239 
1240 	if (spa->spa_upgrade_taskq) {
1241 		taskq_destroy(spa->spa_upgrade_taskq);
1242 		spa->spa_upgrade_taskq = NULL;
1243 	}
1244 
1245 	txg_list_destroy(&spa->spa_vdev_txg_list);
1246 
1247 	list_destroy(&spa->spa_config_dirty_list);
1248 	list_destroy(&spa->spa_evicting_os_list);
1249 	list_destroy(&spa->spa_state_dirty_list);
1250 
1251 	for (int t = 0; t < ZIO_TYPES; t++) {
1252 		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1253 			spa_taskqs_fini(spa, t, q);
1254 		}
1255 	}
1256 
1257 	for (size_t i = 0; i < TXG_SIZE; i++) {
1258 		ASSERT3P(spa->spa_txg_zio[i], !=, NULL);
1259 		VERIFY0(zio_wait(spa->spa_txg_zio[i]));
1260 		spa->spa_txg_zio[i] = NULL;
1261 	}
1262 
1263 	metaslab_class_destroy(spa->spa_normal_class);
1264 	spa->spa_normal_class = NULL;
1265 
1266 	metaslab_class_destroy(spa->spa_log_class);
1267 	spa->spa_log_class = NULL;
1268 
1269 	metaslab_class_destroy(spa->spa_special_class);
1270 	spa->spa_special_class = NULL;
1271 
1272 	metaslab_class_destroy(spa->spa_dedup_class);
1273 	spa->spa_dedup_class = NULL;
1274 
1275 	/*
1276 	 * If this was part of an import or the open otherwise failed, we may
1277 	 * still have errors left in the queues.  Empty them just in case.
1278 	 */
1279 	spa_errlog_drain(spa);
1280 	avl_destroy(&spa->spa_errlist_scrub);
1281 	avl_destroy(&spa->spa_errlist_last);
1282 
1283 	spa_keystore_fini(&spa->spa_keystore);
1284 
1285 	spa->spa_state = POOL_STATE_UNINITIALIZED;
1286 
1287 	mutex_enter(&spa->spa_proc_lock);
1288 	if (spa->spa_proc_state != SPA_PROC_NONE) {
1289 		ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1290 		spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1291 		cv_broadcast(&spa->spa_proc_cv);
1292 		while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1293 			ASSERT(spa->spa_proc != &p0);
1294 			cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1295 		}
1296 		ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1297 		spa->spa_proc_state = SPA_PROC_NONE;
1298 	}
1299 	ASSERT(spa->spa_proc == &p0);
1300 	mutex_exit(&spa->spa_proc_lock);
1301 
1302 	/*
1303 	 * We want to make sure spa_thread() has actually exited the ZFS
1304 	 * module, so that the module can't be unloaded out from underneath
1305 	 * it.
1306 	 */
1307 	if (spa->spa_did != 0) {
1308 		thread_join(spa->spa_did);
1309 		spa->spa_did = 0;
1310 	}
1311 }
1312 
1313 /*
1314  * Verify a pool configuration, and construct the vdev tree appropriately.  This
1315  * will create all the necessary vdevs in the appropriate layout, with each vdev
1316  * in the CLOSED state.  This will prep the pool before open/creation/import.
1317  * All vdev validation is done by the vdev_alloc() routine.
1318  */
1319 static int
spa_config_parse(spa_t * spa,vdev_t ** vdp,nvlist_t * nv,vdev_t * parent,uint_t id,int atype)1320 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1321     uint_t id, int atype)
1322 {
1323 	nvlist_t **child;
1324 	uint_t children;
1325 	int error;
1326 
1327 	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1328 		return (error);
1329 
1330 	if ((*vdp)->vdev_ops->vdev_op_leaf)
1331 		return (0);
1332 
1333 	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1334 	    &child, &children);
1335 
1336 	if (error == ENOENT)
1337 		return (0);
1338 
1339 	if (error) {
1340 		vdev_free(*vdp);
1341 		*vdp = NULL;
1342 		return (SET_ERROR(EINVAL));
1343 	}
1344 
1345 	for (int c = 0; c < children; c++) {
1346 		vdev_t *vd;
1347 		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1348 		    atype)) != 0) {
1349 			vdev_free(*vdp);
1350 			*vdp = NULL;
1351 			return (error);
1352 		}
1353 	}
1354 
1355 	ASSERT(*vdp != NULL);
1356 
1357 	return (0);
1358 }
1359 
1360 static boolean_t
spa_should_flush_logs_on_unload(spa_t * spa)1361 spa_should_flush_logs_on_unload(spa_t *spa)
1362 {
1363 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
1364 		return (B_FALSE);
1365 
1366 	if (!spa_writeable(spa))
1367 		return (B_FALSE);
1368 
1369 	if (!spa->spa_sync_on)
1370 		return (B_FALSE);
1371 
1372 	if (spa_state(spa) != POOL_STATE_EXPORTED)
1373 		return (B_FALSE);
1374 
1375 	if (zfs_keep_log_spacemaps_at_export)
1376 		return (B_FALSE);
1377 
1378 	return (B_TRUE);
1379 }
1380 
1381 /*
1382  * Opens a transaction that will set the flag that will instruct
1383  * spa_sync to attempt to flush all the metaslabs for that txg.
1384  */
1385 static void
spa_unload_log_sm_flush_all(spa_t * spa)1386 spa_unload_log_sm_flush_all(spa_t *spa)
1387 {
1388 	dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
1389 
1390 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
1391 
1392 	ASSERT3U(spa->spa_log_flushall_txg, ==, 0);
1393 	spa->spa_log_flushall_txg = dmu_tx_get_txg(tx);
1394 
1395 	dmu_tx_commit(tx);
1396 	txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg);
1397 }
1398 
1399 static void
spa_unload_log_sm_metadata(spa_t * spa)1400 spa_unload_log_sm_metadata(spa_t *spa)
1401 {
1402 	void *cookie = NULL;
1403 	spa_log_sm_t *sls;
1404 
1405 	while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg,
1406 	    &cookie)) != NULL) {
1407 		VERIFY0(sls->sls_mscount);
1408 		kmem_free(sls, sizeof (spa_log_sm_t));
1409 	}
1410 
1411 	for (log_summary_entry_t *e = list_head(&spa->spa_log_summary);
1412 	    e != NULL; e = list_head(&spa->spa_log_summary)) {
1413 		VERIFY0(e->lse_mscount);
1414 		list_remove(&spa->spa_log_summary, e);
1415 		kmem_free(e, sizeof (log_summary_entry_t));
1416 	}
1417 
1418 	spa->spa_unflushed_stats.sus_nblocks = 0;
1419 	spa->spa_unflushed_stats.sus_memused = 0;
1420 	spa->spa_unflushed_stats.sus_blocklimit = 0;
1421 }
1422 
1423 /*
1424  * Opposite of spa_load().
1425  */
1426 static void
spa_unload(spa_t * spa)1427 spa_unload(spa_t *spa)
1428 {
1429 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1430 	ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED);
1431 
1432 	spa_import_progress_remove(spa);
1433 	spa_load_note(spa, "UNLOADING");
1434 
1435 	/*
1436 	 * If the log space map feature is enabled and the pool is getting
1437 	 * exported (but not destroyed), we want to spend some time flushing
1438 	 * as many metaslabs as we can in an attempt to destroy log space
1439 	 * maps and save import time.
1440 	 */
1441 	if (spa_should_flush_logs_on_unload(spa))
1442 		spa_unload_log_sm_flush_all(spa);
1443 
1444 	/*
1445 	 * Stop async tasks.
1446 	 */
1447 	spa_async_suspend(spa);
1448 
1449 	if (spa->spa_root_vdev) {
1450 		vdev_t *root_vdev = spa->spa_root_vdev;
1451 		vdev_initialize_stop_all(root_vdev, VDEV_INITIALIZE_ACTIVE);
1452 		vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE);
1453 		vdev_autotrim_stop_all(spa);
1454 	}
1455 
1456 	/*
1457 	 * Stop syncing.
1458 	 */
1459 	if (spa->spa_sync_on) {
1460 		txg_sync_stop(spa->spa_dsl_pool);
1461 		spa->spa_sync_on = B_FALSE;
1462 	}
1463 
1464 	/*
1465 	 * This ensures that there is no async metaslab prefetching
1466 	 * while we attempt to unload the spa.
1467 	 */
1468 	if (spa->spa_root_vdev != NULL) {
1469 		for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) {
1470 			vdev_t *vc = spa->spa_root_vdev->vdev_child[c];
1471 			if (vc->vdev_mg != NULL)
1472 				taskq_wait(vc->vdev_mg->mg_taskq);
1473 		}
1474 	}
1475 
1476 	if (spa->spa_mmp.mmp_thread)
1477 		mmp_thread_stop(spa);
1478 
1479 	/*
1480 	 * Wait for any outstanding async I/O to complete.
1481 	 */
1482 	if (spa->spa_async_zio_root != NULL) {
1483 		for (int i = 0; i < max_ncpus; i++)
1484 			(void) zio_wait(spa->spa_async_zio_root[i]);
1485 		kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1486 		spa->spa_async_zio_root = NULL;
1487 	}
1488 
1489 	if (spa->spa_vdev_removal != NULL) {
1490 		spa_vdev_removal_destroy(spa->spa_vdev_removal);
1491 		spa->spa_vdev_removal = NULL;
1492 	}
1493 
1494 	if (spa->spa_condense_zthr != NULL) {
1495 		zthr_destroy(spa->spa_condense_zthr);
1496 		spa->spa_condense_zthr = NULL;
1497 	}
1498 
1499 	if (spa->spa_checkpoint_discard_zthr != NULL) {
1500 		zthr_destroy(spa->spa_checkpoint_discard_zthr);
1501 		spa->spa_checkpoint_discard_zthr = NULL;
1502 	}
1503 
1504 	spa_condense_fini(spa);
1505 
1506 	bpobj_close(&spa->spa_deferred_bpobj);
1507 
1508 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1509 
1510 	/*
1511 	 * Close all vdevs.
1512 	 */
1513 	if (spa->spa_root_vdev)
1514 		vdev_free(spa->spa_root_vdev);
1515 	ASSERT(spa->spa_root_vdev == NULL);
1516 
1517 	/*
1518 	 * Close the dsl pool.
1519 	 */
1520 	if (spa->spa_dsl_pool) {
1521 		dsl_pool_close(spa->spa_dsl_pool);
1522 		spa->spa_dsl_pool = NULL;
1523 		spa->spa_meta_objset = NULL;
1524 	}
1525 
1526 	ddt_unload(spa);
1527 	spa_unload_log_sm_metadata(spa);
1528 
1529 	/*
1530 	 * Drop and purge level 2 cache
1531 	 */
1532 	spa_l2cache_drop(spa);
1533 
1534 	for (int i = 0; i < spa->spa_spares.sav_count; i++)
1535 		vdev_free(spa->spa_spares.sav_vdevs[i]);
1536 	if (spa->spa_spares.sav_vdevs) {
1537 		kmem_free(spa->spa_spares.sav_vdevs,
1538 		    spa->spa_spares.sav_count * sizeof (void *));
1539 		spa->spa_spares.sav_vdevs = NULL;
1540 	}
1541 	if (spa->spa_spares.sav_config) {
1542 		nvlist_free(spa->spa_spares.sav_config);
1543 		spa->spa_spares.sav_config = NULL;
1544 	}
1545 	spa->spa_spares.sav_count = 0;
1546 
1547 	for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
1548 		vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1549 		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1550 	}
1551 	if (spa->spa_l2cache.sav_vdevs) {
1552 		kmem_free(spa->spa_l2cache.sav_vdevs,
1553 		    spa->spa_l2cache.sav_count * sizeof (void *));
1554 		spa->spa_l2cache.sav_vdevs = NULL;
1555 	}
1556 	if (spa->spa_l2cache.sav_config) {
1557 		nvlist_free(spa->spa_l2cache.sav_config);
1558 		spa->spa_l2cache.sav_config = NULL;
1559 	}
1560 	spa->spa_l2cache.sav_count = 0;
1561 
1562 	spa->spa_async_suspended = 0;
1563 
1564 	spa->spa_indirect_vdevs_loaded = B_FALSE;
1565 
1566 	if (spa->spa_comment != NULL) {
1567 		spa_strfree(spa->spa_comment);
1568 		spa->spa_comment = NULL;
1569 	}
1570 
1571 	spa_config_exit(spa, SCL_ALL, spa);
1572 }
1573 
1574 /*
1575  * Load (or re-load) the current list of vdevs describing the active spares for
1576  * this pool.  When this is called, we have some form of basic information in
1577  * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
1578  * then re-generate a more complete list including status information.
1579  */
1580 void
spa_load_spares(spa_t * spa)1581 spa_load_spares(spa_t *spa)
1582 {
1583 	nvlist_t **spares;
1584 	uint_t nspares;
1585 	int i;
1586 	vdev_t *vd, *tvd;
1587 
1588 #ifndef _KERNEL
1589 	/*
1590 	 * zdb opens both the current state of the pool and the
1591 	 * checkpointed state (if present), with a different spa_t.
1592 	 *
1593 	 * As spare vdevs are shared among open pools, we skip loading
1594 	 * them when we load the checkpointed state of the pool.
1595 	 */
1596 	if (!spa_writeable(spa))
1597 		return;
1598 #endif
1599 
1600 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1601 
1602 	/*
1603 	 * First, close and free any existing spare vdevs.
1604 	 */
1605 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
1606 		vd = spa->spa_spares.sav_vdevs[i];
1607 
1608 		/* Undo the call to spa_activate() below */
1609 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1610 		    B_FALSE)) != NULL && tvd->vdev_isspare)
1611 			spa_spare_remove(tvd);
1612 		vdev_close(vd);
1613 		vdev_free(vd);
1614 	}
1615 
1616 	if (spa->spa_spares.sav_vdevs)
1617 		kmem_free(spa->spa_spares.sav_vdevs,
1618 		    spa->spa_spares.sav_count * sizeof (void *));
1619 
1620 	if (spa->spa_spares.sav_config == NULL)
1621 		nspares = 0;
1622 	else
1623 		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1624 		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1625 
1626 	spa->spa_spares.sav_count = (int)nspares;
1627 	spa->spa_spares.sav_vdevs = NULL;
1628 
1629 	if (nspares == 0)
1630 		return;
1631 
1632 	/*
1633 	 * Construct the array of vdevs, opening them to get status in the
1634 	 * process.   For each spare, there is potentially two different vdev_t
1635 	 * structures associated with it: one in the list of spares (used only
1636 	 * for basic validation purposes) and one in the active vdev
1637 	 * configuration (if it's spared in).  During this phase we open and
1638 	 * validate each vdev on the spare list.  If the vdev also exists in the
1639 	 * active configuration, then we also mark this vdev as an active spare.
1640 	 */
1641 	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1642 	    KM_SLEEP);
1643 	for (i = 0; i < spa->spa_spares.sav_count; i++) {
1644 		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1645 		    VDEV_ALLOC_SPARE) == 0);
1646 		ASSERT(vd != NULL);
1647 
1648 		spa->spa_spares.sav_vdevs[i] = vd;
1649 
1650 		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1651 		    B_FALSE)) != NULL) {
1652 			if (!tvd->vdev_isspare)
1653 				spa_spare_add(tvd);
1654 
1655 			/*
1656 			 * We only mark the spare active if we were successfully
1657 			 * able to load the vdev.  Otherwise, importing a pool
1658 			 * with a bad active spare would result in strange
1659 			 * behavior, because multiple pool would think the spare
1660 			 * is actively in use.
1661 			 *
1662 			 * There is a vulnerability here to an equally bizarre
1663 			 * circumstance, where a dead active spare is later
1664 			 * brought back to life (onlined or otherwise).  Given
1665 			 * the rarity of this scenario, and the extra complexity
1666 			 * it adds, we ignore the possibility.
1667 			 */
1668 			if (!vdev_is_dead(tvd))
1669 				spa_spare_activate(tvd);
1670 		}
1671 
1672 		vd->vdev_top = vd;
1673 		vd->vdev_aux = &spa->spa_spares;
1674 
1675 		if (vdev_open(vd) != 0)
1676 			continue;
1677 
1678 		if (vdev_validate_aux(vd) == 0)
1679 			spa_spare_add(vd);
1680 	}
1681 
1682 	/*
1683 	 * Recompute the stashed list of spares, with status information
1684 	 * this time.
1685 	 */
1686 	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1687 	    DATA_TYPE_NVLIST_ARRAY) == 0);
1688 
1689 	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1690 	    KM_SLEEP);
1691 	for (i = 0; i < spa->spa_spares.sav_count; i++)
1692 		spares[i] = vdev_config_generate(spa,
1693 		    spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1694 	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1695 	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1696 	for (i = 0; i < spa->spa_spares.sav_count; i++)
1697 		nvlist_free(spares[i]);
1698 	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1699 }
1700 
1701 /*
1702  * Load (or re-load) the current list of vdevs describing the active l2cache for
1703  * this pool.  When this is called, we have some form of basic information in
1704  * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
1705  * then re-generate a more complete list including status information.
1706  * Devices which are already active have their details maintained, and are
1707  * not re-opened.
1708  */
1709 void
spa_load_l2cache(spa_t * spa)1710 spa_load_l2cache(spa_t *spa)
1711 {
1712 	nvlist_t **l2cache;
1713 	uint_t nl2cache;
1714 	int i, j, oldnvdevs;
1715 	uint64_t guid;
1716 	vdev_t *vd, **oldvdevs, **newvdevs;
1717 	spa_aux_vdev_t *sav = &spa->spa_l2cache;
1718 
1719 #ifndef _KERNEL
1720 	/*
1721 	 * zdb opens both the current state of the pool and the
1722 	 * checkpointed state (if present), with a different spa_t.
1723 	 *
1724 	 * As L2 caches are part of the ARC which is shared among open
1725 	 * pools, we skip loading them when we load the checkpointed
1726 	 * state of the pool.
1727 	 */
1728 	if (!spa_writeable(spa))
1729 		return;
1730 #endif
1731 
1732 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1733 
1734 	nl2cache = 0;
1735 	newvdevs = NULL;
1736 	if (sav->sav_config != NULL) {
1737 		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1738 		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1739 		if (nl2cache > 0) {
1740 			newvdevs = kmem_alloc(
1741 			    nl2cache * sizeof (void *), KM_SLEEP);
1742 		}
1743 	}
1744 
1745 	oldvdevs = sav->sav_vdevs;
1746 	oldnvdevs = sav->sav_count;
1747 	sav->sav_vdevs = NULL;
1748 	sav->sav_count = 0;
1749 
1750 	/*
1751 	 * Process new nvlist of vdevs.
1752 	 */
1753 	for (i = 0; i < nl2cache; i++) {
1754 		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1755 		    &guid) == 0);
1756 
1757 		newvdevs[i] = NULL;
1758 		for (j = 0; j < oldnvdevs; j++) {
1759 			vd = oldvdevs[j];
1760 			if (vd != NULL && guid == vd->vdev_guid) {
1761 				/*
1762 				 * Retain previous vdev for add/remove ops.
1763 				 */
1764 				newvdevs[i] = vd;
1765 				oldvdevs[j] = NULL;
1766 				break;
1767 			}
1768 		}
1769 
1770 		if (newvdevs[i] == NULL) {
1771 			/*
1772 			 * Create new vdev
1773 			 */
1774 			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1775 			    VDEV_ALLOC_L2CACHE) == 0);
1776 			ASSERT(vd != NULL);
1777 			newvdevs[i] = vd;
1778 
1779 			/*
1780 			 * Commit this vdev as an l2cache device,
1781 			 * even if it fails to open.
1782 			 */
1783 			spa_l2cache_add(vd);
1784 
1785 			vd->vdev_top = vd;
1786 			vd->vdev_aux = sav;
1787 
1788 			spa_l2cache_activate(vd);
1789 
1790 			if (vdev_open(vd) != 0)
1791 				continue;
1792 
1793 			(void) vdev_validate_aux(vd);
1794 
1795 			if (!vdev_is_dead(vd))
1796 				l2arc_add_vdev(spa, vd);
1797 		}
1798 	}
1799 
1800 	/*
1801 	 * Purge vdevs that were dropped
1802 	 */
1803 	for (i = 0; i < oldnvdevs; i++) {
1804 		uint64_t pool;
1805 
1806 		vd = oldvdevs[i];
1807 		if (vd != NULL) {
1808 			ASSERT(vd->vdev_isl2cache);
1809 
1810 			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1811 			    pool != 0ULL && l2arc_vdev_present(vd))
1812 				l2arc_remove_vdev(vd);
1813 			vdev_clear_stats(vd);
1814 			vdev_free(vd);
1815 		}
1816 	}
1817 
1818 	if (oldvdevs)
1819 		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1820 
1821 	if (sav->sav_config == NULL)
1822 		goto out;
1823 
1824 	sav->sav_vdevs = newvdevs;
1825 	sav->sav_count = (int)nl2cache;
1826 
1827 	/*
1828 	 * Recompute the stashed list of l2cache devices, with status
1829 	 * information this time.
1830 	 */
1831 	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1832 	    DATA_TYPE_NVLIST_ARRAY) == 0);
1833 
1834 	l2cache = NULL;
1835 	if (sav->sav_count > 0) {
1836 		l2cache = kmem_alloc(
1837 		    sav->sav_count * sizeof (void *), KM_SLEEP);
1838 	}
1839 	for (i = 0; i < sav->sav_count; i++)
1840 		l2cache[i] = vdev_config_generate(spa,
1841 		    sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1842 	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1843 	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1844 out:
1845 	for (i = 0; i < sav->sav_count; i++)
1846 		nvlist_free(l2cache[i]);
1847 	if (sav->sav_count)
1848 		kmem_free(l2cache, sav->sav_count * sizeof (void *));
1849 }
1850 
1851 static int
load_nvlist(spa_t * spa,uint64_t obj,nvlist_t ** value)1852 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1853 {
1854 	dmu_buf_t *db;
1855 	char *packed = NULL;
1856 	size_t nvsize = 0;
1857 	int error;
1858 	*value = NULL;
1859 
1860 	error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1861 	if (error != 0)
1862 		return (error);
1863 
1864 	nvsize = *(uint64_t *)db->db_data;
1865 	dmu_buf_rele(db, FTAG);
1866 
1867 	packed = kmem_alloc(nvsize, KM_SLEEP);
1868 	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1869 	    DMU_READ_PREFETCH);
1870 	if (error == 0)
1871 		error = nvlist_unpack(packed, nvsize, value, 0);
1872 	kmem_free(packed, nvsize);
1873 
1874 	return (error);
1875 }
1876 
1877 /*
1878  * Concrete top-level vdevs that are not missing and are not logs. At every
1879  * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1880  */
1881 static uint64_t
spa_healthy_core_tvds(spa_t * spa)1882 spa_healthy_core_tvds(spa_t *spa)
1883 {
1884 	vdev_t *rvd = spa->spa_root_vdev;
1885 	uint64_t tvds = 0;
1886 
1887 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
1888 		vdev_t *vd = rvd->vdev_child[i];
1889 		if (vd->vdev_islog)
1890 			continue;
1891 		if (vdev_is_concrete(vd) && !vdev_is_dead(vd))
1892 			tvds++;
1893 	}
1894 
1895 	return (tvds);
1896 }
1897 
1898 /*
1899  * Checks to see if the given vdev could not be opened, in which case we post a
1900  * sysevent to notify the autoreplace code that the device has been removed.
1901  */
1902 static void
spa_check_removed(vdev_t * vd)1903 spa_check_removed(vdev_t *vd)
1904 {
1905 	for (uint64_t c = 0; c < vd->vdev_children; c++)
1906 		spa_check_removed(vd->vdev_child[c]);
1907 
1908 	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1909 	    vdev_is_concrete(vd)) {
1910 		zfs_post_autoreplace(vd->vdev_spa, vd);
1911 		spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK);
1912 	}
1913 }
1914 
1915 static int
spa_check_for_missing_logs(spa_t * spa)1916 spa_check_for_missing_logs(spa_t *spa)
1917 {
1918 	vdev_t *rvd = spa->spa_root_vdev;
1919 
1920 	/*
1921 	 * If we're doing a normal import, then build up any additional
1922 	 * diagnostic information about missing log devices.
1923 	 * We'll pass this up to the user for further processing.
1924 	 */
1925 	if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1926 		nvlist_t **child, *nv;
1927 		uint64_t idx = 0;
1928 
1929 		child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1930 		    KM_SLEEP);
1931 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1932 
1933 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
1934 			vdev_t *tvd = rvd->vdev_child[c];
1935 
1936 			/*
1937 			 * We consider a device as missing only if it failed
1938 			 * to open (i.e. offline or faulted is not considered
1939 			 * as missing).
1940 			 */
1941 			if (tvd->vdev_islog &&
1942 			    tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
1943 				child[idx++] = vdev_config_generate(spa, tvd,
1944 				    B_FALSE, VDEV_CONFIG_MISSING);
1945 			}
1946 		}
1947 
1948 		if (idx > 0) {
1949 			fnvlist_add_nvlist_array(nv,
1950 			    ZPOOL_CONFIG_CHILDREN, child, idx);
1951 			fnvlist_add_nvlist(spa->spa_load_info,
1952 			    ZPOOL_CONFIG_MISSING_DEVICES, nv);
1953 
1954 			for (uint64_t i = 0; i < idx; i++)
1955 				nvlist_free(child[i]);
1956 		}
1957 		nvlist_free(nv);
1958 		kmem_free(child, rvd->vdev_children * sizeof (char **));
1959 
1960 		if (idx > 0) {
1961 			spa_load_failed(spa, "some log devices are missing");
1962 			vdev_dbgmsg_print_tree(rvd, 2);
1963 			return (SET_ERROR(ENXIO));
1964 		}
1965 	} else {
1966 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
1967 			vdev_t *tvd = rvd->vdev_child[c];
1968 
1969 			if (tvd->vdev_islog &&
1970 			    tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
1971 				spa_set_log_state(spa, SPA_LOG_CLEAR);
1972 				spa_load_note(spa, "some log devices are "
1973 				    "missing, ZIL is dropped.");
1974 				vdev_dbgmsg_print_tree(rvd, 2);
1975 				break;
1976 			}
1977 		}
1978 	}
1979 
1980 	return (0);
1981 }
1982 
1983 /*
1984  * Check for missing log devices
1985  */
1986 static boolean_t
spa_check_logs(spa_t * spa)1987 spa_check_logs(spa_t *spa)
1988 {
1989 	boolean_t rv = B_FALSE;
1990 	dsl_pool_t *dp = spa_get_dsl(spa);
1991 
1992 	switch (spa->spa_log_state) {
1993 	case SPA_LOG_MISSING:
1994 		/* need to recheck in case slog has been restored */
1995 	case SPA_LOG_UNKNOWN:
1996 		rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1997 		    zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1998 		if (rv)
1999 			spa_set_log_state(spa, SPA_LOG_MISSING);
2000 		break;
2001 	}
2002 	return (rv);
2003 }
2004 
2005 static boolean_t
spa_passivate_log(spa_t * spa)2006 spa_passivate_log(spa_t *spa)
2007 {
2008 	vdev_t *rvd = spa->spa_root_vdev;
2009 	boolean_t slog_found = B_FALSE;
2010 
2011 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
2012 
2013 	if (!spa_has_slogs(spa))
2014 		return (B_FALSE);
2015 
2016 	for (int c = 0; c < rvd->vdev_children; c++) {
2017 		vdev_t *tvd = rvd->vdev_child[c];
2018 		metaslab_group_t *mg = tvd->vdev_mg;
2019 
2020 		if (tvd->vdev_islog) {
2021 			metaslab_group_passivate(mg);
2022 			slog_found = B_TRUE;
2023 		}
2024 	}
2025 
2026 	return (slog_found);
2027 }
2028 
2029 static void
spa_activate_log(spa_t * spa)2030 spa_activate_log(spa_t *spa)
2031 {
2032 	vdev_t *rvd = spa->spa_root_vdev;
2033 
2034 	ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
2035 
2036 	for (int c = 0; c < rvd->vdev_children; c++) {
2037 		vdev_t *tvd = rvd->vdev_child[c];
2038 		metaslab_group_t *mg = tvd->vdev_mg;
2039 
2040 		if (tvd->vdev_islog)
2041 			metaslab_group_activate(mg);
2042 	}
2043 }
2044 
2045 int
spa_reset_logs(spa_t * spa)2046 spa_reset_logs(spa_t *spa)
2047 {
2048 	int error;
2049 
2050 	error = dmu_objset_find(spa_name(spa), zil_reset,
2051 	    NULL, DS_FIND_CHILDREN);
2052 	if (error == 0) {
2053 		/*
2054 		 * We successfully offlined the log device, sync out the
2055 		 * current txg so that the "stubby" block can be removed
2056 		 * by zil_sync().
2057 		 */
2058 		txg_wait_synced(spa->spa_dsl_pool, 0);
2059 	}
2060 	return (error);
2061 }
2062 
2063 static void
spa_aux_check_removed(spa_aux_vdev_t * sav)2064 spa_aux_check_removed(spa_aux_vdev_t *sav)
2065 {
2066 	for (int i = 0; i < sav->sav_count; i++)
2067 		spa_check_removed(sav->sav_vdevs[i]);
2068 }
2069 
2070 void
spa_claim_notify(zio_t * zio)2071 spa_claim_notify(zio_t *zio)
2072 {
2073 	spa_t *spa = zio->io_spa;
2074 
2075 	if (zio->io_error)
2076 		return;
2077 
2078 	mutex_enter(&spa->spa_props_lock);	/* any mutex will do */
2079 	if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
2080 		spa->spa_claim_max_txg = zio->io_bp->blk_birth;
2081 	mutex_exit(&spa->spa_props_lock);
2082 }
2083 
2084 typedef struct spa_load_error {
2085 	uint64_t	sle_meta_count;
2086 	uint64_t	sle_data_count;
2087 } spa_load_error_t;
2088 
2089 static void
spa_load_verify_done(zio_t * zio)2090 spa_load_verify_done(zio_t *zio)
2091 {
2092 	blkptr_t *bp = zio->io_bp;
2093 	spa_load_error_t *sle = zio->io_private;
2094 	dmu_object_type_t type = BP_GET_TYPE(bp);
2095 	int error = zio->io_error;
2096 	spa_t *spa = zio->io_spa;
2097 
2098 	abd_free(zio->io_abd);
2099 	if (error) {
2100 		if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
2101 		    type != DMU_OT_INTENT_LOG)
2102 			atomic_inc_64(&sle->sle_meta_count);
2103 		else
2104 			atomic_inc_64(&sle->sle_data_count);
2105 	}
2106 
2107 	mutex_enter(&spa->spa_scrub_lock);
2108 	spa->spa_load_verify_ios--;
2109 	cv_broadcast(&spa->spa_scrub_io_cv);
2110 	mutex_exit(&spa->spa_scrub_lock);
2111 }
2112 
2113 /*
2114  * Maximum number of concurrent scrub i/os to create while verifying
2115  * a pool while importing it.
2116  */
2117 int spa_load_verify_maxinflight = 10000;
2118 boolean_t spa_load_verify_metadata = B_TRUE;
2119 boolean_t spa_load_verify_data = B_TRUE;
2120 
2121 /*ARGSUSED*/
2122 static int
spa_load_verify_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)2123 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2124     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2125 {
2126 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2127 		return (0);
2128 	/*
2129 	 * Note: normally this routine will not be called if
2130 	 * spa_load_verify_metadata is not set.  However, it may be useful
2131 	 * to manually set the flag after the traversal has begun.
2132 	 */
2133 	if (!spa_load_verify_metadata)
2134 		return (0);
2135 	if (!BP_IS_METADATA(bp) && !spa_load_verify_data)
2136 		return (0);
2137 
2138 	zio_t *rio = arg;
2139 	size_t size = BP_GET_PSIZE(bp);
2140 
2141 	mutex_enter(&spa->spa_scrub_lock);
2142 	while (spa->spa_load_verify_ios >= spa_load_verify_maxinflight)
2143 		cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2144 	spa->spa_load_verify_ios++;
2145 	mutex_exit(&spa->spa_scrub_lock);
2146 
2147 	zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size,
2148 	    spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
2149 	    ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
2150 	    ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
2151 	return (0);
2152 }
2153 
2154 /* ARGSUSED */
2155 int
verify_dataset_name_len(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)2156 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2157 {
2158 	if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
2159 		return (SET_ERROR(ENAMETOOLONG));
2160 
2161 	return (0);
2162 }
2163 
2164 static int
spa_load_verify(spa_t * spa)2165 spa_load_verify(spa_t *spa)
2166 {
2167 	zio_t *rio;
2168 	spa_load_error_t sle = { 0 };
2169 	zpool_load_policy_t policy;
2170 	boolean_t verify_ok = B_FALSE;
2171 	int error = 0;
2172 
2173 	zpool_get_load_policy(spa->spa_config, &policy);
2174 
2175 	if (policy.zlp_rewind & ZPOOL_NEVER_REWIND)
2176 		return (0);
2177 
2178 	dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2179 	error = dmu_objset_find_dp(spa->spa_dsl_pool,
2180 	    spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2181 	    DS_FIND_CHILDREN);
2182 	dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2183 	if (error != 0)
2184 		return (error);
2185 
2186 	rio = zio_root(spa, NULL, &sle,
2187 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
2188 
2189 	if (spa_load_verify_metadata) {
2190 		if (spa->spa_extreme_rewind) {
2191 			spa_load_note(spa, "performing a complete scan of the "
2192 			    "pool since extreme rewind is on. This may take "
2193 			    "a very long time.\n  (spa_load_verify_data=%u, "
2194 			    "spa_load_verify_metadata=%u)",
2195 			    spa_load_verify_data, spa_load_verify_metadata);
2196 		}
2197 		error = traverse_pool(spa, spa->spa_verify_min_txg,
2198 		    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
2199 		    TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio);
2200 	}
2201 
2202 	(void) zio_wait(rio);
2203 
2204 	spa->spa_load_meta_errors = sle.sle_meta_count;
2205 	spa->spa_load_data_errors = sle.sle_data_count;
2206 
2207 	if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) {
2208 		spa_load_note(spa, "spa_load_verify found %llu metadata errors "
2209 		    "and %llu data errors", (u_longlong_t)sle.sle_meta_count,
2210 		    (u_longlong_t)sle.sle_data_count);
2211 	}
2212 
2213 	if (spa_load_verify_dryrun ||
2214 	    (!error && sle.sle_meta_count <= policy.zlp_maxmeta &&
2215 	    sle.sle_data_count <= policy.zlp_maxdata)) {
2216 		int64_t loss = 0;
2217 
2218 		verify_ok = B_TRUE;
2219 		spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2220 		spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2221 
2222 		loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2223 		VERIFY(nvlist_add_uint64(spa->spa_load_info,
2224 		    ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2225 		VERIFY(nvlist_add_int64(spa->spa_load_info,
2226 		    ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2227 		VERIFY(nvlist_add_uint64(spa->spa_load_info,
2228 		    ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2229 	} else {
2230 		spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2231 	}
2232 
2233 	if (spa_load_verify_dryrun)
2234 		return (0);
2235 
2236 	if (error) {
2237 		if (error != ENXIO && error != EIO)
2238 			error = SET_ERROR(EIO);
2239 		return (error);
2240 	}
2241 
2242 	return (verify_ok ? 0 : EIO);
2243 }
2244 
2245 /*
2246  * Find a value in the pool props object.
2247  */
2248 static void
spa_prop_find(spa_t * spa,zpool_prop_t prop,uint64_t * val)2249 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2250 {
2251 	(void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2252 	    zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2253 }
2254 
2255 /*
2256  * Find a value in the pool directory object.
2257  */
2258 static int
spa_dir_prop(spa_t * spa,const char * name,uint64_t * val,boolean_t log_enoent)2259 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent)
2260 {
2261 	int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2262 	    name, sizeof (uint64_t), 1, val);
2263 
2264 	if (error != 0 && (error != ENOENT || log_enoent)) {
2265 		spa_load_failed(spa, "couldn't get '%s' value in MOS directory "
2266 		    "[error=%d]", name, error);
2267 	}
2268 
2269 	return (error);
2270 }
2271 
2272 static int
spa_vdev_err(vdev_t * vdev,vdev_aux_t aux,int err)2273 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2274 {
2275 	vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2276 	return (SET_ERROR(err));
2277 }
2278 
2279 static void
spa_spawn_aux_threads(spa_t * spa)2280 spa_spawn_aux_threads(spa_t *spa)
2281 {
2282 	ASSERT(spa_writeable(spa));
2283 
2284 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2285 
2286 	spa_start_indirect_condensing_thread(spa);
2287 
2288 	ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL);
2289 	spa->spa_checkpoint_discard_zthr =
2290 	    zthr_create(spa_checkpoint_discard_thread_check,
2291 	    spa_checkpoint_discard_thread, spa);
2292 }
2293 
2294 /*
2295  * Fix up config after a partly-completed split.  This is done with the
2296  * ZPOOL_CONFIG_SPLIT nvlist.  Both the splitting pool and the split-off
2297  * pool have that entry in their config, but only the splitting one contains
2298  * a list of all the guids of the vdevs that are being split off.
2299  *
2300  * This function determines what to do with that list: either rejoin
2301  * all the disks to the pool, or complete the splitting process.  To attempt
2302  * the rejoin, each disk that is offlined is marked online again, and
2303  * we do a reopen() call.  If the vdev label for every disk that was
2304  * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2305  * then we call vdev_split() on each disk, and complete the split.
2306  *
2307  * Otherwise we leave the config alone, with all the vdevs in place in
2308  * the original pool.
2309  */
2310 static void
spa_try_repair(spa_t * spa,nvlist_t * config)2311 spa_try_repair(spa_t *spa, nvlist_t *config)
2312 {
2313 	uint_t extracted;
2314 	uint64_t *glist;
2315 	uint_t i, gcount;
2316 	nvlist_t *nvl;
2317 	vdev_t **vd;
2318 	boolean_t attempt_reopen;
2319 
2320 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2321 		return;
2322 
2323 	/* check that the config is complete */
2324 	if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2325 	    &glist, &gcount) != 0)
2326 		return;
2327 
2328 	vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2329 
2330 	/* attempt to online all the vdevs & validate */
2331 	attempt_reopen = B_TRUE;
2332 	for (i = 0; i < gcount; i++) {
2333 		if (glist[i] == 0)	/* vdev is hole */
2334 			continue;
2335 
2336 		vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2337 		if (vd[i] == NULL) {
2338 			/*
2339 			 * Don't bother attempting to reopen the disks;
2340 			 * just do the split.
2341 			 */
2342 			attempt_reopen = B_FALSE;
2343 		} else {
2344 			/* attempt to re-online it */
2345 			vd[i]->vdev_offline = B_FALSE;
2346 		}
2347 	}
2348 
2349 	if (attempt_reopen) {
2350 		vdev_reopen(spa->spa_root_vdev);
2351 
2352 		/* check each device to see what state it's in */
2353 		for (extracted = 0, i = 0; i < gcount; i++) {
2354 			if (vd[i] != NULL &&
2355 			    vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2356 				break;
2357 			++extracted;
2358 		}
2359 	}
2360 
2361 	/*
2362 	 * If every disk has been moved to the new pool, or if we never
2363 	 * even attempted to look at them, then we split them off for
2364 	 * good.
2365 	 */
2366 	if (!attempt_reopen || gcount == extracted) {
2367 		for (i = 0; i < gcount; i++)
2368 			if (vd[i] != NULL)
2369 				vdev_split(vd[i]);
2370 		vdev_reopen(spa->spa_root_vdev);
2371 	}
2372 
2373 	kmem_free(vd, gcount * sizeof (vdev_t *));
2374 }
2375 
2376 static int
spa_load(spa_t * spa,spa_load_state_t state,spa_import_type_t type)2377 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type)
2378 {
2379 	char *ereport = FM_EREPORT_ZFS_POOL;
2380 	int error;
2381 
2382 	spa->spa_load_state = state;
2383 	(void) spa_import_progress_set_state(spa, spa_load_state(spa));
2384 
2385 	gethrestime(&spa->spa_loaded_ts);
2386 	error = spa_load_impl(spa, type, &ereport);
2387 
2388 	/*
2389 	 * Don't count references from objsets that are already closed
2390 	 * and are making their way through the eviction process.
2391 	 */
2392 	spa_evicting_os_wait(spa);
2393 	spa->spa_minref = zfs_refcount_count(&spa->spa_refcount);
2394 	if (error) {
2395 		if (error != EEXIST) {
2396 			spa->spa_loaded_ts.tv_sec = 0;
2397 			spa->spa_loaded_ts.tv_nsec = 0;
2398 		}
2399 		if (error != EBADF) {
2400 			(void) zfs_ereport_post(ereport, spa,
2401 			    NULL, NULL, NULL, 0, 0);
2402 		}
2403 	}
2404 	spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2405 	spa->spa_ena = 0;
2406 
2407 	(void) spa_import_progress_set_state(spa, spa_load_state(spa));
2408 
2409 	return (error);
2410 }
2411 
2412 /*
2413  * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2414  * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2415  * spa's per-vdev ZAP list.
2416  */
2417 static uint64_t
vdev_count_verify_zaps(vdev_t * vd)2418 vdev_count_verify_zaps(vdev_t *vd)
2419 {
2420 	spa_t *spa = vd->vdev_spa;
2421 	uint64_t total = 0;
2422 	if (vd->vdev_top_zap != 0) {
2423 		total++;
2424 		ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2425 		    spa->spa_all_vdev_zaps, vd->vdev_top_zap));
2426 	}
2427 	if (vd->vdev_leaf_zap != 0) {
2428 		total++;
2429 		ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2430 		    spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
2431 	}
2432 
2433 	for (uint64_t i = 0; i < vd->vdev_children; i++) {
2434 		total += vdev_count_verify_zaps(vd->vdev_child[i]);
2435 	}
2436 
2437 	return (total);
2438 }
2439 
2440 /*
2441  * Determine whether the activity check is required.
2442  */
2443 static boolean_t
spa_activity_check_required(spa_t * spa,uberblock_t * ub,nvlist_t * label,nvlist_t * config)2444 spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label,
2445     nvlist_t *config)
2446 {
2447 	uint64_t state = 0;
2448 	uint64_t hostid = 0;
2449 	uint64_t tryconfig_txg = 0;
2450 	uint64_t tryconfig_timestamp = 0;
2451 	uint16_t tryconfig_mmp_seq = 0;
2452 	nvlist_t *nvinfo;
2453 
2454 	if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
2455 		nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2456 		(void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG,
2457 		    &tryconfig_txg);
2458 		(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
2459 		    &tryconfig_timestamp);
2460 		(void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ,
2461 		    &tryconfig_mmp_seq);
2462 	}
2463 
2464 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state);
2465 
2466 	/*
2467 	 * Disable the MMP activity check - This is used by zdb which
2468 	 * is intended to be used on potentially active pools.
2469 	 */
2470 	if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP)
2471 		return (B_FALSE);
2472 
2473 	/*
2474 	 * Skip the activity check when the MMP feature is disabled.
2475 	 */
2476 	if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0)
2477 		return (B_FALSE);
2478 
2479 	/*
2480 	 * If the tryconfig_ values are nonzero, they are the results of an
2481 	 * earlier tryimport.  If they all match the uberblock we just found,
2482 	 * then the pool has not changed and we return false so we do not test
2483 	 * a second time.
2484 	 */
2485 	if (tryconfig_txg && tryconfig_txg == ub->ub_txg &&
2486 	    tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp &&
2487 	    tryconfig_mmp_seq && tryconfig_mmp_seq ==
2488 	    (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0))
2489 		return (B_FALSE);
2490 
2491 	/*
2492 	 * Allow the activity check to be skipped when importing the pool
2493 	 * on the same host which last imported it.  Since the hostid from
2494 	 * configuration may be stale use the one read from the label.
2495 	 */
2496 	if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID))
2497 		hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID);
2498 
2499 	if (hostid == spa_get_hostid())
2500 		return (B_FALSE);
2501 
2502 	/*
2503 	 * Skip the activity test when the pool was cleanly exported.
2504 	 */
2505 	if (state != POOL_STATE_ACTIVE)
2506 		return (B_FALSE);
2507 
2508 	return (B_TRUE);
2509 }
2510 
2511 /*
2512  * Nanoseconds the activity check must watch for changes on-disk.
2513  */
2514 static uint64_t
spa_activity_check_duration(spa_t * spa,uberblock_t * ub)2515 spa_activity_check_duration(spa_t *spa, uberblock_t *ub)
2516 {
2517 	uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1);
2518 	uint64_t multihost_interval = MSEC2NSEC(
2519 	    MMP_INTERVAL_OK(zfs_multihost_interval));
2520 	uint64_t import_delay = MAX(NANOSEC, import_intervals *
2521 	    multihost_interval);
2522 
2523 	/*
2524 	 * Local tunables determine a minimum duration except for the case
2525 	 * where we know when the remote host will suspend the pool if MMP
2526 	 * writes do not land.
2527 	 *
2528 	 * See Big Theory comment at the top of mmp.c for the reasoning behind
2529 	 * these cases and times.
2530 	 */
2531 
2532 	ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100);
2533 
2534 	if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) &&
2535 	    MMP_FAIL_INT(ub) > 0) {
2536 
2537 		/* MMP on remote host will suspend pool after failed writes */
2538 		import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) *
2539 		    MMP_IMPORT_SAFETY_FACTOR / 100;
2540 
2541 		zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
2542 		    "mmp_fails=%llu ub_mmp mmp_interval=%llu "
2543 		    "import_intervals=%u", import_delay, MMP_FAIL_INT(ub),
2544 		    MMP_INTERVAL(ub), import_intervals);
2545 
2546 	} else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) &&
2547 	    MMP_FAIL_INT(ub) == 0) {
2548 
2549 		/* MMP on remote host will never suspend pool */
2550 		import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) +
2551 		    ub->ub_mmp_delay) * import_intervals);
2552 
2553 		zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
2554 		    "mmp_interval=%llu ub_mmp_delay=%llu "
2555 		    "import_intervals=%u", import_delay, MMP_INTERVAL(ub),
2556 		    ub->ub_mmp_delay, import_intervals);
2557 
2558 	} else if (MMP_VALID(ub)) {
2559 		/*
2560 		 * zfs-0.7 compatability case
2561 		 */
2562 
2563 		import_delay = MAX(import_delay, (multihost_interval +
2564 		    ub->ub_mmp_delay) * import_intervals);
2565 
2566 		zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
2567 		    "import_intervals=%u leaves=%u", import_delay,
2568 		    ub->ub_mmp_delay, import_intervals,
2569 		    vdev_count_leaves(spa));
2570 	} else {
2571 		/* Using local tunings is the only reasonable option */
2572 		zfs_dbgmsg("pool last imported on non-MMP aware "
2573 		    "host using import_delay=%llu multihost_interval=%llu "
2574 		    "import_intervals=%u", import_delay, multihost_interval,
2575 		    import_intervals);
2576 	}
2577 
2578 	return (import_delay);
2579 }
2580 
2581 /*
2582  * Perform the import activity check.  If the user canceled the import or
2583  * we detected activity then fail.
2584  */
2585 static int
spa_activity_check(spa_t * spa,uberblock_t * ub,nvlist_t * config)2586 spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config)
2587 {
2588 	uint64_t txg = ub->ub_txg;
2589 	uint64_t timestamp = ub->ub_timestamp;
2590 	uint64_t mmp_config = ub->ub_mmp_config;
2591 	uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0;
2592 	uint64_t import_delay;
2593 	hrtime_t import_expire;
2594 	nvlist_t *mmp_label = NULL;
2595 	vdev_t *rvd = spa->spa_root_vdev;
2596 	kcondvar_t cv;
2597 	kmutex_t mtx;
2598 	int error = 0;
2599 
2600 	cv_init(&cv, NULL, CV_DEFAULT, NULL);
2601 	mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL);
2602 	mutex_enter(&mtx);
2603 
2604 	/*
2605 	 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2606 	 * during the earlier tryimport.  If the txg recorded there is 0 then
2607 	 * the pool is known to be active on another host.
2608 	 *
2609 	 * Otherwise, the pool might be in use on another host.  Check for
2610 	 * changes in the uberblocks on disk if necessary.
2611 	 */
2612 	if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
2613 		nvlist_t *nvinfo = fnvlist_lookup_nvlist(config,
2614 		    ZPOOL_CONFIG_LOAD_INFO);
2615 
2616 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) &&
2617 		    fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) {
2618 			vdev_uberblock_load(rvd, ub, &mmp_label);
2619 			error = SET_ERROR(EREMOTEIO);
2620 			goto out;
2621 		}
2622 	}
2623 
2624 	import_delay = spa_activity_check_duration(spa, ub);
2625 
2626 	/* Add a small random factor in case of simultaneous imports (0-25%) */
2627 	import_delay += import_delay * spa_get_random(250) / 1000;
2628 
2629 	import_expire = gethrtime() + import_delay;
2630 
2631 	while (gethrtime() < import_expire) {
2632 		(void) spa_import_progress_set_mmp_check(spa,
2633 		    NSEC2SEC(import_expire - gethrtime()));
2634 
2635 		vdev_uberblock_load(rvd, ub, &mmp_label);
2636 
2637 		if (txg != ub->ub_txg || timestamp != ub->ub_timestamp ||
2638 		    mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) {
2639 			zfs_dbgmsg("multihost activity detected "
2640 			    "txg %llu ub_txg  %llu "
2641 			    "timestamp %llu ub_timestamp  %llu "
2642 			    "mmp_config %#llx ub_mmp_config %#llx",
2643 			    txg, ub->ub_txg, timestamp, ub->ub_timestamp,
2644 			    mmp_config, ub->ub_mmp_config);
2645 
2646 			error = SET_ERROR(EREMOTEIO);
2647 			break;
2648 		}
2649 
2650 		if (mmp_label) {
2651 			nvlist_free(mmp_label);
2652 			mmp_label = NULL;
2653 		}
2654 
2655 		error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz);
2656 		if (error != -1) {
2657 			error = SET_ERROR(EINTR);
2658 			break;
2659 		}
2660 		error = 0;
2661 	}
2662 
2663 out:
2664 	mutex_exit(&mtx);
2665 	mutex_destroy(&mtx);
2666 	cv_destroy(&cv);
2667 
2668 	/*
2669 	 * If the pool is determined to be active store the status in the
2670 	 * spa->spa_load_info nvlist.  If the remote hostname or hostid are
2671 	 * available from configuration read from disk store them as well.
2672 	 * This allows 'zpool import' to generate a more useful message.
2673 	 *
2674 	 * ZPOOL_CONFIG_MMP_STATE    - observed pool status (mandatory)
2675 	 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2676 	 * ZPOOL_CONFIG_MMP_HOSTID   - hostid from the active pool
2677 	 */
2678 	if (error == EREMOTEIO) {
2679 		char *hostname = "<unknown>";
2680 		uint64_t hostid = 0;
2681 
2682 		if (mmp_label) {
2683 			if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) {
2684 				hostname = fnvlist_lookup_string(mmp_label,
2685 				    ZPOOL_CONFIG_HOSTNAME);
2686 				fnvlist_add_string(spa->spa_load_info,
2687 				    ZPOOL_CONFIG_MMP_HOSTNAME, hostname);
2688 			}
2689 
2690 			if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) {
2691 				hostid = fnvlist_lookup_uint64(mmp_label,
2692 				    ZPOOL_CONFIG_HOSTID);
2693 				fnvlist_add_uint64(spa->spa_load_info,
2694 				    ZPOOL_CONFIG_MMP_HOSTID, hostid);
2695 			}
2696 		}
2697 
2698 		fnvlist_add_uint64(spa->spa_load_info,
2699 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE);
2700 		fnvlist_add_uint64(spa->spa_load_info,
2701 		    ZPOOL_CONFIG_MMP_TXG, 0);
2702 
2703 		error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO);
2704 	}
2705 
2706 	if (mmp_label)
2707 		nvlist_free(mmp_label);
2708 
2709 	return (error);
2710 }
2711 
2712 static int
spa_verify_host(spa_t * spa,nvlist_t * mos_config)2713 spa_verify_host(spa_t *spa, nvlist_t *mos_config)
2714 {
2715 	uint64_t hostid;
2716 	char *hostname;
2717 	uint64_t myhostid = 0;
2718 
2719 	if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config,
2720 	    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2721 		hostname = fnvlist_lookup_string(mos_config,
2722 		    ZPOOL_CONFIG_HOSTNAME);
2723 
2724 		myhostid = zone_get_hostid(NULL);
2725 
2726 		if (hostid != 0 && myhostid != 0 && hostid != myhostid) {
2727 			cmn_err(CE_WARN, "pool '%s' could not be "
2728 			    "loaded as it was last accessed by "
2729 			    "another system (host: %s hostid: 0x%llx). "
2730 			    "See: http://illumos.org/msg/ZFS-8000-EY",
2731 			    spa_name(spa), hostname, (u_longlong_t)hostid);
2732 			spa_load_failed(spa, "hostid verification failed: pool "
2733 			    "last accessed by host: %s (hostid: 0x%llx)",
2734 			    hostname, (u_longlong_t)hostid);
2735 			return (SET_ERROR(EBADF));
2736 		}
2737 	}
2738 
2739 	return (0);
2740 }
2741 
2742 static int
spa_ld_parse_config(spa_t * spa,spa_import_type_t type)2743 spa_ld_parse_config(spa_t *spa, spa_import_type_t type)
2744 {
2745 	int error = 0;
2746 	nvlist_t *nvtree, *nvl, *config = spa->spa_config;
2747 	int parse;
2748 	vdev_t *rvd;
2749 	uint64_t pool_guid;
2750 	char *comment;
2751 
2752 	/*
2753 	 * Versioning wasn't explicitly added to the label until later, so if
2754 	 * it's not present treat it as the initial version.
2755 	 */
2756 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2757 	    &spa->spa_ubsync.ub_version) != 0)
2758 		spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2759 
2760 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
2761 		spa_load_failed(spa, "invalid config provided: '%s' missing",
2762 		    ZPOOL_CONFIG_POOL_GUID);
2763 		return (SET_ERROR(EINVAL));
2764 	}
2765 
2766 	/*
2767 	 * If we are doing an import, ensure that the pool is not already
2768 	 * imported by checking if its pool guid already exists in the
2769 	 * spa namespace.
2770 	 *
2771 	 * The only case that we allow an already imported pool to be
2772 	 * imported again, is when the pool is checkpointed and we want to
2773 	 * look at its checkpointed state from userland tools like zdb.
2774 	 */
2775 #ifdef _KERNEL
2776 	if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
2777 	    spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
2778 	    spa_guid_exists(pool_guid, 0)) {
2779 #else
2780 	if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
2781 	    spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
2782 	    spa_guid_exists(pool_guid, 0) &&
2783 	    !spa_importing_readonly_checkpoint(spa)) {
2784 #endif
2785 		spa_load_failed(spa, "a pool with guid %llu is already open",
2786 		    (u_longlong_t)pool_guid);
2787 		return (SET_ERROR(EEXIST));
2788 	}
2789 
2790 	spa->spa_config_guid = pool_guid;
2791 
2792 	nvlist_free(spa->spa_load_info);
2793 	spa->spa_load_info = fnvlist_alloc();
2794 
2795 	ASSERT(spa->spa_comment == NULL);
2796 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2797 		spa->spa_comment = spa_strdup(comment);
2798 
2799 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2800 	    &spa->spa_config_txg);
2801 
2802 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0)
2803 		spa->spa_config_splitting = fnvlist_dup(nvl);
2804 
2805 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) {
2806 		spa_load_failed(spa, "invalid config provided: '%s' missing",
2807 		    ZPOOL_CONFIG_VDEV_TREE);
2808 		return (SET_ERROR(EINVAL));
2809 	}
2810 
2811 	/*
2812 	 * Create "The Godfather" zio to hold all async IOs
2813 	 */
2814 	spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2815 	    KM_SLEEP);
2816 	for (int i = 0; i < max_ncpus; i++) {
2817 		spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2818 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2819 		    ZIO_FLAG_GODFATHER);
2820 	}
2821 
2822 	/*
2823 	 * Parse the configuration into a vdev tree.  We explicitly set the
2824 	 * value that will be returned by spa_version() since parsing the
2825 	 * configuration requires knowing the version number.
2826 	 */
2827 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2828 	parse = (type == SPA_IMPORT_EXISTING ?
2829 	    VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2830 	error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse);
2831 	spa_config_exit(spa, SCL_ALL, FTAG);
2832 
2833 	if (error != 0) {
2834 		spa_load_failed(spa, "unable to parse config [error=%d]",
2835 		    error);
2836 		return (error);
2837 	}
2838 
2839 	ASSERT(spa->spa_root_vdev == rvd);
2840 	ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2841 	ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2842 
2843 	if (type != SPA_IMPORT_ASSEMBLE) {
2844 		ASSERT(spa_guid(spa) == pool_guid);
2845 	}
2846 
2847 	return (0);
2848 }
2849 
2850 /*
2851  * Recursively open all vdevs in the vdev tree. This function is called twice:
2852  * first with the untrusted config, then with the trusted config.
2853  */
2854 static int
2855 spa_ld_open_vdevs(spa_t *spa)
2856 {
2857 	int error = 0;
2858 
2859 	/*
2860 	 * spa_missing_tvds_allowed defines how many top-level vdevs can be
2861 	 * missing/unopenable for the root vdev to be still considered openable.
2862 	 */
2863 	if (spa->spa_trust_config) {
2864 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds;
2865 	} else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) {
2866 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile;
2867 	} else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) {
2868 		spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan;
2869 	} else {
2870 		spa->spa_missing_tvds_allowed = 0;
2871 	}
2872 
2873 	spa->spa_missing_tvds_allowed =
2874 	    MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed);
2875 
2876 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2877 	error = vdev_open(spa->spa_root_vdev);
2878 	spa_config_exit(spa, SCL_ALL, FTAG);
2879 
2880 	if (spa->spa_missing_tvds != 0) {
2881 		spa_load_note(spa, "vdev tree has %lld missing top-level "
2882 		    "vdevs.", (u_longlong_t)spa->spa_missing_tvds);
2883 		if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) {
2884 			/*
2885 			 * Although theoretically we could allow users to open
2886 			 * incomplete pools in RW mode, we'd need to add a lot
2887 			 * of extra logic (e.g. adjust pool space to account
2888 			 * for missing vdevs).
2889 			 * This limitation also prevents users from accidentally
2890 			 * opening the pool in RW mode during data recovery and
2891 			 * damaging it further.
2892 			 */
2893 			spa_load_note(spa, "pools with missing top-level "
2894 			    "vdevs can only be opened in read-only mode.");
2895 			error = SET_ERROR(ENXIO);
2896 		} else {
2897 			spa_load_note(spa, "current settings allow for maximum "
2898 			    "%lld missing top-level vdevs at this stage.",
2899 			    (u_longlong_t)spa->spa_missing_tvds_allowed);
2900 		}
2901 	}
2902 	if (error != 0) {
2903 		spa_load_failed(spa, "unable to open vdev tree [error=%d]",
2904 		    error);
2905 	}
2906 	if (spa->spa_missing_tvds != 0 || error != 0)
2907 		vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2);
2908 
2909 	return (error);
2910 }
2911 
2912 /*
2913  * We need to validate the vdev labels against the configuration that
2914  * we have in hand. This function is called twice: first with an untrusted
2915  * config, then with a trusted config. The validation is more strict when the
2916  * config is trusted.
2917  */
2918 static int
2919 spa_ld_validate_vdevs(spa_t *spa)
2920 {
2921 	int error = 0;
2922 	vdev_t *rvd = spa->spa_root_vdev;
2923 
2924 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2925 	error = vdev_validate(rvd);
2926 	spa_config_exit(spa, SCL_ALL, FTAG);
2927 
2928 	if (error != 0) {
2929 		spa_load_failed(spa, "vdev_validate failed [error=%d]", error);
2930 		return (error);
2931 	}
2932 
2933 	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
2934 		spa_load_failed(spa, "cannot open vdev tree after invalidating "
2935 		    "some vdevs");
2936 		vdev_dbgmsg_print_tree(rvd, 2);
2937 		return (SET_ERROR(ENXIO));
2938 	}
2939 
2940 	return (0);
2941 }
2942 
2943 static void
2944 spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub)
2945 {
2946 	spa->spa_state = POOL_STATE_ACTIVE;
2947 	spa->spa_ubsync = spa->spa_uberblock;
2948 	spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2949 	    TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2950 	spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2951 	    spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2952 	spa->spa_claim_max_txg = spa->spa_first_txg;
2953 	spa->spa_prev_software_version = ub->ub_software_version;
2954 }
2955 
2956 static int
2957 spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type)
2958 {
2959 	vdev_t *rvd = spa->spa_root_vdev;
2960 	nvlist_t *label;
2961 	uberblock_t *ub = &spa->spa_uberblock;
2962 	boolean_t activity_check = B_FALSE;
2963 
2964 	/*
2965 	 * If we are opening the checkpointed state of the pool by
2966 	 * rewinding to it, at this point we will have written the
2967 	 * checkpointed uberblock to the vdev labels, so searching
2968 	 * the labels will find the right uberblock.  However, if
2969 	 * we are opening the checkpointed state read-only, we have
2970 	 * not modified the labels. Therefore, we must ignore the
2971 	 * labels and continue using the spa_uberblock that was set
2972 	 * by spa_ld_checkpoint_rewind.
2973 	 *
2974 	 * Note that it would be fine to ignore the labels when
2975 	 * rewinding (opening writeable) as well. However, if we
2976 	 * crash just after writing the labels, we will end up
2977 	 * searching the labels. Doing so in the common case means
2978 	 * that this code path gets exercised normally, rather than
2979 	 * just in the edge case.
2980 	 */
2981 	if (ub->ub_checkpoint_txg != 0 &&
2982 	    spa_importing_readonly_checkpoint(spa)) {
2983 		spa_ld_select_uberblock_done(spa, ub);
2984 		return (0);
2985 	}
2986 
2987 	/*
2988 	 * Find the best uberblock.
2989 	 */
2990 	vdev_uberblock_load(rvd, ub, &label);
2991 
2992 	/*
2993 	 * If we weren't able to find a single valid uberblock, return failure.
2994 	 */
2995 	if (ub->ub_txg == 0) {
2996 		nvlist_free(label);
2997 		spa_load_failed(spa, "no valid uberblock found");
2998 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2999 	}
3000 
3001 	if (spa->spa_load_max_txg != UINT64_MAX) {
3002 		(void) spa_import_progress_set_max_txg(spa,
3003 		    (u_longlong_t)spa->spa_load_max_txg);
3004 	}
3005 	spa_load_note(spa, "using uberblock with txg=%llu",
3006 	    (u_longlong_t)ub->ub_txg);
3007 
3008 	/*
3009 	 * For pools which have the multihost property on determine if the
3010 	 * pool is truly inactive and can be safely imported.  Prevent
3011 	 * hosts which don't have a hostid set from importing the pool.
3012 	 */
3013 	activity_check = spa_activity_check_required(spa, ub, label,
3014 	    spa->spa_config);
3015 	if (activity_check) {
3016 		if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay &&
3017 		    spa_get_hostid() == 0) {
3018 			nvlist_free(label);
3019 			fnvlist_add_uint64(spa->spa_load_info,
3020 			    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
3021 			return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
3022 		}
3023 
3024 		int error = spa_activity_check(spa, ub, spa->spa_config);
3025 		if (error) {
3026 			nvlist_free(label);
3027 			return (error);
3028 		}
3029 
3030 		fnvlist_add_uint64(spa->spa_load_info,
3031 		    ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE);
3032 		fnvlist_add_uint64(spa->spa_load_info,
3033 		    ZPOOL_CONFIG_MMP_TXG, ub->ub_txg);
3034 		fnvlist_add_uint16(spa->spa_load_info,
3035 		    ZPOOL_CONFIG_MMP_SEQ,
3036 		    (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0));
3037 	}
3038 
3039 	/*
3040 	 * If the pool has an unsupported version we can't open it.
3041 	 */
3042 	if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
3043 		nvlist_free(label);
3044 		spa_load_failed(spa, "version %llu is not supported",
3045 		    (u_longlong_t)ub->ub_version);
3046 		return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
3047 	}
3048 
3049 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
3050 		nvlist_t *features;
3051 
3052 		/*
3053 		 * If we weren't able to find what's necessary for reading the
3054 		 * MOS in the label, return failure.
3055 		 */
3056 		if (label == NULL) {
3057 			spa_load_failed(spa, "label config unavailable");
3058 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
3059 			    ENXIO));
3060 		}
3061 
3062 		if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ,
3063 		    &features) != 0) {
3064 			nvlist_free(label);
3065 			spa_load_failed(spa, "invalid label: '%s' missing",
3066 			    ZPOOL_CONFIG_FEATURES_FOR_READ);
3067 			return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
3068 			    ENXIO));
3069 		}
3070 
3071 		/*
3072 		 * Update our in-core representation with the definitive values
3073 		 * from the label.
3074 		 */
3075 		nvlist_free(spa->spa_label_features);
3076 		VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
3077 	}
3078 
3079 	nvlist_free(label);
3080 
3081 	/*
3082 	 * Look through entries in the label nvlist's features_for_read. If
3083 	 * there is a feature listed there which we don't understand then we
3084 	 * cannot open a pool.
3085 	 */
3086 	if (ub->ub_version >= SPA_VERSION_FEATURES) {
3087 		nvlist_t *unsup_feat;
3088 
3089 		VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
3090 		    0);
3091 
3092 		for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
3093 		    NULL); nvp != NULL;
3094 		    nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
3095 			if (!zfeature_is_supported(nvpair_name(nvp))) {
3096 				VERIFY(nvlist_add_string(unsup_feat,
3097 				    nvpair_name(nvp), "") == 0);
3098 			}
3099 		}
3100 
3101 		if (!nvlist_empty(unsup_feat)) {
3102 			VERIFY(nvlist_add_nvlist(spa->spa_load_info,
3103 			    ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
3104 			nvlist_free(unsup_feat);
3105 			spa_load_failed(spa, "some features are unsupported");
3106 			return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
3107 			    ENOTSUP));
3108 		}
3109 
3110 		nvlist_free(unsup_feat);
3111 	}
3112 
3113 	if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
3114 		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3115 		spa_try_repair(spa, spa->spa_config);
3116 		spa_config_exit(spa, SCL_ALL, FTAG);
3117 		nvlist_free(spa->spa_config_splitting);
3118 		spa->spa_config_splitting = NULL;
3119 	}
3120 
3121 	/*
3122 	 * Initialize internal SPA structures.
3123 	 */
3124 	spa_ld_select_uberblock_done(spa, ub);
3125 
3126 	return (0);
3127 }
3128 
3129 static int
3130 spa_ld_open_rootbp(spa_t *spa)
3131 {
3132 	int error = 0;
3133 	vdev_t *rvd = spa->spa_root_vdev;
3134 
3135 	error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
3136 	if (error != 0) {
3137 		spa_load_failed(spa, "unable to open rootbp in dsl_pool_init "
3138 		    "[error=%d]", error);
3139 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3140 	}
3141 	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
3142 
3143 	return (0);
3144 }
3145 
3146 static int
3147 spa_ld_trusted_config(spa_t *spa, spa_import_type_t type,
3148     boolean_t reloading)
3149 {
3150 	vdev_t *mrvd, *rvd = spa->spa_root_vdev;
3151 	nvlist_t *nv, *mos_config, *policy;
3152 	int error = 0, copy_error;
3153 	uint64_t healthy_tvds, healthy_tvds_mos;
3154 	uint64_t mos_config_txg;
3155 
3156 	if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE)
3157 	    != 0)
3158 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3159 
3160 	/*
3161 	 * If we're assembling a pool from a split, the config provided is
3162 	 * already trusted so there is nothing to do.
3163 	 */
3164 	if (type == SPA_IMPORT_ASSEMBLE)
3165 		return (0);
3166 
3167 	healthy_tvds = spa_healthy_core_tvds(spa);
3168 
3169 	if (load_nvlist(spa, spa->spa_config_object, &mos_config)
3170 	    != 0) {
3171 		spa_load_failed(spa, "unable to retrieve MOS config");
3172 		return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3173 	}
3174 
3175 	/*
3176 	 * If we are doing an open, pool owner wasn't verified yet, thus do
3177 	 * the verification here.
3178 	 */
3179 	if (spa->spa_load_state == SPA_LOAD_OPEN) {
3180 		error = spa_verify_host(spa, mos_config);
3181 		if (error != 0) {
3182 			nvlist_free(mos_config);
3183 			return (error);
3184 		}
3185 	}
3186 
3187 	nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE);
3188 
3189 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3190 
3191 	/*
3192 	 * Build a new vdev tree from the trusted config
3193 	 */
3194 	VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
3195 
3196 	/*
3197 	 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3198 	 * obtained by scanning /dev/dsk, then it will have the right vdev
3199 	 * paths. We update the trusted MOS config with this information.
3200 	 * We first try to copy the paths with vdev_copy_path_strict, which
3201 	 * succeeds only when both configs have exactly the same vdev tree.
3202 	 * If that fails, we fall back to a more flexible method that has a
3203 	 * best effort policy.
3204 	 */
3205 	copy_error = vdev_copy_path_strict(rvd, mrvd);
3206 	if (copy_error != 0 || spa_load_print_vdev_tree) {
3207 		spa_load_note(spa, "provided vdev tree:");
3208 		vdev_dbgmsg_print_tree(rvd, 2);
3209 		spa_load_note(spa, "MOS vdev tree:");
3210 		vdev_dbgmsg_print_tree(mrvd, 2);
3211 	}
3212 	if (copy_error != 0) {
3213 		spa_load_note(spa, "vdev_copy_path_strict failed, falling "
3214 		    "back to vdev_copy_path_relaxed");
3215 		vdev_copy_path_relaxed(rvd, mrvd);
3216 	}
3217 
3218 	vdev_close(rvd);
3219 	vdev_free(rvd);
3220 	spa->spa_root_vdev = mrvd;
3221 	rvd = mrvd;
3222 	spa_config_exit(spa, SCL_ALL, FTAG);
3223 
3224 	/*
3225 	 * We will use spa_config if we decide to reload the spa or if spa_load
3226 	 * fails and we rewind. We must thus regenerate the config using the
3227 	 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3228 	 * pass settings on how to load the pool and is not stored in the MOS.
3229 	 * We copy it over to our new, trusted config.
3230 	 */
3231 	mos_config_txg = fnvlist_lookup_uint64(mos_config,
3232 	    ZPOOL_CONFIG_POOL_TXG);
3233 	nvlist_free(mos_config);
3234 	mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE);
3235 	if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY,
3236 	    &policy) == 0)
3237 		fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy);
3238 	spa_config_set(spa, mos_config);
3239 	spa->spa_config_source = SPA_CONFIG_SRC_MOS;
3240 
3241 	/*
3242 	 * Now that we got the config from the MOS, we should be more strict
3243