spa.c revision 9adfa60d484ce2435f5af77cc99dcd4e692b6660
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.
28c3d26abMatthew Ahrens * Copyright (c) 2014 Integros []
295aeb947Garrett D'Amore */
323e30c24Will Andrews * SPA: Storage Pool Allocator
333e30c24Will Andrews *
34fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
35fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
36fa9e406ahrens * pool.
37fa9e406ahrens */
39fa9e406ahrens#include <sys/zfs_context.h>
40ea8dc4beschrock#include <sys/fm/fs/zfs.h>
41fa9e406ahrens#include <sys/spa_impl.h>
42fa9e406ahrens#include <sys/zio.h>
43fa9e406ahrens#include <sys/zio_checksum.h>
44fa9e406ahrens#include <sys/dmu.h>
45fa9e406ahrens#include <sys/dmu_tx.h>
46fa9e406ahrens#include <sys/zap.h>
47fa9e406ahrens#include <sys/zil.h>
48b24ab67Jeff Bonwick#include <sys/ddt.h>
49fa9e406ahrens#include <sys/vdev_impl.h>
50fa9e406ahrens#include <sys/metaslab.h>
5188ecc94George Wilson#include <sys/metaslab_impl.h>
52fa9e406ahrens#include <sys/uberblock_impl.h>
53fa9e406ahrens#include <sys/txg.h>
54fa9e406ahrens#include <sys/avl.h>
55fa9e406ahrens#include <sys/dmu_traverse.h>
56b1b8ab3lling#include <sys/dmu_objset.h>
57fa9e406ahrens#include <sys/unique.h>
58fa9e406ahrens#include <sys/dsl_pool.h>
59b1b8ab3lling#include <sys/dsl_dataset.h>
60fa9e406ahrens#include <sys/dsl_dir.h>
61fa9e406ahrens#include <sys/dsl_prop.h>
62b1b8ab3lling#include <sys/dsl_synctask.h>
63fa9e406ahrens#include <sys/fs/zfs.h>
64fa94a07brendan#include <sys/arc.h>
65fa9e406ahrens#include <sys/callb.h>
669517395ek#include <sys/systeminfo.h>
67e7cbe64gw#include <sys/spa_boot.h>
68573ca77George Wilson#include <sys/zfs_ioctl.h>
693f9d6adLin Ling#include <sys/dsl_scan.h>
70ad135b5Christopher Siden#include <sys/zfeature.h>
713b2aab1Matthew Ahrens#include <sys/dsl_destroy.h>
735679c89jv#ifdef	_KERNEL
74dedec47Jack Meng#include <sys/bootprops.h>
7535a5a35Jonathan Adams#include <sys/callb.h>
7635a5a35Jonathan Adams#include <sys/cpupart.h>
7735a5a35Jonathan Adams#include <sys/pool.h>
7835a5a35Jonathan Adams#include <sys/sysdc.h>
7935a5a35Jonathan Adams#include <sys/zone.h>
805679c89jv#endif	/* _KERNEL */
82990b485lling#include "zfs_prop.h"
83b7b9745perrin#include "zfs_comutil.h"
853cb69f7Will Andrews/*
863cb69f7Will Andrews * The interval, in seconds, at which failed configuration cache file writes
873cb69f7Will Andrews * should be retried.
883cb69f7Will Andrews */
893cb69f7Will Andrewsstatic int zfs_ccw_retry_interval = 300;
903cb69f7Will Andrews
9135a5a35Jonathan Adamstypedef enum zti_modes {
92ec94d32Adam Leventhal	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
93ec94d32Adam Leventhal	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
94ec94d32Adam Leventhal	ZTI_MODE_NULL,			/* don't create a taskq */
95ec94d32Adam Leventhal	ZTI_NMODES
9635a5a35Jonathan Adams} zti_modes_t;
98ec94d32Adam Leventhal#define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
99ec94d32Adam Leventhal#define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
100ec94d32Adam Leventhal#define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
1012e0c549Jonathan Adams
102ec94d32Adam Leventhal#define	ZTI_N(n)	ZTI_P(n, 1)
103ec94d32Adam Leventhal#define	ZTI_ONE		ZTI_N(1)
1042e0c549Jonathan Adams
1052e0c549Jonathan Adamstypedef struct zio_taskq_info {
106ec94d32Adam Leventhal	zti_modes_t zti_mode;
10780eb36fGeorge Wilson	uint_t zti_value;
108ec94d32Adam Leventhal	uint_t zti_count;
1092e0c549Jonathan Adams} zio_taskq_info_t;
1102e0c549Jonathan Adams
1112e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
11235a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
1132e0c549Jonathan Adams};
1142e0c549Jonathan Adams
11580eb36fGeorge Wilson/*
116ec94d32Adam Leventhal * This table defines the taskq settings for each ZFS I/O type. When
117ec94d32Adam Leventhal * initializing a pool, we use this table to create an appropriately sized
118ec94d32Adam Leventhal * taskq. Some operations are low volume and therefore have a small, static
119ec94d32Adam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
120ec94d32Adam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH
121ec94d32Adam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations
122ec94d32Adam Leventhal * are so high frequency and short-lived that the taskq itself can become a a
123ec94d32Adam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
124ec94d32Adam Leventhal * additional degree of parallelism specified by the number of threads per-
125ec94d32Adam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the
126ec94d32Adam Leventhal * particular taskq is chosen at random.
127ec94d32Adam Leventhal *
128ec94d32Adam Leventhal * The different taskq priorities are to handle the different contexts (issue
129ec94d32Adam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
130ec94d32Adam Leventhal * need to be handled with minimum delay.
13180eb36fGeorge Wilson */
13280eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
13380eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
134ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
1351b497abAdam H. Leventhal	{ ZTI_N(8),	ZTI_NULL,	ZTI_P(12, 8),	ZTI_NULL }, /* READ */
136ec94d32Adam Leventhal	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
137ec94d32Adam Leventhal	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
138ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
139ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
1402e0c549Jonathan Adams};
1412e0c549Jonathan Adams
1423b2aab1Matthew Ahrensstatic void spa_sync_version(void *arg, dmu_tx_t *tx);
1433b2aab1Matthew Ahrensstatic void spa_sync_props(void *arg, dmu_tx_t *tx);
14489a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1451195e68Mark J Musantestatic int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1461195e68Mark J Musante    spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1471195e68Mark J Musante    char **ereport);
148cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
15069962b5Matthew Ahrensuint_t		zio_taskq_batch_pct = 75;	/* 1 thread per cpu in pset */
15135a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
15235a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
15335a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
15435a5a35Jonathan Adams
15535a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
15601f55e4George Wilsonextern int	zfs_sync_pass_deferred_free;
15735a5a35Jonathan Adams
15835a5a35Jonathan Adams/*
15935a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
16035a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
16135a5a35Jonathan Adams */
16235a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
16335a5a35Jonathan Adams
165990b485lling * ==========================================================================
166990b485lling * SPA properties routines
167990b485lling * ==========================================================================
168990b485lling */
171990b485lling * Add a (source=src, propname=propval) list to an nvlist.
172990b485lling */
1739d82f4fllingstatic void
174990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
175990b485lling    uint64_t intval, zprop_source_t src)
177990b485lling	const char *propname = zpool_prop_to_name(prop);
178990b485lling	nvlist_t *propval;
1809d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1819d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
1839d82f4flling	if (strval != NULL)
1849d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1859d82f4flling	else
1869d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
1889d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
189990b485lling	nvlist_free(propval);
193990b485lling * Get property values from the spa configuration.
194990b485lling */
1959d82f4fllingstatic void
196990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
1984263d13George Wilson	vdev_t *rvd = spa->spa_root_vdev;
199ad135b5Christopher Siden	dsl_pool_t *pool = spa->spa_dsl_pool;
2002e4c998George Wilson	uint64_t size, alloc, cap, version;
201990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
202c5904d1eschrock	spa_config_dirent_t *dp;
2032e4c998George Wilson	metaslab_class_t *mc = spa_normal_class(spa);
205e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
206e14bb32Jeff Bonwick
2074263d13George Wilson	if (rvd != NULL) {
208485bbbfGeorge Wilson		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
209b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
210379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
211379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
212485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
213485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
214485bbbfGeorge Wilson		    size - alloc, src);
2154263d13George Wilson
2162e4c998George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
2172e4c998George Wilson		    metaslab_class_fragmentation(mc), src);
2182e4c998George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
2192e4c998George Wilson		    metaslab_class_expandable_space(mc), src);
220f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
221f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
222379c004Eric Schrock
223485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
224379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
225379c004Eric Schrock
226b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
227b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
228b24ab67Jeff Bonwick
229379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
2304263d13George Wilson		    rvd->vdev_state, src);
231379c004Eric Schrock
232379c004Eric Schrock		version = spa_version(spa);
233379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
234379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
235379c004Eric Schrock		else
236379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
237379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
238379c004Eric Schrock	}
240ad135b5Christopher Siden	if (pool != NULL) {
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		 */
2457fd05acMatthew Ahrens		if (pool->dp_free_dir != NULL) {
246ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
247c137962Justin T. Gibbs			    dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
248c137962Justin T. Gibbs			    src);
249ad135b5Christopher Siden		} else {
250ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
251ad135b5Christopher Siden			    NULL, 0, src);
252ad135b5Christopher Siden		}
2537fd05acMatthew Ahrens
2547fd05acMatthew Ahrens		if (pool->dp_leak_dir != NULL) {
2557fd05acMatthew Ahrens			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
256c137962Justin T. Gibbs			    dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
257c137962Justin T. Gibbs			    src);
2587fd05acMatthew Ahrens		} else {
2597fd05acMatthew Ahrens			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
2607fd05acMatthew Ahrens			    NULL, 0, src);
2617fd05acMatthew Ahrens		}
262ad135b5Christopher Siden	}
263ad135b5Christopher Siden
2649d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
2668704186Dan McDonald	if (spa->spa_comment != NULL) {
2678704186Dan McDonald		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
2688704186Dan McDonald		    0, ZPROP_SRC_LOCAL);
2698704186Dan McDonald	}
2708704186Dan McDonald
2719d82f4flling	if (spa->spa_root != NULL)
2729d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2739d82f4flling		    0, ZPROP_SRC_LOCAL);
275b515258Matthew Ahrens	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
276b515258Matthew Ahrens		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
277b515258Matthew Ahrens		    MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
278b515258Matthew Ahrens	} else {
279b515258Matthew Ahrens		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
280b515258Matthew Ahrens		    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
281b515258Matthew Ahrens	}
282b515258Matthew Ahrens
283c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
284c5904d1eschrock		if (dp->scd_path == NULL) {
2859d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
286c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
287c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2889d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
289c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
2902f8aaabeschrock		}
2912f8aaabeschrock	}
295990b485lling * Get zpool property values.
296990b485lling */
298990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
300b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
301990b485lling	zap_cursor_t zc;
302990b485lling	zap_attribute_t za;
303990b485lling	int err;
3059d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
307e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
308e14bb32Jeff Bonwick
309990b485lling	/*
310990b485lling	 * Get properties from the spa config.
311990b485lling	 */
3129d82f4flling	spa_prop_get_config(spa, nvp);
314990b485lling	/* If no pool property object, no more prop to get. */
315afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
316990b485lling		mutex_exit(&spa->spa_props_lock);
317990b485lling		return (0);
318990b485lling	}
320990b485lling	/*
321990b485lling	 * Get properties from the MOS pool property object.
322990b485lling	 */
323990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
324990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
325990b485lling	    zap_cursor_advance(&zc)) {
326990b485lling		uint64_t intval = 0;
327990b485lling		char *strval = NULL;
328990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
329990b485lling		zpool_prop_t prop;
331990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
332990b485lling			continue;
334990b485lling		switch (za.za_integer_length) {
335990b485lling		case 8:
336990b485lling			/* integer property */
337990b485lling			if (za.za_first_integer !=
338990b485lling			    zpool_prop_default_numeric(prop))
339990b485lling				src = ZPROP_SRC_LOCAL;
341990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
342990b485lling				dsl_pool_t *dp;
343990b485lling				dsl_dataset_t *ds = NULL;
345990b485lling				dp = spa_get_dsl(spa);
3463b2aab1Matthew Ahrens				dsl_pool_config_enter(dp, FTAG);
347745cd3cmaybee				if (err = dsl_dataset_hold_obj(dp,
348745cd3cmaybee				    za.za_first_integer, FTAG, &ds)) {
3493b2aab1Matthew Ahrens					dsl_pool_config_exit(dp, FTAG);
350990b485lling					break;
351990b485lling				}
3539adfa60Matthew Ahrens				strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
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)
3669adfa60Matthew Ahrens				kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
368990b485lling			break;
370990b485lling		case 1:
371990b485lling			/* string property */
372990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
373990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
374990b485lling			    za.za_name, 1, za.za_num_integers, strval);
375990b485lling			if (err) {
376990b485lling				kmem_free(strval, za.za_num_integers);
377990b485lling				break;
378990b485lling			}
3799d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
380990b485lling			kmem_free(strval, za.za_num_integers);
381990b485lling			break;
383990b485lling		default:
384990b485lling			break;
385990b485lling		}
386990b485lling	}
387990b485lling	zap_cursor_fini(&zc);
388990b485lling	mutex_exit(&spa->spa_props_lock);
390990b485lling	if (err && err != ENOENT) {
391990b485lling		nvlist_free(*nvp);
3929d82f4flling		*nvp = NULL;
393990b485lling		return (err);
394990b485lling	}
396990b485lling	return (0);
400990b485lling * Validate the given pool properties nvlist and modify the list
401990b485lling * for the property values to be set.
402990b485lling */
403990b485llingstatic int
404990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
406990b485lling	nvpair_t *elem;
407990b485lling	int error = 0, reset_bootfs = 0;
408d5285caGeorge Wilson	uint64_t objnum = 0;
409ad135b5Christopher Siden	boolean_t has_feature = B_FALSE;
411990b485lling	elem = NULL;
412990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
413990b485lling		uint64_t intval;
414ad135b5Christopher Siden		char *strval, *slash, *check, *fname;
415ad135b5Christopher Siden		const char *propname = nvpair_name(elem);
416ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(propname);
417ad135b5Christopher Siden
418ad135b5Christopher Siden		switch (prop) {
419ad135b5Christopher Siden		case ZPROP_INVAL:
420ad135b5Christopher Siden			if (!zpool_prop_feature(propname)) {
421be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
422ad135b5Christopher Siden				break;
423ad135b5Christopher Siden			}
425ad135b5Christopher Siden			/*
426ad135b5Christopher Siden			 * Sanitize the input.
427ad135b5Christopher Siden			 */
428ad135b5Christopher Siden			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
429be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
430ad135b5Christopher Siden				break;
431ad135b5Christopher Siden			}
433ad135b5Christopher Siden			if (nvpair_value_uint64(elem, &intval) != 0) {
434be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
435ad135b5Christopher Siden				break;
436ad135b5Christopher Siden			}
437ad135b5Christopher Siden
438ad135b5Christopher Siden			if (intval != 0) {
439be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
440ad135b5Christopher Siden				break;
441ad135b5Christopher Siden			}
442ad135b5Christopher Siden
443ad135b5Christopher Siden			fname = strchr(propname, '@') + 1;
444ad135b5Christopher Siden			if (zfeature_lookup_name(fname, NULL) != 0) {
445be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
446ad135b5Christopher Siden				break;
447ad135b5Christopher Siden			}
448ad135b5Christopher Siden
449ad135b5Christopher Siden			has_feature = B_TRUE;
450ad135b5Christopher Siden			break;
452990b485lling		case ZPOOL_PROP_VERSION:
453990b485lling			error = nvpair_value_uint64(elem, &intval);
454990b485lling			if (!error &&
455ad135b5Christopher Siden			    (intval < spa_version(spa) ||
456ad135b5Christopher Siden			    intval > SPA_VERSION_BEFORE_FEATURES ||
457ad135b5Christopher Siden			    has_feature))
458be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
459990b485lling			break;
461990b485lling		case ZPOOL_PROP_DELEGATION:
462990b485lling		case ZPOOL_PROP_AUTOREPLACE:
463d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
464573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
465990b485lling			error = nvpair_value_uint64(elem, &intval);
466990b485lling			if (!error && intval > 1)
467be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
468990b485lling			break;
470990b485lling		case ZPOOL_PROP_BOOTFS:
47125f89eeJeff Bonwick			/*
47225f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
47325f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
47425f89eeJeff Bonwick			 * the bootfs property cannot be set.
47525f89eeJeff Bonwick			 */
476990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
477be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
478990b485lling				break;
479990b485lling			}
481990b485lling			/*
48215e6edfgw			 * Make sure the vdev config is bootable
483990b485lling			 */
48415e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
485be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
486990b485lling				break;
487990b485lling			}
489990b485lling			reset_bootfs = 1;
491990b485lling			error = nvpair_value_string(elem, &strval);
493990b485lling			if (!error) {
494ad135b5Christopher Siden				objset_t *os;
495b515258Matthew Ahrens				uint64_t propval;
497990b485lling				if (strval == NULL || strval[0] == '\0') {
498990b485lling					objnum = zpool_prop_default_numeric(
499990b485lling					    ZPOOL_PROP_BOOTFS);
500990b485lling					break;
501990b485lling				}
503503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
504990b485lling					break;
506b515258Matthew Ahrens				/*
507b515258Matthew Ahrens				 * Must be ZPL, and its property settings
508b515258Matthew Ahrens				 * must be supported by GRUB (compression
509b515258Matthew Ahrens				 * is not gzip, and large blocks are not used).
510b515258Matthew Ahrens				 */
511503ad85Matthew Ahrens
512503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
513be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
5143b2aab1Matthew Ahrens				} else if ((error =
5153b2aab1Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
51615e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
517b515258Matthew Ahrens				    &propval)) == 0 &&
518b515258Matthew Ahrens				    !BOOTFS_COMPRESS_VALID(propval)) {
519b515258Matthew Ahrens					error = SET_ERROR(ENOTSUP);
520b515258Matthew Ahrens				} else if ((error =
521b515258Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
522b515258Matthew Ahrens				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
523b515258Matthew Ahrens				    &propval)) == 0 &&
524b515258Matthew Ahrens				    propval > SPA_OLD_MAXBLOCKSIZE) {
525be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
52615e6edfgw				} else {
52715e6edfgw					objnum = dmu_objset_id(os);
52815e6edfgw				}
529503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
530990b485lling			}
531990b485lling			break;
532e14bb32Jeff Bonwick
5330a4e951gw		case ZPOOL_PROP_FAILUREMODE:
5340a4e951gw			error = nvpair_value_uint64(elem, &intval);
5350a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
5360a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
537be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5390a4e951gw			/*
5400a4e951gw			 * This is a special case which only occurs when
5410a4e951gw			 * the pool has completely failed. This allows
5420a4e951gw			 * the user to change the in-core failmode property
5430a4e951gw			 * without syncing it out to disk (I/Os might
5440a4e951gw			 * currently be blocked). We do this by returning
5450a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
5460a4e951gw			 * into thinking we encountered a property validation
5470a4e951gw			 * error.
5480a4e951gw			 */
549e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
5500a4e951gw				spa->spa_failmode = intval;
551be6fd75Matthew Ahrens				error = SET_ERROR(EIO);
5520a4e951gw			}
5530a4e951gw			break;
5552f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
5562f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
5572f8aaabeschrock				break;
5592f8aaabeschrock			if (strval[0] == '\0')
5602f8aaabeschrock				break;
5622f8aaabeschrock			if (strcmp(strval, "none") == 0)
5632f8aaabeschrock				break;
5652f8aaabeschrock			if (strval[0] != '/') {
566be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5672f8aaabeschrock				break;
5682f8aaabeschrock			}
5702f8aaabeschrock			slash = strrchr(strval, '/');
5712f8aaabeschrock			ASSERT(slash != NULL);
5732f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5742f8aaabeschrock			    strcmp(slash, "/..") == 0)
575be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5762f8aaabeschrock			break;
577b24ab67Jeff Bonwick
5788704186Dan McDonald		case ZPOOL_PROP_COMMENT:
5798704186Dan McDonald			if ((error = nvpair_value_string(elem, &strval)) != 0)
5808704186Dan McDonald				break;
5818704186Dan McDonald			for (check = strval; *check != '\0'; check++) {
5828704186Dan McDonald				/*
5838704186Dan McDonald				 * The kernel doesn't have an easy isprint()
5848704186Dan McDonald				 * check.  For this kernel check, we merely
5858704186Dan McDonald				 * check ASCII apart from DEL.  Fix this if
5868704186Dan McDonald				 * there is an easy-to-use kernel isprint().
5878704186Dan McDonald				 */
5888704186Dan McDonald				if (*check >= 0x7f) {
589be6fd75Matthew Ahrens					error = SET_ERROR(EINVAL);
5908704186Dan McDonald					break;
5918704186Dan McDonald				}
5928704186Dan McDonald			}
5938704186Dan McDonald			if (strlen(strval) > ZPROP_MAX_COMMENT)
5948704186Dan McDonald				error = E2BIG;
5958704186Dan McDonald			break;
5968704186Dan McDonald
597b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
598b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
599be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
600b24ab67Jeff Bonwick			else
601b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
602b24ab67Jeff Bonwick			if (error == 0 &&
603b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
604be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
605b24ab67Jeff Bonwick			break;
606990b485lling		}
608990b485lling		if (error)
609990b485lling			break;
610990b485lling	}
612990b485lling	if (!error && reset_bootfs) {
613990b485lling		error = nvlist_remove(props,
614990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
616990b485lling		if (!error) {
617990b485lling			error = nvlist_add_uint64(props,
618990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
619990b485lling		}
620990b485lling	}
622990b485lling	return (error);
625379c004Eric Schrockvoid
626379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
627379c004Eric Schrock{
628379c004Eric Schrock	char *cachefile;
629379c004Eric Schrock	spa_config_dirent_t *dp;
630379c004Eric Schrock
631379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
632379c004Eric Schrock	    &cachefile) != 0)
633379c004Eric Schrock		return;
634379c004Eric Schrock
635379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
636379c004Eric Schrock	    KM_SLEEP);
637379c004Eric Schrock
638379c004Eric Schrock	if (cachefile[0] == '\0')
639379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
640379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
641379c004Eric Schrock		dp->scd_path = NULL;
642379c004Eric Schrock	else
643379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
644379c004Eric Schrock
645379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
646379c004Eric Schrock	if (need_sync)
647379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
648379c004Eric Schrock}
649379c004Eric Schrock
651990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
653990b485lling	int error;
654ad135b5Christopher Siden	nvpair_t *elem = NULL;
655379c004Eric Schrock	boolean_t need_sync = B_FALSE;
657990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
658990b485lling		return (error);
660379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
661ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
662379c004Eric Schrock
663f9af39bGeorge Wilson		if (prop == ZPOOL_PROP_CACHEFILE ||
664f9af39bGeorge Wilson		    prop == ZPOOL_PROP_ALTROOT ||
665f9af39bGeorge Wilson		    prop == ZPOOL_PROP_READONLY)
666379c004Eric Schrock			continue;
667379c004Eric Schrock
668ad135b5Christopher Siden		if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
669ad135b5Christopher Siden			uint64_t ver;
670ad135b5Christopher Siden
671ad135b5Christopher Siden			if (prop == ZPOOL_PROP_VERSION) {
672ad135b5Christopher Siden				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
673ad135b5Christopher Siden			} else {
674ad135b5Christopher Siden				ASSERT(zpool_prop_feature(nvpair_name(elem)));
675ad135b5Christopher Siden				ver = SPA_VERSION_FEATURES;
676ad135b5Christopher Siden				need_sync = B_TRUE;
677ad135b5Christopher Siden			}
678ad135b5Christopher Siden
679ad135b5Christopher Siden			/* Save time if the version is already set. */
680ad135b5Christopher Siden			if (ver == spa_version(spa))
681ad135b5Christopher Siden				continue;
682ad135b5Christopher Siden
683ad135b5Christopher Siden			/*
684ad135b5Christopher Siden			 * In addition to the pool directory object, we might
685ad135b5Christopher Siden			 * create the pool properties object, the features for
686ad135b5Christopher Siden			 * read object, the features for write object, or the
687ad135b5Christopher Siden			 * feature descriptions object.
688ad135b5Christopher Siden			 */
6893b2aab1Matthew Ahrens			error = dsl_sync_task(spa->spa_name, NULL,
6907d46dc6Matthew Ahrens			    spa_sync_version, &ver,
6917d46dc6Matthew Ahrens			    6, ZFS_SPACE_CHECK_RESERVED);
692ad135b5Christopher Siden			if (error)
693ad135b5Christopher Siden				return (error);
694ad135b5Christopher Siden			continue;
695ad135b5Christopher Siden		}
696ad135b5Christopher Siden
697379c004Eric Schrock		need_sync = B_TRUE;
698379c004Eric Schrock		break;
699379c004Eric Schrock	}
700379c004Eric Schrock
701ad135b5Christopher Siden	if (need_sync) {
7023b2aab1Matthew Ahrens		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
7037d46dc6Matthew Ahrens		    nvp, 6, ZFS_SPACE_CHECK_RESERVED));
704ad135b5Christopher Siden	}
705ad135b5Christopher Siden
706ad135b5Christopher Siden	return (0);
710990b485lling * If the bootfs property value is dsobj, clear it.
711990b485lling */
713990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
715990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
716990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
717990b485lling		    spa->spa_pool_props_object,
718990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
719990b485lling		spa->spa_bootfs = 0;
720990b485lling	}
723dfbb943George Wilson/*ARGSUSED*/
724dfbb943George Wilsonstatic int
7253b2aab1Matthew Ahrensspa_change_guid_check(void *arg, dmu_tx_t *tx)
726dfbb943George Wilson{
7273b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7283b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
729dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
730dfbb943George Wilson	uint64_t vdev_state;
731dfbb943George Wilson
732dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
733dfbb943George Wilson	vdev_state = rvd->vdev_state;
734dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
735dfbb943George Wilson
736dfbb943George Wilson	if (vdev_state != VDEV_STATE_HEALTHY)
737be6fd75Matthew Ahrens		return (SET_ERROR(ENXIO));
738dfbb943George Wilson
739dfbb943George Wilson	ASSERT3U(spa_guid(spa), !=, *newguid);
740dfbb943George Wilson
741dfbb943George Wilson	return (0);
742dfbb943George Wilson}
743dfbb943George Wilson
744dfbb943George Wilsonstatic void
7453b2aab1Matthew Ahrensspa_change_guid_sync(void *arg, dmu_tx_t *tx)
746dfbb943George Wilson{
7473b2aab1Matthew Ahrens	uint64_t *newguid = arg;
7483b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
749dfbb943George Wilson	uint64_t oldguid;
750dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
751dfbb943George Wilson
752dfbb943George Wilson	oldguid = spa_guid(spa);
753dfbb943George Wilson
754dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
755dfbb943George Wilson	rvd->vdev_guid = *newguid;
756dfbb943George Wilson	rvd->vdev_guid_sum += (*newguid - oldguid);
757dfbb943George Wilson	vdev_config_dirty(rvd);
758dfbb943George Wilson	spa_config_exit(spa, SCL_STATE, FTAG);
759dfbb943George Wilson
76020128a0George Wilson	spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
761dfbb943George Wilson	    oldguid, *newguid);
762dfbb943George Wilson}
763dfbb943George Wilson
765e9103aaGarrett D'Amore * Change the GUID for the pool.  This is done so that we can later
766e9103aaGarrett D'Amore * re-import a pool built from a clone of our own vdevs.  We will modify
767e9103aaGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our
768e9103aaGarrett D'Amore * vdevs dirty.  Note that we must make sure that all our vdevs are
769e9103aaGarrett D'Amore * online when we do this, or else any vdevs that weren't present
770e9103aaGarrett D'Amore * would be orphaned from our pool.  We are also going to issue a
771e9103aaGarrett D'Amore * sysevent to update any watchers.
772e9103aaGarrett D'Amore */
773e9103aaGarrett D'Amoreint
774e9103aaGarrett D'Amorespa_change_guid(spa_t *spa)
775e9103aaGarrett D'Amore{
776dfbb943George Wilson	int error;
777dfbb943George Wilson	uint64_t guid;
778e9103aaGarrett D'Amore
7792c1e2b4George Wilson	mutex_enter(&spa->spa_vdev_top_lock);
780dfbb943George Wilson	mutex_enter(&spa_namespace_lock);
781dfbb943George Wilson	guid = spa_generate_guid(NULL);
782e9103aaGarrett D'Amore
7833b2aab1Matthew Ahrens	error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
7847d46dc6Matthew Ahrens	    spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
785e9103aaGarrett D'Amore
786dfbb943George Wilson	if (error == 0) {
787dfbb943George Wilson		spa_config_sync(spa, B_FALSE, B_TRUE);
788dfbb943George Wilson		spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
789dfbb943George Wilson	}
790e9103aaGarrett D'Amore
791dfbb943George Wilson	mutex_exit(&spa_namespace_lock);
7922c1e2b4George Wilson	mutex_exit(&spa->spa_vdev_top_lock);
793e9103aaGarrett D'Amore
794dfbb943George Wilson	return (error);
795e9103aaGarrett D'Amore}
796e9103aaGarrett D'Amore
797e9103aaGarrett D'Amore/*
798fa9e406ahrens * ==========================================================================
799fa9e406ahrens * SPA state manipulation (open/create/destroy/import/export)
800fa9e406ahrens * ==========================================================================
801fa9e406ahrens */