spa.c revision c3d26abc9ee97b4f60233556aadeb57e0bd30bb9
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
2199653d4eschrock
22fa9e406ahrens/*
2398d1cbfGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
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 [integros.com]
295aeb947Garrett D'Amore */
30fa9e406ahrens
31fa9e406ahrens/*
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 */
38fa9e406ahrens
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>
72fa9e406ahrens
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 */
815679c89jv
82990b485lling#include "zfs_prop.h"
83b7b9745perrin#include "zfs_comutil.h"
84990b485lling
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;
97416e0cdek
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);
149990b485lling
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
164990b485lling/*
165990b485lling * ==========================================================================
166990b485lling * SPA properties routines
167990b485lling * ==========================================================================
168990b485lling */
169990b485lling
170990b485lling/*
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)
176990b485lling{
177990b485lling	const char *propname = zpool_prop_to_name(prop);
178990b485lling	nvlist_t *propval;
179990b485lling
1809d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1819d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
182990b485lling
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);
187990b485lling
1889d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
189990b485lling	nvlist_free(propval);
190990b485lling}
191990b485lling
192990b485lling/*
193990b485lling * Get property values from the spa configuration.
194990b485lling */
1959d82f4fllingstatic void
196990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
197990b485lling{
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);
204990b485lling
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	}
239990b485lling
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);
265990b485lling
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);
274990b485lling
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	}
292990b485lling}
293990b485lling
294990b485lling/*
295990b485lling * Get zpool property values.
296990b485lling */
297990b485llingint
298990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
299990b485lling{
300b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
301990b485lling	zap_cursor_t zc;
302990b485lling	zap_attribute_t za;
303990b485lling	int err;
304990b485lling
3059d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
306990b485lling
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);
313990b485lling
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	}
319990b485lling
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;
330990b485lling
331990b485lling		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
332990b485lling			continue;
333990b485lling
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;
340990b485lling
341990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
342990b485lling				dsl_pool_t *dp;
343990b485lling				dsl_dataset_t *ds = NULL;
344990b485lling
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				}
352990b485lling
353990b485lling				strval = kmem_alloc(
354990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
355990b485lling				    KM_SLEEP);
356990b485lling				dsl_dataset_name(ds, strval);
357745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
3583b2aab1Matthew Ahrens				dsl_pool_config_exit(dp, FTAG);
359990b485lling			} else {
360990b485lling				strval = NULL;
361990b485lling				intval = za.za_first_integer;
362990b485lling			}
363990b485lling
3649d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
365990b485lling
366990b485lling			if (strval != NULL)
367990b485lling				kmem_free(strval,
368990b485lling				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
369990b485lling
370990b485lling			break;
371990b485lling
372990b485lling		case 1:
373990b485lling			/* string property */
374990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
375990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
376990b485lling			    za.za_name, 1, za.za_num_integers, strval);
377990b485lling			if (err) {
378990b485lling				kmem_free(strval, za.za_num_integers);
379990b485lling				break;
380990b485lling			}
3819d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
382990b485lling			kmem_free(strval, za.za_num_integers);
383990b485lling			break;
384990b485lling
385990b485lling		default:
386990b485lling			break;
387990b485lling		}
388990b485lling	}
389990b485lling	zap_cursor_fini(&zc);
390990b485lling	mutex_exit(&spa->spa_props_lock);
391990b485llingout:
392990b485lling	if (err && err != ENOENT) {
393990b485lling		nvlist_free(*nvp);
3949d82f4flling		*nvp = NULL;
395990b485lling		return (err);
396990b485lling	}
397990b485lling
398990b485lling	return (0);
399990b485lling}
400990b485lling
401990b485lling/*
402990b485lling * Validate the given pool properties nvlist and modify the list
403990b485lling * for the property values to be set.
404990b485lling */
405990b485llingstatic int
406990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
407990b485lling{
408990b485lling	nvpair_t *elem;
409990b485lling	int error = 0, reset_bootfs = 0;
410d5285caGeorge Wilson	uint64_t objnum = 0;
411ad135b5Christopher Siden	boolean_t has_feature = B_FALSE;
412990b485lling
413990b485lling	elem = NULL;
414990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
415990b485lling		uint64_t intval;
416ad135b5Christopher Siden		char *strval, *slash, *check, *fname;
417ad135b5Christopher Siden		const char *propname = nvpair_name(elem);
418ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(propname);
419ad135b5Christopher Siden
420ad135b5Christopher Siden		switch (prop) {
421ad135b5Christopher Siden		case ZPROP_INVAL:
422ad135b5Christopher Siden			if (!zpool_prop_feature(propname)) {
423be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
424ad135b5Christopher Siden				break;
425ad135b5Christopher Siden			}
426990b485lling
427ad135b5Christopher Siden			/*
428ad135b5Christopher Siden			 * Sanitize the input.
429ad135b5Christopher Siden			 */
430ad135b5Christopher Siden			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
431be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
432ad135b5Christopher Siden				break;
433ad135b5Christopher Siden			}
434990b485lling
435ad135b5Christopher Siden			if (nvpair_value_uint64(elem, &intval) != 0) {
436be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
437ad135b5Christopher Siden				break;
438ad135b5Christopher Siden			}
439ad135b5Christopher Siden
440ad135b5Christopher Siden			if (intval != 0) {
441be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
442ad135b5Christopher Siden				break;
443ad135b5Christopher Siden			}
444ad135b5Christopher Siden
445ad135b5Christopher Siden			fname = strchr(propname, '@') + 1;
446ad135b5Christopher Siden			if (zfeature_lookup_name(fname, NULL) != 0) {
447be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
448ad135b5Christopher Siden				break;
449ad135b5Christopher Siden			}
450ad135b5Christopher Siden
451ad135b5Christopher Siden			has_feature = B_TRUE;
452ad135b5Christopher Siden			break;
453990b485lling
454990b485lling		case ZPOOL_PROP_VERSION:
455990b485lling			error = nvpair_value_uint64(elem, &intval);
456990b485lling			if (!error &&
457ad135b5Christopher Siden			    (intval < spa_version(spa) ||
458ad135b5Christopher Siden			    intval > SPA_VERSION_BEFORE_FEATURES ||
459ad135b5Christopher Siden			    has_feature))
460be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
461990b485lling			break;
462990b485lling
463990b485lling		case ZPOOL_PROP_DELEGATION:
464990b485lling		case ZPOOL_PROP_AUTOREPLACE:
465d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
466573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
467990b485lling			error = nvpair_value_uint64(elem, &intval);
468990b485lling			if (!error && intval > 1)
469be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
470990b485lling			break;
471990b485lling
472990b485lling		case ZPOOL_PROP_BOOTFS:
47325f89eeJeff Bonwick			/*
47425f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
47525f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
47625f89eeJeff Bonwick			 * the bootfs property cannot be set.
47725f89eeJeff Bonwick			 */
478990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
479be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
480990b485lling				break;
481990b485lling			}
482990b485lling
483990b485lling			/*
48415e6edfgw			 * Make sure the vdev config is bootable
485990b485lling			 */
48615e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
487be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
488990b485lling				break;
489990b485lling			}
490990b485lling
491990b485lling			reset_bootfs = 1;
492990b485lling
493990b485lling			error = nvpair_value_string(elem, &strval);
494990b485lling
495990b485lling			if (!error) {
496ad135b5Christopher Siden				objset_t *os;
497b515258Matthew Ahrens				uint64_t propval;
49815e6edfgw
499990b485lling				if (strval == NULL || strval[0] == '\0') {
500990b485lling					objnum = zpool_prop_default_numeric(
501990b485lling					    ZPOOL_PROP_BOOTFS);
502990b485lling					break;
503990b485lling				}
504990b485lling
505503ad85Matthew Ahrens				if (error = dmu_objset_hold(strval, FTAG, &os))
506990b485lling					break;
50715e6edfgw
508b515258Matthew Ahrens				/*
509b515258Matthew Ahrens				 * Must be ZPL, and its property settings
510b515258Matthew Ahrens				 * must be supported by GRUB (compression
511b515258Matthew Ahrens				 * is not gzip, and large blocks are not used).
512b515258Matthew Ahrens				 */
513503ad85Matthew Ahrens
514503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
515be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
5163b2aab1Matthew Ahrens				} else if ((error =
5173b2aab1Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
51815e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
519b515258Matthew Ahrens				    &propval)) == 0 &&
520b515258Matthew Ahrens				    !BOOTFS_COMPRESS_VALID(propval)) {
521b515258Matthew Ahrens					error = SET_ERROR(ENOTSUP);
522b515258Matthew Ahrens				} else if ((error =
523b515258Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
524b515258Matthew Ahrens				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
525b515258Matthew Ahrens				    &propval)) == 0 &&
526b515258Matthew Ahrens				    propval > SPA_OLD_MAXBLOCKSIZE) {
527be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
52815e6edfgw				} else {
52915e6edfgw					objnum = dmu_objset_id(os);
53015e6edfgw				}
531503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
532990b485lling			}
533990b485lling			break;
534e14bb32Jeff Bonwick
5350a4e951gw		case ZPOOL_PROP_FAILUREMODE:
5360a4e951gw			error = nvpair_value_uint64(elem, &intval);
5370a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
5380a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
539be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5400a4e951gw
5410a4e951gw			/*
5420a4e951gw			 * This is a special case which only occurs when
5430a4e951gw			 * the pool has completely failed. This allows
5440a4e951gw			 * the user to change the in-core failmode property
5450a4e951gw			 * without syncing it out to disk (I/Os might
5460a4e951gw			 * currently be blocked). We do this by returning
5470a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
5480a4e951gw			 * into thinking we encountered a property validation
5490a4e951gw			 * error.
5500a4e951gw			 */
551e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
5520a4e951gw				spa->spa_failmode = intval;
553be6fd75Matthew Ahrens				error = SET_ERROR(EIO);
5540a4e951gw			}
5550a4e951gw			break;
5562f8aaabeschrock
5572f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
5582f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
5592f8aaabeschrock				break;
5602f8aaabeschrock
5612f8aaabeschrock			if (strval[0] == '\0')
5622f8aaabeschrock				break;
5632f8aaabeschrock
5642f8aaabeschrock			if (strcmp(strval, "none") == 0)
5652f8aaabeschrock				break;
5662f8aaabeschrock
5672f8aaabeschrock			if (strval[0] != '/') {
568be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5692f8aaabeschrock				break;
5702f8aaabeschrock			}
5712f8aaabeschrock
5722f8aaabeschrock			slash = strrchr(strval, '/');
5732f8aaabeschrock			ASSERT(slash != NULL);
5742f8aaabeschrock
5752f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5762f8aaabeschrock			    strcmp(slash, "/..") == 0)
577be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
5782f8aaabeschrock			break;
579b24ab67Jeff Bonwick
5808704186Dan McDonald		case ZPOOL_PROP_COMMENT:
5818704186Dan McDonald			if ((error = nvpair_value_string(elem, &strval)) != 0)
5828704186Dan McDonald				break;
5838704186Dan McDonald			for (check = strval; *check != '\0'; check++) {
5848704186Dan McDonald				/*
5858704186Dan McDonald				 * The kernel doesn't have an easy isprint()
5868704186Dan McDonald				 * check.  For this kernel check, we merely
5878704186Dan McDonald				 * check ASCII apart from DEL.  Fix this if
5888704186Dan McDonald				 * there is an easy-to-use kernel isprint().
5898704186Dan McDonald				 */
5908704186Dan McDonald				if (*check >= 0x7f) {
591be6fd75Matthew Ahrens					error = SET_ERROR(EINVAL);
5928704186Dan McDonald					break;
5938704186Dan McDonald				}
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		}
609990b485lling
610990b485lling		if (error)
611990b485lling			break;
612990b485lling	}
613990b485lling
614990b485lling	if (!error && reset_bootfs) {
615990b485lling		error = nvlist_remove(props,
616990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
617990b485lling
618990b485lling		if (!error) {
619990b485lling			error = nvlist_add_uint64(props,
620990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
621990b485lling		}
622990b485lling	}
623990b485lling
624990b485lling	return (error);
625990b485lling}
626990b485lling
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
652990b485llingint
653990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
654990b485lling{
655990b485lling	int error;
656ad135b5Christopher Siden	nvpair_t *elem = NULL;
657379c004Eric Schrock	boolean_t need_sync = B_FALSE;
658990b485lling
659990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
660990b485lling		return (error);
661990b485lling
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);
709990b485lling}
710990b485lling
711990b485lling/*
712990b485lling * If the bootfs property value is dsobj, clear it.
713990b485lling */
714990b485llingvoid
715990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
716990b485lling{
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	}
723990b485lling}
724990b485lling
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
766fa9e406ahrens/*
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)
802