spa.c revision ec94d32216ed5705f5176582355cc311cf848e73
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.
245aeb947Garrett D'Amore * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
254263d13George Wilson * Copyright (c) 2012 by Delphix. All rights reserved.
265aeb947Garrett D'Amore */
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 */
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>
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 {
80ec94d32Adam Leventhal	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
81ec94d32Adam Leventhal	ZTI_MODE_ONLINE_PERCENT,	/* value is % of online CPUs */
82ec94d32Adam Leventhal	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
83ec94d32Adam Leventhal	ZTI_MODE_NULL,			/* don't create a taskq */
84ec94d32Adam Leventhal	ZTI_NMODES
8535a5a35Jonathan Adams} zti_modes_t;
87ec94d32Adam Leventhal#define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
88ec94d32Adam Leventhal#define	ZTI_PCT(n)	{ ZTI_MODE_ONLINE_PERCENT, (n), 1 }
89ec94d32Adam Leventhal#define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
90ec94d32Adam Leventhal#define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
912e0c549Jonathan Adams
92ec94d32Adam Leventhal#define	ZTI_N(n)	ZTI_P(n, 1)
93ec94d32Adam Leventhal#define	ZTI_ONE		ZTI_N(1)
942e0c549Jonathan Adams
952e0c549Jonathan Adamstypedef struct zio_taskq_info {
96ec94d32Adam Leventhal	zti_modes_t zti_mode;
9780eb36fGeorge Wilson	uint_t zti_value;
98ec94d32Adam Leventhal	uint_t zti_count;
992e0c549Jonathan Adams} zio_taskq_info_t;
1002e0c549Jonathan Adams
1012e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
10235a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
1032e0c549Jonathan Adams};
1042e0c549Jonathan Adams
10580eb36fGeorge Wilson/*
106ec94d32Adam Leventhal * This table defines the taskq settings for each ZFS I/O type. When
107ec94d32Adam Leventhal * initializing a pool, we use this table to create an appropriately sized
108ec94d32Adam Leventhal * taskq. Some operations are low volume and therefore have a small, static
109ec94d32Adam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
110ec94d32Adam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH
111ec94d32Adam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations
112ec94d32Adam Leventhal * are so high frequency and short-lived that the taskq itself can become a a
113ec94d32Adam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
114ec94d32Adam Leventhal * additional degree of parallelism specified by the number of threads per-
115ec94d32Adam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the
116ec94d32Adam Leventhal * particular taskq is chosen at random.
117ec94d32Adam Leventhal *
118ec94d32Adam Leventhal * The different taskq priorities are to handle the different contexts (issue
119ec94d32Adam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
120ec94d32Adam Leventhal * need to be handled with minimum delay.
12180eb36fGeorge Wilson */
12280eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
12380eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
124ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
125ec94d32Adam Leventhal	{ ZTI_N(8),	ZTI_NULL,	ZTI_BATCH,	ZTI_NULL }, /* READ */
126ec94d32Adam Leventhal	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
127ec94d32Adam Leventhal	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
128ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
129ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
1302e0c549Jonathan Adams};
1312e0c549Jonathan Adams
132ad135b5Christopher Sidenstatic dsl_syncfunc_t spa_sync_version;
1333f9d6adLin Lingstatic dsl_syncfunc_t spa_sync_props;
134dfbb943George Wilsonstatic dsl_checkfunc_t spa_change_guid_check;
135dfbb943George Wilsonstatic dsl_syncfunc_t spa_change_guid_sync;
13689a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1371195e68Mark J Musantestatic int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1381195e68Mark J Musante    spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1391195e68Mark J Musante    char **ereport);
140cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
14235a5a35Jonathan Adamsuint_t		zio_taskq_batch_pct = 100;	/* 1 thread per cpu in pset */
14335a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
14435a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
14535a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
14635a5a35Jonathan Adams
14735a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
14801f55e4George Wilsonextern int	zfs_sync_pass_deferred_free;
14935a5a35Jonathan Adams
15035a5a35Jonathan Adams/*
15135a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
15235a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
15335a5a35Jonathan Adams */
15435a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
15535a5a35Jonathan Adams
157990b485lling * ==========================================================================
158990b485lling * SPA properties routines
159990b485lling * ==========================================================================
160990b485lling */
163990b485lling * Add a (source=src, propname=propval) list to an nvlist.
164990b485lling */
1659d82f4fllingstatic void
166990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
167990b485lling    uint64_t intval, zprop_source_t src)
169990b485lling	const char *propname = zpool_prop_to_name(prop);
170990b485lling	nvlist_t *propval;
1729d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1739d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1759d82f4flling	if (strval != NULL)
1769d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1779d82f4flling	else
1789d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1809d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
181990b485lling	nvlist_free(propval);
185990b485lling * Get property values from the spa configuration.
186990b485lling */
1879d82f4fllingstatic void
188990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1904263d13George Wilson	vdev_t *rvd = spa->spa_root_vdev;
191ad135b5Christopher Siden	dsl_pool_t *pool = spa->spa_dsl_pool;
192379c004Eric Schrock	uint64_t size;
193485bbbfGeorge Wilson	uint64_t alloc;
1944263d13George Wilson	uint64_t space;
195990b485lling	uint64_t cap, version;
196990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
197c5904d1eschrock	spa_config_dirent_t *dp;
199e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
200e14bb32Jeff Bonwick
2014263d13George Wilson	if (rvd != NULL) {
202485bbbfGeorge Wilson		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
203b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
204379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
205379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
206485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
207485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
208485bbbfGeorge Wilson		    size - alloc, src);
2094263d13George Wilson
2104263d13George Wilson		space = 0;
2114263d13George Wilson		for (int c = 0; c < rvd->vdev_children; c++) {
2124263d13George Wilson			vdev_t *tvd = rvd->vdev_child[c];
2134263d13George Wilson			space += tvd->vdev_max_asize - tvd->vdev_asize;
2144263d13George Wilson		}
2154263d13George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
2164263d13George Wilson		    src);
2174263d13George Wilson
218f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
219f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
220379c004Eric Schrock
221485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
222379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
223379c004Eric Schrock
224b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
225b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
226b24ab67Jeff Bonwick
227379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
2284263d13George Wilson		    rvd->vdev_state, src);
229379c004Eric Schrock
230379c004Eric Schrock		version = spa_version(spa);
231379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
232379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
233379c004Eric Schrock		else
234379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
235379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
236379c004Eric Schrock	}
238ad135b5Christopher Siden	if (pool != NULL) {
239ad135b5Christopher Siden		dsl_dir_t *freedir = pool->dp_free_dir;
240ad135b5Christopher Siden
241ad135b5Christopher Siden		/*
242ad135b5Christopher Siden		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
243ad135b5Christopher Siden		 * when opening pools before this version freedir will be NULL.
244ad135b5Christopher Siden		 */
245ad135b5Christopher Siden		if (freedir != NULL) {
246ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
247ad135b5Christopher Siden			    freedir->dd_phys->dd_used_bytes, src);
248ad135b5Christopher Siden		} else {
249ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
250ad135b5Christopher Siden			    NULL, 0, src);
251ad135b5Christopher Siden		}
252ad135b5Christopher Siden	}
253ad135b5Christopher Siden
2549d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2568704186Dan McDonald	if (spa->spa_comment != NULL) {
2578704186Dan McDonald		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
2588704186Dan McDonald		    0, ZPROP_SRC_LOCAL);
2598704186Dan McDonald	}
2608704186Dan McDonald
2619d82f4flling	if (spa->spa_root != NULL)
2629d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2639d82f4flling		    0, ZPROP_SRC_LOCAL);
265c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
266c5904d1eschrock		if (dp->scd_path == NULL) {
2679d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
268c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
269c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2709d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
271c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2722f8aaabeschrock		}
2732f8aaabeschrock	}
277990b485lling * Get zpool property values.
278990b485lling */
280990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
282b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
283990b485lling	zap_cursor_t zc;
284990b485lling	zap_attribute_t za;
285990b485lling	int err;
2879d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
289e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
290e14bb32Jeff Bonwick
291990b485lling	/*
292990b485lling	 * Get properties from the spa config.
293990b485lling	 */
2949d82f4flling	spa_prop_get_config(spa, nvp);
296990b485lling	/* If no pool property object, no more prop to get. */
297afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
298990b485lling		mutex_exit(&spa->spa_props_lock);
299990b485lling		return (0);
300990b485lling	}
302990b485lling	/*
303990b485lling	 * Get properties from the MOS pool property object.
304990b485lling	 */
305990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
306990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
307990b485lling	    zap_cursor_advance(&zc)) {
308990b485lling		uint64_t intval = 0;
309990b485lling		char *strval = NULL;
310990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
311990b485lling		zpool_prop_t prop;
313990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
314990b485lling			continue;
316990b485lling		switch (za.za_integer_length) {
317990b485lling		case 8:
318990b485lling			/* integer property */
319990b485lling			if (za.za_first_integer !=
320990b485lling			    zpool_prop_default_numeric(prop))
321990b485lling				src = ZPROP_SRC_LOCAL;
323990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
324990b485lling				dsl_pool_t *dp;
325990b485lling				dsl_dataset_t *ds = NULL;
327990b485lling				dp = spa_get_dsl(spa);
328990b485lling				rw_enter(&dp->dp_config_rwlock, RW_READER);
329745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
330745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
331990b485lling					rw_exit(&dp->dp_config_rwlock);
332990b485lling					break;
333990b485lling				}
335990b485lling				strval = kmem_alloc(
336990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
337990b485lling				    KM_SLEEP);
338990b485lling				dsl_dataset_name(ds, strval);
339745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
340990b485lling				rw_exit(&dp->dp_config_rwlock);
341990b485lling			} else {
342990b485lling				strval = NULL;
343990b485lling				intval = za.za_first_integer;
344990b485lling			}
3469d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
348990b485lling			if (strval != NULL)
349990b485lling				kmem_free(strval,
350990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
352990b485lling			break;
354990b485lling		case 1:
355990b485lling			/* string property */
356990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
357990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
358990b485lling			    za.za_name, 1, za.za_num_integers, strval);
359990b485lling			if (err) {
360990b485lling				kmem_free(strval, za.za_num_integers);
361990b485lling				break;
362990b485lling			}
3639d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
364990b485lling			kmem_free(strval, za.za_num_integers);
365990b485lling			break;
367990b485lling		default:
368990b485lling			break;
369990b485lling		}
370990b485lling	}
371990b485lling	zap_cursor_fini(&zc);
372990b485lling	mutex_exit(&spa->spa_props_lock);
374990b485lling	if (err && err != ENOENT) {
375990b485lling		nvlist_free(*nvp);
3769d82f4flling		*nvp = NULL;
377990b485lling		return (err);
378990b485lling	}
380990b485lling	return (0);
384990b485lling * Validate the given pool properties nvlist and modify the list
385990b485lling * for the property values to be set.
386990b485lling */
387990b485llingstatic int
388990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
390990b485lling	nvpair_t *elem;
391990b485lling	int error = 0, reset_bootfs = 0;
392d5285caGeorge Wilson	uint64_t objnum = 0;
393ad135b5Christopher Siden	boolean_t has_feature = B_FALSE;
395990b485lling	elem = NULL;
396990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
397990b485lling		uint64_t intval;
398ad135b5Christopher Siden		char *strval, *slash, *check, *fname;
399ad135b5Christopher Siden		const char *propname = nvpair_name(elem);
400ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(propname);
401ad135b5Christopher Siden
402ad135b5Christopher Siden		switch (prop) {
403ad135b5Christopher Siden		case ZPROP_INVAL:
404ad135b5Christopher Siden			if (!zpool_prop_feature(propname)) {
405ad135b5Christopher Siden				error = EINVAL;
406ad135b5Christopher Siden				break;
407ad135b5Christopher Siden			}
409ad135b5Christopher Siden			/*
410ad135b5Christopher Siden			 * Sanitize the input.
411ad135b5Christopher Siden			 */
412ad135b5Christopher Siden			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
413ad135b5Christopher Siden				error = EINVAL;
414ad135b5Christopher Siden				break;
415ad135b5Christopher Siden			}
417ad135b5Christopher Siden			if (nvpair_value_uint64(elem, &intval) != 0) {
418ad135b5Christopher Siden				error = EINVAL;
419ad135b5Christopher Siden				break;
420ad135b5Christopher Siden			}
421ad135b5Christopher Siden
422ad135b5Christopher Siden			if (intval != 0) {
423ad135b5Christopher Siden				error = EINVAL;
424ad135b5Christopher Siden				break;
425ad135b5Christopher Siden			}
426ad135b5Christopher Siden
427ad135b5Christopher Siden			fname = strchr(propname, '@') + 1;
428ad135b5Christopher Siden			if (zfeature_lookup_name(fname, NULL) != 0) {
429ad135b5Christopher Siden				error = EINVAL;
430ad135b5Christopher Siden				break;
431ad135b5Christopher Siden			}
432ad135b5Christopher Siden
433ad135b5Christopher Siden			has_feature = B_TRUE;
434ad135b5Christopher Siden			break;
436990b485lling		case ZPOOL_PROP_VERSION:
437990b485lling			error = nvpair_value_uint64(elem, &intval);
438990b485lling			if (!error &&
439ad135b5Christopher Siden			    (intval < spa_version(spa) ||
440ad135b5Christopher Siden			    intval > SPA_VERSION_BEFORE_FEATURES ||
441ad135b5Christopher Siden			    has_feature))
442990b485lling				error = EINVAL;
443990b485lling			break;
445990b485lling		case ZPOOL_PROP_DELEGATION:
446990b485lling		case ZPOOL_PROP_AUTOREPLACE:
447d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
448573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
449990b485lling			error = nvpair_value_uint64(elem, &intval);
450990b485lling			if (!error && intval > 1)
451990b485lling				error = EINVAL;
452990b485lling			break;
454990b485lling		case ZPOOL_PROP_BOOTFS:
45525f89eeJeff Bonwick			/*
45625f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
45725f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
45825f89eeJeff Bonwick			 * the bootfs property cannot be set.
45925f89eeJeff Bonwick			 */
460990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
461990b485lling				error = ENOTSUP;
462990b485lling				break;
463990b485lling			}
465990b485lling			/*
46615e6edfgw			 * Make sure the vdev config is bootable
467990b485lling			 */
46815e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
469990b485lling				error = ENOTSUP;
470990b485lling				break;
471990b485lling			}
473990b485lling			reset_bootfs = 1;
475990b485lling			error = nvpair_value_string(elem, &strval);
477990b485lling			if (!error) {
478ad135b5Christopher Siden				objset_t *os;
47915e6edfgw				uint64_t compress;
481990b485lling				if (strval == NULL || strval[0] == '\0') {
482990b485lling					objnum = zpool_prop_default_numeric(
483990b485lling					    ZPOOL_PROP_BOOTFS);
484990b485lling					break;
485990b485lling				}
487503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
488990b485lling					break;
490503ad85Matthew Ahrens				/* Must be ZPL and not gzip compressed. */
491503ad85Matthew Ahrens
492503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
493503ad85Matthew Ahrens					error = ENOTSUP;
494503ad85Matthew Ahrens				} else if ((error = dsl_prop_get_integer(strval,
49515e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
49615e6edfgw				    &compress, NULL)) == 0 &&
49715e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
49815e6edfgw					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))
5100a4e951gw				error = EINVAL;
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;
5240a4e951gw				error = EIO;
5250a4e951gw			}
5260a4e951gw			break;
5282f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
5292f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
5302f8aaabeschrock				break;
5322f8aaabeschrock			if (strval[0] == '\0')
5332f8aaabeschrock				break;
5352f8aaabeschrock			if (strcmp(strval, "none") == 0)
5362f8aaabeschrock				break;
5382f8aaabeschrock			if (strval[0] != '/') {
5392f8aaabeschrock				error = EINVAL;
5402f8aaabeschrock				break;
5412f8aaabeschrock			}
5432f8aaabeschrock			slash = strrchr(strval, '/');
5442f8aaabeschrock			ASSERT(slash != NULL);
5462f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5472f8aaabeschrock			    strcmp(slash, "/..") == 0)
5482f8aaabeschrock				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) {
5628704186Dan McDonald					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)
573b24ab67Jeff Bonwick				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)
578b24ab67Jeff Bonwick				error = EINVAL;
579b24ab67Jeff Bonwick			break;
580990b485lling		}
582990b485lling		if (error)
583990b485lling			break;
584990b485lling	}
586990b485lling	if (!error && reset_bootfs) {
587990b485lling		error = nvlist_remove(props,
588990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
590990b485lling		if (!error) {
591990b485lling			error = nvlist_add_uint64(props,
592990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
593990b485lling		}
594990b485lling	}
596990b485lling	return (error);
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
625990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
627990b485lling	int error;
628ad135b5Christopher Siden	nvpair_t *elem = NULL;
629379c004Eric Schrock	boolean_t need_sync = B_FALSE;
631990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
632990b485lling		return (error);
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			 */
663ad135b5Christopher Siden			error = dsl_sync_task_do(spa_get_dsl(spa), NULL,
664ad135b5Christopher Siden			    spa_sync_version, spa, &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) {
675379c004Eric Schrock		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
676ad135b5Christopher Siden		    spa, nvp, 6));
677ad135b5Christopher Siden	}
678ad135b5Christopher Siden
679ad135b5Christopher Siden	return (0);
683990b485lling * If the bootfs property value is dsobj, clear it.
684990b485lling */
686990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
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	}
696dfbb943George Wilson/*ARGSUSED*/
697dfbb943George Wilsonstatic int
698dfbb943George Wilsonspa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx)
699dfbb943George Wilson{
700dfbb943George Wilson	spa_t *spa = arg1;
701dfbb943George Wilson	uint64_t *newguid = arg2;
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)
710dfbb943George Wilson		return (ENXIO);
711dfbb943George Wilson
712dfbb943George Wilson	ASSERT3U(spa_guid(spa), !=, *newguid);
713dfbb943George Wilson
714dfbb943George Wilson	return (0);
715dfbb943George Wilson}
716dfbb943George Wilson
717dfbb943George Wilsonstatic void
718dfbb943George Wilsonspa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx)
719dfbb943George Wilson{
720dfbb943George Wilson	spa_t *spa = arg1;
721dfbb943George Wilson	uint64_t *newguid = arg2;
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
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
755dfbb943George Wilson	error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check,
756dfbb943George Wilson	    spa_change_guid_sync, spa, &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 */
774ea8dc4beschrockstatic int
775ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
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;