spa.c revision 8ad4d6dd86f5bc65fb3afa566c8133f3bac21648
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
2199653d4eschrock
22fa9e406ahrens/*
23b01c3b5eschrock * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24fa9e406ahrens * Use is subject to license terms.
25fa9e406ahrens */
26fa9e406ahrens
27fa9e406ahrens/*
28fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
29fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
30fa9e406ahrens * pool.
31fa9e406ahrens */
32fa9e406ahrens
33fa9e406ahrens#include <sys/zfs_context.h>
34ea8dc4beschrock#include <sys/fm/fs/zfs.h>
35fa9e406ahrens#include <sys/spa_impl.h>
36fa9e406ahrens#include <sys/zio.h>
37fa9e406ahrens#include <sys/zio_checksum.h>
38fa9e406ahrens#include <sys/zio_compress.h>
39fa9e406ahrens#include <sys/dmu.h>
40fa9e406ahrens#include <sys/dmu_tx.h>
41fa9e406ahrens#include <sys/zap.h>
42fa9e406ahrens#include <sys/zil.h>
43fa9e406ahrens#include <sys/vdev_impl.h>
44fa9e406ahrens#include <sys/metaslab.h>
45fa9e406ahrens#include <sys/uberblock_impl.h>
46fa9e406ahrens#include <sys/txg.h>
47fa9e406ahrens#include <sys/avl.h>
48fa9e406ahrens#include <sys/dmu_traverse.h>
49b1b8ab3lling#include <sys/dmu_objset.h>
50fa9e406ahrens#include <sys/unique.h>
51fa9e406ahrens#include <sys/dsl_pool.h>
52b1b8ab3lling#include <sys/dsl_dataset.h>
53fa9e406ahrens#include <sys/dsl_dir.h>
54fa9e406ahrens#include <sys/dsl_prop.h>
55b1b8ab3lling#include <sys/dsl_synctask.h>
56fa9e406ahrens#include <sys/fs/zfs.h>
57fa94a07brendan#include <sys/arc.h>
58fa9e406ahrens#include <sys/callb.h>
599517395ek#include <sys/systeminfo.h>
609517395ek#include <sys/sunddi.h>
61e7cbe64gw#include <sys/spa_boot.h>
62fa9e406ahrens
63990b485lling#include "zfs_prop.h"
64b7b9745perrin#include "zfs_comutil.h"
65990b485lling
66e14bb32Jeff Bonwickint zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
67e14bb32Jeff Bonwick	/*	ISSUE	INTR					*/
68e14bb32Jeff Bonwick	{	1,	1	},	/* ZIO_TYPE_NULL	*/
69e14bb32Jeff Bonwick	{	1,	8	},	/* ZIO_TYPE_READ	*/
70e14bb32Jeff Bonwick	{	8,	1	},	/* ZIO_TYPE_WRITE	*/
71e14bb32Jeff Bonwick	{	1,	1	},	/* ZIO_TYPE_FREE	*/
72e14bb32Jeff Bonwick	{	1,	1	},	/* ZIO_TYPE_CLAIM	*/
73e14bb32Jeff Bonwick	{	1,	1	},	/* ZIO_TYPE_IOCTL	*/
74e14bb32Jeff Bonwick};
75416e0cdek
76990b485llingstatic void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
7789a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
78990b485lling
79990b485lling/*
80990b485lling * ==========================================================================
81990b485lling * SPA properties routines
82990b485lling * ==========================================================================
83990b485lling */
84990b485lling
85990b485lling/*
86990b485lling * Add a (source=src, propname=propval) list to an nvlist.
87990b485lling */
889d82f4fllingstatic void
89990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
90990b485lling    uint64_t intval, zprop_source_t src)
91990b485lling{
92990b485lling	const char *propname = zpool_prop_to_name(prop);
93990b485lling	nvlist_t *propval;
94990b485lling
959d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
969d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
97990b485lling
989d82f4flling	if (strval != NULL)
999d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1009d82f4flling	else
1019d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
102990b485lling
1039d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
104990b485lling	nvlist_free(propval);
105990b485lling}
106990b485lling
107990b485lling/*
108990b485lling * Get property values from the spa configuration.
109990b485lling */
1109d82f4fllingstatic void
111990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
112990b485lling{
113990b485lling	uint64_t size = spa_get_space(spa);
114990b485lling	uint64_t used = spa_get_alloc(spa);
115990b485lling	uint64_t cap, version;
116990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
117c5904d1eschrock	spa_config_dirent_t *dp;
118990b485lling
119e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
120e14bb32Jeff Bonwick
121990b485lling	/*
122990b485lling	 * readonly properties
123990b485lling	 */
124e14bb32Jeff Bonwick	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
1259d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1269d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1279d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
128990b485lling
129990b485lling	cap = (size == 0) ? 0 : (used * 100 / size);
1309d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
131990b485lling
1329d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1339d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1349d82f4flling	    spa->spa_root_vdev->vdev_state, src);
135990b485lling
136990b485lling	/*
137990b485lling	 * settable properties that are not stored in the pool property object.
138990b485lling	 */
139990b485lling	version = spa_version(spa);
140990b485lling	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
141990b485lling		src = ZPROP_SRC_DEFAULT;
142990b485lling	else
143990b485lling		src = ZPROP_SRC_LOCAL;
1449d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
145990b485lling
1469d82f4flling	if (spa->spa_root != NULL)
1479d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1489d82f4flling		    0, ZPROP_SRC_LOCAL);
149990b485lling
150c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
151c5904d1eschrock		if (dp->scd_path == NULL) {
1529d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
153c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
154c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1559d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
156c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1572f8aaabeschrock		}
1582f8aaabeschrock	}
159990b485lling}
160990b485lling
161990b485lling/*
162990b485lling * Get zpool property values.
163990b485lling */
164990b485llingint
165990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
166990b485lling{
167990b485lling	zap_cursor_t zc;
168990b485lling	zap_attribute_t za;
169990b485lling	objset_t *mos = spa->spa_meta_objset;
170990b485lling	int err;
171990b485lling
1729d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
173990b485lling
174e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
175e14bb32Jeff Bonwick
176990b485lling	/*
177990b485lling	 * Get properties from the spa config.
178990b485lling	 */
1799d82f4flling	spa_prop_get_config(spa, nvp);
180990b485lling
181990b485lling	/* If no pool property object, no more prop to get. */
182990b485lling	if (spa->spa_pool_props_object == 0) {
183990b485lling		mutex_exit(&spa->spa_props_lock);
184990b485lling		return (0);
185990b485lling	}
186990b485lling
187990b485lling	/*
188990b485lling	 * Get properties from the MOS pool property object.
189990b485lling	 */
190990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
191990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
192990b485lling	    zap_cursor_advance(&zc)) {
193990b485lling		uint64_t intval = 0;
194990b485lling		char *strval = NULL;
195990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
196990b485lling		zpool_prop_t prop;
197990b485lling
198990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
199990b485lling			continue;
200990b485lling
201990b485lling		switch (za.za_integer_length) {
202990b485lling		case 8:
203990b485lling			/* integer property */
204990b485lling			if (za.za_first_integer !=
205990b485lling			    zpool_prop_default_numeric(prop))
206990b485lling				src = ZPROP_SRC_LOCAL;
207990b485lling
208990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
209990b485lling				dsl_pool_t *dp;
210990b485lling				dsl_dataset_t *ds = NULL;
211990b485lling
212990b485lling				dp = spa_get_dsl(spa);
213990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
214745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
215745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
216990b485lling					rw_exit(&dp->dp_config_rwlock);
217990b485lling					break;
218990b485lling				}
219990b485lling
220990b485lling				strval = kmem_alloc(
221990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
222990b485lling				    KM_SLEEP);
223990b485lling				dsl_dataset_name(ds, strval);
224745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
225990b485lling				rw_exit(&dp->dp_config_rwlock);
226990b485lling			} else {
227990b485lling				strval = NULL;
228990b485lling				intval = za.za_first_integer;
229990b485lling			}
230990b485lling
2319d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
232990b485lling
233990b485lling			if (strval != NULL)
234990b485lling				kmem_free(strval,
235990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
236990b485lling
237990b485lling			break;
238990b485lling
239990b485lling		case 1:
240990b485lling			/* string property */
241990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
242990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
243990b485lling			    za.za_name, 1, za.za_num_integers, strval);
244990b485lling			if (err) {
245990b485lling				kmem_free(strval, za.za_num_integers);
246990b485lling				break;
247990b485lling			}
2489d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
249990b485lling			kmem_free(strval, za.za_num_integers);
250990b485lling			break;
251990b485lling
252990b485lling		default:
253990b485lling			break;
254990b485lling		}
255990b485lling	}
256990b485lling	zap_cursor_fini(&zc);
257990b485lling	mutex_exit(&spa->spa_props_lock);
258990b485llingout:
259990b485lling	if (err && err != ENOENT) {
260990b485lling		nvlist_free(*nvp);
2619d82f4flling		*nvp = NULL;
262990b485lling		return (err);
263990b485lling	}
264990b485lling
265990b485lling	return (0);
266990b485lling}
267990b485lling
268990b485lling/*
269990b485lling * Validate the given pool properties nvlist and modify the list
270990b485lling * for the property values to be set.
271990b485lling */
272990b485llingstatic int
273990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
274990b485lling{
275990b485lling	nvpair_t *elem;
276990b485lling	int error = 0, reset_bootfs = 0;
277990b485lling	uint64_t objnum;
278990b485lling
279990b485lling	elem = NULL;
280990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
281990b485lling		zpool_prop_t prop;
282990b485lling		char *propname, *strval;
283990b485lling		uint64_t intval;
284990b485lling		objset_t *os;
2852f8aaabeschrock		char *slash;
286990b485lling
287990b485lling		propname = nvpair_name(elem);
288990b485lling
289990b485lling		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
290990b485lling			return (EINVAL);
291990b485lling
292990b485lling		switch (prop) {
293990b485lling		case ZPOOL_PROP_VERSION:
294990b485lling			error = nvpair_value_uint64(elem, &intval);
295990b485lling			if (!error &&
296990b485lling			    (intval < spa_version(spa) || intval > SPA_VERSION))
297990b485lling				error = EINVAL;
298990b485lling			break;
299990b485lling
300990b485lling		case ZPOOL_PROP_DELEGATION:
301990b485lling		case ZPOOL_PROP_AUTOREPLACE:
302d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
303990b485lling			error = nvpair_value_uint64(elem, &intval);
304990b485lling			if (!error && intval > 1)
305990b485lling				error = EINVAL;
306990b485lling			break;
307990b485lling
308990b485lling		case ZPOOL_PROP_BOOTFS:
309990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
310990b485lling				error = ENOTSUP;
311990b485lling				break;
312990b485lling			}
313990b485lling
314990b485lling			/*
31515e6edfgw			 * Make sure the vdev config is bootable
316990b485lling			 */
31715e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
318990b485lling				error = ENOTSUP;
319990b485lling				break;
320990b485lling			}
321990b485lling
322990b485lling			reset_bootfs = 1;
323990b485lling
324990b485lling			error = nvpair_value_string(elem, &strval);
325990b485lling
326990b485lling			if (!error) {
32715e6edfgw				uint64_t compress;
32815e6edfgw
329990b485lling				if (strval == NULL || strval[0] == '\0') {
330990b485lling					objnum = zpool_prop_default_numeric(
331990b485lling					    ZPOOL_PROP_BOOTFS);
332990b485lling					break;
333990b485lling				}
334990b485lling
335990b485lling				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
336745cd3cmaybee				    DS_MODE_USER | DS_MODE_READONLY, &os))
337990b485lling					break;
33815e6edfgw
33915e6edfgw				/* We don't support gzip bootable datasets */
34015e6edfgw				if ((error = dsl_prop_get_integer(strval,
34115e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
34215e6edfgw				    &compress, NULL)) == 0 &&
34315e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
34415e6edfgw					error = ENOTSUP;
34515e6edfgw				} else {
34615e6edfgw					objnum = dmu_objset_id(os);
34715e6edfgw				}
348990b485lling				dmu_objset_close(os);
349990b485lling			}
350990b485lling			break;
351e14bb32Jeff Bonwick
3520a4e951gw		case ZPOOL_PROP_FAILUREMODE:
3530a4e951gw			error = nvpair_value_uint64(elem, &intval);
3540a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3550a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
3560a4e951gw				error = EINVAL;
3570a4e951gw
3580a4e951gw			/*
3590a4e951gw			 * This is a special case which only occurs when
3600a4e951gw			 * the pool has completely failed. This allows
3610a4e951gw			 * the user to change the in-core failmode property
3620a4e951gw			 * without syncing it out to disk (I/Os might
3630a4e951gw			 * currently be blocked). We do this by returning
3640a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
3650a4e951gw			 * into thinking we encountered a property validation
3660a4e951gw			 * error.
3670a4e951gw			 */
368e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
3690a4e951gw				spa->spa_failmode = intval;
3700a4e951gw				error = EIO;
3710a4e951gw			}
3720a4e951gw			break;
3732f8aaabeschrock
3742f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
3752f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
3762f8aaabeschrock				break;
3772f8aaabeschrock
3782f8aaabeschrock			if (strval[0] == '\0')
3792f8aaabeschrock				break;
3802f8aaabeschrock
3812f8aaabeschrock			if (strcmp(strval, "none") == 0)
3822f8aaabeschrock				break;
3832f8aaabeschrock
3842f8aaabeschrock			if (strval[0] != '/') {
3852f8aaabeschrock				error = EINVAL;
3862f8aaabeschrock				break;
3872f8aaabeschrock			}
3882f8aaabeschrock
3892f8aaabeschrock			slash = strrchr(strval, '/');
3902f8aaabeschrock			ASSERT(slash != NULL);
3912f8aaabeschrock
3922f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3932f8aaabeschrock			    strcmp(slash, "/..") == 0)
3942f8aaabeschrock				error = EINVAL;
3952f8aaabeschrock			break;
396990b485lling		}
397990b485lling
398990b485lling		if (error)
399990b485lling			break;
400990b485lling	}
401990b485lling
402990b485lling	if (!error && reset_bootfs) {
403990b485lling		error = nvlist_remove(props,
404990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
405990b485lling
406990b485lling		if (!error) {
407990b485lling			error = nvlist_add_uint64(props,
408990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
409990b485lling		}
410990b485lling	}
411990b485lling
412990b485lling	return (error);
413990b485lling}
414990b485lling
415990b485llingint
416990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
417990b485lling{
418990b485lling	int error;
419990b485lling
420990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
421990b485lling		return (error);
422990b485lling
423990b485lling	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
424990b485lling	    spa, nvp, 3));
425990b485lling}
426990b485lling
427990b485lling/*
428990b485lling * If the bootfs property value is dsobj, clear it.
429990b485lling */
430990b485llingvoid
431990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
432990b485lling{
433990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
434990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
435990b485lling		    spa->spa_pool_props_object,
436990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
437990b485lling		spa->spa_bootfs = 0;
438990b485lling	}
439990b485lling}
440990b485lling
441fa9e406ahrens/*
442fa9e406ahrens * ==========================================================================
443fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
444fa9e406ahrens * ==========================================================================
445fa9e406ahrens */
446fa9e406ahrens
447ea8dc4beschrockstatic int
448ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
449ea8dc4beschrock{
450ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
451ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
452ea8dc4beschrock	int ret;
453ea8dc4beschrock
454ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
455ea8dc4beschrock	    sizeof (zbookmark_t));
456ea8dc4beschrock
457ea8dc4beschrock	if (ret < 0)
458ea8dc4beschrock		return (-1);
459ea8dc4beschrock	else if (ret > 0)
460ea8dc4beschrock		return (1);
461ea8dc4beschrock	else
462ea8dc4beschrock		return (0);
463ea8dc4beschrock}
464ea8dc4beschrock
465ea8dc4beschrock/*
466ea8dc4beschrock * Utility function which retrieves copies of the current logs and
467ea8dc4beschrock * re-initializes them in the process.
468ea8dc4beschrock */
469ea8dc4beschrockvoid
470ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
471ea8dc4beschrock{
472ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
473ea8dc4beschrock
474ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
475ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
476ea8dc4beschrock
477ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
478ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
479ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
480ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
481ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
482ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
483ea8dc4beschrock}
484ea8dc4beschrock
485fa9e406ahrens/*
486fa9e406ahrens * Activate an uninitialized pool.
487fa9e406ahrens */
488fa9e406ahrensstatic void
4898ad4d6dJeff Bonwickspa_activate(spa_t *spa, int mode)
490fa9e406ahrens{
491fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
492fa9e406ahrens
493fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
4948ad4d6dJeff Bonwick	spa->spa_mode = mode;
495fa9e406ahrens
496fa9e406ahrens	spa->spa_normal_class = metaslab_class_create();
4978654d02perrin	spa->spa_log_class = metaslab_class_create();
498fa9e406ahrens
499e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
500e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
501e14bb32Jeff Bonwick			spa->spa_zio_taskq[t][q] = taskq_create("spa_zio",
502e14bb32Jeff Bonwick			    zio_taskq_threads[t][q], maxclsyspri, 50,
503e14bb32Jeff Bonwick			    INT_MAX, TASKQ_PREPOPULATE);
504e14bb32Jeff Bonwick		}
505fa9e406ahrens	}
506fa9e406ahrens
507e14bb32Jeff Bonwick	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
508e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_config_dirty_node));
509e14bb32Jeff Bonwick	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
510e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_state_dirty_node));
511fa9e406ahrens
512fa9e406ahrens	txg_list_create(&spa->spa_vdev_txg_list,
513fa9e406ahrens	    offsetof(struct vdev, vdev_txg_node));
514ea8dc4beschrock
515ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
516ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
517ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
518ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
519ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
520ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
521fa9e406ahrens}
522fa9e406ahrens
523fa9e406ahrens/*
524fa9e406ahrens * Opposite of spa_activate().
525fa9e406ahrens */
526fa9e406ahrensstatic void
527fa9e406ahrensspa_deactivate(spa_t *spa)
528fa9e406ahrens{
529fa9e406ahrens	ASSERT(spa->spa_sync_on == B_FALSE);
530fa9e406ahrens	ASSERT(spa->spa_dsl_pool == NULL);
531fa9e406ahrens	ASSERT(spa->spa_root_vdev == NULL);
532fa9e406ahrens
533fa9e406ahrens	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
534fa9e406ahrens
535fa9e406ahrens	txg_list_destroy(&spa->spa_vdev_txg_list);
536fa9e406ahrens
537e14bb32Jeff Bonwick	list_destroy(&spa->spa_config_dirty_list);
538e14bb32Jeff Bonwick	list_destroy(&spa->spa_state_dirty_list);
539fa9e406ahrens
540e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
541e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
542e14bb32Jeff Bonwick			taskq_destroy(spa->spa_zio_taskq[t][q]);
543e14bb32Jeff Bonwick			spa->spa_zio_taskq[t][q] = NULL;
544e14bb32Jeff Bonwick		}
545fa9e406ahrens	}
546fa9e406ahrens
547fa9e406ahrens	metaslab_class_destroy(spa->spa_normal_class);
548fa9e406ahrens	spa->spa_normal_class = NULL;
549fa9e406ahrens
5508654d02perrin	metaslab_class_destroy(spa->spa_log_class);
5518654d02perrin	spa->spa_log_class = NULL;
5528654d02perrin
553ea8dc4beschrock	/*
554ea8dc4beschrock	 * If this was part of an import or the open otherwise failed, we may
555ea8dc4beschrock	 * still have errors left in the queues.  Empty them just in case.
556ea8dc4beschrock	 */
557ea8dc4beschrock	spa_errlog_drain(spa);
558ea8dc4beschrock
559ea8dc4beschrock	avl_destroy(&spa->spa_errlist_scrub);
560ea8dc4beschrock	avl_destroy(&spa->spa_errlist_last);
561ea8dc4beschrock
562fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
563fa9e406ahrens}
564fa9e406ahrens
565fa9e406ahrens/*
566fa9e406ahrens * Verify a pool configuration, and construct the vdev tree appropriately.  This
567fa9e406ahrens * will create all the necessary vdevs in the appropriate layout, with each vdev
568fa9e406ahrens * in the CLOSED state.  This will prep the pool before open/creation/import.
569fa9e406ahrens * All vdev validation is done by the vdev_alloc() routine.
570fa9e406ahrens */
57199653d4eschrockstatic int
57299653d4eschrockspa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
57399653d4eschrock    uint_t id, int atype)
574fa9e406ahrens{
575fa9e406ahrens	nvlist_t **child;
576fa9e406ahrens	uint_t c, children;
57799653d4eschrock	int error;
578fa9e406ahrens
57999653d4eschrock	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
58099653d4eschrock		return (error);
581fa9e406ahrens
58299653d4eschrock	if ((*vdp)->vdev_ops->vdev_op_leaf)
58399653d4eschrock		return (0);
584fa9e406ahrens
585e14bb32Jeff Bonwick	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
586e14bb32Jeff Bonwick	    &child, &children);
587e14bb32Jeff Bonwick
588e14bb32Jeff Bonwick	if (error == ENOENT)
589e14bb32Jeff Bonwick		return (0);
590e14bb32Jeff Bonwick
591e14bb32Jeff Bonwick	if (error) {
59299653d4eschrock		vdev_free(*vdp);
59399653d4eschrock		*vdp = NULL;
59499653d4eschrock		return (EINVAL);
595fa9e406ahrens	}
596fa9e406ahrens
597fa9e406ahrens	for (c = 0; c < children; c++) {
59899653d4eschrock		vdev_t *vd;
59999653d4eschrock		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
60099653d4eschrock		    atype)) != 0) {
60199653d4eschrock			vdev_free(*vdp);
60299653d4eschrock			*vdp = NULL;
60399653d4eschrock			return (error);
604fa9e406ahrens		}
605fa9e406ahrens	}
606fa9e406ahrens
60799653d4eschrock	ASSERT(*vdp != NULL);
60899653d4eschrock
60999653d4eschrock	return (0);
610fa9e406ahrens}
611fa9e406ahrens
612fa9e406ahrens/*
613fa9e406ahrens * Opposite of spa_load().
614fa9e406