spa.c revision 5aeb94743e3be0c51e86f73096334611ae3a058e
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
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 */
2398d1cbfGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24fa9e406ahrens */
255aeb947Garrett D'Amore/*
265aeb947Garrett D'Amore * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
275aeb947Garrett D'Amore */
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 */
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/dmu.h>
41fa9e406ahrens#include <sys/dmu_tx.h>
42fa9e406ahrens#include <sys/zap.h>
43fa9e406ahrens#include <sys/zil.h>
44b24ab67Jeff Bonwick#include <sys/ddt.h>
45fa9e406ahrens#include <sys/vdev_impl.h>
46fa9e406ahrens#include <sys/metaslab.h>
4788ecc94George Wilson#include <sys/metaslab_impl.h>
48fa9e406ahrens#include <sys/uberblock_impl.h>
49fa9e406ahrens#include <sys/txg.h>
50fa9e406ahrens#include <sys/avl.h>
51fa9e406ahrens#include <sys/dmu_traverse.h>
52b1b8ab3lling#include <sys/dmu_objset.h>
53fa9e406ahrens#include <sys/unique.h>
54fa9e406ahrens#include <sys/dsl_pool.h>
55b1b8ab3lling#include <sys/dsl_dataset.h>
56fa9e406ahrens#include <sys/dsl_dir.h>
57fa9e406ahrens#include <sys/dsl_prop.h>
58b1b8ab3lling#include <sys/dsl_synctask.h>
59fa9e406ahrens#include <sys/fs/zfs.h>
60fa94a07brendan#include <sys/arc.h>
61fa9e406ahrens#include <sys/callb.h>
629517395ek#include <sys/systeminfo.h>
63e7cbe64gw#include <sys/spa_boot.h>
64573ca77George Wilson#include <sys/zfs_ioctl.h>
653f9d6adLin Ling#include <sys/dsl_scan.h>
675679c89jv#ifdef	_KERNEL
68dedec47Jack Meng#include <sys/bootprops.h>
6935a5a35Jonathan Adams#include <sys/callb.h>
7035a5a35Jonathan Adams#include <sys/cpupart.h>
7135a5a35Jonathan Adams#include <sys/pool.h>
7235a5a35Jonathan Adams#include <sys/sysdc.h>
7335a5a35Jonathan Adams#include <sys/zone.h>
745679c89jv#endif	/* _KERNEL */
76990b485lling#include "zfs_prop.h"
77b7b9745perrin#include "zfs_comutil.h"
7935a5a35Jonathan Adamstypedef enum zti_modes {
802e0c549Jonathan Adams	zti_mode_fixed,			/* value is # of threads (min 1) */
812e0c549Jonathan Adams	zti_mode_online_percent,	/* value is % of online CPUs */
8235a5a35Jonathan Adams	zti_mode_batch,			/* cpu-intensive; value is ignored */
8380eb36fGeorge Wilson	zti_mode_null,			/* don't create a taskq */
842e0c549Jonathan Adams	zti_nmodes
8535a5a35Jonathan Adams} zti_modes_t;
8780eb36fGeorge Wilson#define	ZTI_FIX(n)	{ zti_mode_fixed, (n) }
8880eb36fGeorge Wilson#define	ZTI_PCT(n)	{ zti_mode_online_percent, (n) }
8935a5a35Jonathan Adams#define	ZTI_BATCH	{ zti_mode_batch, 0 }
9080eb36fGeorge Wilson#define	ZTI_NULL	{ zti_mode_null, 0 }
912e0c549Jonathan Adams
9280eb36fGeorge Wilson#define	ZTI_ONE		ZTI_FIX(1)
932e0c549Jonathan Adams
942e0c549Jonathan Adamstypedef struct zio_taskq_info {
9580eb36fGeorge Wilson	enum zti_modes zti_mode;
9680eb36fGeorge Wilson	uint_t zti_value;
972e0c549Jonathan Adams} zio_taskq_info_t;
982e0c549Jonathan Adams
992e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
10035a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
1012e0c549Jonathan Adams};
1022e0c549Jonathan Adams
10380eb36fGeorge Wilson/*
10480eb36fGeorge Wilson * Define the taskq threads for the following I/O types:
10580eb36fGeorge Wilson * 	NULL, READ, WRITE, FREE, CLAIM, and IOCTL
10680eb36fGeorge Wilson */
10780eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
10880eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
10980eb36fGeorge Wilson	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11035a5a35Jonathan Adams	{ ZTI_FIX(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL },
11135a5a35Jonathan Adams	{ ZTI_BATCH,	ZTI_FIX(5),	ZTI_FIX(8),	ZTI_FIX(5) },
112c7cd242George Wilson	{ ZTI_FIX(100),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11380eb36fGeorge Wilson	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11480eb36fGeorge Wilson	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
1152e0c549Jonathan Adams};
1162e0c549Jonathan Adams
1173f9d6adLin Lingstatic dsl_syncfunc_t spa_sync_props;
11889a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1191195e68Mark J Musantestatic int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1201195e68Mark J Musante    spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1211195e68Mark J Musante    char **ereport);
122cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
12435a5a35Jonathan Adamsuint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
12535a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
12635a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
12735a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
12835a5a35Jonathan Adams
12935a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
13035a5a35Jonathan Adams
13135a5a35Jonathan Adams/*
13235a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
13335a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
13435a5a35Jonathan Adams */
13535a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
13635a5a35Jonathan Adams
138990b485lling * ==========================================================================
139990b485lling * SPA properties routines
140990b485lling * ==========================================================================
141990b485lling */
144990b485lling * Add a (source=src, propname=propval) list to an nvlist.
145990b485lling */
1469d82f4fllingstatic void
147990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
148990b485lling    uint64_t intval, zprop_source_t src)
150990b485lling	const char *propname = zpool_prop_to_name(prop);
151990b485lling	nvlist_t *propval;
1539d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1549d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1569d82f4flling	if (strval != NULL)
1579d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1589d82f4flling	else
1599d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1619d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
162990b485lling	nvlist_free(propval);
166990b485lling * Get property values from the spa configuration.
167990b485lling */
1689d82f4fllingstatic void
169990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
171379c004Eric Schrock	uint64_t size;
172485bbbfGeorge Wilson	uint64_t alloc;
173990b485lling	uint64_t cap, version;
174990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
175c5904d1eschrock	spa_config_dirent_t *dp;
177e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
178e14bb32Jeff Bonwick
179379c004Eric Schrock	if (spa->spa_root_vdev != NULL) {
180485bbbfGeorge Wilson		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
181b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
182379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
183379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
184485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
185485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
186485bbbfGeorge Wilson		    size - alloc, src);
187f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
188f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
189379c004Eric Schrock
190485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
191379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
192379c004Eric Schrock
193b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
194b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
195b24ab67Jeff Bonwick
196379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
197379c004Eric Schrock		    spa->spa_root_vdev->vdev_state, src);
198379c004Eric Schrock
199379c004Eric Schrock		version = spa_version(spa);
200379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
201379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
202379c004Eric Schrock		else
203379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
204379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
205379c004Eric Schrock	}
2079d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2099d82f4flling	if (spa->spa_root != NULL)
2109d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2119d82f4flling		    0, ZPROP_SRC_LOCAL);
213c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
214c5904d1eschrock		if (dp->scd_path == NULL) {
2159d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
216c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
217c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2189d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
219c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2202f8aaabeschrock		}
2212f8aaabeschrock	}
225990b485lling * Get zpool property values.
226990b485lling */
228990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
230b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
231990b485lling	zap_cursor_t zc;
232990b485lling	zap_attribute_t za;
233990b485lling	int err;
2359d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
237e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
238e14bb32Jeff Bonwick
239990b485lling	/*
240990b485lling	 * Get properties from the spa config.
241990b485lling	 */
2429d82f4flling	spa_prop_get_config(spa, nvp);
244990b485lling	/* If no pool property object, no more prop to get. */
245afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
246990b485lling		mutex_exit(&spa->spa_props_lock);
247990b485lling		return (0);
248990b485lling	}
250990b485lling	/*
251990b485lling	 * Get properties from the MOS pool property object.
252990b485lling	 */
253990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
254990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
255990b485lling	    zap_cursor_advance(&zc)) {
256990b485lling		uint64_t intval = 0;
257990b485lling		char *strval = NULL;
258990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
259990b485lling		zpool_prop_t prop;
261990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
262990b485lling			continue;
264990b485lling		switch (za.za_integer_length) {
265990b485lling		case 8:
266990b485lling			/* integer property */
267990b485lling			if (za.za_first_integer !=
268990b485lling			    zpool_prop_default_numeric(prop))
269990b485lling				src = ZPROP_SRC_LOCAL;
271990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
272990b485lling				dsl_pool_t *dp;
273990b485lling				dsl_dataset_t *ds = NULL;
275990b485lling				dp = spa_get_dsl(spa);
276990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
277745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
278745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
279990b485lling					rw_exit(&dp->dp_config_rwlock);
280990b485lling					break;
281990b485lling				}
283990b485lling				strval = kmem_alloc(
284990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
285990b485lling				    KM_SLEEP);
286990b485lling				dsl_dataset_name(ds, strval);
287745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
288990b485lling				rw_exit(&dp->dp_config_rwlock);
289990b485lling			} else {
290990b485lling				strval = NULL;
291990b485lling				intval = za.za_first_integer;
292990b485lling			}
2949d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
296990b485lling			if (strval != NULL)
297990b485lling				kmem_free(strval,
298990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
300990b485lling			break;
302990b485lling		case 1:
303990b485lling			/* string property */
304990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
305990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
306990b485lling			    za.za_name, 1, za.za_num_integers, strval);
307990b485lling			if (err) {
308990b485lling				kmem_free(strval, za.za_num_integers);
309990b485lling				break;
310990b485lling			}
3119d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
312990b485lling			kmem_free(strval, za.za_num_integers);
313990b485lling			break;
315990b485lling		default:
316990b485lling			break;
317990b485lling		}
318990b485lling	}
319990b485lling	zap_cursor_fini(&zc);
320990b485lling	mutex_exit(&spa->spa_props_lock);
322990b485lling	if (err && err != ENOENT) {
323990b485lling		nvlist_free(*nvp);
3249d82f4flling		*nvp = NULL;
325990b485lling		return (err);
326990b485lling	}
328990b485lling	return (0);
332990b485lling * Validate the given pool properties nvlist and modify the list
333990b485lling * for the property values to be set.
334990b485lling */
335990b485llingstatic int
336990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
338990b485lling	nvpair_t *elem;
339990b485lling	int error = 0, reset_bootfs = 0;
340990b485lling	uint64_t objnum;
342990b485lling	elem = NULL;
343990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
344990b485lling		zpool_prop_t prop;
345990b485lling		char *propname, *strval;
346990b485lling		uint64_t intval;
347990b485lling		objset_t *os;
3482f8aaabeschrock		char *slash;
350990b485lling		propname = nvpair_name(elem);
352990b485lling		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
353990b485lling			return (EINVAL);
355990b485lling		switch (prop) {
356990b485lling		case ZPOOL_PROP_VERSION:
357990b485lling			error = nvpair_value_uint64(elem, &intval);
358990b485lling			if (!error &&
359990b485lling			    (intval < spa_version(spa) || intval > SPA_VERSION))
360990b485lling				error = EINVAL;
361990b485lling			break;
363990b485lling		case ZPOOL_PROP_DELEGATION:
364990b485lling		case ZPOOL_PROP_AUTOREPLACE:
365d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
366573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
367990b485lling			error = nvpair_value_uint64(elem, &intval);
368990b485lling			if (!error && intval > 1)
369990b485lling				error = EINVAL;
370990b485lling			break;
372990b485lling		case ZPOOL_PROP_BOOTFS:
37325f89eeJeff Bonwick			/*
37425f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
37525f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
37625f89eeJeff Bonwick			 * the bootfs property cannot be set.
37725f89eeJeff Bonwick			 */
378990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
379990b485lling				error = ENOTSUP;
380990b485lling				break;
381990b485lling			}
383990b485lling			/*
38415e6edfgw			 * Make sure the vdev config is bootable
385990b485lling			 */
38615e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
387990b485lling				error = ENOTSUP;
388990b485lling				break;
389990b485lling			}
391990b485lling			reset_bootfs = 1;
393990b485lling			error = nvpair_value_string(elem, &strval);
395990b485lling			if (!error) {
39615e6edfgw				uint64_t compress;
398990b485lling				if (strval == NULL || strval[0] == '\0') {
399990b485lling					objnum = zpool_prop_default_numeric(
400990b485lling					    ZPOOL_PROP_BOOTFS);
401990b485lling					break;
402990b485lling				}
404503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
405990b485lling					break;
407503ad85Matthew Ahrens				/* Must be ZPL and not gzip compressed. */
408503ad85Matthew Ahrens
409503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
410503ad85Matthew Ahrens					error = ENOTSUP;
411503ad85Matthew Ahrens				} else if ((error = dsl_prop_get_integer(strval,
41215e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
41315e6edfgw				    &compress, NULL)) == 0 &&
41415e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
41515e6edfgw					error = ENOTSUP;
41615e6edfgw				} else {
41715e6edfgw					objnum = dmu_objset_id(os);
41815e6edfgw				}
419503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
420990b485lling			}
421990b485lling			break;
422e14bb32Jeff Bonwick
4230a4e951gw		case ZPOOL_PROP_FAILUREMODE:
4240a4e951gw			error = nvpair_value_uint64(elem, &intval);
4250a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
4260a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
4270a4e951gw				error = EINVAL;
4290a4e951gw			/*
4300a4e951gw			 * This is a special case which only occurs when
4310a4e951gw			 * the pool has completely failed. This allows
4320a4e951gw			 * the user to change the in-core failmode property
4330a4e951gw			 * without syncing it out to disk (I/Os might
4340a4e951gw			 * currently be blocked). We do this by returning
4350a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
4360a4e951gw			 * into thinking we encountered a property validation
4370a4e951gw			 * error.
4380a4e951gw			 */
439e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
4400a4e951gw				spa->spa_failmode = intval;
4410a4e951gw				error = EIO;
4420a4e951gw			}
4430a4e951gw			break;
4452f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
4462f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
4472f8aaabeschrock				break;
4492f8aaabeschrock			if (strval[0] == '\0')
4502f8aaabeschrock				break;
4522f8aaabeschrock			if (strcmp(strval, "none") == 0)
4532f8aaabeschrock				break;
4552f8aaabeschrock			if (strval[0] != '/') {
4562f8aaabeschrock				error = EINVAL;
4572f8aaabeschrock				break;
4582f8aaabeschrock			}
4602f8aaabeschrock			slash = strrchr(strval, '/');
4612f8aaabeschrock			ASSERT(slash != NULL);
4632f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4642f8aaabeschrock			    strcmp(slash, "/..") == 0)
4652f8aaabeschrock				error = EINVAL;
4662f8aaabeschrock			break;
467b24ab67Jeff Bonwick
468b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
469b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
470b24ab67Jeff Bonwick				error = ENOTSUP;
471b24ab67Jeff Bonwick			else
472b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
473b24ab67Jeff Bonwick			if (error == 0 &&
474b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
475b24ab67Jeff Bonwick				error = EINVAL;
476b24ab67Jeff Bonwick			break;
477990b485lling		}
479990b485lling		if (error)
480990b485lling			break;
481990b485lling	}
483990b485lling	if (!error && reset_bootfs) {
484990b485lling		error = nvlist_remove(props,
485990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
487990b485lling		if (!error) {
488990b485lling			error = nvlist_add_uint64(props,
489990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
490990b485lling		}
491990b485lling	}
493990b485lling	return (error);
496379c004Eric Schrockvoid
497379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
498379c004Eric Schrock{
499379c004Eric Schrock	char *cachefile;
500379c004Eric Schrock	spa_config_dirent_t *dp;
501379c004Eric Schrock
502379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
503379c004Eric Schrock	    &cachefile) != 0)
504379c004Eric Schrock		return;
505379c004Eric Schrock
506379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
507379c004Eric Schrock	    KM_SLEEP);
508379c004Eric Schrock
509379c004Eric Schrock	if (cachefile[0] == '\0')
510379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
511379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
512379c004Eric Schrock		dp->scd_path = NULL;
513379c004Eric Schrock	else
514379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
515379c004Eric Schrock
516379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
517379c004Eric Schrock	if (need_sync)
518379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
519379c004Eric Schrock}
520379c004Eric Schrock
522990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
524990b485lling	int error;
525379c004Eric Schrock	nvpair_t *elem;
526379c004Eric Schrock	boolean_t need_sync = B_FALSE;
527379c004Eric Schrock	zpool_prop_t prop;
529990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
530990b485lling		return (error);
532379c004Eric Schrock	elem = NULL;
533379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
534379c004Eric Schrock		if ((prop = zpool_name_to_prop(
535379c004Eric Schrock		    nvpair_name(elem))) == ZPROP_INVAL)
536379c004Eric Schrock			return (EINVAL);
537379c004Eric Schrock
538f9af39bGeorge Wilson		if (prop == ZPOOL_PROP_CACHEFILE ||
539f9af39bGeorge Wilson		    prop == ZPOOL_PROP_ALTROOT ||
540f9af39bGeorge Wilson		    prop == ZPOOL_PROP_READONLY)
541379c004Eric Schrock			continue;
542379c004Eric Schrock
543379c004Eric Schrock		need_sync = B_TRUE;
544379c004Eric Schrock		break;
545379c004Eric Schrock	}
546379c004Eric Schrock
547379c004Eric Schrock	if (need_sync)
548379c004Eric Schrock		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
549379c004Eric Schrock		    spa, nvp, 3));
550379c004Eric Schrock	else
551379c004Eric Schrock		return (0);
555990b485lling * If the bootfs property value is dsobj, clear it.
556990b485lling */
558990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
560990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
561990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
562990b485lling		    spa->spa_pool_props_object,
563990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
564990b485lling		spa->spa_bootfs = 0;
565990b485lling	}
569fa9e406ahrens * ==========================================================================
570fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
571fa9e406ahrens * ==========================================================================
572fa9e406ahrens */
574ea8dc4beschrockstatic int
575ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
577ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
578ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
579ea8dc4beschrock	int ret;
581ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
582ea8dc4beschrock	    sizeof (zbookmark_t));
584ea8dc4beschrock	if (ret < 0)
585ea8dc4beschrock		return (-1);
586ea8dc4beschrock	else if (ret > 0)
587ea8dc4beschrock		return (1);
588ea8dc4beschrock	else
589ea8dc4beschrock		return (0);
593ea8dc4beschrock * Utility function which retrieves copies of the current logs and
594ea8dc4beschrock * re-initializes them in the process.
595ea8dc4beschrock */
597ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
599ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
601ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
602ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
604ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
605ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
606ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
607ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
608ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
609ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
61235a5a35Jonathan Adamsstatic taskq_t *
61335a5a35Jonathan Adamsspa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
61435a5a35Jonathan Adams    uint_t value)
6165aeb947Garrett D'Amore	uint_t flags = 0;
61735a5a35Jonathan Adams	boolean_t batch = B_FALSE;
61935a5a35Jonathan Adams	switch (mode) {
62035a5a35Jonathan Adams	case zti_mode_null:
62135a5a35Jonathan Adams		return (NULL);		/* no taskq needed */
62335a5a35Jonathan Adams	case zti_mode_fixed:
62435a5a35Jonathan Adams		ASSERT3U(value, >=, 1);
62535a5a35Jonathan Adams		value = MAX(value, 1);
62635a5a35Jonathan Adams		break;
62835a5a35Jonathan Adams	case zti_mode_batch:
62935a5a35Jonathan Adams		batch = B_TRUE;
63035a5a35Jonathan Adams		flags |= TASKQ_THREADS_CPU_PCT;
63135a5a35Jonathan Adams		value = zio_taskq_batch_pct;
63235a5a35Jonathan Adams		break;
63335a5a35Jonathan Adams
63435a5a35Jonathan Adams	case zti_mode_online_percent:
63535a5a35Jonathan Adams		flags |= TASKQ_THREADS_CPU_PCT;
63635a5a35Jonathan Adams		break;
63735a5a35Jonathan Adams
63835a5a35Jonathan Adams	default:
63935a5a35Jonathan Adams		panic("unrecognized mode for %s taskq (%u:%u) in "
64035a5a35Jonathan Adams		    "spa_activate()",
64135a5a35Jonathan Adams		    name, mode, value);
64235a5a35Jonathan Adams		break;
64335a5a35Jonathan Adams	}
64435a5a35Jonathan Adams
64535a5a35Jonathan Adams	if (zio_taskq_sysdc && spa->spa_proc != &p0) {
64635a5a35Jonathan Adams		if (batch)
64735a5a35Jonathan Adams			flags |= TASKQ_DC_BATCH;
64835a5a35Jonathan Adams
64935a5a35Jonathan Adams		return (taskq_create_sysdc(name, value, 50, INT_MAX,
65035a5a35Jonathan Adams		    spa->spa_proc, zio_taskq_basedc, flags));
65135a5a35Jonathan Adams	}
65235a5a35Jonathan Adams	return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
65335a5a35Jonathan Adams	    spa->spa_proc, flags));
65435a5a35Jonathan Adams}
65535a5a35Jonathan Adams
65635a5a35Jonathan Adamsstatic void
65735a5a35Jonathan Adamsspa_create_zio_taskqs(spa_t *spa)
65835a5a35Jonathan Adams{
659e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
660e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
66180eb36fGeorge Wilson			const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
66280eb36fGeorge Wilson			enum zti_modes mode = ztip->zti_mode;
66380eb36fGeorge Wilson			uint_t value = ztip->zti_value;
6642e0c549Jonathan Adams			char name[32];
6652e0c549Jonathan Adams
6662e0c549Jonathan Adams			(void) snprintf(name, sizeof (name),
66780eb36fGeorge Wilson			    "%s_%s", zio_type_name[t], zio_taskq_types[q]);
6682e0c549Jonathan Adams
66935a5a35Jonathan Adams			spa->spa_zio_taskq[t][q] =
67035a5a35Jonathan Adams			    spa_taskq_create(spa, name, mode, value);
67135a5a35Jonathan Adams		}
67235a5a35Jonathan Adams	}
67335a5a35Jonathan Adams}
67435a5a35Jonathan Adams
67535a5a35Jonathan Adams#ifdef _KERNEL
67635a5a35Jonathan Adamsstatic void
67735a5a35Jonathan Adamsspa_thread(void *arg)
67835a5a35Jonathan Adams{
67935a5a35Jonathan Adams	callb_cpr_t cprinfo;
6802e0c549Jonathan Adams
68135a5a35Jonathan Adams	spa_t *spa = arg;
68235a5a35Jonathan Adams	user_t *pu = PTOU(curproc);
6832e0c549Jonathan Adams
68435a5a35Jonathan Adams	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
68535a5a35Jonathan Adams	    spa->spa_name);
6862e0c549Jonathan Adams
68735a5a35Jonathan Adams	ASSERT(curproc != &p0);
68835a5a35Jonathan Adams	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
68935a5a35Jonathan Adams	    "zpool-%s", spa->spa_name);
69035a5a35Jonathan Adams	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
6912e0c549Jonathan Adams
69235a5a35Jonathan Adams	/* bind this thread to the requested psrset */
69335a5a35Jonathan Adams	if (zio_taskq_psrset_bind != PS_NONE) {
69435a5a35Jonathan Adams		pool_lock();
69535a5a35Jonathan Adams		mutex_enter(&cpu_lock);
69635a5a35Jonathan Adams		mutex_enter(&pidlock);
69735a5a35Jonathan Adams		mutex_enter(&curproc->p_lock);
69880eb36fGeorge Wilson
69935a5a35Jonathan Adams		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
70035a5a35Jonathan Adams		    0, NULL, NULL) == 0)  {
70135a5a35Jonathan Adams			curthread->t_bind_pset = zio_taskq_psrset_bind;
70235a5a35Jonathan Adams		} else {