spa.c revision f9af39bacaaa0f9dda3b75ff6858b9f3988a39af
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/*
2398d1cbfGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens/*
27fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
28fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
29fa9e406ahrens * pool.
30fa9e406ahrens */
31fa9e406ahrens
32fa9e406ahrens#include <sys/zfs_context.h>
33ea8dc4beschrock#include <sys/fm/fs/zfs.h>
34fa9e406ahrens#include <sys/spa_impl.h>
35fa9e406ahrens#include <sys/zio.h>
36fa9e406ahrens#include <sys/zio_checksum.h>
37fa9e406ahrens#include <sys/dmu.h>
38fa9e406ahrens#include <sys/dmu_tx.h>
39fa9e406ahrens#include <sys/zap.h>
40fa9e406ahrens#include <sys/zil.h>
41b24ab67Jeff Bonwick#include <sys/ddt.h>
42fa9e406ahrens#include <sys/vdev_impl.h>
43fa9e406ahrens#include <sys/metaslab.h>
4488ecc94George Wilson#include <sys/metaslab_impl.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>
60e7cbe64gw#include <sys/spa_boot.h>
61573ca77George Wilson#include <sys/zfs_ioctl.h>
623f9d6adLin Ling#include <sys/dsl_scan.h>
63fa9e406ahrens
645679c89jv#ifdef	_KERNEL
65dedec47Jack Meng#include <sys/bootprops.h>
6635a5a35Jonathan Adams#include <sys/callb.h>
6735a5a35Jonathan Adams#include <sys/cpupart.h>
6835a5a35Jonathan Adams#include <sys/pool.h>
6935a5a35Jonathan Adams#include <sys/sysdc.h>
7035a5a35Jonathan Adams#include <sys/zone.h>
715679c89jv#endif	/* _KERNEL */
725679c89jv
73990b485lling#include "zfs_prop.h"
74b7b9745perrin#include "zfs_comutil.h"
75990b485lling
7635a5a35Jonathan Adamstypedef enum zti_modes {
772e0c549Jonathan Adams	zti_mode_fixed,			/* value is # of threads (min 1) */
782e0c549Jonathan Adams	zti_mode_online_percent,	/* value is % of online CPUs */
7935a5a35Jonathan Adams	zti_mode_batch,			/* cpu-intensive; value is ignored */
8080eb36fGeorge Wilson	zti_mode_null,			/* don't create a taskq */
812e0c549Jonathan Adams	zti_nmodes
8235a5a35Jonathan Adams} zti_modes_t;
83416e0cdek
8480eb36fGeorge Wilson#define	ZTI_FIX(n)	{ zti_mode_fixed, (n) }
8580eb36fGeorge Wilson#define	ZTI_PCT(n)	{ zti_mode_online_percent, (n) }
8635a5a35Jonathan Adams#define	ZTI_BATCH	{ zti_mode_batch, 0 }
8780eb36fGeorge Wilson#define	ZTI_NULL	{ zti_mode_null, 0 }
882e0c549Jonathan Adams
8980eb36fGeorge Wilson#define	ZTI_ONE		ZTI_FIX(1)
902e0c549Jonathan Adams
912e0c549Jonathan Adamstypedef struct zio_taskq_info {
9280eb36fGeorge Wilson	enum zti_modes zti_mode;
9380eb36fGeorge Wilson	uint_t zti_value;
942e0c549Jonathan Adams} zio_taskq_info_t;
952e0c549Jonathan Adams
962e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
9735a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
982e0c549Jonathan Adams};
992e0c549Jonathan Adams
10080eb36fGeorge Wilson/*
10180eb36fGeorge Wilson * Define the taskq threads for the following I/O types:
10280eb36fGeorge Wilson * 	NULL, READ, WRITE, FREE, CLAIM, and IOCTL
10380eb36fGeorge Wilson */
10480eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
10580eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
10680eb36fGeorge Wilson	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
10735a5a35Jonathan Adams	{ ZTI_FIX(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL },
10835a5a35Jonathan Adams	{ ZTI_BATCH,	ZTI_FIX(5),	ZTI_FIX(8),	ZTI_FIX(5) },
109c7cd242George Wilson	{ ZTI_FIX(100),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11080eb36fGeorge Wilson	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
11180eb36fGeorge Wilson	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL },
1122e0c549Jonathan Adams};
1132e0c549Jonathan Adams
1143f9d6adLin Lingstatic dsl_syncfunc_t spa_sync_props;
11589a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1161195e68Mark J Musantestatic int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1171195e68Mark J Musante    spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1181195e68Mark J Musante    char **ereport);
119cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
120990b485lling
12135a5a35Jonathan Adamsuint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
12235a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
12335a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
12435a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
12535a5a35Jonathan Adams
12635a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
12735a5a35Jonathan Adams
12835a5a35Jonathan Adams/*
12935a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
13035a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
13135a5a35Jonathan Adams */
13235a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
13335a5a35Jonathan Adams
134990b485lling/*
135990b485lling * ==========================================================================
136990b485lling * SPA properties routines
137990b485lling * ==========================================================================
138990b485lling */
139990b485lling
140990b485lling/*
141990b485lling * Add a (source=src, propname=propval) list to an nvlist.
142990b485lling */
1439d82f4fllingstatic void
144990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
145990b485lling    uint64_t intval, zprop_source_t src)
146990b485lling{
147990b485lling	const char *propname = zpool_prop_to_name(prop);
148990b485lling	nvlist_t *propval;
149990b485lling
1509d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1519d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
152990b485lling
1539d82f4flling	if (strval != NULL)
1549d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1559d82f4flling	else
1569d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
157990b485lling
1589d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
159990b485lling	nvlist_free(propval);
160990b485lling}
161990b485lling
162990b485lling/*
163990b485lling * Get property values from the spa configuration.
164990b485lling */
1659d82f4fllingstatic void
166990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
167990b485lling{
168379c004Eric Schrock	uint64_t size;
169485bbbfGeorge Wilson	uint64_t alloc;
170990b485lling	uint64_t cap, version;
171990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
172c5904d1eschrock	spa_config_dirent_t *dp;
173990b485lling
174e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
175e14bb32Jeff Bonwick
176379c004Eric Schrock	if (spa->spa_root_vdev != NULL) {
177485bbbfGeorge Wilson		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
178b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
179379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
180379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
181485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
182485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
183485bbbfGeorge Wilson		    size - alloc, src);
184f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
185f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
186379c004Eric Schrock
187485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
188379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
189379c004Eric Schrock
190b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
191b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
192b24ab67Jeff Bonwick
193379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
194379c004Eric Schrock		    spa->spa_root_vdev->vdev_state, src);
195379c004Eric Schrock
196379c004Eric Schrock		version = spa_version(spa);
197379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
198379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
199379c004Eric Schrock		else
200379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
201379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
202379c004Eric Schrock	}
203990b485lling
2049d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
205990b485lling
2069d82f4flling	if (spa->spa_root != NULL)
2079d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2089d82f4flling		    0, ZPROP_SRC_LOCAL);
209990b485lling
210c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
211c5904d1eschrock		if (dp->scd_path == NULL) {
2129d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
213c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
214c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2159d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
216c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2172f8aaabeschrock		}
2182f8aaabeschrock	}
219990b485lling}
220990b485lling
221990b485lling/*
222990b485lling * Get zpool property values.
223990b485lling */
224990b485llingint
225990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
226990b485lling{
227b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
228990b485lling	zap_cursor_t zc;
229990b485lling	zap_attribute_t za;
230990b485lling	int err;
231990b485lling
2329d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
233990b485lling
234e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
235e14bb32Jeff Bonwick
236990b485lling	/*
237990b485lling	 * Get properties from the spa config.
238990b485lling	 */
2399d82f4flling	spa_prop_get_config(spa, nvp);
240990b485lling
241990b485lling	/* If no pool property object, no more prop to get. */
242afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
243990b485lling		mutex_exit(&spa->spa_props_lock);
244990b485lling		return (0);
245990b485lling	}
246990b485lling
247990b485lling	/*
248990b485lling	 * Get properties from the MOS pool property object.
249990b485lling	 */
250990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
251990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
252990b485lling	    zap_cursor_advance(&zc)) {
253990b485lling		uint64_t intval = 0;
254990b485lling		char *strval = NULL;
255990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
256990b485lling		zpool_prop_t prop;
257990b485lling
258990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
259990b485lling			continue;
260990b485lling
261990b485lling		switch (za.za_integer_length) {
262990b485lling		case 8:
263990b485lling			/* integer property */
264990b485lling			if (za.za_first_integer !=
265990b485lling			    zpool_prop_default_numeric(prop))
266990b485lling				src = ZPROP_SRC_LOCAL;
267990b485lling
268990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
269990b485lling				dsl_pool_t *dp;
270990b485lling				dsl_dataset_t *ds = NULL;
271990b485lling
272990b485lling				dp = spa_get_dsl(spa);
273990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
274745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
275745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
276990b485lling					rw_exit(&dp->dp_config_rwlock);
277990b485lling					break;
278990b485lling				}
279990b485lling
280990b485lling				strval = kmem_alloc(
281990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
282990b485lling				    KM_SLEEP);
283990b485lling				dsl_dataset_name(ds, strval);
284745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
285990b485lling				rw_exit(&dp->dp_config_rwlock);
286990b485lling			} else {
287990b485lling				strval = NULL;
288990b485lling				intval = za.za_first_integer;
289990b485lling			}
290990b485lling
2919d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
292990b485lling
293990b485lling			if (strval != NULL)
294990b485lling				kmem_free(strval,
295990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
296990b485lling
297990b485lling			break;
298990b485lling
299990b485lling		case 1:
300990b485lling			/* string property */
301990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
302990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
303990b485lling			    za.za_name, 1, za.za_num_integers, strval);
304990b485lling			if (err) {
305990b485lling				kmem_free(strval, za.za_num_integers);
306990b485lling				break;
307990b485lling			}
3089d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
309990b485lling			kmem_free(strval, za.za_num_integers);
310990b485lling			break;
311990b485lling
312990b485lling		default:
313990b485lling			break;
314990b485lling		}
315990b485lling	}
316990b485lling	zap_cursor_fini(&zc);
317990b485lling	mutex_exit(&spa->spa_props_lock);
318990b485llingout:
319990b485lling	if (err && err != ENOENT) {
320990b485lling		nvlist_free(*nvp);
3219d82f4flling		*nvp = NULL;
322990b485lling		return (err);
323990b485lling	}
324990b485lling
325990b485lling	return (0);
326990b485lling}
327990b485lling
328990b485lling/*
329990b485lling * Validate the given pool properties nvlist and modify the list
330990b485lling * for the property values to be set.
331990b485lling */
332990b485llingstatic int
333990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
334990b485lling{
335990b485lling	nvpair_t *elem;
336990b485lling	int error = 0, reset_bootfs = 0;
337990b485lling	uint64_t objnum;
338990b485lling
339990b485lling	elem = NULL;
340990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
341990b485lling		zpool_prop_t prop;
342990b485lling		char *propname, *strval;
343990b485lling		uint64_t intval;
344990b485lling		objset_t *os;
3452f8aaabeschrock		char *slash;
346990b485lling
347990b485lling		propname = nvpair_name(elem);
348990b485lling
349990b485lling		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
350990b485lling			return (EINVAL);
351990b485lling
352990b485lling		switch (prop) {
353990b485lling		case ZPOOL_PROP_VERSION:
354990b485lling			error = nvpair_value_uint64(elem, &intval);
355990b485lling			if (!error &&
356990b485lling			    (intval < spa_version(spa) || intval > SPA_VERSION))
357990b485lling				error = EINVAL;
358990b485lling			break;
359990b485lling
360990b485lling		case ZPOOL_PROP_DELEGATION:
361990b485lling		case ZPOOL_PROP_AUTOREPLACE:
362d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
363573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
364990b485lling			error = nvpair_value_uint64(elem, &intval);
365990b485lling			if (!error && intval > 1)
366990b485lling				error = EINVAL;
367990b485lling			break;
368990b485lling
369990b485lling		case ZPOOL_PROP_BOOTFS:
37025f89eeJeff Bonwick			/*
37125f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
37225f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
37325f89eeJeff Bonwick			 * the bootfs property cannot be set.
37425f89eeJeff Bonwick			 */
375990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
376990b485lling				error = ENOTSUP;
377990b485lling				break;
378990b485lling			}
379990b485lling
380990b485lling			/*
38115e6edfgw			 * Make sure the vdev config is bootable
382990b485lling			 */
38315e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
384990b485lling				error = ENOTSUP;
385990b485lling				break;
386990b485lling			}
387990b485lling
388990b485lling			reset_bootfs = 1;
389990b485lling
390990b485lling			error = nvpair_value_string(elem, &strval);
391990b485lling
392990b485lling			if (!error) {
39315e6edfgw				uint64_t compress;
39415e6edfgw
395990b485lling				if (strval == NULL || strval[0] == '\0') {
396990b485lling					objnum = zpool_prop_default_numeric(
397990b485lling					    ZPOOL_PROP_BOOTFS);
398990b485lling					break;
399990b485lling				}
400990b485lling
401503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
402990b485lling					break;
40315e6edfgw
404503ad85Matthew Ahrens				/* Must be ZPL and not gzip compressed. */
405503ad85Matthew Ahrens
406503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
407503ad85Matthew Ahrens					error = ENOTSUP;
408503ad85Matthew Ahrens				} else if ((error = dsl_prop_get_integer(strval,
40915e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
41015e6edfgw				    &compress, NULL)) == 0 &&
41115e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
41215e6edfgw					error = ENOTSUP;
41315e6edfgw				} else {
41415e6edfgw					objnum = dmu_objset_id(os);
41515e6edfgw				}
416503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
417990b485lling			}
418990b485lling			break;
419e14bb32Jeff Bonwick
4200a4e951gw		case ZPOOL_PROP_FAILUREMODE:
4210a4e951gw			error = nvpair_value_uint64(elem, &intval);
4220a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
4230a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
4240a4e951gw				error = EINVAL;
4250a4e951gw
4260a4e951gw			/*
4270a4e951gw			 * This is a special case which only occurs when
4280a4e951gw			 * the pool has completely failed. This allows
4290a4e951gw			 * the user to change the in-core failmode property
4300a4e951gw			 * without syncing it out to disk (I/Os might
4310a4e951gw			 * currently be blocked). We do this by returning
4320a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
4330a4e951gw			 * into thinking we encountered a property validation
4340a4e951gw			 * error.
4350a4e951gw			 */
436e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
4370a4e951gw				spa->spa_failmode = intval;
4380a4e951gw				error = EIO;
4390a4e951gw			}
4400a4e951gw			break;
4412f8aaabeschrock
4422f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
4432f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
4442f8aaabeschrock				break;
4452f8aaabeschrock
4462f8aaabeschrock			if (strval[0] == '\0')
4472f8aaabeschrock				break;
4482f8aaabeschrock
4492f8aaabeschrock			if (strcmp(strval, "none") == 0)
4502f8aaabeschrock				break;
4512f8aaabeschrock
4522f8aaabeschrock			if (strval[0] != '/') {
4532f8aaabeschrock				error = EINVAL;
4542f8aaabeschrock				break;
4552f8aaabeschrock			}
4562f8aaabeschrock
4572f8aaabeschrock			slash = strrchr(strval, '/');
4582f8aaabeschrock			ASSERT(slash != NULL);
4592f8aaabeschrock
4602f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4612f8aaabeschrock			    strcmp(slash, "/..") == 0)
4622f8aaabeschrock				error = EINVAL;
4632f8aaabeschrock			break;
464b24ab67Jeff Bonwick
465b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
466b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
467b24ab67Jeff Bonwick				error = ENOTSUP;
468b24ab67Jeff Bonwick			else
469b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
470b24ab67Jeff Bonwick			if (error == 0 &&
471b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
472b24ab67Jeff Bonwick				error = EINVAL;
473b24ab67Jeff Bonwick			break;
474990b485lling		}
475990b485lling
476990b485lling		if (error)
477990b485lling			break;
478990b485lling	}
479990b485lling
480990b485lling	if (!error && reset_bootfs) {
481990b485lling		error = nvlist_remove(props,
482990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
483990b485lling
484990b485lling		if (!error) {
485990b485lling			error = nvlist_add_uint64(props,
486990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
487990b485lling		}
488990b485lling	}
489990b485lling
490990b485lling	return (error);
491990b485lling}
492990b485lling
493379c004Eric Schrockvoid
494379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
495379c004Eric Schrock{
496379c004Eric Schrock	char *cachefile;
497379c004Eric Schrock	spa_config_dirent_t *dp;
498379c004Eric Schrock
499379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
500379c004Eric Schrock	    &cachefile) != 0)
501379c004Eric Schrock		return;
502379c004Eric Schrock
503379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
504379c004Eric Schrock	    KM_SLEEP);
505379c004Eric Schrock
506379c004Eric Schrock	if (cachefile[0] == '\0')
507379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
508379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
509379c004Eric Schrock		dp->scd_path = NULL;
510379c004Eric Schrock	else
511379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
512379c004Eric Schrock
513379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
514379c004Eric Schrock	if (need_sync)
515379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
516379c004Eric Schrock}
517379c004Eric Schrock
518990b485llingint
519990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
520990b485lling{
521990b485lling	int error;
522379c004Eric Schrock	nvpair_t *elem;
523379c004Eric Schrock	boolean_t need_sync = B_FALSE;
524379c004Eric Schrock	zpool_prop_t prop;
525990b485lling
526990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
527990b485lling		return (error);
528990b485lling
529379c004Eric Schrock	elem = NULL;
530379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
531379c004Eric Schrock		if ((prop = zpool_name_to_prop(
532379c004Eric Schrock		    nvpair_name(elem))) == ZPROP_INVAL)
533379c004Eric Schrock			return (EINVAL);
534379c004Eric Schrock
535f9af39bGeorge Wilson		if (prop == ZPOOL_PROP_CACHEFILE ||
536f9af39bGeorge Wilson		    prop == ZPOOL_PROP_ALTROOT ||
537f9af39bGeorge Wilson		    prop == ZPOOL_PROP_READONLY)
538379c004Eric Schrock			continue;
539379c004Eric Schrock
540379c004Eric Schrock		need_sync = B_TRUE;
541379c004Eric Schrock		break;
542379c004Eric Schrock	}
543379c004Eric Schrock
544379c004Eric Schrock	if (need_sync)
545379c004Eric Schrock		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
546379c004Eric Schrock		    spa, nvp, 3));
547379c004Eric Schrock	else
548379c004Eric Schrock		return (0);
549990b485lling}
550990b485lling
551990b485lling/*
552990b485lling * If the bootfs property value is dsobj, clear it.
553990b485lling */
554990b485llingvoid
555990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
556990b485lling{
557990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
558990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
559990b485lling		    spa->spa_pool_props_object,
560990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
561990b485lling		spa->spa_bootfs = 0;
562990b485lling	}
563990b485lling}
564990b485lling
565fa9e406ahrens/*
566fa9e406ahrens * ==========================================================================
567fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
568fa9e406ahrens * ==========================================================================
569fa9e406ahrens */
570fa9e406ahrens
571ea8dc4beschrockstatic int
572ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
573ea8dc4beschrock{
574ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
575ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
576ea8dc4beschrock	int ret;
577ea8dc4beschrock
578ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
579ea8dc4beschrock	    sizeof (zbookmark_t));
580ea8dc4beschrock
581ea8dc4beschrock	if (ret < 0)
582ea8dc4beschrock		return (-1);
583ea8dc4beschrock	else if (ret > 0)
584ea8dc4beschrock		return (1);
585ea8dc4beschrock	else
586ea8dc4beschrock		return (0);
587ea8dc4beschrock}
588ea8dc4beschrock
589ea8dc4beschrock/*
590ea8dc4beschrock * Utility function which retrieves copies of the current logs and
591ea8dc4beschrock * re-initializes them in the process.
592ea8dc4beschrock */
593ea8dc4beschrockvoid
594ea8dc4beschrockspa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
595ea8dc4beschrock{
596ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
597ea8dc4beschrock
598ea8dc4beschrock	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
599ea8dc4beschrock	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
600ea8dc4beschrock
601ea8dc4beschrock	avl_create(&spa->spa_errlist_scrub,
602ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
603ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
604ea8dc4beschrock	avl_create(&spa->spa_errlist_last,
605ea8dc4beschrock	    spa_error_entry_compare, sizeof (spa_error_entry_t),
606ea8dc4beschrock	    offsetof(spa_error_entry_t, se_avl));
607ea8dc4beschrock}
608ea8dc4beschrock
60935a5a35Jonathan Adamsstatic taskq_t *
61035a5a35Jonathan Adamsspa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
61135a5a35Jonathan Adams    uint_t value)
612fa9e406ahrens{
61335a5a35Jonathan Adams	uint_t flags = TASKQ_PREPOPULATE;
61435a5a35Jonathan Adams	boolean_t batch = B_FALSE;
615fa9e406ahrens
61635a5a35Jonathan Adams	switch (mode) {
61735a5a35Jonathan Adams	case zti_mode_null:
61835a5a35Jonathan Adams		return (NULL);		/* no taskq needed */
619fa9e406ahrens
62035a5a35Jonathan Adams	case zti_mode_fixed:
62135a5a35Jonathan Adams		ASSERT3U(value, >=, 1);
62235a5a35Jonathan Adams		value = MAX(value, 1);
62335a5a35Jonathan Adams		break;
624fa9e406ahrens
62535a5a35Jonathan Adams	case zti_mode_batch:
62635a5a35Jonathan Adams		batch = B_TRUE;
62735a5a35Jonathan Adams		flags |= TASKQ_THREADS_CPU_PCT;
62835a5a35Jonathan Adams		value = zio_taskq_batch_pct;
62935a5a35Jonathan Adams		break;
63035a5a35Jonathan Adams
63135a5a35Jonathan Adams	case zti_mode_online_percent:
63235a5a35Jonathan Adams		flags |= TASKQ_THREADS_CPU_PCT;
63335a5a35Jonathan Adams		break;
63435a5a35Jonathan Adams
63535a5a35Jonathan Adams	default:
63635a5a35Jonathan Adams		panic("unrecognized mode for %s taskq (%u:%u) in "
63735a5a35Jonathan Adams		    "spa_activate()",
63835a5a35Jonathan Adams		    name, mode, value);
63935a5a35Jonathan Adams		break;
64035a5a35Jonathan Adams	}
64135a5a35Jonathan Adams
64235a5a35Jonathan Adams	if (zio_taskq_sysdc && spa->spa_proc != &p0) {
64335a5a35Jonathan Adams		if (batch)
64435a5a35Jonathan Adams			flags |= TASKQ_DC_BATCH;
64535a5a35Jonathan Adams
64635a5a35Jonathan Adams		return (taskq_create_sysdc(name, value, 50, INT_MAX,
64735a5a35Jonathan Adams		    spa->spa_proc, zio_taskq_basedc, flags));
64835a5a35Jonathan Adams	}
64935a5a35Jonathan Adams	return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
65035a5a35Jonathan Adams	    spa->spa_proc, flags));
65135a5a35Jonathan Adams}
65235a5a35Jonathan Adams
65335a5a35Jonathan Adamsstatic void
65435a5a35Jonathan Adamsspa_create_zio_taskqs(spa_t *spa)
65535a5a35Jonathan Adams{
656e14bb32Jeff Bonwick	for (int t = 0; t < ZIO_TYPES; t++) {
657e14bb32Jeff Bonwick		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
65880eb36fGeorge Wilson			const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
65980eb36fGeorge Wilson			enum zti_modes mode = ztip->zti_mode;
66080eb36fGeorge Wilson			uint_t value = ztip->zti_value;
6612e0c549Jonathan Adams			char name[32];
6622e0c549Jonathan Adams
6632e0c549Jonathan Adams			(void) snprintf(name, sizeof (name),
66480eb36fGeorge Wilson			    "%s_%s", zio_type_name[t], zio_taskq_types[q]);
6652e0c549Jonathan Adams
66635a5a35Jonathan Adams			spa->spa_zio_taskq[t][q] =
66735a5a35Jonathan Adams			    spa_taskq_create(spa, name, mode, value);
66835a5a35Jonathan Adams		}
66935a5a35Jonathan Adams	}
67035a5a35Jonathan Adams}
67135a5a35Jonathan Adams
67235a5a35Jonathan Adams#ifdef _KERNEL
67335a5a35Jonathan Adamsstatic void
67435a5a35Jonathan Adamsspa_thread(void *arg)
67535a5a35Jonathan Adams{
67635a5a35Jonathan Adams	callb_cpr_t cprinfo;
6772e0c549Jonathan Adams
67835a5a35Jonathan Adams	spa_t *spa = arg;
67935a5a35Jonathan Adams	user_t *pu = PTOU(curproc);
6802e0c549Jonathan Adams
68135a5a35Jonathan Adams	CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
68235a5a35Jonathan Adams	    spa->spa_name);
6832e0c549Jonathan Adams
68435a5a35Jonathan Adams	ASSERT(curproc != &p0);
68535a5a35Jonathan Adams	(void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
68635a5a35Jonathan Adams	    "zpool-%s", spa->spa_name);
68735a5a35Jonathan Adams	(void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
6882e0c549Jonathan Adams
68935a5a35Jonathan Adams	/* bind this thread to the requested psrset */
69035a5a35Jonathan Adams	if (zio_taskq_psrset_bind != PS_NONE) {
69135a5a35Jonathan Adams		pool_lock();
69235a5a35Jonathan Adams		mutex_enter(&cpu_lock);
69335a5a35Jonathan Adams		mutex_enter(&pidlock);
69435a5a35Jonathan Adams		mutex_enter(&curproc->p_lock);
69580eb36fGeorge Wilson
69635a5a35Jonathan Adams		if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
69735a5a35Jonathan Adams		    0, NULL, NULL) == 0)  {
69835a5a35Jonathan Adams			curthread->t_bind_pset = zio_taskq_psrset_bind;
69935a5a35Jonathan Adams		} else {
70035a5a35Jonathan Adams			cmn_err(CE_WARN,
70135a5a35Jonathan Adams			    "Couldn't bind process for zfs pool \"%s\" to "
70235a5a35Jonathan Adams			    "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
70335a5a35Jonathan Adams		}
70435a5a35Jonathan Adams
705