spa.c revision b24ab6762772a3f6a89393947930c7fa61306783
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/dmu.h>
39fa9e406ahrens#include <sys/dmu_tx.h>
40fa9e406ahrens#include <sys/zap.h>
41fa9e406ahrens#include <sys/zil.h>
42b24ab67Jeff Bonwick#include <sys/ddt.h>
43fa9e406ahrens#include <sys/vdev_impl.h>
44fa9e406ahrens#include <sys/metaslab.h>
4588ecc94George Wilson#include <sys/metaslab_impl.h>
46fa9e406ahrens#include <sys/uberblock_impl.h>
47fa9e406ahrens#include <sys/txg.h>
48fa9e406ahrens#include <sys/avl.h>
49fa9e406ahrens#include <sys/dmu_traverse.h>
50b1b8ab3lling#include <sys/dmu_objset.h>
51fa9e406ahrens#include <sys/unique.h>
52fa9e406ahrens#include <sys/dsl_pool.h>
53b1b8ab3lling#include <sys/dsl_dataset.h>
54fa9e406ahrens#include <sys/dsl_dir.h>
55fa9e406ahrens#include <sys/dsl_prop.h>
56b1b8ab3lling#include <sys/dsl_synctask.h>
57fa9e406ahrens#include <sys/fs/zfs.h>
58fa94a07brendan#include <sys/arc.h>
59fa9e406ahrens#include <sys/callb.h>
609517395ek#include <sys/systeminfo.h>
619517395ek#include <sys/sunddi.h>
62e7cbe64gw#include <sys/spa_boot.h>
63573ca77George Wilson#include <sys/zfs_ioctl.h>
64fa9e406ahrens
655679c89jv#ifdef	_KERNEL
665679c89jv#include <sys/zone.h>
67dedec47Jack Meng#include <sys/bootprops.h>
685679c89jv#endif	/* _KERNEL */
695679c89jv
70990b485lling#include "zfs_prop.h"
71b7b9745perrin#include "zfs_comutil.h"
72990b485lling
732e0c549Jonathan Adamsenum zti_modes {
742e0c549Jonathan Adams	zti_mode_fixed,			/* value is # of threads (min 1) */
752e0c549Jonathan Adams	zti_mode_online_percent,	/* value is % of online CPUs */
762e0c549Jonathan Adams	zti_mode_tune,			/* fill from zio_taskq_tune_* */
772e0c549Jonathan Adams	zti_nmodes
78e14bb32Jeff Bonwick};
79416e0cdek
802e0c549Jonathan Adams#define	ZTI_THREAD_FIX(n)	{ zti_mode_fixed, (n) }
812e0c549Jonathan Adams#define	ZTI_THREAD_PCT(n)	{ zti_mode_online_percent, (n) }
822e0c549Jonathan Adams#define	ZTI_THREAD_TUNE		{ zti_mode_tune, 0 }
832e0c549Jonathan Adams
842e0c549Jonathan Adams#define	ZTI_THREAD_ONE		ZTI_THREAD_FIX(1)
852e0c549Jonathan Adams
862e0c549Jonathan Adamstypedef struct zio_taskq_info {
872e0c549Jonathan Adams	const char *zti_name;
882e0c549Jonathan Adams	struct {
892e0c549Jonathan Adams		enum zti_modes zti_mode;
902e0c549Jonathan Adams		uint_t zti_value;
912e0c549Jonathan Adams	} zti_nthreads[ZIO_TASKQ_TYPES];
922e0c549Jonathan Adams} zio_taskq_info_t;
932e0c549Jonathan Adams
942e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
952e0c549Jonathan Adams				"issue",		"intr"
962e0c549Jonathan Adams};
972e0c549Jonathan Adams
982e0c549Jonathan Adamsconst zio_taskq_info_t zio_taskqs[ZIO_TYPES] = {
992e0c549Jonathan Adams	/*			ISSUE			INTR		*/
1002e0c549Jonathan Adams	{ "spa_zio_null",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1012e0c549Jonathan Adams	{ "spa_zio_read",	{ ZTI_THREAD_FIX(8),	ZTI_THREAD_TUNE } },
1022e0c549Jonathan Adams	{ "spa_zio_write",	{ ZTI_THREAD_TUNE,	ZTI_THREAD_FIX(8) } },
1032e0c549Jonathan Adams	{ "spa_zio_free",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1042e0c549Jonathan Adams	{ "spa_zio_claim",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1052e0c549Jonathan Adams	{ "spa_zio_ioctl",	{ ZTI_THREAD_ONE,	ZTI_THREAD_ONE } },
1062e0c549Jonathan Adams};
1072e0c549Jonathan Adams
1082e0c549Jonathan Adamsenum zti_modes zio_taskq_tune_mode = zti_mode_online_percent;
1092e0c549Jonathan Adamsuint_t zio_taskq_tune_value = 80;	/* #threads = 80% of # online CPUs */
1102e0c549Jonathan Adams
111990b485llingstatic void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
11289a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
113990b485lling
114990b485lling/*
115990b485lling * ==========================================================================
116990b485lling * SPA properties routines
117990b485lling * ==========================================================================
118990b485lling */
119990b485lling
120990b485lling/*
121990b485lling * Add a (source=src, propname=propval) list to an nvlist.
122990b485lling */
1239d82f4fllingstatic void
124990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
125990b485lling    uint64_t intval, zprop_source_t src)
126990b485lling{
127990b485lling	const char *propname = zpool_prop_to_name(prop);
128990b485lling	nvlist_t *propval;
129990b485lling
1309d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1319d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
132990b485lling
1339d82f4flling	if (strval != NULL)
1349d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1359d82f4flling	else
1369d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
137990b485lling
1389d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
139990b485lling	nvlist_free(propval);
140990b485lling}
141990b485lling
142990b485lling/*
143990b485lling * Get property values from the spa configuration.
144990b485lling */
1459d82f4fllingstatic void
146990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
147990b485lling{
148379c004Eric Schrock	uint64_t size;
149379c004Eric Schrock	uint64_t used;
150990b485lling	uint64_t cap, version;
151990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
152c5904d1eschrock	spa_config_dirent_t *dp;
153990b485lling
154e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
155e14bb32Jeff Bonwick
156379c004Eric Schrock	if (spa->spa_root_vdev != NULL) {
157b24ab67Jeff Bonwick		used = metaslab_class_get_alloc(spa_normal_class(spa));
158b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
159379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
160379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
161379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
162379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
163379c004Eric Schrock		    size - used, src);
164379c004Eric Schrock
165379c004Eric Schrock		cap = (size == 0) ? 0 : (used * 100 / size);
166379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
167379c004Eric Schrock
168b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
169b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
170b24ab67Jeff Bonwick
171379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
172379c004Eric Schrock		    spa->spa_root_vdev->vdev_state, src);
173379c004Eric Schrock
174379c004Eric Schrock		version = spa_version(spa);
175379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
176379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
177379c004Eric Schrock		else
178379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
179379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
180379c004Eric Schrock	}
181990b485lling
1829d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
183990b485lling
1849d82f4flling	if (spa->spa_root != NULL)
1859d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1869d82f4flling		    0, ZPROP_SRC_LOCAL);
187990b485lling
188c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
189c5904d1eschrock		if (dp->scd_path == NULL) {
1909d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
191c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
192c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1939d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
194c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1952f8aaabeschrock		}
1962f8aaabeschrock	}
197990b485lling}
198990b485lling
199990b485lling/*
200990b485lling * Get zpool property values.
201990b485lling */
202990b485llingint
203990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
204990b485lling{
205b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
206990b485lling	zap_cursor_t zc;
207990b485lling	zap_attribute_t za;
208990b485lling	int err;
209990b485lling
2109d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
211990b485lling
212e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
213e14bb32Jeff Bonwick
214990b485lling	/*
215990b485lling	 * Get properties from the spa config.
216990b485lling	 */
2179d82f4flling	spa_prop_get_config(spa, nvp);
218990b485lling
219990b485lling	/* If no pool property object, no more prop to get. */
220990b485lling	if (spa->spa_pool_props_object == 0) {
221990b485lling		mutex_exit(&spa->spa_props_lock);
222990b485lling		return (0);
223990b485lling	}
224990b485lling
225990b485lling	/*
226990b485lling	 * Get properties from the MOS pool property object.
227990b485lling	 */
228990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
229990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
230990b485lling	    zap_cursor_advance(&zc)) {
231990b485lling		uint64_t intval = 0;
232990b485lling		char *strval = NULL;
233990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
234990b485lling		zpool_prop_t prop;
235990b485lling
236990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
237990b485lling			continue;
238990b485lling
239990b485lling		switch (za.za_integer_length) {
240990b485lling		case 8:
241990b485lling			/* integer property */
242990b485lling			if (za.za_first_integer !=
243990b485lling			    zpool_prop_default_numeric(prop))
244990b485lling				src = ZPROP_SRC_LOCAL;
245990b485lling
246990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
247990b485lling				dsl_pool_t *dp;
248990b485lling				dsl_dataset_t *ds = NULL;
249990b485lling
250990b485lling				dp = spa_get_dsl(spa);
251990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
252745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
253745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
254990b485lling					rw_exit(&dp->dp_config_rwlock);
255990b485lling					break;
256990b485lling				}
257990b485lling
258990b485lling				strval = kmem_alloc(
259990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
260990b485lling				    KM_SLEEP);
261990b485lling				dsl_dataset_name(ds, strval);
262745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
263990b485lling				rw_exit(&dp->dp_config_rwlock);
264990b485lling			} else {
265990b485lling				strval = NULL;
266990b485lling				intval = za.za_first_integer;
267990b485lling			}
268990b485lling
2699d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
270990b485lling
271990b485lling			if (strval != NULL)
272990b485lling				kmem_free(strval,
273990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
274990b485lling
275990b485lling			break;
276990b485lling
277990b485lling		case 1:
278990b485lling			/* string property */
279990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
280990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
281990b485lling			    za.za_name, 1, za.za_num_integers, strval);
282990b485lling			if (err) {
283990b485lling				kmem_free(strval, za.za_num_integers);
284990b485lling				break;
285990b485lling			}
2869d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
287990b485lling			kmem_free(strval, za.za_num_integers);
288990b485lling			break;
289990b485lling
290990b485lling		default:
291990b485lling			break;
292990b485lling		}
293990b485lling	}
294990b485lling	zap_cursor_fini(&zc);
295990b485lling	mutex_exit(&spa->spa_props_lock);
296990b485llingout:
297990b485lling	if (err && err != ENOENT) {
298990b485lling		nvlist_free(*nvp);
2999d82f4flling		*nvp = NULL;
300990b485lling		return (err);
301990b485lling	}
302990b485lling
303990b485lling	return (0);
304990b485lling}
305990b485lling
306990b485lling/*
307990b485lling * Validate the given pool properties nvlist and modify the list
308990b485lling * for the property values to be set.
309990b485lling */
310990b485llingstatic int
311990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
312990b485lling{
313990b485lling	nvpair_t *elem;
314990b485lling	int error = 0, reset_bootfs = 0;
315990b485lling	uint64_t objnum;
316990b485lling
317990b485lling	elem = NULL;
318990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
319990b485lling		zpool_prop_t prop;
320990b485lling		char *propname, *strval;
321990b485lling		uint64_t intval;
322990b485lling		objset_t *os;
3232f8aaabeschrock		char *slash;
324990b485lling
325990b485lling		propname = nvpair_name(elem);
326990b485lling
327990b485lling		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
328990b485lling			return (EINVAL);
329990b485lling
330990b485lling		switch (prop) {
331990b485lling		case ZPOOL_PROP_VERSION:
332990b485lling			error = nvpair_value_uint64(elem, &intval);
333990b485lling			if (!error &&
334990b485lling			    (intval < spa_version(spa) || intval > SPA_VERSION))
335990b485lling				error = EINVAL;
336990b485lling			break;
337990b485lling
338990b485lling		case ZPOOL_PROP_DELEGATION:
339990b485lling		case ZPOOL_PROP_AUTOREPLACE:
340d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
341573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
342990b485lling			error = nvpair_value_uint64(elem, &intval);
343990b485lling			if (!error && intval > 1)
344990b485lling				error = EINVAL;
345990b485lling			break;
346990b485lling
347990b485lling		case ZPOOL_PROP_BOOTFS:
34825f89eeJeff Bonwick			/*
34925f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
35025f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
35125f89eeJeff Bonwick			 * the bootfs property cannot be set.
35225f89eeJeff Bonwick			 */
353990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
354990b485lling				error = ENOTSUP;
355990b485lling				break;
356990b485lling			}
357990b485lling
358990b485lling			/*
35915e6edfgw			 * Make sure the vdev config is bootable
360990b485lling			 */
36115e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
362990b485lling				error = ENOTSUP;
363990b485lling				break;
364990b485lling			}
365990b485lling
366990b485lling			reset_bootfs = 1;
367990b485lling
368990b485lling			error = nvpair_value_string(elem, &strval);
369990b485lling
370990b485lling			if (!error) {
37115e6edfgw				uint64_t compress;
37215e6edfgw
373990b485lling				if (strval == NULL || strval[0] == '\0') {
374990b485lling					objnum = zpool_prop_default_numeric(
375990b485lling					    ZPOOL_PROP_BOOTFS);
376990b485lling					break;
377990b485lling				}
378990b485lling
379503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
380990b485lling					break;
38115e6edfgw
382503ad85Matthew Ahrens				/* Must be ZPL and not gzip compressed. */
383503ad85Matthew Ahrens
384503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
385503ad85Matthew Ahrens					error = ENOTSUP;
386503ad85Matthew Ahrens				} else if ((error = dsl_prop_get_integer(strval,
38715e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
38815e6edfgw				    &compress, NULL)) == 0 &&
38915e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
39015e6edfgw					error = ENOTSUP;
39115e6edfgw				} else {
39215e6edfgw					objnum = dmu_objset_id(os);
39315e6edfgw				}
394503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
395990b485lling			}
396990b485lling			break;
397e14bb32Jeff Bonwick
3980a4e951gw		case ZPOOL_PROP_FAILUREMODE:
3990a4e951gw			error = nvpair_value_uint64(elem, &intval);
4000a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
4010a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
4020a4e951gw				error = EINVAL;
4030a4e951gw
4040a4e951gw			/*
4050a4e951gw			 * This is a special case which only occurs when
4060a4e951gw			 * the pool has completely failed. This allows
4070a4e951gw			 * the user to change the in-core failmode property
4080a4e951gw			 * without syncing it out to disk (I/Os might
4090a4e951gw			 * currently be blocked). We do this by returning
4100a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
4110a4e951gw			 * into thinking we encountered a property validation
4120a4e951gw			 * error.
4130a4e951gw			 */
414e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
4150a4e951gw				spa->spa_failmode = intval;
4160a4e951gw				error = EIO;
4170a4e951gw			}
4180a4e951gw			break;
4192f8aaabeschrock
4202f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
4212f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
4222f8aaabeschrock				break;
4232f8aaabeschrock
4242f8aaabeschrock			if (strval[0] == '\0')
4252f8aaabeschrock				break;
4262f8aaabeschrock
4272f8aaabeschrock			if (strcmp(strval, "none") == 0)
4282f8aaabeschrock				break;
4292f8aaabeschrock
4302f8aaabeschrock			if (strval[0] != '/') {
4312f8aaabeschrock				error = EINVAL;
4322f8aaabeschrock				break;
4332f8aaabeschrock			}
4342f8aaabeschrock
4352f8aaabeschrock			slash = strrchr(strval, '/');
4362f8aaabeschrock			ASSERT(slash != NULL);
4372f8aaabeschrock
4382f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4392f8aaabeschrock			    strcmp(slash, "/..") == 0)
4402f8aaabeschrock				error = EINVAL;
4412f8aaabeschrock			break;
442b24ab67Jeff Bonwick
443b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
444b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
445b24ab67Jeff Bonwick				error = ENOTSUP;
446b24ab67Jeff Bonwick			else
447b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
448b24ab67Jeff Bonwick			if (error == 0 &&
449b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
450b24ab67Jeff Bonwick				error = EINVAL;
451b24ab67Jeff Bonwick			break;
452990b485lling		}
453990b485lling
454990b485lling		if (error)
455990b485lling			break;
456990b485lling	}
457990b485lling
458990b485lling	if (!error && reset_bootfs) {
459990b485lling		error = nvlist_remove(props,
460990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
461990b485lling
462990b485lling		if (!error) {
463990b485lling			error = nvlist_add_uint64(props,
464990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
465990b485lling		}
466990b485lling	}
467990b485lling
468990b485lling	return (error);
469990b485lling}
470990b485lling
471379c004Eric Schrockvoid
472379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
473379c004Eric Schrock{
474379c004Eric Schrock	char *cachefile;
475379c004Eric Schrock	spa_config_dirent_t *dp;
476379c004Eric Schrock
477379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
478379c004Eric Schrock	    &cachefile) != 0)
479379c004Eric Schrock		return;
480379c004Eric Schrock
481379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
482379c004Eric Schrock	    KM_SLEEP);
483379c004Eric Schrock
484379c004Eric Schrock	if (cachefile[0] == '\0')
485379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
486379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
487379c004Eric Schrock		dp->scd_path = NULL;
488379c004Eric Schrock	else
489379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
490379c004Eric Schrock
491379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
492379c004Eric Schrock	if (need_sync)
493379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
494379c004Eric Schrock}
495379c004Eric Schrock
496990b485llingint
497990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
498990b485lling{
499990b485lling	int error;
500379c004Eric Schrock	nvpair_t *elem;
501379c004Eric Schrock	boolean_t need_sync = B_FALSE;
502379c004Eric Schrock	zpool_prop_t prop;
503990b485lling
504990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
505990b485lling		return (error);
506990b485lling
507379c004Eric Schrock	elem = NULL;
508379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
509379c004Eric Schrock		if ((prop = zpool_name_to_prop(
510379c004Eric Schrock		    nvpair_name(elem))) == ZPROP_INVAL)
511379c004Eric Schrock			return (EINVAL);
512379c004Eric Schrock
513379c004Eric Schrock		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
514379c004Eric Schrock			continue;
515379c004Eric Schrock
516379c004Eric Schrock		need_sync = B_TRUE;
517379c004Eric Schrock		break;
518379c004Eric Schrock	}
519379c004Eric Schrock
520379c004Eric Schrock	if (need_sync)
521379c004Eric Schrock		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
522379c004Eric Schrock		    spa, nvp, 3));
523379c004Eric Schrock	else
524379c004Eric Schrock		return (0);
525990b485lling}
526990b485lling
527990b485lling/*
528990b485lling * If the bootfs property value is dsobj, clear it.
529990b485lling */
530990b485llingvoid
531990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
532990b485lling{
533990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
534990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
535990b485lling		    spa->spa_pool_props_object,
536990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
537990b485lling		spa->spa_bootfs = 0;
538990b485lling	}
539990b485lling}
540990b485lling
541fa9e406ahrens/*
542fa9e406ahrens * ==========================================================================
543fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
544fa9e406ahrens * ==========================================================================
545fa9e406ahrens */
546fa9e406ahrens
547ea8dc4beschrockstatic int
548ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
549ea8dc4beschrock{
550ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
551ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
552ea8dc4beschrock	int ret;
553ea8dc4beschrock
554ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
555ea8dc4beschrock	    sizeof (zbookmark_t));
556ea8dc4beschrock
557ea8dc4beschrock	if (ret < 0)
558ea8dc4beschrock		return (-1);
559ea8dc4beschrock	else if (ret > 0)
560ea8dc4beschrock		return (1);
561ea8dc4beschrock	else
562ea8dc4beschrock		return (0);
563ea8dc4beschrock}
564ea8dc4beschrock
565ea8dc4beschrock/*
566ea8dc4beschrock * Utility function which retrieves copies of the current logs and
567ea8dc4beschrock * re-initializes them in the process.
568ea8dc4beschrock */
569ea8dc4beschrockvoid
570ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
571ea8dc4beschrock{
572ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
573ea8dc4beschrock
574ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
575ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
576ea8dc4beschrock
577ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
578ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
579ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
580ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
581ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
582ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
583ea8dc4beschrock}
584ea8dc4beschrock
585fa9e406ahrens/*
586fa9e406ahrens * Activate an uninitialized pool.
587fa9e406ahrens */
588fa9e406ahrensstatic void
5898ad4d6dJeff Bonwickspa_activate(spa_t *spa, int mode)
590fa9e406ahrens{
591fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
592fa9e406ahrens
593fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
5948ad4d6dJeff Bonwick	spa->spa_mode = mode;
595fa9e406ahrens
59688ecc94George Wilson	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
59788ecc94George Wilson	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
598fa9e406ahrens
599e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
6002e0c549Jonathan Adams		const zio_taskq_info_t *ztip = &zio_taskqs[t];
601e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
6022e0c549Jonathan Adams			enum zti_modes mode = ztip->zti_nthreads[q].zti_mode;
6032e0c549Jonathan Adams			uint_t value = ztip->zti_nthreads[q].zti_value;
6042e0c549Jonathan Adams			char name[32];
6052e0c549Jonathan Adams
6062e0c549Jonathan Adams			(void) snprintf(name, sizeof (name),
6072e0c549Jonathan Adams			    "%s_%s", ztip->zti_name, zio_taskq_types[q]);
6082e0c549Jonathan Adams
6092e0c549Jonathan Adams			if (mode == zti_mode_tune) {
6102e0c549Jonathan Adams				mode = zio_taskq_tune_mode;
6112e0c549Jonathan Adams				value = zio_taskq_tune_value;
6122e0c549Jonathan Adams				if (mode == zti_mode_tune)
6132e0c549Jonathan Adams					mode = zti_mode_online_percent;
6142e0c549Jonathan Adams			}
6152e0c549Jonathan Adams
6162e0c549Jonathan Adams			switch (mode) {
6172e0c549Jonathan Adams			case zti_mode_fixed:
6182e0c549Jonathan Adams				ASSERT3U(value, >=, 1);
6192e0c549Jonathan Adams				value = MAX(value, 1);
6202e0c549Jonathan Adams
6212e0c549Jonathan Adams				spa->spa_zio_taskq[t][q] = taskq_create(name,
6222e0c549Jonathan Adams				    value, maxclsyspri, 50, INT_MAX,
6232e0c549Jonathan Adams				    TASKQ_PREPOPULATE);
6242e0c549Jonathan Adams				break;
6252e0c549Jonathan Adams
6262e0c549Jonathan Adams			case zti_mode_online_percent:
6272e0c549Jonathan Adams				spa->spa_zio_taskq[t][q] = taskq_create(name,
6282e0c549Jonathan Adams				    value, maxclsyspri, 50, INT_MAX,
6292e0c549Jonathan Adams				    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
6302e0c549Jonathan Adams				break;
6312e0c549Jonathan Adams
6322e0c549Jonathan Adams			case zti_mode_tune:
6332e0c549Jonathan Adams			default:
6342e0c549Jonathan Adams				panic("unrecognized mode for "
6352e0c549Jonathan Adams				    "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) "
6362e0c549Jonathan Adams				    "in spa_activate()",
6372e0c549Jonathan Adams				    t, q, mode, value);
6382e0c549Jonathan Adams				break;
6392e0c549Jonathan Adams			}
640e14bb32Jeff Bonwick		}
641fa9e406ahrens	}
642fa9e406ahrens
643e14bb32Jeff Bonwick	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
644e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_config_dirty_node));
645e14bb32Jeff Bonwick	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
646e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_state_dirty_node));
647fa9e406ahrens
648fa9e406ahrens	txg_list_create(&spa->spa_vdev_txg_list,
649fa9e406ahrens	    offsetof(struct vdev, vdev_txg_node));
650ea8dc4beschrock
651ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
652ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
653ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
654ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
655ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
656ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
657fa9e406ahrens}
658fa9e406ahrens
659fa9e406ahrens/*
660fa9e406ahrens * Opposite of spa_activate().
661fa9e406ahrens */
662fa9e406ahrensstatic void
663fa9e406ahrensspa_deactivate(spa_t *spa)
664fa9e406ahrens{
665fa9e406ahrens	ASSERT(spa->spa_sync_on == B_FALSE);
666fa9e406ahrens	ASSERT(spa->spa_dsl_pool == NULL);
667fa9e406ahrens	ASSERT(spa->spa_root_vdev == NULL);
66825f89eeJeff Bonwick	ASSERT(spa->spa_async_zio_root == NULL);
669fa9e406ahrens	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
670fa9e406ahrens
671fa9e406ahrens	txg_list_destroy(&spa->spa_vdev_txg_list);
672fa9e406ahrens
673e14bb32Jeff Bonwick	list_destroy(&spa->spa_config_dirty_list);
674e14bb32Jeff Bonwick	list_destroy(&spa->spa_state_dirty_list);
675fa9e406ahrens
676e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
677e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
678e14bb32Jeff Bonwick			taskq_destroy(spa->spa_zio_taskq[t][q]);
679e14bb32Jeff Bonwick			spa->spa_zio_taskq[t][q] = NULL;
680e14bb32Jeff Bonwick		}
681fa9e406ahrens	}
682fa9e406ahrens
683fa9e406ahrens	metaslab_class_destroy(spa->spa_normal_class);
684fa9e406ahrens	spa->spa_normal_class = NULL;
685fa9e406ahrens
6868654d02perrin	metaslab_class_destroy(spa->spa_log_class);
6878654d02perrin	spa->spa_log_class = NULL;
6888654d02perrin
689