spa.c revision 45818ee124adeaaf947698996b4f4c722afc6d1f
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.
251437283Hans Rosenfeld * Copyright (c) 2015, Nexenta Systems, Inc.  All rights reserved.
26bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2745818eeMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved.
285aeb947Garrett D'Amore */
313e30c24Will Andrews * SPA: Storage Pool Allocator
323e30c24Will Andrews *
33fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
34fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
35fa9e406ahrens * pool.
36fa9e406ahrens */
38fa9e406ahrens#include <sys/zfs_context.h>
39ea8dc4beschrock#include <sys/fm/fs/zfs.h>
40fa9e406ahrens#include <sys/spa_impl.h>
41fa9e406ahrens#include <sys/zio.h>
42fa9e406ahrens#include <sys/zio_checksum.h>
43fa9e406ahrens#include <sys/dmu.h>
44fa9e406ahrens#include <sys/dmu_tx.h>
45fa9e406ahrens#include <sys/zap.h>
46fa9e406ahrens#include <sys/zil.h>
47b24ab67Jeff Bonwick#include <sys/ddt.h>
48fa9e406ahrens#include <sys/vdev_impl.h>
49fa9e406ahrens#include <sys/metaslab.h>
5088ecc94George Wilson#include <sys/metaslab_impl.h>
51fa9e406ahrens#include <sys/uberblock_impl.h>
52fa9e406ahrens#include <sys/txg.h>
53fa9e406ahrens#include <sys/avl.h>
54fa9e406ahrens#include <sys/dmu_traverse.h>
55b1b8ab3lling#include <sys/dmu_objset.h>
56fa9e406ahrens#include <sys/unique.h>
57fa9e406ahrens#include <sys/dsl_pool.h>
58b1b8ab3lling#include <sys/dsl_dataset.h>
59fa9e406ahrens#include <sys/dsl_dir.h>
60fa9e406ahrens#include <sys/dsl_prop.h>
61b1b8ab3lling#include <sys/dsl_synctask.h>
62fa9e406ahrens#include <sys/fs/zfs.h>
63fa94a07brendan#include <sys/arc.h>
64fa9e406ahrens#include <sys/callb.h>
659517395ek#include <sys/systeminfo.h>
66e7cbe64gw#include <sys/spa_boot.h>
67573ca77George Wilson#include <sys/zfs_ioctl.h>
683f9d6adLin Ling#include <sys/dsl_scan.h>
69ad135b5Christopher Siden#include <sys/zfeature.h>
703b2aab1Matthew Ahrens#include <sys/dsl_destroy.h>
725679c89jv#ifdef	_KERNEL
73dedec47Jack Meng#include <sys/bootprops.h>
7435a5a35Jonathan Adams#include <sys/callb.h>
7535a5a35Jonathan Adams#include <sys/cpupart.h>
7635a5a35Jonathan Adams#include <sys/pool.h>
7735a5a35Jonathan Adams#include <sys/sysdc.h>
7835a5a35Jonathan Adams#include <sys/zone.h>
795679c89jv#endif	/* _KERNEL */
81990b485lling#include "zfs_prop.h"
82b7b9745perrin#include "zfs_comutil.h"
843cb69f7Will Andrews/*
853cb69f7Will Andrews * The interval, in seconds, at which failed configuration cache file writes
863cb69f7Will Andrews * should be retried.
873cb69f7Will Andrews */
883cb69f7Will Andrewsstatic int zfs_ccw_retry_interval = 300;
893cb69f7Will Andrews
9035a5a35Jonathan Adamstypedef enum zti_modes {
91ec94d32Adam Leventhal	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
92ec94d32Adam Leventhal	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
93ec94d32Adam Leventhal	ZTI_MODE_NULL,			/* don't create a taskq */
94ec94d32Adam Leventhal	ZTI_NMODES
9535a5a35Jonathan Adams} zti_modes_t;
97ec94d32Adam Leventhal#define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
98ec94d32Adam Leventhal#define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
99ec94d32Adam Leventhal#define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
1002e0c549Jonathan Adams
101ec94d32Adam Leventhal#define	ZTI_N(n)	ZTI_P(n, 1)
102ec94d32Adam Leventhal#define	ZTI_ONE		ZTI_N(1)
1032e0c549Jonathan Adams
1042e0c549Jonathan Adamstypedef struct zio_taskq_info {
105ec94d32Adam Leventhal	zti_modes_t zti_mode;
10680eb36fGeorge Wilson	uint_t zti_value;
107ec94d32Adam Leventhal	uint_t zti_count;
1082e0c549Jonathan Adams} zio_taskq_info_t;
1092e0c549Jonathan Adams
1102e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
11135a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
1122e0c549Jonathan Adams};
1132e0c549Jonathan Adams
11480eb36fGeorge Wilson/*
115ec94d32Adam Leventhal * This table defines the taskq settings for each ZFS I/O type. When
116ec94d32Adam Leventhal * initializing a pool, we use this table to create an appropriately sized
117ec94d32Adam Leventhal * taskq. Some operations are low volume and therefore have a small, static
118ec94d32Adam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
119ec94d32Adam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH
120ec94d32Adam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations
121ec94d32Adam Leventhal * are so high frequency and short-lived that the taskq itself can become a a
122ec94d32Adam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
123ec94d32Adam Leventhal * additional degree of parallelism specified by the number of threads per-
124ec94d32Adam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the
125ec94d32Adam Leventhal * particular taskq is chosen at random.
126ec94d32Adam Leventhal *
127ec94d32Adam Leventhal * The different taskq priorities are to handle the different contexts (issue
128ec94d32Adam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
129ec94d32Adam Leventhal * need to be handled with minimum delay.
13080eb36fGeorge Wilson */
13180eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
13280eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
133ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
1341b497abAdam H. Leventhal	{ ZTI_N(8),	ZTI_NULL,	ZTI_P(12, 8),	ZTI_NULL }, /* READ */
135ec94d32Adam Leventhal	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
136ec94d32Adam Leventhal	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
137ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
138ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
1392e0c549Jonathan Adams};
1402e0c549Jonathan Adams
1413b2aab1Matthew Ahrensstatic void spa_sync_version(void *arg, dmu_tx_t *tx);
1423b2aab1Matthew Ahrensstatic void spa_sync_props(void *arg, dmu_tx_t *tx);
14389a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1441195e68Mark J Musantestatic int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1451195e68Mark J Musante    spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1461195e68Mark J Musante    char **ereport);
147cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
14969962b5Matthew Ahrensuint_t		zio_taskq_batch_pct = 75;	/* 1 thread per cpu in pset */
15035a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
15135a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
15235a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
15335a5a35Jonathan Adams
15435a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
15501f55e4George Wilsonextern int	zfs_sync_pass_deferred_free;
15635a5a35Jonathan Adams
15735a5a35Jonathan Adams/*
15835a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
15935a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
16035a5a35Jonathan Adams */
16135a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
16235a5a35Jonathan Adams
164990b485lling * ==========================================================================
165990b485lling * SPA properties routines
166990b485lling * ==========================================================================
167990b485lling */
170990b485lling * Add a (source=src, propname=propval) list to an nvlist.
171990b485lling */
1729d82f4fllingstatic void
173990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
174990b485lling    uint64_t intval, zprop_source_t src)
176990b485lling	const char *propname = zpool_prop_to_name(prop);
177990b485lling	nvlist_t *propval;
1799d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1809d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1829d82f4flling	if (strval != NULL)
1839d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1849d82f4flling	else
1859d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1879d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
188990b485lling	nvlist_free(propval);
192990b485lling * Get property values from the spa configuration.
193990b485lling */
1949d82f4fllingstatic void
195990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1974263d13George Wilson	vdev_t *rvd = spa->spa_root_vdev;
198ad135b5Christopher Siden	dsl_pool_t *pool = spa->spa_dsl_pool;
1992e4c998George Wilson	uint64_t size, alloc, cap, version;
200990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
201c5904d1eschrock	spa_config_dirent_t *dp;
2022e4c998George Wilson	metaslab_class_t *mc = spa_normal_class(spa);
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
2152e4c998George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
2162e4c998George Wilson		    metaslab_class_fragmentation(mc), src);
2172e4c998George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
2182e4c998George Wilson		    metaslab_class_expandable_space(mc), src);
219f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
220f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
221379c004Eric Schrock
222485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
223379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
224379c004Eric Schrock
225b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
226b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
227b24ab67Jeff Bonwick
228379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
2294263d13George Wilson		    rvd->vdev_state, src);
230379c004Eric Schrock
231379c004Eric Schrock		version = spa_version(spa);
232379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
233379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
234379c004Eric Schrock		else
235379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
236379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
237379c004Eric Schrock	}
239ad135b5Christopher Siden	if (pool != NULL) {
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		 */
2447fd05acMatthew Ahrens		if (pool->dp_free_dir != NULL) {
245ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
246c137962Justin T. Gibbs			    dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
247c137962Justin T. Gibbs			    src);
248ad135b5Christopher Siden		} else {
249ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
250ad135b5Christopher Siden			    NULL, 0, src);
251ad135b5Christopher Siden		}
2527fd05acMatthew Ahrens
2537fd05acMatthew Ahrens		if (pool->dp_leak_dir != NULL) {
2547fd05acMatthew Ahrens			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
255c137962Justin T. Gibbs			    dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
256c137962Justin T. Gibbs			    src);
2577fd05acMatthew Ahrens		} else {
2587fd05acMatthew Ahrens			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
2597fd05acMatthew Ahrens			    NULL, 0, src);
2607fd05acMatthew Ahrens		}
261ad135b5Christopher Siden	}
262ad135b5Christopher Siden
2639d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2658704186Dan McDonald	if (spa->spa_comment != NULL) {
2668704186Dan McDonald		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
2678704186Dan McDonald		    0, ZPROP_SRC_LOCAL);
2688704186Dan McDonald	}
2698704186Dan McDonald
2709d82f4flling	if (spa->spa_root != NULL)
2719d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2729d82f4flling		    0, ZPROP_SRC_LOCAL);
274b515258Matthew Ahrens	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
275b515258Matthew Ahrens		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
276b515258Matthew Ahrens		    MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
277b515258Matthew Ahrens	} else {
278b515258Matthew Ahrens		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
279b515258Matthew Ahrens		    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
280b515258Matthew Ahrens	}
281b515258Matthew Ahrens
282c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
283c5904d1eschrock		if (dp->scd_path == NULL) {
2849d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
285c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
286c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2879d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
288c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2892f8aaabeschrock		}
2902f8aaabeschrock	}
294990b485lling * Get zpool property values.
295990b485lling */
297990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
299b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
300990b485lling	zap_cursor_t zc;
301990b485lling	zap_attribute_t za;
302990b485lling	int err;
3049d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
306e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
307e14bb32Jeff Bonwick
308990b485lling	/*
309990b485lling	 * Get properties from the spa config.
310990b485lling	 */
3119d82f4flling	spa_prop_get_config(spa, nvp);
313990b485lling	/* If no pool property object, no more prop to get. */
314afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
315990b485lling		mutex_exit(&spa->spa_props_lock);
316990b485lling		return (0);
317990b485lling	}
319990b485lling	/*
320990b485lling	 * Get properties from the MOS pool property object.
321990b485lling	 */
322990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
323990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
324990b485lling	    zap_cursor_advance(&zc)) {
325990b485lling		uint64_t intval = 0;
326990b485lling		char *strval = NULL;
327990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
328990b485lling		zpool_prop_t prop;
330990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
331990b485lling			continue;
333990b485lling		switch (za.za_integer_length) {
334990b485lling		case 8:
335990b485lling			/* integer property */
336990b485lling			if (za.za_first_integer !=
337990b485lling			    zpool_prop_default_numeric(prop))
338990b485lling				src = ZPROP_SRC_LOCAL;
340990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
341990b485lling				dsl_pool_t *dp;
342990b485lling				dsl_dataset_t *ds = NULL;
344990b485lling				dp = spa_get_dsl(spa);
3453b2aab1Matthew Ahrens				dsl_pool_config_enter(dp, FTAG);
346745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
347745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
3483b2aab1Matthew Ahrens					dsl_pool_config_exit(dp, FTAG);
349990b485lling					break;
350990b485lling				}
352990b485lling				strval = kmem_alloc(
353990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
354990b485lling				    KM_SLEEP);
355990b485lling				dsl_dataset_name(ds, strval);
356745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
3573b2aab1Matthew Ahrens				dsl_pool_config_exit(dp, FTAG);
358990b485lling			} else {
359990b485lling				strval = NULL;
360990b485lling				intval = za.za_first_integer;
361990b485lling			}
3639d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
365990b485lling			if (strval != NULL)
366990b485lling				kmem_free(strval,
367990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
369990b485lling			break;
371990b485lling		case 1:
372990b485lling			/* string property */
373990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
374990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
375990b485lling			    za.za_name, 1, za.za_num_integers, strval);
376990b485lling			if (err) {
377990b485lling				kmem_free(strval, za.za_num_integers);
378990b485lling				break;
379990b485lling			}
3809d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
381990b485lling			kmem_free(strval, za.za_num_integers);
382990b485lling			break;
384990b485lling		default:
385990b485lling			break;
386990b485lling		}
387990b485lling	}
388990b485lling	zap_cursor_fini(&zc);
389990b485lling	mutex_exit(&spa->spa_props_lock);
391990b485lling	if (err && err != ENOENT) {
392990b485lling		nvlist_free(*nvp);
3939d82f4flling		*nvp = NULL;
394990b485lling		return (err);
395990b485lling	}
397990b485lling	return (0);
401990b485lling * Validate the given pool properties nvlist and modify the list
402990b485lling * for the property values to be set.
403990b485lling */
404990b485llingstatic int
405990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
407990b485lling	nvpair_t *elem;
408990b485lling	int error = 0, reset_bootfs = 0;
409d5285caGeorge Wilson	uint64_t objnum = 0;
410ad135b5Christopher Siden	boolean_t has_feature = B_FALSE;
412990b485lling	elem = NULL;
413990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
414990b485lling		uint64_t intval;
415ad135b5Christopher Siden		char *strval, *slash, *check, *fname;
416ad135b5Christopher Siden		const char *propname = nvpair_name(elem);
417ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(propname);
418ad135b5Christopher Siden
419ad135b5Christopher Siden		switch (prop) {
420ad135b5Christopher Siden		case ZPROP_INVAL:
421ad135b5Christopher Siden			if (!zpool_prop_feature(propname)) {
422be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
423ad135b5Christopher Siden				break;
424ad135b5Christopher Siden			}
426ad135b5Christopher Siden			/*
427ad135b5Christopher Siden			 * Sanitize the input.
428ad135b5Christopher Siden			 */
429ad135b5Christopher Siden			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
430be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
431ad135b5Christopher Siden				break;
432ad135b5Christopher Siden			}
434ad135b5Christopher Siden			if (nvpair_value_uint64(elem, &intval) != 0) {
435be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
436ad135b5Christopher Siden				break;
437ad135b5Christopher Siden			}
438ad135b5Christopher Siden
439ad135b5Christopher Siden			if (intval != 0) {
440be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
441ad135b5Christopher Siden				break;
442ad135b5Christopher Siden			}
443ad135b5Christopher Siden
444ad135b5Christopher Siden			fname = strchr(propname, '@') + 1;
445ad135b5Christopher Siden			if (zfeature_lookup_name(fname, NULL) != 0) {
446be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
447ad135b5Christopher Siden				break;
448ad135b5Christopher Siden			}
449ad135b5Christopher Siden
450ad135b5Christopher Siden			has_feature = B_TRUE;
451ad135b5Christopher Siden			break;
453990b485lling		case ZPOOL_PROP_VERSION:
454990b485lling			error = nvpair_value_uint64(elem, &intval);
455990b485lling			if (!error &&
456ad135b5Christopher Siden			    (intval < spa_version(spa) ||
457ad135b5Christopher Siden			    intval > SPA_VERSION_BEFORE_FEATURES ||
458ad135b5Christopher Siden			    has_feature))
459be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
460990b485lling			break;
462990b485lling		case ZPOOL_PROP_DELEGATION:
463990b485lling		case ZPOOL_PROP_AUTOREPLACE:
464d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
465573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
466990b485lling			error = nvpair_value_uint64(elem, &intval);
467990b485lling			if (!error && intval > 1)
468be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
469990b485lling			break;
471990b485lling		case ZPOOL_PROP_BOOTFS:
47225f89eeJeff Bonwick			/*
47325f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
47425f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
47525f89eeJeff Bonwick			 * the bootfs property cannot be set.
47625f89eeJeff Bonwick			 */
477990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
478be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
479990b485lling				break;
480990b485lling			}
482990b485lling			/*
48315e6edfgw			 * Make sure the vdev config is bootable
484990b485lling			 */
48515e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
486be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
487990b485lling				break;
488990b485lling			}
490990b485lling			reset_bootfs = 1;
492990b485lling			error = nvpair_value_string(elem, &strval);
494990b485lling			if (!error) {
495ad135b5Christopher Siden				objset_t *os;
496b515258Matthew Ahrens				uint64_t propval;
498990b485lling				if (strval == NULL || strval[0] == '\0') {
499990b485lling					objnum = zpool_prop_default_numeric(
500990b485lling					    ZPOOL_PROP_BOOTFS);
501990b485lling					break;
502990b485lling				}
504503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
505990b485lling					break;
507b515258Matthew Ahrens				/*
508b515258Matthew Ahrens				 * Must be ZPL, and its property settings
509b515258Matthew Ahrens				 * must be supported by GRUB (compression
510b515258Matthew Ahrens				 * is not gzip, and large blocks are not used).
511b515258Matthew Ahrens				 */
512503ad85Matthew Ahrens
513503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
514be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
5153b2aab1Matthew Ahrens				} else if ((error =
5163b2aab1Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
51715e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
518b515258Matthew Ahrens				    &propval)) == 0 &&
519b515258Matthew Ahrens				    !BOOTFS_COMPRESS_VALID(propval)) {
520b515258Matthew Ahrens					error = SET_ERROR(ENOTSUP);
521b515258Matthew Ahrens				} else if ((error =
522b515258Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
523b515258Matthew Ahrens				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
524b515258Matthew Ahrens				    &propval)) == 0 &&
525b515258Matthew Ahrens				    propval > SPA_OLD_MAXBLOCKSIZE) {
526be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
52715e6edfgw				} else {
52815e6edfgw					objnum = dmu_objset_id(os);
52915e6edfgw				}
530503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
531990b485lling			}
532990b485lling			break;
533e14bb32Jeff Bonwick
5340a4e951gw		case ZPOOL_PROP_FAILUREMODE:
5350a4e951gw			error = nvpair_value_uint64(elem, &intval);
5360a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
5370a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
538be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5400a4e951gw			/*
5410a4e951gw			 * This is a special case which only occurs when
5420a4e951gw			 * the pool has completely failed. This allows
5430a4e951gw			 * the user to change the in-core failmode property
5440a4e951gw			 * without syncing it out to disk (I/Os might
5450a4e951gw			 * currently be blocked). We do this by returning
5460a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
5470a4e951gw			 * into thinking we encountered a property validation
5480a4e951gw			 * error.
5490a4e951gw			 */
550e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
5510a4e951gw				spa->spa_failmode = intval;
552be6fd75Matthew Ahrens				error = SET_ERROR(EIO);
5530a4e951gw			}
5540a4e951gw			break;
5562f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
5572f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
5582f8aaabeschrock				break;
5602f8aaabeschrock			if (strval[0] == '\0')
5612f8aaabeschrock				break;
5632f8aaabeschrock			if (strcmp(strval, "none") == 0)
5642f8aaabeschrock				break;
5662f8aaabeschrock			if (strval[0] != '/') {
567be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5682f8aaabeschrock				break;
5692f8aaabeschrock			}
5712f8aaabeschrock			slash = strrchr(strval, '/');
5722f8aaabeschrock			ASSERT(slash != NULL);
5742f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5752f8aaabeschrock			    strcmp(slash, "/..") == 0)
576be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5772f8aaabeschrock			break;
578b24ab67Jeff Bonwick
5798704186Dan McDonald		case ZPOOL_PROP_COMMENT:
5808704186Dan McDonald			if ((error = nvpair_value_string(elem, &strval)) != 0)
5818704186Dan McDonald				break;
5828704186Dan McDonald			for (check = strval; *check != '\0'; check++) {
5838704186Dan McDonald				/*
5848704186Dan McDonald				 * The kernel doesn't have an easy isprint()
5858704186Dan McDonald				 * check.  For this kernel check, we merely
5868704186Dan McDonald				 * check ASCII apart from DEL.  Fix this if
5878704186Dan McDonald				 * there is an easy-to-use kernel isprint().
5888704186Dan McDonald				 */
5898704186Dan McDonald				if (*check >= 0x7f) {
590be6fd75Matthew Ahrens					error = SET_ERROR(EINVAL);
5918704186Dan McDonald					break;
5928704186Dan McDonald				}
5938704186Dan McDonald				check++;
5948704186Dan McDonald			}
5958704186Dan McDonald			if (strlen(strval) > ZPROP_MAX_COMMENT)
5968704186Dan McDonald				error = E2BIG;
5978704186Dan McDonald			break;
5988704186Dan McDonald
599b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
600b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
601be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
602b24ab67Jeff Bonwick			else
603b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
604b24ab67Jeff Bonwick			if (error == 0 &&
605b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
606be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
607b24ab67Jeff Bonwick			break;
608990b485lling		}
610990b485lling		if (error)
611990b485lling			break;
612990b485lling	}
614990b485lling	if (!error && reset_bootfs) {
615990b485lling		error = nvlist_remove(props,
616990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
618990b485lling		if (!error) {
619990b485lling			error = nvlist_add_uint64(props,
620990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
621990b485lling		}
622990b485lling	}
624990b485lling	return (error);
627379c004Eric Schrockvoid
628379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
629379c004Eric Schrock{
630379c004Eric Schrock	char *cachefile;
631379c004Eric Schrock	spa_config_dirent_t *dp;
632379c004Eric Schrock
633379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
634379c004Eric Schrock	    &cachefile) != 0)
635379c004Eric Schrock		return;
636379c004Eric Schrock
637379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
638379c004Eric Schrock	    KM_SLEEP);
639379c004Eric Schrock
640379c004Eric Schrock	if (cachefile[0] == '\0')
641379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
642379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
643379c004Eric Schrock		dp->scd_path = NULL;
644379c004Eric Schrock	else
645379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
646379c004Eric Schrock
647379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
648379c004Eric Schrock	if (need_sync)
649379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
650379c004Eric Schrock}
651379c004Eric Schrock
653990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
655990b485lling	int error;
656ad135b5Christopher Siden	nvpair_t *elem = NULL;
657379c004Eric Schrock	boolean_t need_sync = B_FALSE;
659990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
660990b485lling		return (error);
662379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
663ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
664379c004Eric Schrock
665f9af39bGeorge Wilson		if (prop == ZPOOL_PROP_CACHEFILE ||
666f9af39bGeorge Wilson		    prop == ZPOOL_PROP_ALTROOT ||
667f9af39bGeorge Wilson		    prop == ZPOOL_PROP_READONLY)
668379c004Eric Schrock			continue;
669379c004Eric Schrock
670ad135b5Christopher Siden		if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
671ad135b5Christopher Siden			uint64_t ver;
672ad135b5Christopher Siden
673ad135b5Christopher Siden			if (prop == ZPOOL_PROP_VERSION) {
674ad135b5Christopher Siden				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
675ad135b5Christopher Siden			} else {
676ad135b5Christopher Siden				ASSERT(zpool_prop_feature(nvpair_name(elem)));
677ad135b5Christopher Siden				ver = SPA_VERSION_FEATURES;
678ad135b5Christopher Siden				need_sync = B_TRUE;
679ad135b5Christopher Siden			}
680ad135b5Christopher Siden
681ad135b5Christopher Siden			/* Save time if the version is already set. */
682ad135b5Christopher Siden			if (ver == spa_version(spa))
683ad135b5Christopher Siden				continue;
684ad135b5Christopher Siden
685ad135b5Christopher Siden			/*
686ad135b5Christopher Siden			 * In addition to the pool directory object, we might
687ad135b5Christopher Siden			 * create the pool properties object, the features for
688ad135b5Christopher Siden			 * read object, the features for write object, or the
689ad135b5Christopher Siden			 * feature descriptions object.
690ad135b5Christopher Siden			 */
6913b2aab1Matthew Ahrens			error = dsl_sync_task(spa->spa_name, NULL,
6927d46dc6Matthew Ahrens			    spa_sync_version, &ver,
6937d46dc6Matthew Ahrens			    6, ZFS_SPACE_CHECK_RESERVED);
694ad135b5Christopher Siden			if (error)
695ad135b5Christopher Siden				return (error);
696ad135b5Christopher Siden			continue;
697ad135b5Christopher Siden		}
698ad135b5Christopher Siden
699379c004Eric Schrock		need_sync = B_TRUE;
700379c004Eric Schrock		break;
701379c004Eric Schrock	}
702379c004Eric Schrock
703ad135b5Christopher Siden	if (need_sync) {
7043b2aab1Matthew Ahrens		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
7057d46dc6Matthew Ahrens		    nvp, 6, ZFS_SPACE_CHECK_RESERVED));
706ad135b5Christopher Siden	}
707ad135b5Christopher Siden
708ad135b5Christopher Siden	return (0);
712990b485lling * If the bootfs property value is dsobj, clear it.
713990b485lling */
715990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
717990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
718990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
719990b485lling		    spa->spa_pool_props_object,
720990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
721990b485lling		spa->spa_bootfs = 0;
722990b485lling	}
725dfbb943George Wilson/*ARGSUSED*/
726dfbb943George Wilsonstatic int
7273b2aab1Matthew Ahrensspa_change_guid_check(void *arg, dmu_tx_t *tx)
728dfbb943George Wilson{
7293b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7303b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
731dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
732dfbb943George Wilson	uint64_t vdev_state;
733dfbb943George Wilson
734dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
735dfbb943George Wilson	vdev_state = rvd->vdev_state;
736dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
737dfbb943George Wilson
738dfbb943George Wilson	if (vdev_state != VDEV_STATE_HEALTHY)
739be6fd75Matthew Ahrens		return (SET_ERROR(ENXIO));
740dfbb943George Wilson
741dfbb943George Wilson	ASSERT3U(spa_guid(spa), !=, *newguid);
742dfbb943George Wilson
743dfbb943George Wilson	return (0);
744dfbb943George Wilson}
745dfbb943George Wilson
746dfbb943George Wilsonstatic void
7473b2aab1Matthew Ahrensspa_change_guid_sync(void *arg, dmu_tx_t *tx)
748dfbb943George Wilson{
7493b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7503b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
751dfbb943George Wilson	uint64_t oldguid;
752dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
753dfbb943George Wilson
754dfbb943George Wilson	oldguid = spa_guid(spa);
755dfbb943George Wilson
756dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
757dfbb943George Wilson	rvd->vdev_guid = *newguid;
758dfbb943George Wilson	rvd->vdev_guid_sum += (*newguid - oldguid);
759dfbb943George Wilson	vdev_config_dirty(rvd);
760dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
761dfbb943George Wilson
76220128a0George Wilson	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
763dfbb943George Wilson	    oldguid, *newguid);
764dfbb943George Wilson}
765dfbb943George Wilson
767e9103aaGarrett D'Amore * Change the GUID for the pool.  This is done so that we can later
768e9103aaGarrett D'Amore * re-import a pool built from a clone of our own vdevs.  We will modify
769e9103aaGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our
770e9103aaGarrett D'Amore * vdevs dirty.  Note that we must make sure that all our vdevs are
771e9103aaGarrett D'Amore * online when we do this, or else any vdevs that weren't present
772e9103aaGarrett D'Amore * would be orphaned from our pool.  We are also going to issue a
773e9103aaGarrett D'Amore * sysevent to update any watchers.
774e9103aaGarrett D'Amore */
775e9103aaGarrett D'Amoreint
776e9103aaGarrett D'Amorespa_change_guid(spa_t *spa)
777e9103aaGarrett D'Amore{
778dfbb943George Wilson	int error;
779dfbb943George Wilson	uint64_t guid;
780e9103aaGarrett D'Amore
7812c1e2b4George Wilson	mutex_enter(&spa->spa_vdev_top_lock);
782dfbb943George Wilson	mutex_enter(&spa_namespace_lock);
783dfbb943George Wilson	guid = spa_generate_guid(NULL);
784e9103aaGarrett D'Amore
7853b2aab1Matthew Ahrens	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
7867d46dc6Matthew Ahrens	    spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
787e9103aaGarrett D'Amore
788dfbb943George Wilson	if (error == 0) {
789dfbb943George Wilson		spa_config_sync(spa, B_FALSE, B_TRUE);
790dfbb943George Wilson		spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
791dfbb943George Wilson	}
792e9103aaGarrett D'Amore
793dfbb943George Wilson	mutex_exit(&spa_namespace_lock);
7942c1e2b4George Wilson	mutex_exit(&spa->spa_vdev_top_lock);
795e9103aaGarrett D'Amore
796dfbb943George Wilson	return (error);
797e9103aaGarrett D'Amore}
798e9103aaGarrett D'Amore
799e9103aaGarrett D'Amore/*
800fa9e406ahrens * ==========================================================================
801fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)