spa.c revision 2e0c549eb612aef12ce21941172e3abe2e62ba85
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/*
23379c004Eric Schrock * Copyright 2009 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
635679c89jv#ifdef	_KERNEL
645679c89jv#include <sys/zone.h>
655679c89jv#endif	/* _KERNEL */
665679c89jv
67990b485lling#include "zfs_prop.h"
68b7b9745perrin#include "zfs_comutil.h"
69990b485lling
702e0c549Jonathan Adamsenum zti_modes {
712e0c549Jonathan Adams	zti_mode_fixed,			/* value is # of threads (min 1) */
722e0c549Jonathan Adams	zti_mode_online_percent,	/* value is % of online CPUs */
732e0c549Jonathan Adams	zti_mode_tune,			/* fill from zio_taskq_tune_* */
742e0c549Jonathan Adams	zti_nmodes
75e14bb32Jeff Bonwick};
76416e0cdek
772e0c549Jonathan Adams#define	ZTI_THREAD_FIX(n)	{ zti_mode_fixed, (n) }
782e0c549Jonathan Adams#define	ZTI_THREAD_PCT(n)	{ zti_mode_online_percent, (n) }
792e0c549Jonathan Adams#define	ZTI_THREAD_TUNE		{ zti_mode_tune, 0 }
802e0c549Jonathan Adams
812e0c549Jonathan Adams#define	ZTI_THREAD_ONE		ZTI_THREAD_FIX(1)
822e0c549Jonathan Adams
832e0c549Jonathan Adamstypedef struct zio_taskq_info {
842e0c549Jonathan Adams	const char *zti_name;
852e0c549Jonathan Adams	struct {
862e0c549Jonathan Adams		enum zti_modes zti_mode;
872e0c549Jonathan Adams		uint_t zti_value;
882e0c549Jonathan Adams	} zti_nthreads[ZIO_TASKQ_TYPES];
892e0c549Jonathan Adams} zio_taskq_info_t;
902e0c549Jonathan Adams
912e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
922e0c549Jonathan Adams				"issue",		"intr"
932e0c549Jonathan Adams};
942e0c549Jonathan Adams
952e0c549Jonathan Adamsconst zio_taskq_info_t zio_taskqs[ZIO_TYPES] = {
962e0c549Jonathan Adams	/*			ISSUE			INTR		*/
972e0c549Jonathan Adams	{ "spa_zio_null",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
982e0c549Jonathan Adams	{ "spa_zio_read",	{ ZTI_THREAD_FIX(8),	ZTI_THREAD_TUNE } },
992e0c549Jonathan Adams	{ "spa_zio_write",	{ ZTI_THREAD_TUNE,	ZTI_THREAD_FIX(8) } },
1002e0c549Jonathan Adams	{ "spa_zio_free",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1012e0c549Jonathan Adams	{ "spa_zio_claim",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1022e0c549Jonathan Adams	{ "spa_zio_ioctl",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1032e0c549Jonathan Adams};
1042e0c549Jonathan Adams
1052e0c549Jonathan Adamsenum zti_modes zio_taskq_tune_mode = zti_mode_online_percent;
1062e0c549Jonathan Adamsuint_t zio_taskq_tune_value = 80;	/* #threads = 80% of # online CPUs */
1072e0c549Jonathan Adams
108990b485llingstatic void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
10989a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
110990b485lling
111990b485lling/*
112990b485lling * ==========================================================================
113990b485lling * SPA properties routines
114990b485lling * ==========================================================================
115990b485lling */
116990b485lling
117990b485lling/*
118990b485lling * Add a (source=src, propname=propval) list to an nvlist.
119990b485lling */
1209d82f4fllingstatic void
121990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
122990b485lling    uint64_t intval, zprop_source_t src)
123990b485lling{
124990b485lling	const char *propname = zpool_prop_to_name(prop);
125990b485lling	nvlist_t *propval;
126990b485lling
1279d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1289d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
129990b485lling
1309d82f4flling	if (strval != NULL)
1319d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1329d82f4flling	else
1339d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
134990b485lling
1359d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
136990b485lling	nvlist_free(propval);
137990b485lling}
138990b485lling
139990b485lling/*
140990b485lling * Get property values from the spa configuration.
141990b485lling */
1429d82f4fllingstatic void
143990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
144990b485lling{
145379c004Eric Schrock	uint64_t size;
146379c004Eric Schrock	uint64_t used;
147990b485lling	uint64_t cap, version;
148990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
149c5904d1eschrock	spa_config_dirent_t *dp;
150990b485lling
151e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
152e14bb32Jeff Bonwick
153379c004Eric Schrock	if (spa->spa_root_vdev != NULL) {
154379c004Eric Schrock		size = spa_get_space(spa);
155379c004Eric Schrock		used = spa_get_alloc(spa);
156379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
157379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
158379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
159379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
160379c004Eric Schrock		    size - used, src);
161379c004Eric Schrock
162379c004Eric Schrock		cap = (size == 0) ? 0 : (used * 100 / size);
163379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
164379c004Eric Schrock
165379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
166379c004Eric Schrock		    spa->spa_root_vdev->vdev_state, src);
167379c004Eric Schrock
168379c004Eric Schrock		version = spa_version(spa);
169379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
170379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
171379c004Eric Schrock		else
172379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
173379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
174379c004Eric Schrock	}
175990b485lling
1769d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
177990b485lling
1789d82f4flling	if (spa->spa_root != NULL)
1799d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1809d82f4flling		    0, ZPROP_SRC_LOCAL);
181990b485lling
182c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
183c5904d1eschrock		if (dp->scd_path == NULL) {
1849d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
185c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
186c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1879d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
188c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1892f8aaabeschrock		}
1902f8aaabeschrock	}
191990b485lling}
192990b485lling
193990b485lling/*
194990b485lling * Get zpool property values.
195990b485lling */
196990b485llingint
197990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
198990b485lling{
199990b485lling	zap_cursor_t zc;
200990b485lling	zap_attribute_t za;
201990b485lling	objset_t *mos = spa->spa_meta_objset;
202990b485lling	int err;
203990b485lling
2049d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
205990b485lling
206e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
207e14bb32Jeff Bonwick
208990b485lling	/*
209990b485lling	 * Get properties from the spa config.
210990b485lling	 */
2119d82f4flling	spa_prop_get_config(spa, nvp);
212990b485lling
213990b485lling	/* If no pool property object, no more prop to get. */
214990b485lling	if (spa->spa_pool_props_object == 0) {
215990b485lling		mutex_exit(&spa->spa_props_lock);
216990b485lling		return (0);
217990b485lling	}
218990b485lling
219990b485lling	/*
220990b485lling	 * Get properties from the MOS pool property object.
221990b485lling	 */
222990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
223990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
224990b485lling	    zap_cursor_advance(&zc)) {
225990b485lling		uint64_t intval = 0;
226990b485lling		char *strval = NULL;
227990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
228990b485lling		zpool_prop_t prop;
229990b485lling
230990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
231990b485lling			continue;
232990b485lling
233990b485lling		switch (za.za_integer_length) {
234990b485lling		case 8:
235990b485lling			/* integer property */
236990b485lling			if (za.za_first_integer !=
237990b485lling			    zpool_prop_default_numeric(prop))
238990b485lling				src = ZPROP_SRC_LOCAL;
239990b485lling
240990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
241990b485lling				dsl_pool_t *dp;
242990b485lling				dsl_dataset_t *ds = NULL;
243990b485lling
244990b485lling				dp = spa_get_dsl(spa);
245990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
246745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
247745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
248990b485lling					rw_exit(&dp->dp_config_rwlock);
249990b485lling					break;
250990b485lling				}
251990b485lling
252990b485lling				strval = kmem_alloc(
253990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
254990b485lling				    KM_SLEEP);
255990b485lling				dsl_dataset_name(ds, strval);
256745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
257990b485lling				rw_exit(&dp->dp_config_rwlock);
258990b485lling			} else {
259990b485lling				strval = NULL;
260990b485lling				intval = za.za_first_integer;
261990b485lling			}
262990b485lling
2639d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
264990b485lling
265990b485lling			if (strval != NULL)
266990b485lling				kmem_free(strval,
267990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
268990b485lling
269990b485lling			break;
270990b485lling
271990b485lling		case 1:
272990b485lling			/* string property */
273990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
274990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
275990b485lling			    za.za_name, 1, za.za_num_integers, strval);
276990b485lling			if (err) {
277990b485lling				kmem_free(strval, za.za_num_integers);
278990b485lling				break;
279990b485lling			}
2809d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
281990b485lling			kmem_free(strval, za.za_num_integers);
282990b485lling			break;
283990b485lling
284990b485lling		default:
285990b485lling			break;
286990b485lling		}
287990b485lling	}
288990b485lling	zap_cursor_fini(&zc);
289990b485lling	mutex_exit(&spa->spa_props_lock);
290990b485llingout:
291990b485lling	if (err && err != ENOENT) {
292990b485lling		nvlist_free(*nvp);
2939d82f4flling		*nvp = NULL;
294990b485lling		return (err);
295990b485lling	}
296990b485lling
297990b485lling	return (0);
298990b485lling}
299990b485lling
300990b485lling/*
301990b485lling * Validate the given pool properties nvlist and modify the list
302990b485lling * for the property values to be set.
303990b485lling */
304990b485llingstatic int
305990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
306990b485lling{
307990b485lling	nvpair_t *elem;
308990b485lling	int error = 0, reset_bootfs = 0;
309990b485lling	uint64_t objnum;
310990b485lling
311990b485lling	elem = NULL;
312990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
313990b485lling		zpool_prop_t prop;
314990b485lling		char *propname, *strval;
315990b485lling		uint64_t intval;
316990b485lling		objset_t *os;
3172f8aaabeschrock		char *slash;
318990b485lling
319990b485lling		propname = nvpair_name(elem);
320990b485lling
321990b485lling		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
322990b485lling			return (EINVAL);
323990b485lling
324990b485lling		switch (prop) {
325990b485lling		case ZPOOL_PROP_VERSION:
326990b485lling			error = nvpair_value_uint64(elem, &intval);
327990b485lling			if (!error &&
328990b485lling			    (intval < spa_version(spa) || intval > SPA_VERSION))
329990b485lling				error = EINVAL;
330990b485lling			break;
331990b485lling
332990b485lling		case ZPOOL_PROP_DELEGATION:
333990b485lling		case ZPOOL_PROP_AUTOREPLACE:
334d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
335990b485lling			error = nvpair_value_uint64(elem, &intval);
336990b485lling			if (!error && intval > 1)
337990b485lling				error = EINVAL;
338990b485lling			break;
339990b485lling
340990b485lling		case ZPOOL_PROP_BOOTFS:
341990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
342990b485lling				error = ENOTSUP;
343990b485lling				break;
344990b485lling			}
345990b485lling
346990b485lling			/*
34715e6edfgw			 * Make sure the vdev config is bootable
348990b485lling			 */
34915e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
350990b485lling				error = ENOTSUP;
351990b485lling				break;
352990b485lling			}
353990b485lling
354990b485lling			reset_bootfs = 1;
355990b485lling
356990b485lling			error = nvpair_value_string(elem, &strval);
357990b485lling
358990b485lling			if (!error) {
35915e6edfgw				uint64_t compress;
36015e6edfgw
361990b485lling				if (strval == NULL || strval[0] == '\0') {
362990b485lling					objnum = zpool_prop_default_numeric(
363990b485lling					    ZPOOL_PROP_BOOTFS);
364990b485lling					break;
365990b485lling				}
366990b485lling
367990b485lling				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
368745cd3cmaybee				    DS_MODE_USER | DS_MODE_READONLY, &os))
369990b485lling					break;
37015e6edfgw
37115e6edfgw				/* We don't support gzip bootable datasets */
37215e6edfgw				if ((error = dsl_prop_get_integer(strval,
37315e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
37415e6edfgw				    &compress, NULL)) == 0 &&
37515e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
37615e6edfgw					error = ENOTSUP;
37715e6edfgw				} else {
37815e6edfgw					objnum = dmu_objset_id(os);
37915e6edfgw				}
380990b485lling				dmu_objset_close(os);
381990b485lling			}
382990b485lling			break;
383e14bb32Jeff Bonwick
3840a4e951gw		case ZPOOL_PROP_FAILUREMODE:
3850a4e951gw			error = nvpair_value_uint64(elem, &intval);
3860a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3870a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
3880a4e951gw				error = EINVAL;
3890a4e951gw
3900a4e951gw			/*
3910a4e951gw			 * This is a special case which only occurs when
3920a4e951gw			 * the pool has completely failed. This allows
3930a4e951gw			 * the user to change the in-core failmode property
3940a4e951gw			 * without syncing it out to disk (I/Os might
3950a4e951gw			 * currently be blocked). We do this by returning
3960a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
3970a4e951gw			 * into thinking we encountered a property validation
3980a4e951gw			 * error.
3990a4e951gw			 */
400e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
4010a4e951gw				spa->spa_failmode = intval;
4020a4e951gw				error = EIO;
4030a4e951gw			}
4040a4e951gw			break;
4052f8aaabeschrock
4062f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
4072f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
4082f8aaabeschrock				break;
4092f8aaabeschrock
4102f8aaabeschrock			if (strval[0] == '\0')
4112f8aaabeschrock				break;
4122f8aaabeschrock
4132f8aaabeschrock			if (strcmp(strval, "none") == 0)
4142f8aaabeschrock				break;
4152f8aaabeschrock
4162f8aaabeschrock			if (strval[0] != '/') {
4172f8aaabeschrock				error = EINVAL;
4182f8aaabeschrock				break;
4192f8aaabeschrock			}
4202f8aaabeschrock
4212f8aaabeschrock			slash = strrchr(strval, '/');
4222f8aaabeschrock			ASSERT(slash != NULL);
4232f8aaabeschrock
4242f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4252f8aaabeschrock			    strcmp(slash, "/..") == 0)
4262f8aaabeschrock				error = EINVAL;
4272f8aaabeschrock			break;
428990b485lling		}
429990b485lling
430990b485lling		if (error)
431990b485lling			break;
432990b485lling	}
433990b485lling
434990b485lling	if (!error && reset_bootfs) {
435990b485lling		error = nvlist_remove(props,
436990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
437990b485lling
438990b485lling		if (!error) {
439990b485lling			error = nvlist_add_uint64(props,
440990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
441990b485lling		}
442990b485lling	}
443990b485lling
444990b485lling	return (error);
445990b485lling}
446990b485lling
447379c004Eric Schrockvoid
448379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
449379c004Eric Schrock{
450379c004Eric Schrock	char *cachefile;
451379c004Eric Schrock	spa_config_dirent_t *dp;
452379c004Eric Schrock
453379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
454379c004Eric Schrock	    &cachefile) != 0)
455379c004Eric Schrock		return;
456379c004Eric Schrock
457379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
458379c004Eric Schrock	    KM_SLEEP);
459379c004Eric Schrock
460379c004Eric Schrock	if (cachefile[0] == '\0')
461379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
462379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
463379c004Eric Schrock		dp->scd_path = NULL;
464379c004Eric Schrock	else
465379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
466379c004Eric Schrock
467379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
468379c004Eric Schrock	if (need_sync)
469379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
470379c004Eric Schrock}
471379c004Eric Schrock
472990b485llingint
473990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
474990b485lling{
475990b485lling	int error;
476379c004Eric Schrock	nvpair_t *elem;
477379c004Eric Schrock	boolean_t need_sync = B_FALSE;
478379c004Eric Schrock	zpool_prop_t prop;
479990b485lling
480990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
481990b485lling		return (error);
482990b485lling
483379c004Eric Schrock	elem = NULL;
484379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
485379c004Eric Schrock		if ((prop = zpool_name_to_prop(
486379c004Eric Schrock		    nvpair_name(elem))) == ZPROP_INVAL)
487379c004Eric Schrock			return (EINVAL);
488379c004Eric Schrock
489379c004Eric Schrock		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
490379c004Eric Schrock			continue;
491379c004Eric Schrock
492379c004Eric Schrock		need_sync = B_TRUE;
493379c004Eric Schrock		break;
494379c004Eric Schrock	}
495379c004Eric Schrock
496379c004Eric Schrock	if (need_sync)
497379c004Eric Schrock		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
498379c004Eric Schrock		    spa, nvp, 3));
499379c004Eric Schrock	else
500379c004Eric Schrock		return (0);
501990b485lling}
502990b485lling
503990b485lling/*
504990b485lling * If the bootfs property value is dsobj, clear it.
505990b485lling */
506990b485llingvoid
507990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
508990b485lling{
509990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
510990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
511990b485lling		    spa->spa_pool_props_object,
512990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
513990b485lling		spa->spa_bootfs = 0;
514990b485lling	}
515990b485lling}
516990b485lling
517fa9e406ahrens/*
518fa9e406ahrens * ==========================================================================
519fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
520fa9e406ahrens * ==========================================================================
521fa9e406ahrens */
522fa9e406ahrens
523ea8dc4beschrockstatic int
524ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
525ea8dc4beschrock{
526ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
527ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
528ea8dc4beschrock	int ret;
529ea8dc4beschrock
530ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
531ea8dc4beschrock	    sizeof (zbookmark_t));
532ea8dc4beschrock
533ea8dc4beschrock	if (ret < 0)
534ea8dc4beschrock		return (-1);
535ea8dc4beschrock	else if (ret > 0)
536ea8dc4beschrock		return (1);
537ea8dc4beschrock	else
538ea8dc4beschrock		return (0);
539ea8dc4beschrock}
540ea8dc4beschrock
541ea8dc4beschrock/*
542ea8dc4beschrock * Utility function which retrieves copies of the current logs and
543ea8dc4beschrock * re-initializes them in the process.
544ea8dc4beschrock */
545ea8dc4beschrockvoid
546ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
547ea8dc4beschrock{
548ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
549ea8dc4beschrock
550ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
551ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
552ea8dc4beschrock
553ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
554ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
555ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
556ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
557ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
558ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
559ea8dc4beschrock}
560ea8dc4beschrock
561fa9e406ahrens/*
562fa9e406ahrens * Activate an uninitialized pool.
563fa9e406ahrens */
564fa9e406ahrensstatic void
5658ad4d6dJeff Bonwickspa_activate(spa_t *spa, int mode)
566fa9e406ahrens{
567fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
568fa9e406ahrens
569fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
5708ad4d6dJeff Bonwick	spa->spa_mode = mode;
571fa9e406ahrens
572d6e555bGeorge Wilson	spa->spa_normal_class = metaslab_class_create(zfs_metaslab_ops);
573d6e555bGeorge Wilson	spa->spa_log_class = metaslab_class_create(zfs_metaslab_ops);
574fa9e406ahrens
575e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
5762e0c549Jonathan Adams		const zio_taskq_info_t *ztip = &zio_taskqs[t];
577e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5782e0c549Jonathan Adams			enum zti_modes mode = ztip->zti_nthreads[q].zti_mode;
5792e0c549Jonathan Adams			uint_t value = ztip->zti_nthreads[q].zti_value;
5802e0c549Jonathan Adams			char name[32];
5812e0c549Jonathan Adams
5822e0c549Jonathan Adams			(void) snprintf(name, sizeof (name),
5832e0c549Jonathan Adams			    "%s_%s", ztip->zti_name, zio_taskq_types[q]);
5842e0c549Jonathan Adams
5852e0c549Jonathan Adams			if (mode == zti_mode_tune) {
5862e0c549Jonathan Adams				mode = zio_taskq_tune_mode;
5872e0c549Jonathan Adams				value = zio_taskq_tune_value;
5882e0c549Jonathan Adams				if (mode == zti_mode_tune)
5892e0c549Jonathan Adams					mode = zti_mode_online_percent;
5902e0c549Jonathan Adams			}
5912e0c549Jonathan Adams
5922e0c549Jonathan Adams			switch (mode) {
5932e0c549Jonathan Adams			case zti_mode_fixed:
5942e0c549Jonathan Adams				ASSERT3U(value, >=, 1);
5952e0c549Jonathan Adams				value = MAX(value, 1);
5962e0c549Jonathan Adams
5972e0c549Jonathan Adams				spa->spa_zio_taskq[t][q] = taskq_create(name,
5982e0c549Jonathan Adams				    value, maxclsyspri, 50, INT_MAX,
5992e0c549Jonathan Adams				    TASKQ_PREPOPULATE);
6002e0c549Jonathan Adams				break;
6012e0c549Jonathan Adams
6022e0c549Jonathan Adams			case zti_mode_online_percent:
6032e0c549Jonathan Adams				spa->spa_zio_taskq[t][q] = taskq_create(name,
6042e0c549Jonathan Adams				    value, maxclsyspri, 50, INT_MAX,
6052e0c549Jonathan Adams				    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
6062e0c549Jonathan Adams				break;
6072e0c549Jonathan Adams
6082e0c549Jonathan Adams			case zti_mode_tune:
6092e0c549Jonathan Adams			default:
6102e0c549Jonathan Adams				panic("unrecognized mode for "
6112e0c549Jonathan Adams				    "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) "
6122e0c549Jonathan Adams				    "in spa_activate()",
6132e0c549Jonathan Adams				    t, q, mode, value);
6142e0c549Jonathan Adams				break;
6152e0c549Jonathan Adams			}
616e14bb32Jeff Bonwick		}
617fa9e406ahrens	}
618fa9e406ahrens
619e14bb32Jeff Bonwick	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
620e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_config_dirty_node));
621e14bb32Jeff Bonwick	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
622e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_state_dirty_node));
623fa9e406ahrens
624fa9e406ahrens	txg_list_create(&spa->spa_vdev_txg_list,
625fa9e406ahrens	    offsetof(struct vdev, vdev_txg_node));
626ea8dc4beschrock
627ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
628ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
629ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
630ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
631ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
632ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
633fa9e406ahrens}
634fa9e406ahrens
635fa9e406ahrens/*
636fa9e406ahrens * Opposite of spa_activate().
637fa9e406ahrens */
638fa9e406ahrensstatic void
639fa9e406ahrensspa_deactivate(spa_t *spa)
640fa9e406ahrens{
641fa9e406ahrens	ASSERT(spa->spa_sync_on == B_FALSE);
642fa9e406ahrens	ASSERT(spa->spa_dsl_pool == NULL);
643fa9e406ahrens	ASSERT(spa->spa_root_vdev == NULL);
644fa9e406ahrens
645fa9e406ahrens	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
646fa9e406ahrens
647fa9e406ahrens	txg_list_destroy(&spa->spa_vdev_txg_list);
648fa9e406ahrens
649e14bb32Jeff Bonwick	list_destroy(&spa->spa_config_dirty_list);
650e14bb32Jeff Bonwick	list_destroy(&spa->spa_state_dirty_list);
651fa9e406ahrens
652e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
653e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
654e14bb32Jeff Bonwick			taskq_destroy(spa->spa_zio_taskq[t][q]);
655e14bb32Jeff Bonwick			spa->spa_zio_taskq[t][q] = NULL;
656e14bb32Jeff Bonwick		}
657fa9e406ahrens	}
658fa9e406ahrens
659fa9e406ahrens	metaslab_class_destroy(spa->spa_normal_class);
660fa9e406ahrens	spa->spa_normal_class = NULL;
661fa9e406ahrens
6628654d02perrin	metaslab_class_destroy(spa->spa_log_class);
6638654d02perrin	spa->spa_log_class = NULL;
6648654d02perrin
665ea8dc4beschrock	/*
666ea8dc4beschrock	 * If this was part of an import or the open otherwise failed, we may
667ea8dc4beschrock	 * still have errors left in the queues.  Empty them just in case.
668