spa.c revision dedec472759b1a1a25044d504201ef59ccbffb56
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>
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) {
157379c004Eric Schrock		size = spa_get_space(spa);
158379c004Eric Schrock		used = spa_get_alloc(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
168379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
169379c004Eric Schrock		    spa->spa_root_vdev->vdev_state, src);
170379c004Eric Schrock
171379c004Eric Schrock		version = spa_version(spa);
172379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
173379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
174379c004Eric Schrock		else
175379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
176379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
177379c004Eric Schrock	}
178990b485lling
1799d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
180990b485lling
1819d82f4flling	if (spa->spa_root != NULL)
1829d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
1839d82f4flling		    0, ZPROP_SRC_LOCAL);
184990b485lling
185c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
186c5904d1eschrock		if (dp->scd_path == NULL) {
1879d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
188c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
189c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
1909d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
191c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
1922f8aaabeschrock		}
1932f8aaabeschrock	}
194990b485lling}
195990b485lling
196990b485lling/*
197990b485lling * Get zpool property values.
198990b485lling */
199990b485llingint
200990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
201990b485lling{
202990b485lling	zap_cursor_t zc;
203990b485lling	zap_attribute_t za;
204990b485lling	objset_t *mos = spa->spa_meta_objset;
205990b485lling	int err;
206990b485lling
2079d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
208990b485lling
209e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
210e14bb32Jeff Bonwick
211990b485lling	/*
212990b485lling	 * Get properties from the spa config.
213990b485lling	 */
2149d82f4flling	spa_prop_get_config(spa, nvp);
215990b485lling
216990b485lling	/* If no pool property object, no more prop to get. */
217990b485lling	if (spa->spa_pool_props_object == 0) {
218990b485lling		mutex_exit(&spa->spa_props_lock);
219990b485lling		return (0);
220990b485lling	}
221990b485lling
222990b485lling	/*
223990b485lling	 * Get properties from the MOS pool property object.
224990b485lling	 */
225990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
226990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
227990b485lling	    zap_cursor_advance(&zc)) {
228990b485lling		uint64_t intval = 0;
229990b485lling		char *strval = NULL;
230990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
231990b485lling		zpool_prop_t prop;
232990b485lling
233990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
234990b485lling			continue;
235990b485lling
236990b485lling		switch (za.za_integer_length) {
237990b485lling		case 8:
238990b485lling			/* integer property */
239990b485lling			if (za.za_first_integer !=
240990b485lling			    zpool_prop_default_numeric(prop))
241990b485lling				src = ZPROP_SRC_LOCAL;
242990b485lling
243990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
244990b485lling				dsl_pool_t *dp;
245990b485lling				dsl_dataset_t *ds = NULL;
246990b485lling
247990b485lling				dp = spa_get_dsl(spa);
248990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
249745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
250745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
251990b485lling					rw_exit(&dp->dp_config_rwlock);
252990b485lling					break;
253990b485lling				}
254990b485lling
255990b485lling				strval = kmem_alloc(
256990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
257990b485lling				    KM_SLEEP);
258990b485lling				dsl_dataset_name(ds, strval);
259745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
260990b485lling				rw_exit(&dp->dp_config_rwlock);
261990b485lling			} else {
262990b485lling				strval = NULL;
263990b485lling				intval = za.za_first_integer;
264990b485lling			}
265990b485lling
2669d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
267990b485lling
268990b485lling			if (strval != NULL)
269990b485lling				kmem_free(strval,
270990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
271990b485lling
272990b485lling			break;
273990b485lling
274990b485lling		case 1:
275990b485lling			/* string property */
276990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
277990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
278990b485lling			    za.za_name, 1, za.za_num_integers, strval);
279990b485lling			if (err) {
280990b485lling				kmem_free(strval, za.za_num_integers);
281990b485lling				break;
282990b485lling			}
2839d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
284990b485lling			kmem_free(strval, za.za_num_integers);
285990b485lling			break;
286990b485lling
287990b485lling		default:
288990b485lling			break;
289990b485lling		}
290990b485lling	}
291990b485lling	zap_cursor_fini(&zc);
292990b485lling	mutex_exit(&spa->spa_props_lock);
293990b485llingout:
294990b485lling	if (err && err != ENOENT) {
295990b485lling		nvlist_free(*nvp);
2969d82f4flling		*nvp = NULL;
297990b485lling		return (err);
298990b485lling	}
299990b485lling
300990b485lling	return (0);
301990b485lling}
302990b485lling
303990b485lling/*
304990b485lling * Validate the given pool properties nvlist and modify the list
305990b485lling * for the property values to be set.
306990b485lling */
307990b485llingstatic int
308990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
309990b485lling{
310990b485lling	nvpair_t *elem;
311990b485lling	int error = 0, reset_bootfs = 0;
312990b485lling	uint64_t objnum;
313990b485lling
314990b485lling	elem = NULL;
315990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
316990b485lling		zpool_prop_t prop;
317990b485lling		char *propname, *strval;
318990b485lling		uint64_t intval;
319990b485lling		objset_t *os;
3202f8aaabeschrock		char *slash;
321990b485lling
322990b485lling		propname = nvpair_name(elem);
323990b485lling
324990b485lling		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
325990b485lling			return (EINVAL);
326990b485lling
327990b485lling		switch (prop) {
328990b485lling		case ZPOOL_PROP_VERSION:
329990b485lling			error = nvpair_value_uint64(elem, &intval);
330990b485lling			if (!error &&
331990b485lling			    (intval < spa_version(spa) || intval > SPA_VERSION))
332990b485lling				error = EINVAL;
333990b485lling			break;
334990b485lling
335990b485lling		case ZPOOL_PROP_DELEGATION:
336990b485lling		case ZPOOL_PROP_AUTOREPLACE:
337d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
338573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
339990b485lling			error = nvpair_value_uint64(elem, &intval);
340990b485lling			if (!error && intval > 1)
341990b485lling				error = EINVAL;
342990b485lling			break;
343990b485lling
344990b485lling		case ZPOOL_PROP_BOOTFS:
34525f89eeJeff Bonwick			/*
34625f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
34725f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
34825f89eeJeff Bonwick			 * the bootfs property cannot be set.
34925f89eeJeff Bonwick			 */
350990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
351990b485lling				error = ENOTSUP;
352990b485lling				break;
353990b485lling			}
354990b485lling
355990b485lling			/*
35615e6edfgw			 * Make sure the vdev config is bootable
357990b485lling			 */
35815e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
359990b485lling				error = ENOTSUP;
360990b485lling				break;
361990b485lling			}
362990b485lling
363990b485lling			reset_bootfs = 1;
364990b485lling
365990b485lling			error = nvpair_value_string(elem, &strval);
366990b485lling
367990b485lling			if (!error) {
36815e6edfgw				uint64_t compress;
36915e6edfgw
370990b485lling				if (strval == NULL || strval[0] == '\0') {
371990b485lling					objnum = zpool_prop_default_numeric(
372990b485lling					    ZPOOL_PROP_BOOTFS);
373990b485lling					break;
374990b485lling				}
375990b485lling
376503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
377990b485lling					break;
37815e6edfgw
379503ad85Matthew Ahrens				/* Must be ZPL and not gzip compressed. */
380503ad85Matthew Ahrens
381503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
382503ad85Matthew Ahrens					error = ENOTSUP;
383503ad85Matthew Ahrens				} else if ((error = dsl_prop_get_integer(strval,
38415e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
38515e6edfgw				    &compress, NULL)) == 0 &&
38615e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
38715e6edfgw					error = ENOTSUP;
38815e6edfgw				} else {
38915e6edfgw					objnum = dmu_objset_id(os);
39015e6edfgw				}
391503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
392990b485lling			}
393990b485lling			break;
394e14bb32Jeff Bonwick
3950a4e951gw		case ZPOOL_PROP_FAILUREMODE:
3960a4e951gw			error = nvpair_value_uint64(elem, &intval);
3970a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
3980a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
3990a4e951gw				error = EINVAL;
4000a4e951gw
4010a4e951gw			/*
4020a4e951gw			 * This is a special case which only occurs when
4030a4e951gw			 * the pool has completely failed. This allows
4040a4e951gw			 * the user to change the in-core failmode property
4050a4e951gw			 * without syncing it out to disk (I/Os might
4060a4e951gw			 * currently be blocked). We do this by returning
4070a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
4080a4e951gw			 * into thinking we encountered a property validation
4090a4e951gw			 * error.
4100a4e951gw			 */
411e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
4120a4e951gw				spa->spa_failmode = intval;
4130a4e951gw				error = EIO;
4140a4e951gw			}
4150a4e951gw			break;
4162f8aaabeschrock
4172f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
4182f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
4192f8aaabeschrock				break;
4202f8aaabeschrock
4212f8aaabeschrock			if (strval[0] == '\0')
4222f8aaabeschrock				break;
4232f8aaabeschrock
4242f8aaabeschrock			if (strcmp(strval, "none") == 0)
4252f8aaabeschrock				break;
4262f8aaabeschrock
4272f8aaabeschrock			if (strval[0] != '/') {
4282f8aaabeschrock				error = EINVAL;
4292f8aaabeschrock				break;
4302f8aaabeschrock			}
4312f8aaabeschrock
4322f8aaabeschrock			slash = strrchr(strval, '/');
4332f8aaabeschrock			ASSERT(slash != NULL);
4342f8aaabeschrock
4352f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4362f8aaabeschrock			    strcmp(slash, "/..") == 0)
4372f8aaabeschrock				error = EINVAL;
4382f8aaabeschrock			break;
439990b485lling		}
440990b485lling
441990b485lling		if (error)
442990b485lling			break;
443990b485lling	}
444990b485lling
445990b485lling	if (!error && reset_bootfs) {
446990b485lling		error = nvlist_remove(props,
447990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
448990b485lling
449990b485lling		if (!error) {
450990b485lling			error = nvlist_add_uint64(props,
451990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
452990b485lling		}
453990b485lling	}
454990b485lling
455990b485lling	return (error);
456990b485lling}
457990b485lling
458379c004Eric Schrockvoid
459379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
460379c004Eric Schrock{
461379c004Eric Schrock	char *cachefile;
462379c004Eric Schrock	spa_config_dirent_t *dp;
463379c004Eric Schrock
464379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
465379c004Eric Schrock	    &cachefile) != 0)
466379c004Eric Schrock		return;
467379c004Eric Schrock
468379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
469379c004Eric Schrock	    KM_SLEEP);
470379c004Eric Schrock
471379c004Eric Schrock	if (cachefile[0] == '\0')
472379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
473379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
474379c004Eric Schrock		dp->scd_path = NULL;
475379c004Eric Schrock	else
476379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
477379c004Eric Schrock
478379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
479379c004Eric Schrock	if (need_sync)
480379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
481379c004Eric Schrock}
482379c004Eric Schrock
483990b485llingint
484990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
485990b485lling{
486990b485lling	int error;
487379c004Eric Schrock	nvpair_t *elem;
488379c004Eric Schrock	boolean_t need_sync = B_FALSE;
489379c004Eric Schrock	zpool_prop_t prop;
490990b485lling
491990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
492990b485lling		return (error);
493990b485lling
494379c004Eric Schrock	elem = NULL;
495379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
496379c004Eric Schrock		if ((prop = zpool_name_to_prop(
497379c004Eric Schrock		    nvpair_name(elem))) == ZPROP_INVAL)
498379c004Eric Schrock			return (EINVAL);
499379c004Eric Schrock
500379c004Eric Schrock		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
501379c004Eric Schrock			continue;
502379c004Eric Schrock
503379c004Eric Schrock		need_sync = B_TRUE;
504379c004Eric Schrock		break;
505379c004Eric Schrock	}
506379c004Eric Schrock
507379c004Eric Schrock	if (need_sync)
508379c004Eric Schrock		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
509379c004Eric Schrock		    spa, nvp, 3));
510379c004Eric Schrock	else
511379c004Eric Schrock		return (0);
512990b485lling}
513990b485lling
514990b485lling/*
515990b485lling * If the bootfs property value is dsobj, clear it.
516990b485lling */
517990b485llingvoid
518990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
519990b485lling{
520990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
521990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
522990b485lling		    spa->spa_pool_props_object,
523990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
524990b485lling		spa->spa_bootfs = 0;
525990b485lling	}
526990b485lling}
527990b485lling
528fa9e406ahrens/*
529fa9e406ahrens * ==========================================================================
530fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
531fa9e406ahrens * ==========================================================================
532fa9e406ahrens */
533fa9e406ahrens
534ea8dc4beschrockstatic int
535ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
536ea8dc4beschrock{
537ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
538ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
539ea8dc4beschrock	int ret;
540ea8dc4beschrock
541ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
542ea8dc4beschrock	    sizeof (zbookmark_t));
543ea8dc4beschrock
544ea8dc4beschrock	if (ret < 0)
545ea8dc4beschrock		return (-1);
546ea8dc4beschrock	else if (ret > 0)
547ea8dc4beschrock		return (1);
548ea8dc4beschrock	else
549ea8dc4beschrock		return (0);
550ea8dc4beschrock}
551ea8dc4beschrock
552ea8dc4beschrock/*
553ea8dc4beschrock * Utility function which retrieves copies of the current logs and
554ea8dc4beschrock * re-initializes them in the process.
555ea8dc4beschrock */
556ea8dc4beschrockvoid
557ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
558ea8dc4beschrock{
559ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
560ea8dc4beschrock
561ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
562ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
563ea8dc4beschrock
564ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
565ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
566ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
567ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
568ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
569ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
570ea8dc4beschrock}
571ea8dc4beschrock
572fa9e406ahrens/*
573fa9e406ahrens * Activate an uninitialized pool.
574fa9e406ahrens */
575fa9e406ahrensstatic void
5768ad4d6dJeff Bonwickspa_activate(spa_t *spa, int mode)
577fa9e406ahrens{
578fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
579fa9e406ahrens
580fa9e406ahrens	spa->spa_state = POOL_STATE_ACTIVE;
5818ad4d6dJeff Bonwick	spa->spa_mode = mode;
582fa9e406ahrens
58388ecc94George Wilson	spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
58488ecc94George Wilson	spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
585fa9e406ahrens
586e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
5872e0c549Jonathan Adams		const zio_taskq_info_t *ztip = &zio_taskqs[t];
588e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
5892e0c549Jonathan Adams			enum zti_modes mode = ztip->zti_nthreads[q].zti_mode;
5902e0c549Jonathan Adams			uint_t value = ztip->zti_nthreads[q].zti_value;
5912e0c549Jonathan Adams			char name[32];
5922e0c549Jonathan Adams
5932e0c549Jonathan Adams			(void) snprintf(name, sizeof (name),
5942e0c549Jonathan Adams			    "%s_%s", ztip->zti_name, zio_taskq_types[q]);
5952e0c549Jonathan Adams
5962e0c549Jonathan Adams			if (mode == zti_mode_tune) {
5972e0c549Jonathan Adams				mode = zio_taskq_tune_mode;
5982e0c549Jonathan Adams				value = zio_taskq_tune_value;
5992e0c549Jonathan Adams				if (mode == zti_mode_tune)
6002e0c549Jonathan Adams					mode = zti_mode_online_percent;
6012e0c549Jonathan Adams			}
6022e0c549Jonathan Adams
6032e0c549Jonathan Adams			switch (mode) {
6042e0c549Jonathan Adams			case zti_mode_fixed:
6052e0c549Jonathan Adams				ASSERT3U(value, >=, 1);
6062e0c549Jonathan Adams				value = MAX(value, 1);
6072e0c549Jonathan Adams
6082e0c549Jonathan Adams				spa->spa_zio_taskq[t][q] = taskq_create(name,
6092e0c549Jonathan Adams				    value, maxclsyspri, 50, INT_MAX,
6102e0c549Jonathan Adams				    TASKQ_PREPOPULATE);
6112e0c549Jonathan Adams				break;
6122e0c549Jonathan Adams
6132e0c549Jonathan Adams			case zti_mode_online_percent:
6142e0c549Jonathan Adams				spa->spa_zio_taskq[t][q] = taskq_create(name,
6152e0c549Jonathan Adams				    value, maxclsyspri, 50, INT_MAX,
6162e0c549Jonathan Adams				    TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT);
6172e0c549Jonathan Adams				break;
6182e0c549Jonathan Adams
6192e0c549Jonathan Adams			case zti_mode_tune:
6202e0c549Jonathan Adams			default:
6212e0c549Jonathan Adams				panic("unrecognized mode for "
6222e0c549Jonathan Adams				    "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) "
6232e0c549Jonathan Adams				    "in spa_activate()",
6242e0c549Jonathan Adams				    t, q, mode, value);
6252e0c549Jonathan Adams				break;
6262e0c549Jonathan Adams			}
627e14bb32Jeff Bonwick		}
628fa9e406ahrens	}
629fa9e406ahrens
630e14bb32Jeff Bonwick	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
631e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_config_dirty_node));
632e14bb32Jeff Bonwick	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
633e14bb32Jeff Bonwick	    offsetof(vdev_t, vdev_state_dirty_node));
634fa9e406ahrens
635fa9e406ahrens	txg_list_create(&spa->spa_vdev_txg_list,
636fa9e406ahrens	    offsetof(struct vdev, vdev_txg_node));
637ea8dc4beschrock
638ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
639ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
640ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
641ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
642ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
643ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
644fa9e406ahrens}
645fa9e406ahrens
646fa9e406ahrens/*
647fa9e406ahrens * Opposite of spa_activate().
648fa9e406ahrens */
649fa9e406ahrensstatic void
650fa9e406ahrensspa_deactivate(spa_t *spa)
651fa9e406ahrens{
652fa9e406ahrens	ASSERT(spa->spa_sync_on == B_FALSE);
653fa9e406ahrens	ASSERT(spa->spa_dsl_pool == NULL);
654fa9e406ahrens	ASSERT(spa->spa_root_vdev == NULL);
65525f89eeJeff Bonwick	ASSERT(spa->spa_async_zio_root == NULL);
656fa9e406ahrens	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
657fa9e406ahrens
658fa9e406ahrens	txg_list_destroy(&spa->spa_vdev_txg_list);
659fa9e406ahrens
660e14bb32Jeff Bonwick	list_destroy(&spa->spa_config_dirty_list);
661e14bb32Jeff Bonwick	list_destroy(&spa->spa_state_dirty_list);
662fa9e406ahrens
663e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
664e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
665e14bb32Jeff Bonwick			taskq_destroy(spa->spa_zio_taskq[t][q]);
666e14bb32Jeff Bonwick			spa->spa_zio_taskq[t][q] = NULL;
667e14bb32Jeff Bonwick		}
668fa9e406ahrens	}
669fa9e406ahrens
670fa9e406ahrens	metaslab_class_destroy(spa->spa_normal_class);
671fa9e406ahrens	spa->spa_normal_class = NULL;
672fa9e406ahrens
6738654d02perrin	metaslab_class_destroy(spa->spa_log_class);
6748654d02perrin	spa->spa_log_class = NULL;
6758654d02perrin
676ea8dc4beschrock	/*
677ea8dc4beschrock	 * If this was part of an import or the open otherwise failed, we may
678