spa.c revision c5904d138f3bdf0762dbf452a43d5a5c387ea6a8
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#pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e406ahrens
29fa9e406ahrens/*
30fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
31fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
32fa9e406ahrens * pool.
33fa9e406ahrens */
34fa9e406ahrens
35fa9e406ahrens#include <sys/zfs_context.h>
36ea8dc4beschrock#include <sys/fm/fs/zfs.h>
37fa9e406ahrens#include <sys/spa_impl.h>
38fa9e406ahrens#include <sys/zio.h>
39fa9e406ahrens#include <sys/zio_checksum.h>
40fa9e406ahrens#include <sys/zio_compress.h>
41fa9e406ahrens#include <sys/dmu.h>
42fa9e406ahrens#include <sys/dmu_tx.h>
43fa9e406ahrens#include <sys/zap.h>
44fa9e406ahrens#include <sys/zil.h>
45fa9e406ahrens#include <sys/vdev_impl.h>
46fa9e406ahrens#include <sys/metaslab.h>
47fa9e406ahrens#include <sys/uberblock_impl.h>
48fa9e406ahrens#include <sys/txg.h>
49fa9e406ahrens#include <sys/avl.h>
50fa9e406ahrens#include <sys/dmu_traverse.h>
51b1b8ab3lling#include <sys/dmu_objset.h>
52fa9e406ahrens#include <sys/unique.h>
53fa9e406ahrens#include <sys/dsl_pool.h>
54b1b8ab3lling#include <sys/dsl_dataset.h>
55fa9e406ahrens#include <sys/dsl_dir.h>
56fa9e406ahrens#include <sys/dsl_prop.h>
57b1b8ab3lling#include <sys/dsl_synctask.h>
58fa9e406ahrens#include <sys/fs/zfs.h>
59fa94a07brendan#include <sys/arc.h>
60fa9e406ahrens#include <sys/callb.h>
619517395ek#include <sys/systeminfo.h>
629517395ek#include <sys/sunddi.h>
63e7cbe64gw#include <sys/spa_boot.h>
64fa9e406ahrens
65990b485lling#include "zfs_prop.h"
66b7b9745perrin#include "zfs_comutil.h"
67990b485lling
68416e0cdekint zio_taskq_threads = 8;
69416e0cdek
70990b485llingstatic void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
71990b485lling
72990b485lling/*
73990b485lling * ==========================================================================
74990b485lling * SPA properties routines
75990b485lling * ==========================================================================
76990b485lling */
77990b485lling
78990b485lling/*
79990b485lling * Add a (source=src, propname=propval) list to an nvlist.
80990b485lling */
819d82f4fllingstatic void
82990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
83990b485lling    uint64_t intval, zprop_source_t src)
84990b485lling{
85990b485lling	const char *propname = zpool_prop_to_name(prop);
86990b485lling	nvlist_t *propval;
87990b485lling
889d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
899d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
90990b485lling
919d82f4flling	if (strval != NULL)
929d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
939d82f4flling	else
949d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
95990b485lling
969d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
97990b485lling	nvlist_free(propval);
98990b485lling}
99990b485lling
100990b485lling/*
101990b485lling * Get property values from the spa configuration.
102990b485lling */
1039d82f4fllingstatic void
104990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
105990b485lling{
106990b485lling	uint64_t size = spa_get_space(spa);
107990b485lling	uint64_t used = spa_get_alloc(spa);
108990b485lling	uint64_t cap, version;
109990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
110c5904d1eschrock	spa_config_dirent_t *dp;
111990b485lling
112990b485lling	/*
113990b485lling	 * readonly properties
114990b485lling	 */
1159d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa->spa_name, 0, src);
1169d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
1179d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
1189d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
119990b485lling
120990b485lling	cap = (size == 0) ? 0 : (used * 100 / size);
1219d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
122990b485lling
1239d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
1249d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1259d82f4flling	    spa->spa_root_vdev->vdev_state, src);
126990b485lling
127990b485lling	/*
128990b485lling	 * settable properties that are not stored in the pool property object.
129990b485lling	 */
130990b485lling	version = spa_version(spa);
131990b485lling	if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
132990b485lling		src = ZPROP_SRC_DEFAULT;
133990b485lling	else
134990b485lling		src = ZPROP_SRC_LOCAL;
1359d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
136990b485lling
1379d82f4flling	if (spa->spa_root != NULL)
1389d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1399d82f4flling		    0, ZPROP_SRC_LOCAL);
140990b485lling
141c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
142c5904d1eschrock		if (dp->scd_path == NULL) {
1439d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
144c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
145c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1469d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
147c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1482f8aaabeschrock		}
1492f8aaabeschrock	}
150990b485lling}
151990b485lling
152990b485lling/*
153990b485lling * Get zpool property values.
154990b485lling */
155990b485llingint
156990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
157990b485lling{
158990b485lling	zap_cursor_t zc;
159990b485lling	zap_attribute_t za;
160990b485lling	objset_t *mos = spa->spa_meta_objset;
161990b485lling	int err;
162990b485lling
1639d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
164990b485lling
165990b485lling	/*
166990b485lling	 * Get properties from the spa config.
167990b485lling	 */
1689d82f4flling	spa_prop_get_config(spa, nvp);
169990b485lling
170990b485lling	mutex_enter(&spa->spa_props_lock);
171990b485lling	/* If no pool property object, no more prop to get. */
172990b485lling	if (spa->spa_pool_props_object == 0) {
173990b485lling		mutex_exit(&spa->spa_props_lock);
174990b485lling		return (0);
175990b485lling	}
176990b485lling
177990b485lling	/*
178990b485lling	 * Get properties from the MOS pool property object.
179990b485lling	 */
180990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
181990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
182990b485lling	    zap_cursor_advance(&zc)) {
183990b485lling		uint64_t intval = 0;
184990b485lling		char *strval = NULL;
185990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
186990b485lling		zpool_prop_t prop;
187990b485lling
188990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
189990b485lling			continue;
190990b485lling
191990b485lling		switch (za.za_integer_length) {
192990b485lling		case 8:
193990b485lling			/* integer property */
194990b485lling			if (za.za_first_integer !=
195990b485lling			    zpool_prop_default_numeric(prop))
196990b485lling				src = ZPROP_SRC_LOCAL;
197990b485lling
198990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
199990b485lling				dsl_pool_t *dp;
200990b485lling				dsl_dataset_t *ds = NULL;
201990b485lling
202990b485lling				dp = spa_get_dsl(spa);
203990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
204990b485lling				if (err = dsl_dataset_open_obj(dp,
205990b485lling				    za.za_first_integer, NULL, DS_MODE_NONE,
206990b485lling				    FTAG, &ds)) {
207990b485lling					rw_exit(&dp->dp_config_rwlock);
208990b485lling					break;
209990b485lling				}
210990b485lling
211990b485lling				strval = kmem_alloc(
212990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
213990b485lling				    KM_SLEEP);
214990b485lling				dsl_dataset_name(ds, strval);
215990b485lling				dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
216990b485lling				rw_exit(&dp->dp_config_rwlock);
217990b485lling			} else {
218990b485lling				strval = NULL;
219990b485lling				intval = za.za_first_integer;
220990b485lling			}
221990b485lling
2229d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
223990b485lling
224990b485lling			if (strval != NULL)
225990b485lling				kmem_free(strval,
226990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
227990b485lling
228990b485lling			break;
229990b485lling
230990b485lling		case 1:
231990b485lling			/* string property */
232990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
233990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
234990b485lling			    za.za_name, 1, za.za_num_integers, strval);
235990b485lling			if (err) {
236990b485lling				kmem_free(strval, za.za_num_integers);
237990b485lling				break;
238990b485lling			}
2399d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
240990b485lling			kmem_free(strval, za.za_num_integers);
241990b485lling			break;
242990b485lling
243990b485lling		default:
244990b485lling			break;
245990b485lling		}
246990b485lling	}
247990b485lling	zap_cursor_fini(&zc);
248990b485lling	mutex_exit(&spa->spa_props_lock);
249990b485llingout:
250990b485lling	if (err && err != ENOENT) {
251990b485lling		nvlist_free(*nvp);
2529d82f4flling		*nvp = NULL;
253990b485lling		return (err);
254990b485lling	}
255990b485lling
256990b485lling	return (0);
257990b485lling}
258990b485lling
259990b485lling/*
260990b485lling * Validate the given pool properties nvlist and modify the list
261990b485lling * for the property values to be set.
262990b485lling */
263990b485llingstatic int
264990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
265990b485lling{
266990b485lling	nvpair_t *elem;
267990b485lling	int error = 0, reset_bootfs = 0;
268990b485lling	uint64_t objnum;
269990b485lling
270990b485lling	elem = NULL;
271990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
272990b485lling		zpool_prop_t prop;
273990b485lling		char *propname, *strval;
274990b485lling		uint64_t intval;
275990b485lling		vdev_t *rvdev;
276990b485lling		char *vdev_type;
277990b485lling		objset_t *os;
2782f8aaabeschrock		char *slash;
279990b485lling
280990b485lling		propname = nvpair_name(elem);
281990b485lling
282990b485lling		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
283990b485lling			return (EINVAL);
284990b485lling
285990b485lling		switch (prop) {
286990b485lling		case ZPOOL_PROP_VERSION:
287990b485lling			error = nvpair_value_uint64(elem, &intval);
288990b485lling			if (!error &&
289990b485lling			    (intval < spa_version(spa) || intval > SPA_VERSION))
290990b485lling				error = EINVAL;
291990b485lling			break;
292990b485lling
293990b485lling		case ZPOOL_PROP_DELEGATION:
294990b485lling		case ZPOOL_PROP_AUTOREPLACE:
295990b485lling			error = nvpair_value_uint64(elem, &intval);
296990b485lling			if (!error && intval > 1)
297990b485lling				error = EINVAL;
298990b485lling			break;
299990b485lling
300990b485lling		case ZPOOL_PROP_BOOTFS:
301990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
302990b485lling				error = ENOTSUP;
303990b485lling				break;
304990b485lling			}
305990b485lling
306990b485lling			/*
307990b485lling			 * A bootable filesystem can not be on a RAIDZ pool
308990b485lling			 * nor a striped pool with more than 1 device.
309990b485lling			 */
310990b485lling			rvdev = spa->spa_root_vdev;
311990b485lling			vdev_type =
312990b485lling			    rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
313990b485lling			if (rvdev->vdev_children > 1 ||
314990b485lling			    strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
315990b485lling			    strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
316990b485lling				error = ENOTSUP;
317990b485lling				break;
318990b485lling			}
319990b485lling
320990b485lling			reset_bootfs = 1;
321990b485lling
322990b485lling			error = nvpair_value_string(elem, &strval);
323990b485lling
324990b485lling			if (!error) {
325990b485lling				if (strval == NULL || strval[0] == '\0') {
326990b485lling					objnum = zpool_prop_default_numeric(
327990b485lling					    ZPOOL_PROP_BOOTFS);
328990b485lling					break;
329990b485lling				}
330990b485lling
331990b485lling				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
332990b485lling				    DS_MODE_STANDARD | DS_MODE_READONLY, &os))
333990b485lling					break;
334990b485lling				objnum = dmu_objset_id(os);
335990b485lling				dmu_objset_close(os);
336990b485lling			}
337990b485lling			break;
3380a4e951gw		case ZPOOL_PROP_FAILUREMODE:
3390a4e951gw			error = nvpair_value_uint64(elem, &intval);
3400a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3410a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
3420a4e951gw				error = EINVAL;
3430a4e951gw
3440a4e951gw			/*
3450a4e951gw			 * This is a special case which only occurs when
3460a4e951gw			 * the pool has completely failed. This allows
3470a4e951gw			 * the user to change the in-core failmode property
3480a4e951gw			 * without syncing it out to disk (I/Os might
3490a4e951gw			 * currently be blocked). We do this by returning
3500a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
3510a4e951gw			 * into thinking we encountered a property validation
3520a4e951gw			 * error.
3530a4e951gw			 */
3540a4e951gw			if (!error && spa_state(spa) == POOL_STATE_IO_FAILURE) {
3550a4e951gw				spa->spa_failmode = intval;
3560a4e951gw				error = EIO;
3570a4e951gw			}
3580a4e951gw			break;
3592f8aaabeschrock
3602f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
3612f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
3622f8aaabeschrock				break;
3632f8aaabeschrock
3642f8aaabeschrock			if (strval[0] == '\0')
3652f8aaabeschrock				break;
3662f8aaabeschrock
3672f8aaabeschrock			if (strcmp(strval, "none") == 0)
3682f8aaabeschrock				break;
3692f8aaabeschrock
3702f8aaabeschrock			if (strval[0] != '/') {
3712f8aaabeschrock				error = EINVAL;
3722f8aaabeschrock				break;
3732f8aaabeschrock			}
3742f8aaabeschrock
3752f8aaabeschrock			slash = strrchr(strval, '/');
3762f8aaabeschrock			ASSERT(slash != NULL);
3772f8aaabeschrock
3782f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
3792f8aaabeschrock			    strcmp(slash, "/..") == 0)
3802f8aaabeschrock				error = EINVAL;
3812f8aaabeschrock			break;
382990b485lling		}
383990b485lling
384990b485lling		if (error)
385990b485lling			break;
386990b485lling	}
387990b485lling
388990b485lling	if (!error && reset_bootfs) {
389990b485lling		error = nvlist_remove(props,
390990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
391990b485lling
392990b485lling		if (!error) {
393990b485lling			error = nvlist_add_uint64(props,
394990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
395990b485lling		}
396990b485lling	}
397990b485lling
398990b485lling	return (error);
399990b485lling}
400990b485lling
401990b485llingint
402990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
403990b485lling{
404990b485lling	int error;
405990b485lling
406990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
407990b485lling		return (error);
408990b485lling
409990b485lling	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
410990b485lling	    spa, nvp, 3));
411990b485lling}
412990b485lling
413990b485lling/*
414990b485lling * If the bootfs property value is dsobj, clear it.
415990b485lling */
416990b485llingvoid
417990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
418990b485lling{
419990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
420990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
421990b485lling		    spa->spa_pool_props_object,
422990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
423990b485lling		spa->spa_bootfs = 0;
424990b485lling	}
425990b485lling}
426990b485lling
427fa9e406ahrens/*
428fa9e406ahrens * ==========================================================================
429fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
430fa9e406ahrens * ==========================================================================
431fa9e406ahrens */
432fa9e406ahrens
433ea8dc4beschrockstatic int
434ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
435ea8dc4beschrock{
436ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
437ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
438ea8dc4beschrock	int ret;
439ea8dc4beschrock
440ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
441ea8dc4beschrock	    sizeof (zbookmark_t));
442ea8dc4beschrock
443ea8dc4beschrock	if (ret < 0)
444ea8dc4beschrock		return (-1);
445ea8dc4beschrock	else if (ret > 0)
446ea8dc4beschrock		return (1);
447ea8dc4beschrock	else
448ea8dc4beschrock		return (0);
449ea8dc4beschrock}
450ea8dc4beschrock
451ea8dc4beschrock/*
452ea8dc4beschrock * Utility function which retrieves copies of the current logs and
453ea8dc4beschrock * re-initializes them in the process.
454ea8dc4beschrock */
455ea8dc4beschrockvoid
456ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
457ea8dc4beschrock{
458ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
459ea8dc4beschrock
460ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
461ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
462ea8dc4beschrock
463ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
464ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
465ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
466ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
467ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
468ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
469ea8dc4beschrock}
470ea8dc4beschrock
471fa9e406ahrens/*
472fa9e406ahrens * Activate an uninitialized pool.
473fa9e406ahrens */
474fa9e406ahrensstatic void
475fa9e406ahrensspa_activate(spa_t *spa)
476fa9e406ahrens{
477fa9e406ahrens	int t;
478fa9e406ahrens
479fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
480fa9e406ahrens
481fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
482fa9e406ahrens
483fa9e406ahrens	spa->spa_normal_class = metaslab_class_create();
4848654d02perrin	spa->spa_log_class = metaslab_class_create();
485fa9e406ahrens
486fa9e406ahrens	for (t = 0; t < ZIO_TYPES; t++) {
487fa9e406ahrens		spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue",
488416e0cdek		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
489fa9e406ahrens		    TASKQ_PREPOPULATE);
490fa9e406ahrens		spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr",
491416e0cdek		    zio_taskq_threads, maxclsyspri, 50, INT_MAX,
492fa9e406ahrens		    TASKQ_PREPOPULATE);
493fa9e406ahrens	}
494fa9e406ahrens
495fa9e406ahrens	list_create(&spa->spa_dirty_list, sizeof (vdev_t),
496fa9e406ahrens	    offsetof(vdev_t, vdev_dirty_node));
4970a4e951gw	list_create(&spa->spa_zio_list, sizeof (zio_t),
4980a4e951gw	    offsetof(zio_t, zio_link_node));
499fa9e406ahrens
500fa9e406ahrens	txg_list_create(&spa->spa_vdev_txg_list,
501fa9e406ahrens	    offsetof(struct vdev, vdev_txg_node));
502ea8dc4beschrock
503ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
504ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
505ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
506ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
507ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
508ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
509fa9e406ahrens}
510fa9e406ahrens
511fa9e406ahrens/*
512fa9e406ahrens * Opposite of spa_activate().
513fa9e406ahrens */
514fa9e406ahrensstatic void
515fa9e406ahrensspa_deactivate(spa_t *spa)
516fa9e406ahrens{
517fa9e406ahrens	int t;
518fa9e406ahrens
519fa9e406ahrens	ASSERT(spa->spa_sync_on == B_FALSE);
520fa9e406ahrens	ASSERT(spa->spa_dsl_pool == NULL);
521fa9e406ahrens	ASSERT(spa->spa_root_vdev == NULL);
522fa9e406ahrens
523fa9e406ahrens	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
524fa9e406ahrens
525fa9e406ahrens	txg_list_destroy(&spa->spa_vdev_txg_list);
526fa9e406ahrens
527fa9e406ahrens	list_destroy(&spa->spa_dirty_list);
5280a4e951gw	list_destroy(&spa->spa_zio_list);
529fa9e406ahrens
530fa9e406ahrens	for (t = 0; t < ZIO_TYPES; t++) {
531fa9e406ahrens		taskq_destroy(spa->spa_zio_issue_taskq[t]);
532fa9e406ahrens		taskq_destroy(spa->spa_zio_intr_taskq[t]);
533fa9e406ahrens		spa->spa_zio_issue_taskq[t] = NULL;
534fa9e406ahrens		spa->spa_zio_intr_taskq[t] = NULL;
535fa9e406ahrens	}
536fa9e406ahrens
537fa9e406ahrens	metaslab_class_destroy(spa->spa_normal_class);
538fa9e406ahrens	spa->spa_normal_class = NULL;
539fa9e406ahrens
5408654d02perrin	metaslab_class_destroy(spa->spa_log_class);
5418654d02perrin	spa->spa_log_class = NULL;
5428654d02perrin
543ea8dc4beschrock	/*
544ea8dc4beschrock	 * If this was part of an import or the open otherwise failed, we may
545ea8dc4beschrock	 * still have errors left in the queues.  Empty them just in case.
546ea8dc4beschrock	 */
547ea8dc4beschrock	spa_errlog_drain(spa);
548ea8dc4beschrock
549ea8dc4beschrock	avl_destroy(&spa->spa_errlist_scrub);
550ea8dc4beschrock	avl_destroy(&spa->spa_errlist_last);
551ea8dc4beschrock
552fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
553fa9e406ahrens}
554fa9e406ahrens
555fa9e406ahrens/*
556fa9e406ahrens * Verify a pool configuration, and construct the vdev tree appropriately.  This
557fa9e406ahrens * will create all the necessary vdevs in the appropriate layout, with each vdev
558fa9e406ahrens * in the CLOSED state.  This will prep the pool before open/creation/import.
559fa9e406ahrens * All vdev validation is done by the vdev_alloc() routine.
560fa9e406ahrens */
56199653d4eschrockstatic int
56299653d4eschrockspa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
56399653d4eschrock    uint_t id, int atype)
564fa9e406ahrens{
565fa9e406ahrens	nvlist_t **child;
566fa9e406ahrens	uint_t c, children;
56799653d4eschrock	int error;
568fa9e406ahrens
56999653d4eschrock	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
57099653d4eschrock		return (error);
571fa9e406ahrens
57299653d4eschrock	if ((*vdp)->vdev_ops->vdev_op_leaf)
57399653d4eschrock		return (0);
574fa9e406ahrens
575fa9e406ahrens	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
576fa9e406ahrens	    &child, &children) != 0) {
57799653d4eschrock		vdev_free(*vdp);
57899653d4eschrock		*vdp = NULL;
57999653d4eschrock		return (EINVAL);
580fa9e406ahrens	}
581fa9e406ahrens
582fa9e406ahrens	for (c = 0; c < children; c++) {
58399653d4eschrock		vdev_t *vd;
58499653d4eschrock		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
58599653d4eschrock		    atype)) != 0) {
58699653d4eschrock			vdev_free(*vdp);
58799653d4eschrock			*vdp = NULL;
58899653d4eschrock			return (error);
589fa9e406ahrens		}
590fa9e406ahrens	}
591fa9e406ahrens
59299653d4eschrock	ASSERT(*vdp != NULL);
59399653d4eschrock
59499653d4eschrock	return (0);
595fa9e406ahrens}
596fa9e406ahrens
597fa9e406ahrens/*
598fa9e406ahrens * Opposite of spa_load().
599fa9e406ahrens */
600fa9e406ahrensstatic void
601fa9e406ahrensspa_unload(spa_t *spa)
602fa9e406ahrens{
60399653d4eschrock	int i;
604