spa.c revision 1b497ab83e8f1c58bba5da59c649207a442a4720
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.
241b497abAdam H. Leventhal * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25efb4a87Yuri Pankov * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
265aeb947Garrett D'Amore */
293e30c24Will Andrews * SPA: Storage Pool Allocator
303e30c24Will Andrews *
31fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
32fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
33fa9e406ahrens * pool.
34fa9e406ahrens */
36fa9e406ahrens#include <sys/zfs_context.h>
37ea8dc4beschrock#include <sys/fm/fs/zfs.h>
38fa9e406ahrens#include <sys/spa_impl.h>
39fa9e406ahrens#include <sys/zio.h>
40fa9e406ahrens#include <sys/zio_checksum.h>
41fa9e406ahrens#include <sys/dmu.h>
42fa9e406ahrens#include <sys/dmu_tx.h>
43fa9e406ahrens#include <sys/zap.h>
44fa9e406ahrens#include <sys/zil.h>
45b24ab67Jeff Bonwick#include <sys/ddt.h>
46fa9e406ahrens#include <sys/vdev_impl.h>
47fa9e406ahrens#include <sys/metaslab.h>
4888ecc94George Wilson#include <sys/metaslab_impl.h>
49fa9e406ahrens#include <sys/uberblock_impl.h>
50fa9e406ahrens#include <sys/txg.h>
51fa9e406ahrens#include <sys/avl.h>
52fa9e406ahrens#include <sys/dmu_traverse.h>
53b1b8ab3lling#include <sys/dmu_objset.h>
54fa9e406ahrens#include <sys/unique.h>
55fa9e406ahrens#include <sys/dsl_pool.h>
56b1b8ab3lling#include <sys/dsl_dataset.h>
57fa9e406ahrens#include <sys/dsl_dir.h>
58fa9e406ahrens#include <sys/dsl_prop.h>
59b1b8ab3lling#include <sys/dsl_synctask.h>
60fa9e406ahrens#include <sys/fs/zfs.h>
61fa94a07brendan#include <sys/arc.h>
62fa9e406ahrens#include <sys/callb.h>
639517395ek#include <sys/systeminfo.h>
64e7cbe64gw#include <sys/spa_boot.h>
65573ca77George Wilson#include <sys/zfs_ioctl.h>
663f9d6adLin Ling#include <sys/dsl_scan.h>
67ad135b5Christopher Siden#include <sys/zfeature.h>
683b2aab1Matthew Ahrens#include <sys/dsl_destroy.h>
705679c89jv#ifdef	_KERNEL
71dedec47Jack Meng#include <sys/bootprops.h>
7235a5a35Jonathan Adams#include <sys/callb.h>
7335a5a35Jonathan Adams#include <sys/cpupart.h>
7435a5a35Jonathan Adams#include <sys/pool.h>
7535a5a35Jonathan Adams#include <sys/sysdc.h>
7635a5a35Jonathan Adams#include <sys/zone.h>
775679c89jv#endif	/* _KERNEL */
79990b485lling#include "zfs_prop.h"
80b7b9745perrin#include "zfs_comutil.h"
823cb69f7Will Andrews/*
833cb69f7Will Andrews * The interval, in seconds, at which failed configuration cache file writes
843cb69f7Will Andrews * should be retried.
853cb69f7Will Andrews */
863cb69f7Will Andrewsstatic int zfs_ccw_retry_interval = 300;
873cb69f7Will Andrews
8835a5a35Jonathan Adamstypedef enum zti_modes {
89ec94d32Adam Leventhal	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
90ec94d32Adam Leventhal	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
91ec94d32Adam Leventhal	ZTI_MODE_NULL,			/* don't create a taskq */
92ec94d32Adam Leventhal	ZTI_NMODES
9335a5a35Jonathan Adams} zti_modes_t;
95ec94d32Adam Leventhal#define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
96ec94d32Adam Leventhal#define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
97ec94d32Adam Leventhal#define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
982e0c549Jonathan Adams
99ec94d32Adam Leventhal#define	ZTI_N(n)	ZTI_P(n, 1)
100ec94d32Adam Leventhal#define	ZTI_ONE		ZTI_N(1)
1012e0c549Jonathan Adams
1022e0c549Jonathan Adamstypedef struct zio_taskq_info {
103ec94d32Adam Leventhal	zti_modes_t zti_mode;
10480eb36fGeorge Wilson	uint_t zti_value;
105ec94d32Adam Leventhal	uint_t zti_count;
1062e0c549Jonathan Adams} zio_taskq_info_t;
1072e0c549Jonathan Adams
1082e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
10935a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
1102e0c549Jonathan Adams};
1112e0c549Jonathan Adams
11280eb36fGeorge Wilson/*
113ec94d32Adam Leventhal * This table defines the taskq settings for each ZFS I/O type. When
114ec94d32Adam Leventhal * initializing a pool, we use this table to create an appropriately sized
115ec94d32Adam Leventhal * taskq. Some operations are low volume and therefore have a small, static
116ec94d32Adam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
117ec94d32Adam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH
118ec94d32Adam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations
119ec94d32Adam Leventhal * are so high frequency and short-lived that the taskq itself can become a a
120ec94d32Adam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
121ec94d32Adam Leventhal * additional degree of parallelism specified by the number of threads per-
122ec94d32Adam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the
123ec94d32Adam Leventhal * particular taskq is chosen at random.
124ec94d32Adam Leventhal *
125ec94d32Adam Leventhal * The different taskq priorities are to handle the different contexts (issue
126ec94d32Adam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
127ec94d32Adam Leventhal * need to be handled with minimum delay.
12880eb36fGeorge Wilson */
12980eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
13080eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
131ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
1321b497abAdam H. Leventhal	{ ZTI_N(8),	ZTI_NULL,	ZTI_P(12, 8),	ZTI_NULL }, /* READ */
133ec94d32Adam Leventhal	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
134ec94d32Adam Leventhal	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
135ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
136ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
1372e0c549Jonathan Adams};
1382e0c549Jonathan Adams
1393b2aab1Matthew Ahrensstatic void spa_sync_version(void *arg, dmu_tx_t *tx);
1403b2aab1Matthew Ahrensstatic void spa_sync_props(void *arg, dmu_tx_t *tx);
14189a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1421195e68Mark J Musantestatic int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1431195e68Mark J Musante    spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1441195e68Mark J Musante    char **ereport);
145cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
14769962b5Matthew Ahrensuint_t		zio_taskq_batch_pct = 75;	/* 1 thread per cpu in pset */
14835a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
14935a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
15035a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
15135a5a35Jonathan Adams
15235a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
15301f55e4George Wilsonextern int	zfs_sync_pass_deferred_free;
15435a5a35Jonathan Adams
15535a5a35Jonathan Adams/*
15635a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
15735a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
15835a5a35Jonathan Adams */
15935a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
16035a5a35Jonathan Adams
162990b485lling * ==========================================================================
163990b485lling * SPA properties routines
164990b485lling * ==========================================================================
165990b485lling */
168990b485lling * Add a (source=src, propname=propval) list to an nvlist.
169990b485lling */
1709d82f4fllingstatic void
171990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
172990b485lling    uint64_t intval, zprop_source_t src)
174990b485lling	const char *propname = zpool_prop_to_name(prop);
175990b485lling	nvlist_t *propval;
1779d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1789d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1809d82f4flling	if (strval != NULL)
1819d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1829d82f4flling	else
1839d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1859d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
186990b485lling	nvlist_free(propval);
190990b485lling * Get property values from the spa configuration.
191990b485lling */
1929d82f4fllingstatic void
193990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1954263d13George Wilson	vdev_t *rvd = spa->spa_root_vdev;
196ad135b5Christopher Siden	dsl_pool_t *pool = spa->spa_dsl_pool;
197379c004Eric Schrock	uint64_t size;
198485bbbfGeorge Wilson	uint64_t alloc;
1994263d13George Wilson	uint64_t space;
200990b485lling	uint64_t cap, version;
201990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
202c5904d1eschrock	spa_config_dirent_t *dp;
204e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
205e14bb32Jeff Bonwick
2064263d13George Wilson	if (rvd != NULL) {
207485bbbfGeorge Wilson		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
208b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
209379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
210379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
211485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
212485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
213485bbbfGeorge Wilson		    size - alloc, src);
2144263d13George Wilson
2154263d13George Wilson		space = 0;
2164263d13George Wilson		for (int c = 0; c < rvd->vdev_children; c++) {
2174263d13George Wilson			vdev_t *tvd = rvd->vdev_child[c];
2184263d13George Wilson			space += tvd->vdev_max_asize - tvd->vdev_asize;
2194263d13George Wilson		}
2204263d13George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
2214263d13George Wilson		    src);
2224263d13George Wilson
223f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
224f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
225379c004Eric Schrock
226485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
227379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
228379c004Eric Schrock
229b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
230b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
231b24ab67Jeff Bonwick
232379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
2334263d13George Wilson		    rvd->vdev_state, src);
234379c004Eric Schrock
235379c004Eric Schrock		version = spa_version(spa);
236379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
237379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
238379c004Eric Schrock		else
239379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
240379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
241379c004Eric Schrock	}
243ad135b5Christopher Siden	if (pool != NULL) {
244ad135b5Christopher Siden		dsl_dir_t *freedir = pool->dp_free_dir;
245ad135b5Christopher Siden
246ad135b5Christopher Siden		/*
247ad135b5Christopher Siden		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
248ad135b5Christopher Siden		 * when opening pools before this version freedir will be NULL.
249ad135b5Christopher Siden		 */
250ad135b5Christopher Siden		if (freedir != NULL) {
251ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
252ad135b5Christopher Siden			    freedir->dd_phys->dd_used_bytes, src);
253ad135b5Christopher Siden		} else {
254ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
255ad135b5Christopher Siden			    NULL, 0, src);
256ad135b5Christopher Siden		}
257ad135b5Christopher Siden	}
258ad135b5Christopher Siden
2599d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2618704186Dan McDonald	if (spa->spa_comment != NULL) {
2628704186Dan McDonald		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
2638704186Dan McDonald		    0, ZPROP_SRC_LOCAL);
2648704186Dan McDonald	}
2658704186Dan McDonald
2669d82f4flling	if (spa->spa_root != NULL)
2679d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2689d82f4flling		    0, ZPROP_SRC_LOCAL);
270c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
271c5904d1eschrock		if (dp->scd_path == NULL) {
2729d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
273c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
274c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2759d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
276c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2772f8aaabeschrock		}
2782f8aaabeschrock	}
282990b485lling * Get zpool property values.
283990b485lling */
285990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
287b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
288990b485lling	zap_cursor_t zc;
289990b485lling	zap_attribute_t za;
290990b485lling	int err;
2929d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
294e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
295e14bb32Jeff Bonwick
296990b485lling	/*
297990b485lling	 * Get properties from the spa config.
298990b485lling	 */
2999d82f4flling	spa_prop_get_config(spa, nvp);
301990b485lling	/* If no pool property object, no more prop to get. */
302afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
303990b485lling		mutex_exit(&spa->spa_props_lock);
304990b485lling		return (0);
305990b485lling	}
307990b485lling	/*
308990b485lling	 * Get properties from the MOS pool property object.
309990b485lling	 */
310990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
311990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
312990b485lling	    zap_cursor_advance(&zc)) {
313990b485lling		uint64_t intval = 0;
314990b485lling		char *strval = NULL;
315990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
316990b485lling		zpool_prop_t prop;
318990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
319990b485lling			continue;
321990b485lling		switch (za.za_integer_length) {
322990b485lling		case 8:
323990b485lling			/* integer property */
324990b485lling			if (za.za_first_integer !=
325990b485lling			    zpool_prop_default_numeric(prop))
326990b485lling				src = ZPROP_SRC_LOCAL;
328990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
329990b485lling				dsl_pool_t *dp;
330990b485lling				dsl_dataset_t *ds = NULL;
332990b485lling				dp = spa_get_dsl(spa);
3333b2aab1Matthew Ahrens				dsl_pool_config_enter(dp, FTAG);
334745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
335745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
3363b2aab1Matthew Ahrens					dsl_pool_config_exit(dp, FTAG);
337990b485lling					break;
338990b485lling				}
340990b485lling				strval = kmem_alloc(
341990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
342990b485lling				    KM_SLEEP);
343990b485lling				dsl_dataset_name(ds, strval);
344745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
3453b2aab1Matthew Ahrens				dsl_pool_config_exit(dp, FTAG);
346990b485lling			} else {
347990b485lling				strval = NULL;
348990b485lling				intval = za.za_first_integer;
349990b485lling			}
3519d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
353990b485lling			if (strval != NULL)
354990b485lling				kmem_free(strval,
355990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
357990b485lling			break;
359990b485lling		case 1:
360990b485lling			/* string property */
361990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
362990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
363990b485lling			    za.za_name, 1, za.za_num_integers, strval);
364990b485lling			if (err) {
365990b485lling				kmem_free(strval, za.za_num_integers);
366990b485lling				break;
367990b485lling			}
3689d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
369990b485lling			kmem_free(strval, za.za_num_integers);
370990b485lling			break;
372990b485lling		default:
373990b485lling			break;
374990b485lling		}
375990b485lling	}
376990b485lling	zap_cursor_fini(&zc);
377990b485lling	mutex_exit(&spa->spa_props_lock);
379990b485lling	if (err && err != ENOENT) {
380990b485lling		nvlist_free(*nvp);
3819d82f4flling		*nvp = NULL;
382990b485lling		return (err);
383990b485lling	}
385990b485lling	return (0);
389990b485lling * Validate the given pool properties nvlist and modify the list
390990b485lling * for the property values to be set.
391990b485lling */
392990b485llingstatic int
393990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
395990b485lling	nvpair_t *elem;
396990b485lling	int error = 0, reset_bootfs = 0;
397d5285caGeorge Wilson	uint64_t objnum = 0;
398ad135b5Christopher Siden	boolean_t has_feature = B_FALSE;
400990b485lling	elem = NULL;
401990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
402990b485lling		uint64_t intval;
403ad135b5Christopher Siden		char *strval, *slash, *check, *fname;
404ad135b5Christopher Siden		const char *propname = nvpair_name(elem);
405ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(propname);
406ad135b5Christopher Siden
407ad135b5Christopher Siden		switch (prop) {
408ad135b5Christopher Siden		case ZPROP_INVAL:
409ad135b5Christopher Siden			if (!zpool_prop_feature(propname)) {
410be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
411ad135b5Christopher Siden				break;
412ad135b5Christopher Siden			}
414ad135b5Christopher Siden			/*
415ad135b5Christopher Siden			 * Sanitize the input.
416ad135b5Christopher Siden			 */
417ad135b5Christopher Siden			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
418be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
419ad135b5Christopher Siden				break;
420ad135b5Christopher Siden			}
422ad135b5Christopher Siden			if (nvpair_value_uint64(elem, &intval) != 0) {
423be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
424ad135b5Christopher Siden				break;
425ad135b5Christopher Siden			}
426ad135b5Christopher Siden
427ad135b5Christopher Siden			if (intval != 0) {
428be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
429ad135b5Christopher Siden				break;
430ad135b5Christopher Siden			}
431ad135b5Christopher Siden
432ad135b5Christopher Siden			fname = strchr(propname, '@') + 1;
433ad135b5Christopher Siden			if (zfeature_lookup_name(fname, NULL) != 0) {
434be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
435ad135b5Christopher Siden				break;
436ad135b5Christopher Siden			}
437ad135b5Christopher Siden
438ad135b5Christopher Siden			has_feature = B_TRUE;
439ad135b5Christopher Siden			break;
441990b485lling		case ZPOOL_PROP_VERSION:
442990b485lling			error = nvpair_value_uint64(elem, &intval);
443990b485lling			if (!error &&
444ad135b5Christopher Siden			    (intval < spa_version(spa) ||
445ad135b5Christopher Siden			    intval > SPA_VERSION_BEFORE_FEATURES ||
446ad135b5Christopher Siden			    has_feature))
447be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
448990b485lling			break;
450990b485lling		case ZPOOL_PROP_DELEGATION:
451990b485lling		case ZPOOL_PROP_AUTOREPLACE:
452d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
453573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
454990b485lling			error = nvpair_value_uint64(elem, &intval);
455990b485lling			if (!error && intval > 1)
456be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
457990b485lling			break;
459990b485lling		case ZPOOL_PROP_BOOTFS:
46025f89eeJeff Bonwick			/*
46125f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
46225f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
46325f89eeJeff Bonwick			 * the bootfs property cannot be set.
46425f89eeJeff Bonwick			 */
465990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
466be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
467990b485lling				break;
468990b485lling			}
470990b485lling			/*
47115e6edfgw			 * Make sure the vdev config is bootable
472990b485lling			 */
47315e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
474be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
475990b485lling				break;
476990b485lling			}
478990b485lling			reset_bootfs = 1;
480990b485lling			error = nvpair_value_string(elem, &strval);
482990b485lling			if (!error) {
483ad135b5Christopher Siden				objset_t *os;
48415e6edfgw				uint64_t compress;
486990b485lling				if (strval == NULL || strval[0] == '\0') {
487990b485lling					objnum = zpool_prop_default_numeric(
488990b485lling					    ZPOOL_PROP_BOOTFS);
489990b485lling					break;
490990b485lling				}
492503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
493990b485lling					break;
495503ad85Matthew Ahrens				/* Must be ZPL and not gzip compressed. */
496503ad85Matthew Ahrens
497503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
498be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
4993b2aab1Matthew Ahrens				} else if ((error =
5003b2aab1Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
50115e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
5023b2aab1Matthew Ahrens				    &compress)) == 0 &&
50315e6edfgw				    !BOOTFS_COMPRESS_VALID(compress)) {
504be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
50515e6edfgw				} else {
50615e6edfgw					objnum = dmu_objset_id(os);
50715e6edfgw				}
508503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
509990b485lling			}
510990b485lling			break;
511e14bb32Jeff Bonwick
5120a4e951gw		case ZPOOL_PROP_FAILUREMODE:
5130a4e951gw			error = nvpair_value_uint64(elem, &intval);
5140a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
5150a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
516be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5180a4e951gw			/*
5190a4e951gw			 * This is a special case which only occurs when
5200a4e951gw			 * the pool has completely failed. This allows
5210a4e951gw			 * the user to change the in-core failmode property
5220a4e951gw			 * without syncing it out to disk (I/Os might
5230a4e951gw			 * currently be blocked). We do this by returning
5240a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
5250a4e951gw			 * into thinking we encountered a property validation
5260a4e951gw			 * error.
5270a4e951gw			 */
528e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
5290a4e951gw				spa->spa_failmode = intval;
530be6fd75Matthew Ahrens				error = SET_ERROR(EIO);
5310a4e951gw			}
5320a4e951gw			break;
5342f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
5352f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
5362f8aaabeschrock				break;
5382f8aaabeschrock			if (strval[0] == '\0')
5392f8aaabeschrock				break;
5412f8aaabeschrock			if (strcmp(strval, "none") == 0)
5422f8aaabeschrock				break;
5442f8aaabeschrock			if (strval[0] != '/') {
545be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5462f8aaabeschrock				break;
5472f8aaabeschrock			}
5492f8aaabeschrock			slash = strrchr(strval, '/');
5502f8aaabeschrock			ASSERT(slash != NULL);
5522f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5532f8aaabeschrock			    strcmp(slash, "/..") == 0)
554be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5552f8aaabeschrock			break;
556b24ab67Jeff Bonwick
5578704186Dan McDonald		case ZPOOL_PROP_COMMENT:
5588704186Dan McDonald			if ((error = nvpair_value_string(elem, &strval)) != 0)
5598704186Dan McDonald				break;
5608704186Dan McDonald			for (check = strval; *check != '\0'; check++) {
5618704186Dan McDonald				/*
5628704186Dan McDonald				 * The kernel doesn't have an easy isprint()
5638704186Dan McDonald				 * check.  For this kernel check, we merely
5648704186Dan McDonald				 * check ASCII apart from DEL.  Fix this if
5658704186Dan McDonald				 * there is an easy-to-use kernel isprint().
5668704186Dan McDonald				 */
5678704186Dan McDonald				if (*check >= 0x7f) {
568be6fd75Matthew Ahrens					error = SET_ERROR(EINVAL);
5698704186Dan McDonald					break;
5708704186Dan McDonald				}
5718704186Dan McDonald				check++;
5728704186Dan McDonald			}
5738704186Dan McDonald			if (strlen(strval) > ZPROP_MAX_COMMENT)
5748704186Dan McDonald				error = E2BIG;
5758704186Dan McDonald			break;
5768704186Dan McDonald
577b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
578b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
579be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
580b24ab67Jeff Bonwick			else
581b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
582b24ab67Jeff Bonwick			if (error == 0 &&
583b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
584be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
585b24ab67Jeff Bonwick			break;
586990b485lling		}
588990b485lling		if (error)
589990b485lling			break;
590990b485lling	}
592990b485lling	if (!error && reset_bootfs) {
593990b485lling		error = nvlist_remove(props,
594990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
596990b485lling		if (!error) {
597990b485lling			error = nvlist_add_uint64(props,
598990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
599990b485lling		}
600990b485lling	}
602990b485lling	return (error);
605379c004Eric Schrockvoid
606379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
607379c004Eric Schrock{
608379c004Eric Schrock	char *cachefile;
609379c004Eric Schrock	spa_config_dirent_t *dp;
610379c004Eric Schrock
611379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
612379c004Eric Schrock	    &cachefile) != 0)
613379c004Eric Schrock		return;
614379c004Eric Schrock
615379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
616379c004Eric Schrock	    KM_SLEEP);
617379c004Eric Schrock
618379c004Eric Schrock	if (cachefile[0] == '\0')
619379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
620379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
621379c004Eric Schrock		dp->scd_path = NULL;
622379c004Eric Schrock	else
623379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
624379c004Eric Schrock
625379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
626379c004Eric Schrock	if (need_sync)
627379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
628379c004Eric Schrock}
629379c004Eric Schrock
631990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
633990b485lling	int error;
634ad135b5Christopher Siden	nvpair_t *elem = NULL;
635379c004Eric Schrock	boolean_t need_sync = B_FALSE;
637990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
638990b485lling		return (error);
640379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
641ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
642379c004Eric Schrock
643f9af39bGeorge Wilson		if (prop == ZPOOL_PROP_CACHEFILE ||
644f9af39bGeorge Wilson		    prop == ZPOOL_PROP_ALTROOT ||
645f9af39bGeorge Wilson		    prop == ZPOOL_PROP_READONLY)
646379c004Eric Schrock			continue;
647379c004Eric Schrock
648ad135b5Christopher Siden		if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
649ad135b5Christopher Siden			uint64_t ver;
650ad135b5Christopher Siden
651ad135b5Christopher Siden			if (prop == ZPOOL_PROP_VERSION) {
652ad135b5Christopher Siden				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
653ad135b5Christopher Siden			} else {
654ad135b5Christopher Siden				ASSERT(zpool_prop_feature(nvpair_name(elem)));
655ad135b5Christopher Siden				ver = SPA_VERSION_FEATURES;
656ad135b5Christopher Siden				need_sync = B_TRUE;
657ad135b5Christopher Siden			}
658ad135b5Christopher Siden
659ad135b5Christopher Siden			/* Save time if the version is already set. */
660ad135b5Christopher Siden			if (ver == spa_version(spa))
661ad135b5Christopher Siden				continue;
662ad135b5Christopher Siden
663ad135b5Christopher Siden			/*
664ad135b5Christopher Siden			 * In addition to the pool directory object, we might
665ad135b5Christopher Siden			 * create the pool properties object, the features for
666ad135b5Christopher Siden			 * read object, the features for write object, or the
667ad135b5Christopher Siden			 * feature descriptions object.
668ad135b5Christopher Siden			 */
6693b2aab1Matthew Ahrens			error = dsl_sync_task(spa->spa_name, NULL,
6703b2aab1Matthew Ahrens			    spa_sync_version, &ver, 6);
671ad135b5Christopher Siden			if (error)
672ad135b5Christopher Siden				return (error);
673ad135b5Christopher Siden			continue;
674ad135b5Christopher Siden		}
675ad135b5Christopher Siden
676379c004Eric Schrock		need_sync = B_TRUE;
677379c004Eric Schrock		break;
678379c004Eric Schrock	}
679379c004Eric Schrock
680ad135b5Christopher Siden	if (need_sync) {
6813b2aab1Matthew Ahrens		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
6823b2aab1Matthew Ahrens		    nvp, 6));
683ad135b5Christopher Siden	}
684ad135b5Christopher Siden
685ad135b5Christopher Siden	return (0);
689990b485lling * If the bootfs property value is dsobj, clear it.
690990b485lling */
692990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
694990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
695990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
696990b485lling		    spa->spa_pool_props_object,
697990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
698990b485lling		spa->spa_bootfs = 0;
699990b485lling	}
702dfbb943George Wilson/*ARGSUSED*/
703dfbb943George Wilsonstatic int
7043b2aab1Matthew Ahrensspa_change_guid_check(void *arg, dmu_tx_t *tx)
705dfbb943George Wilson{
7063b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7073b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
708dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
709dfbb943George Wilson	uint64_t vdev_state;
710dfbb943George Wilson
711dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
712dfbb943George Wilson	vdev_state = rvd->vdev_state;
713dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
714dfbb943George Wilson
715dfbb943George Wilson	if (vdev_state != VDEV_STATE_HEALTHY)
716be6fd75Matthew Ahrens		return (SET_ERROR(ENXIO));
717dfbb943George Wilson
718dfbb943George Wilson	ASSERT3U(spa_guid(spa), !=, *newguid);
719dfbb943George Wilson
720dfbb943George Wilson	return (0);
721dfbb943George Wilson}
722dfbb943George Wilson
723dfbb943George Wilsonstatic void
7243b2aab1Matthew Ahrensspa_change_guid_sync(void *arg, dmu_tx_t *tx)
725dfbb943George Wilson{
7263b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7273b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
728dfbb943George Wilson	uint64_t oldguid;
729dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
730dfbb943George Wilson
731dfbb943George Wilson	oldguid = spa_guid(spa);
732dfbb943George Wilson
733dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
734dfbb943George Wilson	rvd->vdev_guid = *newguid;
735dfbb943George Wilson	rvd->vdev_guid_sum += (*newguid - oldguid);
736dfbb943George Wilson	vdev_config_dirty(rvd);
737dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
738dfbb943George Wilson
73920128a0George Wilson	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
740dfbb943George Wilson	    oldguid, *newguid);
741dfbb943George Wilson}
742dfbb943George Wilson
744e9103aaGarrett D'Amore * Change the GUID for the pool.  This is done so that we can later
745e9103aaGarrett D'Amore * re-import a pool built from a clone of our own vdevs.  We will modify
746e9103aaGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our
747e9103aaGarrett D'Amore * vdevs dirty.  Note that we must make sure that all our vdevs are
748e9103aaGarrett D'Amore * online when we do this, or else any vdevs that weren't present
749e9103aaGarrett D'Amore * would be orphaned from our pool.  We are also going to issue a
750e9103aaGarrett D'Amore * sysevent to update any watchers.
751e9103aaGarrett D'Amore */
752e9103aaGarrett D'Amoreint
753e9103aaGarrett D'Amorespa_change_guid(spa_t *spa)
754e9103aaGarrett D'Amore{
755dfbb943George Wilson	int error;
756dfbb943George Wilson	uint64_t guid;
757e9103aaGarrett D'Amore
7582c1e2b4George Wilson	mutex_enter(&spa->spa_vdev_top_lock);
759dfbb943George Wilson	mutex_enter(&spa_namespace_lock);
760dfbb943George Wilson	guid = spa_generate_guid(NULL);
761e9103aaGarrett D'Amore
7623b2aab1Matthew Ahrens	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
7633b2aab1Matthew Ahrens	    spa_change_guid_sync, &guid, 5);
764e9103aaGarrett D'Amore
765dfbb943George Wilson	if (error == 0) {
766dfbb943George Wilson		spa_config_sync(spa, B_FALSE, B_TRUE);
767dfbb943George Wilson		spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
768dfbb943George Wilson	}
769e9103aaGarrett D'Amore
770dfbb943George Wilson	mutex_exit(&spa_namespace_lock);
7712c1e2b4George Wilson	mutex_exit(&spa->spa_vdev_top_lock);
772e9103aaGarrett D'Amore
773dfbb943George Wilson	return (error);
774e9103aaGarrett D'Amore}
775e9103aaGarrett D'Amore
776e9103aaGarrett D'Amore/*
777fa9e406ahrens * ==========================================================================
778fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
779fa9e406ahrens * ==========================================================================
780fa9e406ahrens */
782ea8dc4beschrockstatic int
783ea8dc4beschrockspa_error_entry_compare(const void *a, const void *b)
785ea8dc4beschrock	spa_error_entry_t *sa = (spa_error_entry_t *)a;
786ea8dc4beschrock	spa_error_entry_t *sb = (spa_error_entry_t *)b;
787ea8dc4beschrock	int ret;