spa.c revision 54811da5ac6b517992fdc173df5d605e4e61fdc0
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.
245cabbc6Prashanth Sreenivasa * Copyright (c) 2011, 2018 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 []
29c8811bdToomas Soome * Copyright 2016 Toomas Soome <>
3082f63c3Jerry Jelinek * Copyright 2018 Joyent, Inc.
311702cceAlek Pinchuk * Copyright (c) 2017 Datto Inc.
320fb055eAndy Fiddaman * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
335aeb947Garrett D'Amore */
363e30c24Will Andrews * SPA: Storage Pool Allocator
373e30c24Will Andrews *
38fa9e406ahrens * This file contains all the routines used when modifying on-disk SPA state.
39fa9e406ahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
40fa9e406ahrens * pool.
41fa9e406ahrens */
43fa9e406ahrens#include <sys/zfs_context.h>
44ea8dc4beschrock#include <sys/fm/fs/zfs.h>
45fa9e406ahrens#include <sys/spa_impl.h>
46fa9e406ahrens#include <sys/zio.h>
47fa9e406ahrens#include <sys/zio_checksum.h>
48fa9e406ahrens#include <sys/dmu.h>
49fa9e406ahrens#include <sys/dmu_tx.h>
50fa9e406ahrens#include <sys/zap.h>
51fa9e406ahrens#include <sys/zil.h>
52b24ab67Jeff Bonwick#include <sys/ddt.h>
53fa9e406ahrens#include <sys/vdev_impl.h>
545cabbc6Prashanth Sreenivasa#include <sys/vdev_removal.h>
555cabbc6Prashanth Sreenivasa#include <sys/vdev_indirect_mapping.h>
565cabbc6Prashanth Sreenivasa#include <sys/vdev_indirect_births.h>
57094e47eGeorge Wilson#include <sys/vdev_initialize.h>
58fa9e406ahrens#include <sys/metaslab.h>
5988ecc94George Wilson#include <sys/metaslab_impl.h>
60fa9e406ahrens#include <sys/uberblock_impl.h>
61fa9e406ahrens#include <sys/txg.h>
62fa9e406ahrens#include <sys/avl.h>
635cabbc6Prashanth Sreenivasa#include <sys/bpobj.h>
64fa9e406ahrens#include <sys/dmu_traverse.h>
65b1b8ab3lling#include <sys/dmu_objset.h>
66fa9e406ahrens#include <sys/unique.h>
67fa9e406ahrens#include <sys/dsl_pool.h>
68b1b8ab3lling#include <sys/dsl_dataset.h>
69fa9e406ahrens#include <sys/dsl_dir.h>
70fa9e406ahrens#include <sys/dsl_prop.h>
71b1b8ab3lling#include <sys/dsl_synctask.h>
72fa9e406ahrens#include <sys/fs/zfs.h>
73fa94a07brendan#include <sys/arc.h>
74fa9e406ahrens#include <sys/callb.h>
759517395ek#include <sys/systeminfo.h>
76e7cbe64gw#include <sys/spa_boot.h>
77573ca77George Wilson#include <sys/zfs_ioctl.h>
783f9d6adLin Ling#include <sys/dsl_scan.h>
79ad135b5Christopher Siden#include <sys/zfeature.h>
803b2aab1Matthew Ahrens#include <sys/dsl_destroy.h>
81770499eDan Kimmel#include <sys/abd.h>
835679c89jv#ifdef	_KERNEL
84dedec47Jack Meng#include <sys/bootprops.h>
8535a5a35Jonathan Adams#include <sys/callb.h>
8635a5a35Jonathan Adams#include <sys/cpupart.h>
8735a5a35Jonathan Adams#include <sys/pool.h>
8835a5a35Jonathan Adams#include <sys/sysdc.h>
8935a5a35Jonathan Adams#include <sys/zone.h>
905679c89jv#endif	/* _KERNEL */
92990b485lling#include "zfs_prop.h"
93b7b9745perrin#include "zfs_comutil.h"
953cb69f7Will Andrews/*
963cb69f7Will Andrews * The interval, in seconds, at which failed configuration cache file writes
973cb69f7Will Andrews * should be retried.
983cb69f7Will Andrews */
995cabbc6Prashanth Sreenivasaint zfs_ccw_retry_interval = 300;
1003cb69f7Will Andrews
10135a5a35Jonathan Adamstypedef enum zti_modes {
102ec94d32Adam Leventhal	ZTI_MODE_FIXED,			/* value is # of threads (min 1) */
103ec94d32Adam Leventhal	ZTI_MODE_BATCH,			/* cpu-intensive; value is ignored */
104ec94d32Adam Leventhal	ZTI_MODE_NULL,			/* don't create a taskq */
105ec94d32Adam Leventhal	ZTI_NMODES
10635a5a35Jonathan Adams} zti_modes_t;
108ec94d32Adam Leventhal#define	ZTI_P(n, q)	{ ZTI_MODE_FIXED, (n), (q) }
109ec94d32Adam Leventhal#define	ZTI_BATCH	{ ZTI_MODE_BATCH, 0, 1 }
110ec94d32Adam Leventhal#define	ZTI_NULL	{ ZTI_MODE_NULL, 0, 0 }
1112e0c549Jonathan Adams
112ec94d32Adam Leventhal#define	ZTI_N(n)	ZTI_P(n, 1)
113ec94d32Adam Leventhal#define	ZTI_ONE		ZTI_N(1)
1142e0c549Jonathan Adams
1152e0c549Jonathan Adamstypedef struct zio_taskq_info {
116ec94d32Adam Leventhal	zti_modes_t zti_mode;
11780eb36fGeorge Wilson	uint_t zti_value;
118ec94d32Adam Leventhal	uint_t zti_count;
1192e0c549Jonathan Adams} zio_taskq_info_t;
1202e0c549Jonathan Adams
1212e0c549Jonathan Adamsstatic const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
12235a5a35Jonathan Adams	"issue", "issue_high", "intr", "intr_high"
1232e0c549Jonathan Adams};
1242e0c549Jonathan Adams
12580eb36fGeorge Wilson/*
126ec94d32Adam Leventhal * This table defines the taskq settings for each ZFS I/O type. When
127ec94d32Adam Leventhal * initializing a pool, we use this table to create an appropriately sized
128ec94d32Adam Leventhal * taskq. Some operations are low volume and therefore have a small, static
129ec94d32Adam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
130ec94d32Adam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH
131ec94d32Adam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations
132ec94d32Adam Leventhal * are so high frequency and short-lived that the taskq itself can become a a
133ec94d32Adam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
134ec94d32Adam Leventhal * additional degree of parallelism specified by the number of threads per-
135ec94d32Adam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the
136ec94d32Adam Leventhal * particular taskq is chosen at random.
137ec94d32Adam Leventhal *
138ec94d32Adam Leventhal * The different taskq priorities are to handle the different contexts (issue
139ec94d32Adam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
140ec94d32Adam Leventhal * need to be handled with minimum delay.
14180eb36fGeorge Wilson */
14280eb36fGeorge Wilsonconst zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
14380eb36fGeorge Wilson	/* ISSUE	ISSUE_HIGH	INTR		INTR_HIGH */
144ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* NULL */
1451b497abAdam H. Leventhal	{ ZTI_N(8),	ZTI_NULL,	ZTI_P(12, 8),	ZTI_NULL }, /* READ */
146ec94d32Adam Leventhal	{ ZTI_BATCH,	ZTI_N(5),	ZTI_N(8),	ZTI_N(5) }, /* WRITE */
147ec94d32Adam Leventhal	{ ZTI_P(12, 8),	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* FREE */
148ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* CLAIM */
149ec94d32Adam Leventhal	{ ZTI_ONE,	ZTI_NULL,	ZTI_ONE,	ZTI_NULL }, /* IOCTL */
1502e0c549Jonathan Adams};
1512e0c549Jonathan Adams
1523b2aab1Matthew Ahrensstatic void spa_sync_version(void *arg, dmu_tx_t *tx);
1533b2aab1Matthew Ahrensstatic void spa_sync_props(void *arg, dmu_tx_t *tx);
15489a89ebllingstatic boolean_t spa_has_active_shared_spare(spa_t *spa);
1558671400Serapheim Dimitropoulosstatic int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport);
156cb04b87Mark J Musantestatic void spa_vdev_resilver_done(spa_t *spa);
15869962b5Matthew Ahrensuint_t		zio_taskq_batch_pct = 75;	/* 1 thread per cpu in pset */
15935a5a35Jonathan Adamsid_t		zio_taskq_psrset_bind = PS_NONE;
16035a5a35Jonathan Adamsboolean_t	zio_taskq_sysdc = B_TRUE;	/* use SDC scheduling class */
16135a5a35Jonathan Adamsuint_t		zio_taskq_basedc = 80;		/* base duty cycle */
16235a5a35Jonathan Adams
16335a5a35Jonathan Adamsboolean_t	spa_create_process = B_TRUE;	/* no process ==> no sysdc */
16401f55e4George Wilsonextern int	zfs_sync_pass_deferred_free;
16535a5a35Jonathan Adams
16635a5a35Jonathan Adams/*
167e144c4ePavel Zakharov * Report any spa_load_verify errors found, but do not fail spa_load.
168e144c4ePavel Zakharov * This is used by zdb to analyze non-idle pools.
169e144c4ePavel Zakharov */
170e144c4ePavel Zakharovboolean_t	spa_load_verify_dryrun = B_FALSE;
171e144c4ePavel Zakharov
172e144c4ePavel Zakharov/*
17335a5a35Jonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
17435a5a35Jonathan Adams * to get the vdev stats associated with the imported devices.
17535a5a35Jonathan Adams */
17635a5a35Jonathan Adams#define	TRYIMPORT_NAME	"$import"
17735a5a35Jonathan Adams
1796f79381Pavel Zakharov * For debugging purposes: print out vdev tree during pool import.
1806f79381Pavel Zakharov */
1816f79381Pavel Zakharovboolean_t	spa_load_print_vdev_tree = B_FALSE;
1826f79381Pavel Zakharov
1836f79381Pavel Zakharov/*
1846f79381Pavel Zakharov * A non-zero value for zfs_max_missing_tvds means that we allow importing
1856f79381Pavel Zakharov * pools with missing top-level vdevs. This is strictly intended for advanced
1866f79381Pavel Zakharov * pool recovery cases since missing data is almost inevitable. Pools with
1876f79381Pavel Zakharov * missing devices can only be imported read-only for safety reasons, and their
1886f79381Pavel Zakharov * fail-mode will be automatically set to "continue".
1896f79381Pavel Zakharov *
1906f79381Pavel Zakharov * With 1 missing vdev we should be able to import the pool and mount all
1916f79381Pavel Zakharov * datasets. User data that was not modified after the missing device has been
1926f79381Pavel Zakharov * added should be recoverable. This means that snapshots created prior to the
1936f79381Pavel Zakharov * addition of that device should be completely intact.
1946f79381Pavel Zakharov *
1956f79381Pavel Zakharov * With 2 missing vdevs, some datasets may fail to mount since there are
1966f79381Pavel Zakharov * dataset statistics that are stored as regular metadata. Some data might be
1976f79381Pavel Zakharov * recoverable if those vdevs were added recently.
1986f79381Pavel Zakharov *
1996f79381Pavel Zakharov * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
2006f79381Pavel Zakharov * may be missing entirely. Chances of data recovery are very low. Note that
2016f79381Pavel Zakharov * there are also risks of performing an inadvertent rewind as we might be
2026f79381Pavel Zakharov * missing all the vdevs with the latest uberblocks.
2036f79381Pavel Zakharov */
2046f79381Pavel Zakharovuint64_t	zfs_max_missing_tvds = 0;
2056f79381Pavel Zakharov
2066f79381Pavel Zakharov/*
2076f79381Pavel Zakharov * The parameters below are similar to zfs_max_missing_tvds but are only
2086f79381Pavel Zakharov * intended for a preliminary open of the pool with an untrusted config which
2096f79381Pavel Zakharov * might be incomplete or out-dated.
2106f79381Pavel Zakharov *
2116f79381Pavel Zakharov * We are more tolerant for pools opened from a cachefile since we could have
2126f79381Pavel Zakharov * an out-dated cachefile where a device removal was not registered.
2136f79381Pavel Zakharov * We could have set the limit arbitrarily high but in the case where devices
2146f79381Pavel Zakharov * are really missing we would want to return the proper error codes; we chose
2156f79381Pavel Zakharov * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
2166f79381Pavel Zakharov * and we get a chance to retrieve the trusted config.
2176f79381Pavel Zakharov */
2186f79381Pavel Zakharovuint64_t	zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1;
2198671400Serapheim Dimitropoulos
2206f79381Pavel Zakharov/*
2216f79381Pavel Zakharov * In the case where config was assembled by scanning device paths (/dev/dsks
2226f79381Pavel Zakharov * by default) we are less tolerant since all the existing devices should have
2236f79381Pavel Zakharov * been detected and we want spa_load to return the right error codes.
2246f79381Pavel Zakharov */
2256f79381Pavel Zakharovuint64_t	zfs_max_missing_tvds_scan = 0;
2266f79381Pavel Zakharov
2276f79381Pavel Zakharov/*
2288671400Serapheim Dimitropoulos * Debugging aid that pauses spa_sync() towards the end.
2298671400Serapheim Dimitropoulos */
2308671400Serapheim Dimitropoulosboolean_t	zfs_pause_spa_sync = B_FALSE;
2318671400Serapheim Dimitropoulos
2328671400Serapheim Dimitropoulos/*
233990b485lling * ==========================================================================
234990b485lling * SPA properties routines
235990b485lling * ==========================================================================
236990b485lling */
239990b485lling * Add a (source=src, propname=propval) list to an nvlist.
240990b485lling */
2419d82f4fllingstatic void
242990b485llingspa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
243990b485lling    uint64_t intval, zprop_source_t src)
245990b485lling	const char *propname = zpool_prop_to_name(prop);
246990b485lling	nvlist_t *propval;
2489d82f4flling	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2499d82f4flling	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
2519d82f4flling	if (strval != NULL)
2529d82f4flling		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
2539d82f4flling	else
2549d82f4flling		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
2569d82f4flling	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
257990b485lling	nvlist_free(propval);
261990b485lling * Get property values from the spa configuration.
262990b485lling */
2639d82f4fllingstatic void
264990b485llingspa_prop_get_config(spa_t *spa, nvlist_t **nvp)
2664263d13George Wilson	vdev_t *rvd = spa->spa_root_vdev;
267ad135b5Christopher Siden	dsl_pool_t *pool = spa->spa_dsl_pool;
2682e4c998George Wilson	uint64_t size, alloc, cap, version;
269990b485lling	zprop_source_t src = ZPROP_SRC_NONE;
270c5904d1eschrock	spa_config_dirent_t *dp;
2712e4c998George Wilson	metaslab_class_t *mc = spa_normal_class(spa);
273e14bb32Jeff Bonwick	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
274e14bb32Jeff Bonwick
2754263d13George Wilson	if (rvd != NULL) {
276485bbbfGeorge Wilson		alloc = metaslab_class_get_alloc(spa_normal_class(spa));
277b24ab67Jeff Bonwick		size = metaslab_class_get_space(spa_normal_class(spa));
278379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
279379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
280485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
281485bbbfGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
282485bbbfGeorge Wilson		    size - alloc, src);
2838671400Serapheim Dimitropoulos		spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL,
2848671400Serapheim Dimitropoulos		    spa->spa_checkpoint_info.sci_dspace, src);
2854263d13George Wilson
2862e4c998George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
2872e4c998George Wilson		    metaslab_class_fragmentation(mc), src);
2882e4c998George Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
2892e4c998George Wilson		    metaslab_class_expandable_space(mc), src);
290f9af39bGeorge Wilson		spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
291f9af39bGeorge Wilson		    (spa_mode(spa) == FREAD), src);
292379c004Eric Schrock
293485bbbfGeorge Wilson		cap = (size == 0) ? 0 : (alloc * 100 / size);
294379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
295379c004Eric Schrock
296b24ab67Jeff Bonwick		spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
297b24ab67Jeff Bonwick		    ddt_get_pool_dedup_ratio(spa), src);
298b24ab67Jeff Bonwick
299379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
3004263d13George Wilson		    rvd->vdev_state, src);
301379c004Eric Schrock
302379c004Eric Schrock		version = spa_version(spa);
303379c004Eric Schrock		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
304379c004Eric Schrock			src = ZPROP_SRC_DEFAULT;
305379c004Eric Schrock		else
306379c004Eric Schrock			src = ZPROP_SRC_LOCAL;
307379c004Eric Schrock		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
308379c004Eric Schrock	}
310ad135b5Christopher Siden	if (pool != NULL) {
311ad135b5Christopher Siden		/*
312ad135b5Christopher Siden		 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
313ad135b5Christopher Siden		 * when opening pools before this version freedir will be NULL.
314ad135b5Christopher Siden		 */
3157fd05acMatthew Ahrens		if (pool->dp_free_dir != NULL) {
316ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
317c137962Justin T. Gibbs			    dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
318c137962Justin T. Gibbs			    src);
319ad135b5Christopher Siden		} else {
320ad135b5Christopher Siden			spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
321ad135b5Christopher Siden			    NULL, 0, src);
322ad135b5Christopher Siden		}
3237fd05acMatthew Ahrens
3247fd05acMatthew Ahrens		if (pool->dp_leak_dir != NULL) {
3257fd05acMatthew Ahrens			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
326c137962Justin T. Gibbs			    dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
327c137962Justin T. Gibbs			    src);
3287fd05acMatthew Ahrens		} else {
3297fd05acMatthew Ahrens			spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
3307fd05acMatthew Ahrens			    NULL, 0, src);
3317fd05acMatthew Ahrens		}
332ad135b5Christopher Siden	}
333ad135b5Christopher Siden
3349d82f4flling	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
3368704186Dan McDonald	if (spa->spa_comment != NULL) {
3378704186Dan McDonald		spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
3388704186Dan McDonald		    0, ZPROP_SRC_LOCAL);
3398704186Dan McDonald	}
3408704186Dan McDonald
3419d82f4flling	if (spa->spa_root != NULL)
3429d82f4flling		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
3439d82f4flling		    0, ZPROP_SRC_LOCAL);
345b515258Matthew Ahrens	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
346b515258Matthew Ahrens		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
347b515258Matthew Ahrens		    MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
348b515258Matthew Ahrens	} else {
349b515258Matthew Ahrens		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
350b515258Matthew Ahrens		    SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
351b515258Matthew Ahrens	}
352b515258Matthew Ahrens
35354811daToomas Soome	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) {
35454811daToomas Soome		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
35554811daToomas Soome		    DNODE_MAX_SIZE, ZPROP_SRC_NONE);
35654811daToomas Soome	} else {
35754811daToomas Soome		spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
35854811daToomas Soome		    DNODE_MIN_SIZE, ZPROP_SRC_NONE);
35954811daToomas Soome	}
36054811daToomas Soome
361c5904d1eschrock	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
362c5904d1eschrock		if (dp->scd_path == NULL) {
3639d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
364c5904d1eschrock			    "none", 0, ZPROP_SRC_LOCAL);
365c5904d1eschrock		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
3669d82f4flling			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
367c5904d1eschrock			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
3682f8aaabeschrock		}
3692f8aaabeschrock	}
373990b485lling * Get zpool property values.
374990b485lling */
376990b485llingspa_prop_get(spa_t *spa, nvlist_t **nvp)
378b24ab67Jeff Bonwick	objset_t *mos = spa->spa_meta_objset;
379990b485lling	zap_cursor_t zc;
380990b485lling	zap_attribute_t za;
381990b485lling	int err;
3839d82f4flling	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
385e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
386e14bb32Jeff Bonwick
387990b485lling	/*
388990b485lling	 * Get properties from the spa config.
389990b485lling	 */
3909d82f4flling	spa_prop_get_config(spa, nvp);
392990b485lling	/* If no pool property object, no more prop to get. */
393afee20eGeorge Wilson	if (mos == NULL || spa->spa_pool_props_object == 0) {
394990b485lling		mutex_exit(&spa->spa_props_lock);
395990b485lling		return (0);
396990b485lling	}
398990b485lling	/*
399990b485lling	 * Get properties from the MOS pool property object.
400990b485lling	 */
401990b485lling	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
402990b485lling	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
403990b485lling	    zap_cursor_advance(&zc)) {
404990b485lling		uint64_t intval = 0;
405990b485lling		char *strval = NULL;
406990b485lling		zprop_source_t src = ZPROP_SRC_DEFAULT;
407990b485lling		zpool_prop_t prop;
4094ae5f5fAlan Somers		if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL)
410990b485lling			continue;
412990b485lling		switch (za.za_integer_length) {
413990b485lling		case 8:
414990b485lling			/* integer property */
415990b485lling			if (za.za_first_integer !=
416990b485lling			    zpool_prop_default_numeric(prop))
417990b485lling				src = ZPROP_SRC_LOCAL;
419990b485lling			if (prop == ZPOOL_PROP_BOOTFS) {
420990b485lling				dsl_pool_t *dp;
421990b485lling				dsl_dataset_t *ds = NULL;
423990b485lling				dp = spa_get_dsl(spa);
4243b2aab1Matthew Ahrens				dsl_pool_config_enter(dp, FTAG);
425094e47eGeorge Wilson				err = dsl_dataset_hold_obj(dp,
426094e47eGeorge Wilson				    za.za_first_integer, FTAG, &ds);
427094e47eGeorge Wilson				if (err != 0) {
4283b2aab1Matthew Ahrens					dsl_pool_config_exit(dp, FTAG);
429990b485lling					break;
430990b485lling				}
4329adfa60Matthew Ahrens				strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
433990b485lling				    KM_SLEEP);
434990b485lling				dsl_dataset_name(ds, strval);
435745cd3cmaybee				dsl_dataset_rele(ds, FTAG);
4363b2aab1Matthew Ahrens				dsl_pool_config_exit(dp, FTAG);
437990b485lling			} else {
438990b485lling				strval = NULL;
439990b485lling				intval = za.za_first_integer;
440990b485lling			}
4429d82f4flling			spa_prop_add_list(*nvp, prop, strval, intval, src);
444990b485lling			if (strval != NULL)
4459adfa60Matthew Ahrens				kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
447990b485lling			break;
449990b485lling		case 1:
450990b485lling			/* string property */
451990b485lling			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
452990b485lling			err = zap_lookup(mos, spa->spa_pool_props_object,
453990b485lling			    za.za_name, 1, za.za_num_integers, strval);
454990b485lling			if (err) {
455990b485lling				kmem_free(strval, za.za_num_integers);
456990b485lling				break;
457990b485lling			}
4589d82f4flling			spa_prop_add_list(*nvp, prop, strval, 0, src);
459990b485lling			kmem_free(strval, za.za_num_integers);
460990b485lling			break;
462990b485lling		default:
463990b485lling			break;
464990b485lling		}
465990b485lling	}
466990b485lling	zap_cursor_fini(&zc);
467990b485lling	mutex_exit(&spa->spa_props_lock);
469990b485lling	if (err && err != ENOENT) {
470990b485lling		nvlist_free(*nvp);
4719d82f4flling		*nvp = NULL;
472990b485lling		return (err);
473990b485lling	}
475990b485lling	return (0);
479990b485lling * Validate the given pool properties nvlist and modify the list
480990b485lling * for the property values to be set.
481990b485lling */
482990b485llingstatic int
483990b485llingspa_prop_validate(spa_t *spa, nvlist_t *props)
485990b485lling	nvpair_t *elem;
486990b485lling	int error = 0, reset_bootfs = 0;
487d5285caGeorge Wilson	uint64_t objnum = 0;
488ad135b5Christopher Siden	boolean_t has_feature = B_FALSE;
490990b485lling	elem = NULL;
491990b485lling	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
492990b485lling		uint64_t intval;
493ad135b5Christopher Siden		char *strval, *slash, *check, *fname;
494ad135b5Christopher Siden		const char *propname = nvpair_name(elem);
495ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(propname);
496ad135b5Christopher Siden
497ad135b5Christopher Siden		switch (prop) {
4984ae5f5fAlan Somers		case ZPOOL_PROP_INVAL:
499ad135b5Christopher Siden			if (!zpool_prop_feature(propname)) {
500be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
501ad135b5Christopher Siden				break;
502ad135b5Christopher Siden			}
504ad135b5Christopher Siden			/*
505ad135b5Christopher Siden			 * Sanitize the input.
506ad135b5Christopher Siden			 */
507ad135b5Christopher Siden			if (nvpair_type(elem) != DATA_TYPE_UINT64) {
508be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
509ad135b5Christopher Siden				break;
510ad135b5Christopher Siden			}
512ad135b5Christopher Siden			if (nvpair_value_uint64(elem, &intval) != 0) {
513be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
514ad135b5Christopher Siden				break;
515ad135b5Christopher Siden			}
516ad135b5Christopher Siden
517ad135b5Christopher Siden			if (intval != 0) {
518be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
519ad135b5Christopher Siden				break;
520ad135b5Christopher Siden			}
521ad135b5Christopher Siden
522ad135b5Christopher Siden			fname = strchr(propname, '@') + 1;
523ad135b5Christopher Siden			if (zfeature_lookup_name(fname, NULL) != 0) {
524be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
525ad135b5Christopher Siden				break;
526ad135b5Christopher Siden			}
527ad135b5Christopher Siden
528ad135b5Christopher Siden			has_feature = B_TRUE;
529ad135b5Christopher Siden			break;
531990b485lling		case ZPOOL_PROP_VERSION:
532990b485lling			error = nvpair_value_uint64(elem, &intval);
533990b485lling			if (!error &&
534ad135b5Christopher Siden			    (intval < spa_version(spa) ||
535ad135b5Christopher Siden			    intval > SPA_VERSION_BEFORE_FEATURES ||
536ad135b5Christopher Siden			    has_feature))
537be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
538990b485lling			break;
540990b485lling		case ZPOOL_PROP_DELEGATION:
541990b485lling		case ZPOOL_PROP_AUTOREPLACE:
542d5b5bb2Rich Morris		case ZPOOL_PROP_LISTSNAPS:
543573ca77George Wilson		case ZPOOL_PROP_AUTOEXPAND:
544990b485lling			error = nvpair_value_uint64(elem, &intval);
545990b485lling			if (!error && intval > 1)
546be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
547990b485lling			break;
549990b485lling		case ZPOOL_PROP_BOOTFS:
55025f89eeJeff Bonwick			/*
55125f89eeJeff Bonwick			 * If the pool version is less than SPA_VERSION_BOOTFS,
55225f89eeJeff Bonwick			 * or the pool is still being created (version == 0),
55325f89eeJeff Bonwick			 * the bootfs property cannot be set.
55425f89eeJeff Bonwick			 */
555990b485lling			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
556be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
557990b485lling				break;
558990b485lling			}
560990b485lling			/*
56115e6edfgw			 * Make sure the vdev config is bootable
562990b485lling			 */
56315e6edfgw			if (!vdev_is_bootable(spa->spa_root_vdev)) {
564be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
565990b485lling				break;
566990b485lling			}
568990b485lling			reset_bootfs = 1;
570990b485lling			error = nvpair_value_string(elem, &strval);
572990b485lling			if (!error) {
573ad135b5Christopher Siden				objset_t *os;
574b515258Matthew Ahrens				uint64_t propval;
576990b485lling				if (strval == NULL || strval[0] == '\0') {
577990b485lling					objnum = zpool_prop_default_numeric(
578990b485lling					    ZPOOL_PROP_BOOTFS);
579990b485lling					break;
580990b485lling				}
582094e47eGeorge Wilson				error = dmu_objset_hold(strval, FTAG, &os);
583094e47eGeorge Wilson				if (error != 0)
584990b485lling					break;
586b515258Matthew Ahrens				/*
587b515258Matthew Ahrens				 * Must be ZPL, and its property settings
58854811daToomas Soome				 * must be supported.
589b515258Matthew Ahrens				 */
590503ad85Matthew Ahrens
591503ad85Matthew Ahrens				if (dmu_objset_type(os) != DMU_OST_ZFS) {
592be6fd75Matthew Ahrens					error = SET_ERROR(ENOTSUP);
5933b2aab1Matthew Ahrens				} else if ((error =
5943b2aab1Matthew Ahrens				    dsl_prop_get_int_ds(dmu_objset_ds(os),
59515e6edfgw				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
596b515258Matthew Ahrens				    &propval)) == 0 &&
597b515258Matthew Ahrens				    !BOOTFS_COMPRESS_VALID(propval)) {
598b515258Matthew Ahrens					error = SET_ERROR(ENOTSUP);
59915e6edfgw				} else {
60015e6edfgw					objnum = dmu_objset_id(os);
60115e6edfgw				}
602503ad85Matthew Ahrens				dmu_objset_rele(os, FTAG);
603990b485lling			}
604990b485lling			break;
605e14bb32Jeff Bonwick
6060a4e951gw		case ZPOOL_PROP_FAILUREMODE:
6070a4e951gw			error = nvpair_value_uint64(elem, &intval);
6080a4e951gw			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
6090a4e951gw			    intval > ZIO_FAILURE_MODE_PANIC))
610be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
6120a4e951gw			/*
6130a4e951gw			 * This is a special case which only occurs when
6140a4e951gw			 * the pool has completely failed. This allows
6150a4e951gw			 * the user to change the in-core failmode property
6160a4e951gw			 * without syncing it out to disk (I/Os might
6170a4e951gw			 * currently be blocked). We do this by returning
6180a4e951gw			 * EIO to the caller (spa_prop_set) to trick it
6190a4e951gw			 * into thinking we encountered a property validation
6200a4e951gw			 * error.
6210a4e951gw			 */
622e14bb32Jeff Bonwick			if (!error && spa_suspended(spa)) {
6230a4e951gw				spa->spa_failmode = intval;
624be6fd75Matthew Ahrens				error = SET_ERROR(EIO);
6250a4e951gw			}
6260a4e951gw			break;
6282f8aaabeschrock		case ZPOOL_PROP_CACHEFILE:
6292f8aaabeschrock			if ((error = nvpair_value_string(elem, &strval)) != 0)
6302f8aaabeschrock				break;
6322f8aaabeschrock			if (strval[0] == '\0')
6332f8aaabeschrock				break;
6352f8aaabeschrock			if (strcmp(strval, "none") == 0)
6362f8aaabeschrock				break;
6382f8aaabeschrock			if (strval[0] != '/') {
639be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
6402f8aaabeschrock				break;
6412f8aaabeschrock			}
6432f8aaabeschrock			slash = strrchr(strval, '/');
6442f8aaabeschrock			ASSERT(slash != NULL);
6462f8aaabeschrock			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
6472f8aaabeschrock			    strcmp(slash, "/..") == 0)
648be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
6492f8aaabeschrock			break;
650b24ab67Jeff Bonwick
6518704186Dan McDonald		case ZPOOL_PROP_COMMENT:
6528704186Dan McDonald			if ((error = nvpair_value_string(elem, &strval)) != 0)
6538704186Dan McDonald				break;
6548704186Dan McDonald			for (check = strval; *check != '\0'; check++) {
6558704186Dan McDonald				/*
6568704186Dan McDonald				 * The kernel doesn't have an easy isprint()
6578704186Dan McDonald				 * check.  For this kernel check, we merely
6588704186Dan McDonald				 * check ASCII apart from DEL.  Fix this if
6598704186Dan McDonald				 * there is an easy-to-use kernel isprint().
6608704186Dan McDonald				 */
6618704186Dan McDonald				if (*check >= 0x7f) {
662be6fd75Matthew Ahrens					error = SET_ERROR(EINVAL);
6638704186Dan McDonald					break;
6648704186Dan McDonald				}
6658704186Dan McDonald			}
6668704186Dan McDonald			if (strlen(strval) > ZPROP_MAX_COMMENT)
6678704186Dan McDonald				error = E2BIG;
6688704186Dan McDonald			break;
6698704186Dan McDonald
670b24ab67Jeff Bonwick		case ZPOOL_PROP_DEDUPDITTO:
671b24ab67Jeff Bonwick			if (spa_version(spa) < SPA_VERSION_DEDUP)
672be6fd75Matthew Ahrens				error = SET_ERROR(ENOTSUP);
673b24ab67Jeff Bonwick			else
674b24ab67Jeff Bonwick				error = nvpair_value_uint64(elem, &intval);
675b24ab67Jeff Bonwick			if (error == 0 &&
676b24ab67Jeff Bonwick			    intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
677be6fd75Matthew Ahrens				error = SET_ERROR(EINVAL);
678b24ab67Jeff Bonwick			break;
679990b485lling		}
681990b485lling		if (error)
682990b485lling			break;
683990b485lling	}
685990b485lling	if (!error && reset_bootfs) {
686990b485lling		error = nvlist_remove(props,
687990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
689990b485lling		if (!error) {
690990b485lling			error = nvlist_add_uint64(props,
691990b485lling			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
692990b485lling		}
693990b485lling	}
695990b485lling	return (error);
698379c004Eric Schrockvoid
699379c004Eric Schrockspa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
700379c004Eric Schrock{
701379c004Eric Schrock	char *cachefile;
702379c004Eric Schrock	spa_config_dirent_t *dp;
703379c004Eric Schrock
704379c004Eric Schrock	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
705379c004Eric Schrock	    &cachefile) != 0)
706379c004Eric Schrock		return;
707379c004Eric Schrock
708379c004Eric Schrock	dp = kmem_alloc(sizeof (spa_config_dirent_t),
709379c004Eric Schrock	    KM_SLEEP);
710379c004Eric Schrock
711379c004Eric Schrock	if (cachefile[0] == '\0')
712379c004Eric Schrock		dp->scd_path = spa_strdup(spa_config_path);
713379c004Eric Schrock	else if (strcmp(cachefile, "none") == 0)
714379c004Eric Schrock		dp->scd_path = NULL;
715379c004Eric Schrock	else
716379c004Eric Schrock		dp->scd_path = spa_strdup(cachefile);
717379c004Eric Schrock
718379c004Eric Schrock	list_insert_head(&spa->spa_config_list, dp);
719379c004Eric Schrock	if (need_sync)
720379c004Eric Schrock		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
721379c004Eric Schrock}
722379c004Eric Schrock
724990b485llingspa_prop_set(spa_t *spa, nvlist_t *nvp)
726990b485lling	int error;
727ad135b5Christopher Siden	nvpair_t *elem = NULL;
728379c004Eric Schrock	boolean_t need_sync = B_FALSE;
730990b485lling	if ((error = spa_prop_validate(spa, nvp)) != 0)
731990b485lling		return (error);
733379c004Eric Schrock	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
734ad135b5Christopher Siden		zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
735379c004Eric Schrock
736f9af39bGeorge Wilson		if (prop == ZPOOL_PROP_CACHEFILE ||
737f9af39bGeorge Wilson		    prop == ZPOOL_PROP_ALTROOT ||
738f9af39bGeorge Wilson		    prop == ZPOOL_PROP_READONLY)
739379c004Eric Schrock			continue;
740379c004Eric Schrock
7414ae5f5fAlan Somers		if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) {
742ad135b5Christopher Siden			uint64_t ver;
743ad135b5Christopher Siden
744ad135b5Christopher Siden			if (prop == ZPOOL_PROP_VERSION) {
745ad135b5Christopher Siden				VERIFY(nvpair_value_uint64(elem, &ver) == 0);
746ad135b5Christopher Siden			} else {
747ad135b5Christopher Siden				ASSERT(zpool_prop_feature(nvpair_name(elem)));
748ad135b5Christopher Siden				ver = SPA_VERSION_FEATURES;
749ad135b5Christopher Siden				need_sync = B_TRUE;
750ad135b5Christopher Siden			}
751ad135b5Christopher Siden
752ad135b5Christopher Siden			/* Save time if the version is already set. */
753ad135b5Christopher Siden			if (ver == spa_version(spa))
754ad135b5Christopher Siden				continue;
755ad135b5Christopher Siden
756ad135b5Christopher Siden			/*
757ad135b5Christopher Siden			 * In addition to the pool directory object, we might
758ad135b5Christopher Siden			 * create the pool properties object, the features for
759ad135b5Christopher Siden			 * read object, the features for write object, or the
760ad135b5Christopher Siden			 * feature descriptions object.
761ad135b5Christopher Siden			 */
7623b2aab1Matthew Ahrens			error = dsl_sync_task(spa->spa_name, NULL,
7637d46dc6Matthew Ahrens			    spa_sync_version, &ver,
7647d46dc6Matthew Ahrens			    6, ZFS_SPACE_CHECK_RESERVED);
765ad135b5Christopher Siden			if (error)
766ad135b5Christopher Siden				return (error);
767ad135b5Christopher Siden			continue;
768ad135b5Christopher Siden		}
769ad135b5Christopher Siden
770379c004Eric Schrock		need_sync = B_TRUE;
771379c004Eric Schrock		break;
772379c004Eric Schrock	}
773379c004Eric Schrock
774ad135b5Christopher Siden	if (need_sync) {
7753b2aab1Matthew Ahrens		return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
7767d46dc6Matthew Ahrens		    nvp, 6, ZFS_SPACE_CHECK_RESERVED));
777ad135b5Christopher Siden	}
778ad135b5Christopher Siden
779ad135b5Christopher Siden	return (0);
783990b485lling * If the bootfs property value is dsobj, clear it.
784990b485lling */
786990b485llingspa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
788990b485lling	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
789990b485lling		VERIFY(zap_remove(spa->spa_meta_objset,
790990b485lling		    spa->spa_pool_props_object,
791990b485lling		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
792990b485lling		spa->spa_bootfs = 0;
793990b485lling	}
796dfbb943George Wilson/*ARGSUSED*/
797dfbb943George Wilsonstatic int
7983b2aab1Matthew Ahrensspa_change_guid_check(void *arg, dmu_tx_t *tx)
799dfbb943George Wilson{
8003b2aab1Matthew Ahrens	uint64_t *newguid = arg;
8013b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
802dfbb943George Wilson	vdev_t *rvd = spa->spa_root_vdev;
803dfbb943George Wilson	uint64_t vdev_state;
804dfbb943George Wilson
8058671400Serapheim Dimitropoulos	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
8068671400Serapheim Dimitropoulos		int error = (spa_has_checkpoint(spa)) ?
8088671400Serapheim Dimitropoulos		return (SET_ERROR(error));
8098671400Serapheim Dimitropoulos	}
8108671400Serapheim Dimitropoulos
811dfbb943George Wilson	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);