spa.c revision be6fd75a69ae679453d9cda5bff3326111e6d1ca
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.
24be6fd75Matthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
25efb4a87Yuri Pankov * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
265aeb947Garrett D'Amore */
27fa9e406ahrens
28fa9e406ahrens/*
29fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
30fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
31fa9e406ahrens * pool.
32fa9e406ahrens */
33fa9e406ahrens
34fa9e406ahrens#include <sys/zfs_context.h>
35ea8dc4beschrock#include <sys/fm/fs/zfs.h>
36fa9e406ahrens#include <sys/spa_impl.h>
37fa9e406ahrens#include <sys/zio.h>
38fa9e406ahrens#include <sys/zio_checksum.h>
39fa9e406ahrens#include <sys/dmu.h>
40fa9e406ahrens#include <sys/dmu_tx.h>
41fa9e406ahrens#include <sys/zap.h>
42fa9e406ahrens#include <sys/zil.h>
43b24ab67Jeff Bonwick#include <sys/ddt.h>
44fa9e406ahrens#include <sys/vdev_impl.h>
45fa9e406ahrens#include <sys/metaslab.h>
4688ecc94George Wilson#include <sys/metaslab_impl.h>
47fa9e406ahrens#include <sys/uberblock_impl.h>
48fa9e406ahrens#include <sys/txg.h>
49fa9e406ahrens#include <sys/avl.h>
50fa9e406ahrens#include <sys/dmu_traverse.h>
51b1b8ab3lling#include <sys/dmu_objset.h>
52fa9e406ahrens#include <sys/unique.h>
53fa9e406ahrens#include <sys/dsl_pool.h>
54b1b8ab3lling#include <sys/dsl_dataset.h>
55fa9e406ahrens#include <sys/dsl_dir.h>
56fa9e406ahrens#include <sys/dsl_prop.h>
57b1b8ab3lling#include <sys/dsl_synctask.h>
58fa9e406ahrens#include <sys/fs/zfs.h>
59fa94a07brendan#include <sys/arc.h>
60fa9e406ahrens#include <sys/callb.h>
619517395ek#include <sys/systeminfo.h>
62e7cbe64gw#include <sys/spa_boot.h>
63573ca77George Wilson#include <sys/zfs_ioctl.h>
643f9d6adLin Ling#include <sys/dsl_scan.h>
65ad135b5Christopher Siden#include <sys/zfeature.h>
663b2aab1Matthew Ahrens#include <sys/dsl_destroy.h>
67fa9e406ahrens
685679c89jv#ifdef	_KERNEL
69dedec47Jack Meng#include <sys/bootprops.h>
7035a5a35Jonathan Adams#include <sys/callb.h>
7135a5a35Jonathan Adams#include <sys/cpupart.h>
7235a5a35Jonathan Adams#include <sys/pool.h>
7335a5a35Jonathan Adams#include <sys/sysdc.h>
7435a5a35Jonathan Adams#include <sys/zone.h>
755679c89jv#endif	/* _KERNEL */
765679c89jv
77990b485lling#include "zfs_prop.h"
78b7b9745perrin#include "zfs_comutil.h"
79990b485lling
8035a5a35Jonathan Adamstypedef enum zti_modes {
81ec94d32Adam Leventhal	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
82ec94d32Adam Leventhal	ZTI_MODE_ONLINE_PERCENT,	/* value is % of online CPUs */
83ec94d32Adam Leventhal	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
84ec94d32Adam Leventhal	ZTI_MODE_NULL,			/* don't create a taskq */
85ec94d32Adam Leventhal	ZTI_NMODES
8635a5a35Jonathan Adams} zti_modes_t;
87416e0cdek
88ec94d32Adam Leventhal#define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
89ec94d32Adam Leventhal#define	ZTI_PCT(n)	{ ZTI_MODE_ONLINE_PERCENT, (n), 1 }
90ec94d32Adam Leventhal#define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
91ec94d32Adam Leventhal#define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
922e0c549Jonathan Adams
93ec94d32Adam Leventhal#define	ZTI_N(n)	ZTI_P(n, 1)
94ec94d32Adam Leventhal#define	ZTI_ONE		ZTI_N(1)
952e0c549Jonathan Adams
962e0c549Jonathan Adamstypedef struct zio_taskq_info {
97ec94d32Adam Leventhal	zti_modes_t zti_mode;
9880eb36fGeorge Wilson	uint_t zti_value;
99ec94d32Adam Leventhal	uint_t zti_count;
1002e0c549Jonathan Adams} zio_taskq_info_t;
1012e0c549Jonathan Adams
1022e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
10335a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
1042e0c549Jonathan Adams};
1052e0c549Jonathan Adams
10680eb36fGeorge Wilson/*
107ec94d32Adam Leventhal * This table defines the taskq settings for each ZFS I/O type. When
108ec94d32Adam Leventhal * initializing a pool, we use this table to create an appropriately sized
109ec94d32Adam Leventhal * taskq. Some operations are low volume and therefore have a small, static
110ec94d32Adam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
111ec94d32Adam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH
112ec94d32Adam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations
113ec94d32Adam Leventhal * are so high frequency and short-lived that the taskq itself can become a a
114ec94d32Adam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
115ec94d32Adam Leventhal * additional degree of parallelism specified by the number of threads per-
116ec94d32Adam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the
117ec94d32Adam Leventhal * particular taskq is chosen at random.
118ec94d32Adam Leventhal *
119ec94d32Adam Leventhal * The different taskq priorities are to handle the different contexts (issue
120ec94d32Adam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
121ec94d32Adam Leventhal * need to be handled with minimum delay.
12280eb36fGeorge Wilson */
12380eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
12480eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
125ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
126ec94d32Adam Leventhal	{ ZTI_N(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL }, /* READ */
127ec94d32Adam Leventhal	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
128ec94d32Adam Leventhal	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
129ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
130ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
1312e0c549Jonathan Adams};
1322e0c549Jonathan Adams
1333b2aab1Matthew Ahrensstatic void spa_sync_version(void *arg, dmu_tx_t *tx);
1343b2aab1Matthew Ahrensstatic void spa_sync_props(void *arg, dmu_tx_t *tx);
13589a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1361195e68Mark J Musantestatic int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1371195e68Mark J Musante    spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1381195e68Mark J Musante    char **ereport);
139cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
140990b485lling
14135a5a35Jonathan Adamsuint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
14235a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
14335a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
14435a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
14535a5a35Jonathan Adams
14635a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
14701f55e4George Wilsonextern int	zfs_sync_pass_deferred_free;
14835a5a35Jonathan Adams
14935a5a35Jonathan Adams/*
15035a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
15135a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
15235a5a35Jonathan Adams */
15335a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
15435a5a35Jonathan Adams
155990b485lling/*
156990b485lling * ==========================================================================
157990b485lling * SPA properties routines
158990b485lling * ==========================================================================
159990b485lling */
160990b485lling
161990b485lling/*
162990b485lling * Add a (source=src, propname=propval) list to an nvlist.
163990b485lling */
1649d82f4fllingstatic void
165990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
166990b485lling    uint64_t intval, zprop_source_t src)
167990b485lling{
168990b485lling	const char *propname = zpool_prop_to_name(prop);
169990b485lling	nvlist_t *propval;
170990b485lling
1719d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1729d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
173990b485lling
1749d82f4flling	if (strval != NULL)
1759d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1769d82f4flling	else
1779d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
178990b485lling
1799d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
180990b485lling	nvlist_free(propval);
181990b485lling}
182990b485lling
183990b485lling/*
184990b485lling * Get property values from the spa configuration.
185990b485lling */
1869d82f4fllingstatic void
187990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
188990b485lling{
1894263d13George Wilson	vdev_t *rvd = spa->spa_root_vdev;
190ad135b5Christopher Siden	dsl_pool_t *pool = spa->spa_dsl_pool;
191379c004Eric Schrock	uint64_t size;
192485bbbfGeorge Wilson	uint64_t alloc;
1934263d13George Wilson	uint64_t space;
194990b485lling	uint64_t cap, version;
195990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
196c5904d1eschrock	spa_config_dirent_t *dp;
197990b485lling
198e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
199e14bb32Jeff Bonwick
2004263d13George Wilson	if (rvd != NULL) {
201485bbbfGeorge Wilson		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
202b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
203379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
204379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
205485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
206485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
207485bbbfGeorge Wilson		    size - alloc, src);
2084263d13George Wilson
2094263d13George Wilson		space = 0;
2104263d13George Wilson		for (int c = 0; c < rvd->vdev_children; c++) {
2114263d13George Wilson			vdev_t *tvd = rvd->vdev_child[c];
2124263d13George Wilson			space += tvd->vdev_max_asize - tvd->vdev_asize;
2134263d13George Wilson		}
2144263d13George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
2154263d13George Wilson		    src);
2164263d13George Wilson
217f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
218f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
219379c004Eric Schrock
220485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
221379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
222379c004Eric Schrock
223b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
224b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
225b24ab67Jeff Bonwick
226379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
2274263d13George Wilson		    rvd->vdev_state, src);
228379c004Eric Schrock
229379c004Eric Schrock		version = spa_version(spa);
230379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
231379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
232379c004Eric Schrock		else
233379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
234379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
235379c004Eric Schrock	}
236990b485lling
237ad135b5Christopher Siden	if (pool != NULL) {
238ad135b5Christopher Siden		dsl_dir_t *freedir = pool->dp_free_dir;
239ad135b5Christopher Siden
240ad135b5Christopher Siden		/*
241ad135b5Christopher Siden		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
242ad135b5Christopher Siden		 * when opening pools before this version freedir will be NULL.
243ad135b5Christopher Siden		 */
244ad135b5Christopher Siden		if (freedir != NULL) {
245ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
246ad135b5Christopher Siden			    freedir->dd_phys->dd_used_bytes, src);
247ad135b5Christopher Siden		} else {
248ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
249ad135b5Christopher Siden			    NULL, 0, src);
250ad135b5Christopher Siden		}
251ad135b5Christopher Siden	}
252ad135b5Christopher Siden
2539d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
254990b485lling
2558704186Dan McDonald	if (spa->spa_comment != NULL) {
2568704186Dan McDonald		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
2578704186Dan McDonald		    0, ZPROP_SRC_LOCAL);
2588704186Dan McDonald	}
2598704186Dan McDonald
2609d82f4flling	if (spa->spa_root != NULL)
2619d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2629d82f4flling		    0, ZPROP_SRC_LOCAL);
263990b485lling
264c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
265c5904d1eschrock		if (dp->scd_path == NULL) {
2669d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
267c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
268c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2699d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
270c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2712f8aaabeschrock		}
2722f8aaabeschrock	}
273990b485lling}
274990b485lling
275990b485lling/*
276990b485lling * Get zpool property values.
277990b485lling */
278990b485llingint
279990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
280990b485lling{
281b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
282990b485lling	zap_cursor_t zc;
283990b485lling	zap_attribute_t za;
284990b485lling	int err;
285990b485lling
2869d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
287990b485lling
288e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
289e14bb32Jeff Bonwick
290990b485lling	/*
291990b485lling	 * Get properties from the spa config.
292990b485lling	 */
2939d82f4flling	spa_prop_get_config(spa, nvp);
294990b485lling
295990b485lling	/* If no pool property object, no more prop to get. */
296afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
297990b485lling		mutex_exit(&spa->spa_props_lock);
298990b485lling		return (0);
299990b485lling	}
300990b485lling
301990b485lling	/*
302990b485lling	 * Get properties from the MOS pool property object.
303990b485lling	 */
304990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
305990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
306990b485lling	    zap_cursor_advance(&zc)) {
307990b485lling		uint64_t intval = 0;
308990b485lling		char *strval = NULL;
309990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
310990b485lling		zpool_prop_t prop;
311990b485lling
312990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
313990b485lling			continue;
314990b485lling
315990b485lling		switch (za.za_integer_length) {
316990b485lling		case 8:
317990b485lling			/* integer property */
318990b485lling			if (za.za_first_integer !=
319990b485lling			    zpool_prop_default_numeric(prop))
320990b485lling				src = ZPROP_SRC_LOCAL;
321990b485lling
322990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
323990b485lling				dsl_pool_t *dp;
324990b485lling				dsl_dataset_t *ds = NULL;
325990b485lling
326990b485lling				dp = spa_get_dsl(spa);
3273b2aab1Matthew Ahrens				dsl_pool_config_enter(dp, FTAG);
328745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
329745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
3303b2aab1Matthew Ahrens					dsl_pool_config_exit(dp, FTAG);
331990b485lling					break;
332990b485lling				}
333990b485lling
334990b485lling				strval = kmem_alloc(
335990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
336990b485lling				    KM_SLEEP);
337990b485lling				dsl_dataset_name(ds, strval);
338745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
3393b2aab1Matthew Ahrens				dsl_pool_config_exit(dp, FTAG);
340990b485lling			} else {
341990b485lling				strval = NULL;
342990b485lling				intval = za.za_first_integer;
343990b485lling			}
344990b485lling
3459d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
346990b485lling
347990b485lling			if (strval != NULL)
348990b485lling				kmem_free(strval,
349990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
350990b485lling
351990b485lling			break;
352990b485lling
353990b485lling		case 1:
354990b485lling			/* string property */
355990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
356990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
357990b485lling			    za.za_name, 1, za.za_num_integers, strval);
358990b485lling			if (err) {
359990b485lling				kmem_free(strval, za.za_num_integers);
360990b485lling				break;
361990b485lling			}
3629d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
363990b485lling			kmem_free(strval, za.za_num_integers);
364990b485lling			break;
365990b485lling
366990b485lling		default:
367990b485lling			break;
368990b485lling		}
369990b485lling	}
370990b485lling	zap_cursor_fini(&zc);
371990b485lling	mutex_exit(&spa->spa_props_lock);
372990b485llingout:
373990b485lling	if (err && err != ENOENT) {
374990b485lling		nvlist_free(*nvp);
3759d82f4flling		*nvp = NULL;
376990b485lling		return (err);
377990b485lling	}
378990b485lling
379990b485lling	return (0);
380990b485lling}
381990b485lling
382990b485lling/*
383990b485lling * Validate the given pool properties nvlist and modify the list
384990b485lling * for the property values to be set.
385990b485lling */
386990b485llingstatic int
387990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
388990b485lling{
389990b485lling	nvpair_t *elem;
390990b485lling	int error = 0, reset_bootfs = 0;
391d5285caGeorge Wilson	uint64_t objnum = 0;
392ad135b5Christopher Siden	boolean_t has_feature = B_FALSE;
393990b485lling
394990b485lling	elem = NULL;
395990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
396990b485lling		uint64_t intval;
397ad135b5Christopher Siden		char *strval, *slash, *check, *fname;
398ad135b5Christopher Siden		const char *propname = nvpair_name(elem);
399ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(propname);
400ad135b5Christopher Siden
401ad135b5Christopher Siden		switch (prop) {
402ad135b5Christopher Siden		case ZPROP_INVAL:
403ad135b5Christopher Siden			if (!zpool_prop_feature(propname)) {
404be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
405ad135b5Christopher Siden				break;
406ad135b5Christopher Siden			}
407990b485lling
408ad135b5Christopher Siden			/*
409ad135b5Christopher Siden			 * Sanitize the input.
410ad135b5Christopher Siden			 */
411ad135b5Christopher Siden			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
412be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
413ad135b5Christopher Siden				break;
414ad135b5Christopher Siden			}
415990b485lling
416ad135b5Christopher Siden			if (nvpair_value_uint64(elem, &intval) != 0) {
417be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
418ad135b5Christopher Siden				break;
419ad135b5Christopher Siden			}
420ad135b5Christopher Siden
421ad135b5Christopher Siden			if (intval != 0) {
422be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
423ad135b5Christopher Siden				break;
424ad135b5Christopher Siden			}
425ad135b5Christopher Siden
426ad135b5Christopher Siden			fname = strchr(propname, '@') + 1;
427ad135b5Christopher Siden			if (zfeature_lookup_name(fname, NULL) != 0) {
428be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
429ad135b5Christopher Siden				break;
430ad135b5Christopher Siden			}
431ad135b5Christopher Siden
432ad135b5Christopher Siden			has_feature = B_TRUE;
433ad135b5Christopher Siden			break;
434990b485lling
435990b485lling		case ZPOOL_PROP_VERSION:
436990b485lling			error = nvpair_value_uint64(elem, &intval);
437990b485lling			if (!error &&
438ad135b5Christopher Siden			    (intval < spa_version(spa) ||
439ad135b5Christopher Siden			    intval > SPA_VERSION_BEFORE_FEATURES ||
440ad135b5Christopher Siden			    has_feature))
441be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
442990b485lling			break;
443990b485lling
444990b485lling		case ZPOOL_PROP_DELEGATION:
445990b485lling		case ZPOOL_PROP_AUTOREPLACE:
446d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
447573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
448990b485lling			error = nvpair_value_uint64(elem, &intval);
449990b485lling			if (!error && intval > 1)
450be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
451990b485lling			break;
452990b485lling
453990b485lling		case ZPOOL_PROP_BOOTFS:
45425f89eeJeff Bonwick			/*
45525f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
45625f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
45725f89eeJeff Bonwick			 * the bootfs property cannot be set.
45825f89eeJeff Bonwick			 */
459990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
460be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
461990b485lling				break;
462990b485lling			}
463990b485lling
464990b485lling			/*
46515e6edfgw			 * Make sure the vdev config is bootable
466990b485lling			 */
46715e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
468be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
469990b485lling				break;
470990b485lling			}
471990b485lling
472990b485lling			reset_bootfs = 1;
473990b485lling
474990b485lling			error = nvpair_value_string(elem, &strval);
475990b485lling
476990b485lling			if (!error) {
477ad135b5Christopher Siden				objset_t *os;
47815e6edfgw				uint64_t compress;
47915e6edfgw
480990b485lling				if (strval == NULL || strval[0] == '\0') {
481990b485lling					objnum = zpool_prop_default_numeric(
482990b485lling					    ZPOOL_PROP_BOOTFS);
483990b485lling					break;
484990b485lling				}
485990b485lling
486503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
487990b485lling					break;
48815e6edfgw
489503ad85Matthew Ahrens				/* Must be ZPL and not gzip compressed. */
490503ad85Matthew Ahrens
491503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
492be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
4933b2aab1Matthew Ahrens				} else if ((error =
4943b2aab1Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
49515e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
4963b2aab1Matthew Ahrens				    &compress)) == 0 &&
49715e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
498be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
49915e6edfgw				} else {
50015e6edfgw					objnum = dmu_objset_id(os);
50115e6edfgw				}
502503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
503990b485lling			}
504990b485lling			break;
505e14bb32Jeff Bonwick
5060a4e951gw		case ZPOOL_PROP_FAILUREMODE:
5070a4e951gw			error = nvpair_value_uint64(elem, &intval);
5080a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
5090a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
510be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5110a4e951gw
5120a4e951gw			/*
5130a4e951gw			 * This is a special case which only occurs when
5140a4e951gw			 * the pool has completely failed. This allows
5150a4e951gw			 * the user to change the in-core failmode property
5160a4e951gw			 * without syncing it out to disk (I/Os might
5170a4e951gw			 * currently be blocked). We do this by returning
5180a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
5190a4e951gw			 * into thinking we encountered a property validation
5200a4e951gw			 * error.
5210a4e951gw			 */
522e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
5230a4e951gw				spa->spa_failmode = intval;
524be6fd75Matthew Ahrens				error = SET_ERROR(EIO);
5250a4e951gw			}
5260a4e951gw			break;
5272f8aaabeschrock
5282f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
5292f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
5302f8aaabeschrock				break;
5312f8aaabeschrock
5322f8aaabeschrock			if (strval[0] == '\0')
5332f8aaabeschrock				break;
5342f8aaabeschrock
5352f8aaabeschrock			if (strcmp(strval, "none") == 0)
5362f8aaabeschrock				break;
5372f8aaabeschrock
5382f8aaabeschrock			if (strval[0] != '/') {
539be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5402f8aaabeschrock				break;
5412f8aaabeschrock			}
5422f8aaabeschrock
5432f8aaabeschrock			slash = strrchr(strval, '/');
5442f8aaabeschrock			ASSERT(slash != NULL);
5452f8aaabeschrock
5462f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5472f8aaabeschrock			    strcmp(slash, "/..") == 0)
548be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5492f8aaabeschrock			break;
550b24ab67Jeff Bonwick
5518704186Dan McDonald		case ZPOOL_PROP_COMMENT:
5528704186Dan McDonald			if ((error = nvpair_value_string(elem, &strval)) != 0)
5538704186Dan McDonald				break;
5548704186Dan McDonald			for (check = strval; *check != '\0'; check++) {
5558704186Dan McDonald				/*
5568704186Dan McDonald				 * The kernel doesn't have an easy isprint()
5578704186Dan McDonald				 * check.  For this kernel check, we merely
5588704186Dan McDonald				 * check ASCII apart from DEL.  Fix this if
5598704186Dan McDonald				 * there is an easy-to-use kernel isprint().
5608704186Dan McDonald				 */
5618704186Dan McDonald				if (*check >= 0x7f) {
562be6fd75Matthew Ahrens					error = SET_ERROR(EINVAL);
5638704186Dan McDonald					break;
5648704186Dan McDonald				}
5658704186Dan McDonald				check++;
5668704186Dan McDonald			}
5678704186Dan McDonald			if (strlen(strval) > ZPROP_MAX_COMMENT)
5688704186Dan McDonald				error = E2BIG;
5698704186Dan McDonald			break;
5708704186Dan McDonald
571b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
572b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
573be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
574b24ab67Jeff Bonwick			else
575b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
576b24ab67Jeff Bonwick			if (error == 0 &&
577b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
578be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
579b24ab67Jeff Bonwick			break;
580990b485lling		}
581990b485lling
582990b485lling		if (error)
583990b485lling			break;
584990b485lling	}
585990b485lling
586990b485lling	if (!error && reset_bootfs) {
587990b485lling		error = nvlist_remove(props,
588990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
589990b485lling
590990b485lling		if (!error) {
591990b485lling			error = nvlist_add_uint64(props,
592990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
593990b485lling		}
594990b485lling	}
595990b485lling
596990b485lling	return (error);
597990b485lling}
598990b485lling
599379c004Eric Schrockvoid
600379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
601379c004Eric Schrock{
602379c004Eric Schrock	char *cachefile;
603379c004Eric Schrock	spa_config_dirent_t *dp;
604379c004Eric Schrock
605379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
606379c004Eric Schrock	    &cachefile) != 0)
607379c004Eric Schrock		return;
608379c004Eric Schrock
609379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
610379c004Eric Schrock	    KM_SLEEP);
611379c004Eric Schrock
612379c004Eric Schrock	if (cachefile[0] == '\0')
613379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
614379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
615379c004Eric Schrock		dp->scd_path = NULL;
616379c004Eric Schrock	else
617379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
618379c004Eric Schrock
619379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
620379c004Eric Schrock	if (need_sync)
621379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
622379c004Eric Schrock}
623379c004Eric Schrock
624990b485llingint
625990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
626990b485lling{
627990b485lling	int error;
628ad135b5Christopher Siden	nvpair_t *elem = NULL;
629379c004Eric Schrock	boolean_t need_sync = B_FALSE;
630990b485lling
631990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
632990b485lling		return (error);
633990b485lling
634379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
635ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
636379c004Eric Schrock
637f9af39bGeorge Wilson		if (prop == ZPOOL_PROP_CACHEFILE ||
638f9af39bGeorge Wilson		    prop == ZPOOL_PROP_ALTROOT ||
639f9af39bGeorge Wilson		    prop == ZPOOL_PROP_READONLY)
640379c004Eric Schrock			continue;
641379c004Eric Schrock
642ad135b5Christopher Siden		if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
643ad135b5Christopher Siden			uint64_t ver;
644ad135b5Christopher Siden
645ad135b5Christopher Siden			if (prop == ZPOOL_PROP_VERSION) {
646ad135b5Christopher Siden				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
647ad135b5Christopher Siden			} else {
648ad135b5Christopher Siden				ASSERT(zpool_prop_feature(nvpair_name(elem)));
649ad135b5Christopher Siden				ver = SPA_VERSION_FEATURES;
650ad135b5Christopher Siden				need_sync = B_TRUE;
651ad135b5Christopher Siden			}
652ad135b5Christopher Siden
653ad135b5Christopher Siden			/* Save time if the version is already set. */
654ad135b5Christopher Siden			if (ver == spa_version(spa))
655ad135b5Christopher Siden				continue;
656ad135b5Christopher Siden
657ad135b5Christopher Siden			/*
658ad135b5Christopher Siden			 * In addition to the pool directory object, we might
659ad135b5Christopher Siden			 * create the pool properties object, the features for
660ad135b5Christopher Siden			 * read object, the features for write object, or the
661ad135b5Christopher Siden			 * feature descriptions object.
662ad135b5Christopher Siden			 */
6633b2aab1Matthew Ahrens			error = dsl_sync_task(spa->spa_name, NULL,
6643b2aab1Matthew Ahrens			    spa_sync_version, &ver, 6);
665ad135b5Christopher Siden			if (error)
666ad135b5Christopher Siden				return (error);
667ad135b5Christopher Siden			continue;
668ad135b5Christopher Siden		}
669ad135b5Christopher Siden
670379c004Eric Schrock		need_sync = B_TRUE;
671379c004Eric Schrock		break;
672379c004Eric Schrock	}
673379c004Eric Schrock
674ad135b5Christopher Siden	if (need_sync) {
6753b2aab1Matthew Ahrens		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
6763b2aab1Matthew Ahrens		    nvp, 6));
677ad135b5Christopher Siden	}
678ad135b5Christopher Siden
679ad135b5Christopher Siden	return (0);
680990b485lling}
681990b485lling
682990b485lling/*
683990b485lling * If the bootfs property value is dsobj, clear it.
684990b485lling */
685990b485llingvoid
686990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
687990b485lling{
688990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
689990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
690990b485lling		    spa->spa_pool_props_object,
691990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
692990b485lling		spa->spa_bootfs = 0;
693990b485lling	}
694990b485lling}
695990b485lling
696dfbb943George Wilson/*ARGSUSED*/
697dfbb943George Wilsonstatic int
6983b2aab1Matthew Ahrensspa_change_guid_check(void *arg, dmu_tx_t *tx)
699dfbb943George Wilson{
7003b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7013b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
702dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
703dfbb943George Wilson	uint64_t vdev_state;
704dfbb943George Wilson
705dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
706dfbb943George Wilson	vdev_state = rvd->vdev_state;
707dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
708dfbb943George Wilson
709dfbb943George Wilson	if (vdev_state != VDEV_STATE_HEALTHY)
710be6fd75Matthew Ahrens		return (SET_ERROR(ENXIO));
711dfbb943George Wilson
712dfbb943George Wilson	ASSERT3U(spa_guid(spa), !=, *newguid);
713dfbb943George Wilson
714dfbb943George Wilson	return (0);
715dfbb943George Wilson}
716dfbb943George Wilson
717dfbb943George Wilsonstatic void
7183b2aab1Matthew Ahrensspa_change_guid_sync(void *arg, dmu_tx_t *tx)
719dfbb943George Wilson{
7203b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7213b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
722dfbb943George Wilson	uint64_t oldguid;
723dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
724dfbb943George Wilson
725dfbb943George Wilson	oldguid = spa_guid(spa);
726dfbb943George Wilson
727dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
728dfbb943George Wilson	rvd->vdev_guid = *newguid;
729dfbb943George Wilson	rvd->vdev_guid_sum += (*newguid - oldguid);
730dfbb943George Wilson	vdev_config_dirty(rvd);
731dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
732dfbb943George Wilson
73320128a0George Wilson	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
734dfbb943George Wilson	    oldguid, *newguid);
735dfbb943George Wilson}
736dfbb943George Wilson
737fa9e406ahrens/*
738e9103aaGarrett D'Amore * Change the GUID for the pool.  This is done so that we can later
739e9103aaGarrett D'Amore * re-import a pool built from a clone of our own vdevs.  We will modify
740e9103aaGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our
741e9103aaGarrett D'Amore * vdevs dirty.  Note that we must make sure that all our vdevs are
742e9103aaGarrett D'Amore * online when we do this, or else any vdevs that weren't present
743e9103aaGarrett D'Amore * would be orphaned from our pool.  We are also going to issue a
744e9103aaGarrett D'Amore * sysevent to update any watchers.
745e9103aaGarrett D'Amore */
746e9103aaGarrett D'Amoreint
747e9103aaGarrett D'Amorespa_change_guid(spa_t *spa)
748e9103aaGarrett D'Amore{
749dfbb943George Wilson	int error;
750dfbb943George Wilson	uint64_t guid;
751e9103aaGarrett D'Amore
752dfbb943George Wilson	mutex_enter(&spa_namespace_lock);
753dfbb943George Wilson	guid = spa_generate_guid(NULL);
754e9103aaGarrett D'Amore
7553b2aab1Matthew Ahrens	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
7563b2aab1Matthew Ahrens	    spa_change_guid_sync, &guid, 5);
757e9103aaGarrett D'Amore
758dfbb943George Wilson	if (error == 0) {
759dfbb943George Wilson		spa_config_sync(spa, B_FALSE, B_TRUE);
760dfbb943George Wilson		spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
761dfbb943George Wilson	}
762e9103aaGarrett D'Amore
763dfbb943George Wilson	mutex_exit(&spa_namespace_lock);
764e9103aaGarrett D'Amore
765dfbb943George Wilson	return (error);
766e9103aaGarrett D'Amore}
767e9103aaGarrett D'Amore
768e9103aaGarrett D'Amore/*
769fa9e406ahrens * ==========================================================================
770fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
771fa9e406ahrens * ==========================================================================
772fa9e406ahrens */
773fa9e406ahrens
774ea8dc4beschrockstatic int
775ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
776ea8dc4beschrock{
777ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
778ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
779ea8dc4beschrock	int ret;
780ea8dc4beschrock
781ea8dc4beschrock	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
782