253089abeschrock * CDDL HEADER START
353089abeschrock *
453089abeschrock * The contents of this file are subject to the terms of the
553089abeschrock * Common Development and Distribution License (the "License").
653089abeschrock * You may not use this file except in compliance with the License.
753089abeschrock *
853089abeschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
953089abeschrock * or http://www.opensolaris.org/os/licensing.
1053089abeschrock * See the License for the specific language governing permissions
1153089abeschrock * and limitations under the License.
1253089abeschrock *
1353089abeschrock * When distributing Covered Code, include this CDDL HEADER in each
1453089abeschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1553089abeschrock * If applicable, add the following below this CDDL HEADER, with the
1653089abeschrock * fields enclosed by brackets "[]" replaced with your own identifying
1753089abeschrock * information: Portions Copyright [yyyy] [name of copyright owner]
1853089abeschrock *
1953089abeschrock * CDDL HEADER END
2053089abeschrock */
23ca0cc39Matthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
2453089abeschrock */
2653089abeschrock#include <sys/zfs_context.h>
2753089abeschrock#include <sys/zfeature.h>
2853089abeschrock#include <sys/dmu.h>
2953089abeschrock#include <sys/nvpair.h>
3053089abeschrock#include <sys/zap.h>
3153089abeschrock#include <sys/dmu_tx.h>
3253089abeschrock#include "zfeature_common.h"
3353089abeschrock#include <sys/spa_impl.h>
3653089abeschrock * ZFS Feature Flags
3753089abeschrock * -----------------
3853089abeschrock *
3953089abeschrock * ZFS feature flags are used to provide fine-grained versioning to the ZFS
4053089abeschrock * on-disk format. Once enabled on a pool feature flags replace the old
4153089abeschrock * spa_version() number.
4253089abeschrock *
4353089abeschrock * Each new on-disk format change will be given a uniquely identifying string
4453089abeschrock * guid rather than a version number. This avoids the problem of different
4553089abeschrock * organizations creating new on-disk formats with the same version number. To
4653089abeschrock * keep feature guids unique they should consist of the reverse dns name of the
4753089abeschrock * organization which implemented the feature and a short name for the feature,
4853089abeschrock * separated by a colon (e.g. com.delphix:async_destroy).
4953089abeschrock *
5053089abeschrock * Reference Counts
5153089abeschrock * ----------------
5253089abeschrock *
5353089abeschrock * Within each pool features can be in one of three states: disabled, enabled,
5453089abeschrock * or active. These states are differentiated by a reference count stored on
5553089abeschrock * disk for each feature:
5653089abeschrock *
5753089abeschrock *   1) If there is no reference count stored on disk the feature is disabled.
5853089abeschrock *   2) If the reference count is 0 a system administrator has enabled the
5953089abeschrock *      feature, but the feature has not been used yet, so no on-disk
6053089abeschrock *      format changes have been made.
6153089abeschrock *   3) If the reference count is greater than 0 the feature is active.
6253089abeschrock *      The format changes required by the feature are currently on disk.
6353089abeschrock *      Note that if the feature's format changes are reversed the feature
6453089abeschrock *      may choose to set its reference count back to 0.
6553089abeschrock *
6653089abeschrock * Feature flags makes no differentiation between non-zero reference counts
6753089abeschrock * for an active feature (e.g. a reference count of 1 means the same thing as a
6853089abeschrock * reference count of 27834721), but feature implementations may choose to use
6953089abeschrock * the reference count to store meaningful information. For example, a new RAID
7053089abeschrock * implementation might set the reference count to the number of vdevs using
7153089abeschrock * it. If all those disks are removed from the pool the feature goes back to
7253089abeschrock * having a reference count of 0.
7353089abeschrock *
7453089abeschrock * It is the responsibility of the individual features to maintain a non-zero
7553089abeschrock * reference count as long as the feature's format changes are present on disk.
7653089abeschrock *
7753089abeschrock * Dependencies
7853089abeschrock * ------------
7953089abeschrock *
8053089abeschrock * Each feature may depend on other features. The only effect of this
8153089abeschrock * relationship is that when a feature is enabled all of its dependencies are
8253089abeschrock * automatically enabled as well. Any future work to support disabling of
8353089abeschrock * features would need to ensure that features cannot be disabled if other
8453089abeschrock * enabled features depend on them.
8553089abeschrock *
8653089abeschrock * On-disk Format
8753089abeschrock * --------------
8853089abeschrock *
8953089abeschrock * When feature flags are enabled spa_version() is set to SPA_VERSION_FEATURES
9053089abeschrock * (5000). In order for this to work the pool is automatically upgraded to
9153089abeschrock * SPA_VERSION_BEFORE_FEATURES (28) first, so all pre-feature flags on disk
9253089abeschrock * format changes will be in use.
9353089abeschrock *
9453089abeschrock * Information about features is stored in 3 ZAP objects in the pool's MOS.
9553089abeschrock * These objects are linked to by the following names in the pool directory
9653089abeschrock * object:
9753089abeschrock *
9853089abeschrock * 1) features_for_read: feature guid -> reference count
9953089abeschrock *    Features needed to open the pool for reading.
10053089abeschrock * 2) features_for_write: feature guid -> reference count
10153089abeschrock *    Features needed to open the pool for writing.
10253089abeschrock * 3) feature_descriptions: feature guid -> descriptive string
10353089abeschrock *    A human readable string.
10453089abeschrock *
10553089abeschrock * All enabled features appear in either features_for_read or
10653089abeschrock * features_for_write, but not both.
10753089abeschrock *
10853089abeschrock * To open a pool in read-only mode only the features listed in
10953089abeschrock * features_for_read need to be supported.
11053089abeschrock *
11153089abeschrock * To open the pool in read-write mode features in both features_for_read and
11253089abeschrock * features_for_write need to be supported.
11353089abeschrock *
11453089abeschrock * Some features may be required to read the ZAP objects containing feature
11553089abeschrock * information. To allow software to check for compatibility with these features
11653089abeschrock * before the pool is opened their names must be stored in the label in a
11753089abeschrock * new "features_for_read" entry (note that features that are only required
11853089abeschrock * to write to a pool never need to be stored in the label since the
11953089abeschrock * features_for_write ZAP object can be read before the pool is written to).
12053089abeschrock * To save space in the label features must be explicitly marked as needing to
12153089abeschrock * be written to the label. Also, reference counts are not stored in the label,
12253089abeschrock * instead any feature whose reference count drops to 0 is removed from the
12353089abeschrock * label.
12453089abeschrock *
12553089abeschrock * Adding New Features
12653089abeschrock * -------------------
12753089abeschrock *
12853089abeschrock * Features must be registered in zpool_feature_init() function in
12953089abeschrock * zfeature_common.c using the zfeature_register() function. This function
13053089abeschrock * has arguments to specify if the feature should be stored in the
13153089abeschrock * features_for_read or features_for_write ZAP object and if it needs to be
13253089abeschrock * written to the label when active.
13353089abeschrock *
13453089abeschrock * Once a feature is registered it will appear as a "feature@<feature name>"
13553089abeschrock * property which can be set by an administrator. Feature implementors should
13653089abeschrock * use the spa_feature_is_enabled() and spa_feature_is_active() functions to
13753089abeschrock * query the state of a feature and the spa_feature_incr() and
13853089abeschrock * spa_feature_decr() functions to change an enabled feature's reference count.
13953089abeschrock * Reference counts may only be updated in the syncing context.
14053089abeschrock *
14153089abeschrock * Features may not perform enable-time initialization. Instead, any such
14253089abeschrock * initialization should occur when the feature is first used. This design
14353089abeschrock * enforces that on-disk changes be made only when features are used. Code
14453089abeschrock * should only check if a feature is enabled using spa_feature_is_enabled(),
14553089abeschrock * not by relying on any feature specific metadata existing. If a feature is
14653089abeschrock * enabled, but the feature's metadata is not on disk yet then it should be
14753089abeschrock * created as needed.
14853089abeschrock *
14953089abeschrock * As an example, consider the com.delphix:async_destroy feature. This feature
15053089abeschrock * relies on the existence of a bptree in the MOS that store blocks for
15153089abeschrock * asynchronous freeing. This bptree is not created when async_destroy is
15253089abeschrock * enabled. Instead, when a dataset is destroyed spa_feature_is_enabled() is
15353089abeschrock * called to check if async_destroy is enabled. If it is and the bptree object
15453089abeschrock * does not exist yet, the bptree object is created as part of the dataset
15553089abeschrock * destroy and async_destroy's reference count is incremented to indicate it
15653089abeschrock * has made an on-disk format change. Later, after the destroyed dataset's
15753089abeschrock * blocks have all been asynchronously freed there is no longer any use for the
15853089abeschrock * bptree object, so it is destroyed and async_destroy's reference count is
15953089abeschrock * decremented back to 0 to indicate that it has undone its on-disk format
16053089abeschrock * changes.
16153089abeschrock */
16353089abeschrocktypedef enum {
16453089abeschrock	FEATURE_ACTION_INCR,
16553089abeschrock	FEATURE_ACTION_DECR,
16653089abeschrock} feature_action_t;
1692acef22Matthew Ahrens * Checks that the active features in the pool are supported by
17053089abeschrock * this software.  Adds each unsupported feature (name -> description) to
17153089abeschrock * the supplied nvlist.
17253089abeschrock */
1742acef22Matthew Ahrensspa_features_check(spa_t *spa, boolean_t for_write,
1755722177Christopher Siden    nvlist_t *unsup_feat, nvlist_t *enabled_feat)
1772acef22Matthew Ahrens	objset_t *os = spa->spa_meta_objset;
17853089abeschrock	boolean_t supported;
17953089abeschrock	zap_cursor_t zc;
18053089abeschrock	zap_attribute_t za;
1812acef22Matthew Ahrens	uint64_t obj = for_write ?
1822acef22Matthew Ahrens	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
18453089abeschrock	supported = B_TRUE;
18553089abeschrock	for (zap_cursor_init(&zc, os, obj);
18653089abeschrock	    zap_cursor_retrieve(&zc, &za) == 0;
18753089abeschrock	    zap_cursor_advance(&zc)) {
18853089abeschrock		ASSERT(za.za_integer_length == sizeof (uint64_t) &&
18953089abeschrock		    za.za_num_integers == 1);
1915722177Christopher Siden		if (NULL != enabled_feat) {
1925722177Christopher Siden			fnvlist_add_uint64(enabled_feat, za.za_name,
1935722177Christopher Siden			    za.za_first_integer);
1945722177Christopher Siden		}
1955722177Christopher Siden
19653089abeschrock		if (za.za_first_integer != 0 &&
19753089abeschrock		    !zfeature_is_supported(za.za_name)) {
19853089abeschrock			supported = B_FALSE;
2005722177Christopher Siden			if (NULL != unsup_feat) {
20153089abeschrock				char *desc = "";
20253089abeschrock				char buf[MAXPATHLEN];
2042acef22Matthew Ahrens				if (zap_lookup(os, spa->spa_feat_desc_obj,
2052acef22Matthew Ahrens				    za.za_name, 1, sizeof (buf), buf) == 0)
20653089abeschrock					desc = buf;
20853089abeschrock				VERIFY(nvlist_add_string(unsup_feat, za.za_name,
20953089abeschrock				    desc) == 0);
21053089abeschrock			}
21153089abeschrock		}
21253089abeschrock	}
21353089abeschrock	zap_cursor_fini(&zc);
21553089abeschrock	return (supported);
2182acef22Matthew Ahrens/*
21943466aaMax Grossman * Use an in-memory cache of feature refcounts for quick retrieval.
22043466aaMax Grossman *
2212acef22Matthew Ahrens * Note: well-designed features will not need to use this; they should
2222acef22Matthew Ahrens * use spa_feature_is_enabled() and spa_feature_is_active() instead.
2232acef22Matthew Ahrens * However, this is non-static for zdb and zhack.
2242acef22Matthew Ahrens */
2252acef22Matthew Ahrensint
2262acef22Matthew Ahrensfeature_get_refcount(spa_t *spa, zfeature_info_t *feature, uint64_t *res)
22843466aaMax Grossman	ASSERT(VALID_FEATURE_FID(feature->fi_feature));
22943466aaMax Grossman	if (spa->spa_feat_refcount_cache[feature->fi_feature] ==
23043466aaMax Grossman	    SPA_FEATURE_DISABLED) {
23143466aaMax Grossman		return (SET_ERROR(ENOTSUP));
23243466aaMax Grossman	}
23343466aaMax Grossman	*res = spa->spa_feat_refcount_cache[feature->fi_feature];
23443466aaMax Grossman	return (0);
23543466aaMax Grossman}
23643466aaMax Grossman
23743466aaMax Grossman/*
23843466aaMax Grossman * Note: well-designed features will not need to use this; they should
23943466aaMax Grossman * use spa_feature_is_enabled() and spa_feature_is_active() instead.
24043466aaMax Grossman * However, this is non-static for zdb and zhack.
24143466aaMax Grossman */
24243466aaMax Grossmanint
24343466aaMax Grossmanfeature_get_refcount_from_disk(spa_t *spa, zfeature_info_t *feature,
24443466aaMax Grossman    uint64_t *res)
24543466aaMax Grossman{
24653089abeschrock	int err;
24753089abeschrock	uint64_t refcount;
248ca0cc39Matthew Ahrens	uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
2492acef22Matthew Ahrens	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
251f174573Matthew Ahrens	/*
252f174573Matthew Ahrens	 * If the pool is currently being created, the feature objects may not
253f174573Matthew Ahrens	 * have been allocated yet.  Act as though all features are disabled.
254f174573Matthew Ahrens	 */
255f174573Matthew Ahrens	if (zapobj == 0)
256be6fd75Matthew Ahrens		return (SET_ERROR(ENOTSUP));
2582acef22Matthew Ahrens	err = zap_lookup(spa->spa_meta_objset, zapobj,
2592acef22Matthew Ahrens	    feature->fi_guid, sizeof (uint64_t), 1, &refcount);
26053089abeschrock	if (err != 0) {
26153089abeschrock		if (err == ENOENT)
262be6fd75Matthew Ahrens			return (SET_ERROR(ENOTSUP));
26353089abeschrock		else
26453089abeschrock			return (err);
26553089abeschrock	}
26653089abeschrock	*res = refcount;
26753089abeschrock	return (0);
27043466aaMax Grossman
27143466aaMax Grossmanstatic int
2729a686fbPaul Dagneliefeature_get_enabled_txg(spa_t *spa, zfeature_info_t *feature, uint64_t *res)
2739a686fbPaul Dagnelie{
27443466aaMax Grossman	uint64_t enabled_txg_obj = spa->spa_feat_enabled_txg_obj;
27543466aaMax Grossman
27643466aaMax Grossman	ASSERT(zfeature_depends_on(feature->fi_feature,
27743466aaMax Grossman	    SPA_FEATURE_ENABLED_TXG));
27843466aaMax Grossman
27943466aaMax Grossman	if (!spa_feature_is_enabled(spa, feature->fi_feature)) {
28043466aaMax Grossman		return (SET_ERROR(ENOTSUP));
28143466aaMax Grossman	}
28243466aaMax Grossman
28343466aaMax Grossman	ASSERT(enabled_txg_obj != 0);
28443466aaMax Grossman
28543466aaMax Grossman	VERIFY0(zap_lookup(spa->spa_meta_objset, spa->spa_feat_enabled_txg_obj,
28643466aaMax Grossman	    feature->fi_guid, sizeof (uint64_t), 1, res));
28743466aaMax Grossman
28843466aaMax Grossman	return (0);
28943466aaMax Grossman}
29043466aaMax Grossman
2912acef22Matthew Ahrens/*
2922acef22Matthew Ahrens * This function is non-static for zhack; it should otherwise not be used
2932acef22Matthew Ahrens * outside this file.
2942acef22Matthew Ahrens */
2952acef22Matthew Ahrensvoid
2962acef22Matthew Ahrensfeature_sync(spa_t *spa, zfeature_info_t *feature, uint64_t refcount,
29753089abeschrock    dmu_tx_t *tx)
29943466aaMax Grossman	ASSERT(VALID_FEATURE_OR_NONE(feature->fi_feature));
300ca0cc39Matthew Ahrens	uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
3012acef22Matthew Ahrens	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
3022acef22Matthew Ahrens
3032acef22Matthew Ahrens	VERIFY0(zap_update(spa->spa_meta_objset, zapobj, feature->fi_guid,
3042acef22Matthew Ahrens	    sizeof (uint64_t), 1, &refcount, tx));
3052acef22Matthew Ahrens
30643466aaMax Grossman	/*
30743466aaMax Grossman	 * feature_sync is called directly from zhack, allowing the
30843466aaMax Grossman	 * creation of arbitrary features whose fi_feature field may
30943466aaMax Grossman	 * be greater than SPA_FEATURES. When called from zhack, the
31043466aaMax Grossman	 * zfeature_info_t object's fi_feature field will be set to
31143466aaMax Grossman	 * SPA_FEATURE_NONE.
31243466aaMax Grossman	 */
31343466aaMax Grossman	if (feature->fi_feature != SPA_FEATURE_NONE) {
31443466aaMax Grossman		uint64_t *refcount_cache =
31543466aaMax Grossman		    &spa->spa_feat_refcount_cache[feature->fi_feature];
31643466aaMax Grossman		VERIFY3U(*refcount_cache, ==,
31743466aaMax Grossman		    atomic_swap_64(refcount_cache, refcount));
31843466aaMax Grossman	}
31943466aaMax Grossman
3202acef22Matthew Ahrens	if (refcount == 0)
3212acef22Matthew Ahrens		spa_deactivate_mos_feature(spa, feature->fi_guid);
322ca0cc39Matthew Ahrens	else if (feature->fi_flags & ZFEATURE_FLAG_MOS)
32343466aaMax Grossman		spa_activate_mos_feature(spa, feature->fi_guid, tx);
3242acef22Matthew Ahrens}
3252acef22Matthew Ahrens
3262acef22Matthew Ahrens/*
3272acef22Matthew Ahrens * This function is non-static for zhack; it should otherwise not be used
3282acef22Matthew Ahrens * outside this file.
3292acef22Matthew Ahrens */
3302acef22Matthew Ahrensvoid
3312acef22Matthew Ahrensfeature_enable_sync(spa_t *spa, zfeature_info_t *feature, dmu_tx_t *tx)
3322acef22Matthew Ahrens{
333ca0cc39Matthew Ahrens	uint64_t initial_refcount =
334ca0cc39Matthew Ahrens	    (feature->fi_flags & ZFEATURE_FLAG_ACTIVATE_ON_ENABLE) ? 1 : 0;
335ca0cc39Matthew Ahrens	uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
3362acef22Matthew Ahrens	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
33853089abeschrock	ASSERT(0 != zapobj);
33953089abeschrock	ASSERT(zfeature_is_valid_guid(feature->fi_guid));
3402acef22Matthew Ahrens	ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
34253089abeschrock	/*
3432acef22Matthew Ahrens	 * If the feature is already enabled, ignore the request.
34453089abeschrock	 */
3452acef22Matthew Ahrens	if (zap_contains(spa->spa_meta_objset, zapobj, feature->fi_guid) == 0)
3462acef22Matthew Ahrens		return;
3472acef22Matthew Ahrens
3482acef22Matthew Ahrens	for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++)
3492acef22Matthew Ahrens		spa_feature_enable(spa, feature->fi_depends[i], tx);
3502acef22Matthew Ahrens
3512acef22Matthew Ahrens	VERIFY0(zap_update(spa->spa_meta_objset, spa->spa_feat_desc_obj,
3522acef22Matthew Ahrens	    feature->fi_guid, 1, strlen(feature->fi_desc) + 1,
3532acef22Matthew Ahrens	    feature->fi_desc, tx));
35443466aaMax Grossman
35543466aaMax Grossman	feature_sync(spa, feature, initial_refcount, tx);
35643466aaMax Grossman
35743466aaMax Grossman	if (spa_feature_is_enabled(spa, SPA_FEATURE_ENABLED_TXG)) {
35843466aaMax Grossman		uint64_t enabling_txg = dmu_tx_get_txg(tx);
35943466aaMax Grossman
36043466aaMax Grossman		if (spa->spa_feat_enabled_txg_obj == 0ULL) {
36143466aaMax Grossman			spa->spa_feat_enabled_txg_obj =
36243466aaMax Grossman			    zap_create_link(spa->spa_meta_objset,
36443466aaMax Grossman			    DMU_POOL_FEATURE_ENABLED_TXG, tx);
36543466aaMax Grossman		}
36643466aaMax Grossman		spa_feature_incr(spa, SPA_FEATURE_ENABLED_TXG, tx);
36743466aaMax Grossman
36843466aaMax Grossman		VERIFY0(zap_add(spa->spa_meta_objset,
36943466aaMax Grossman		    spa->spa_feat_enabled_txg_obj, feature->fi_guid,
37043466aaMax Grossman		    sizeof (uint64_t), 1, &enabling_txg, tx));
37143466aaMax Grossman	}
372eb63303Tom Caputi
373eb63303Tom Caputi	/*
374eb63303Tom Caputi	 * Errata #4 is mostly a problem with encrypted datasets, but it
375eb63303Tom Caputi	 * is also a problem where the old encryption feature did not
376eb63303Tom Caputi	 * depend on the bookmark_v2 feature. If the pool does not have
377eb63303Tom Caputi	 * any encrypted datasets we can resolve this issue simply by
378eb63303Tom Caputi	 * enabling this dependency.
379eb63303Tom Caputi	 */
380eb63303Tom Caputi	if (spa->spa_errata == ZPOOL_ERRATA_ZOL_8308_ENCRYPTION &&
381eb63303Tom Caputi	    spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) &&
382eb63303Tom Caputi	    !spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION) &&
383eb63303Tom Caputi	    feature->fi_feature == SPA_FEATURE_BOOKMARK_V2)
384eb63303Tom Caputi		spa->spa_errata = 0;
3852acef22Matthew Ahrens}
3862acef22Matthew Ahrens
3872acef22Matthew Ahrensstatic void
3882acef22Matthew Ahrensfeature_do_action(spa_t *spa, spa_feature_t fid, feature_action_t action,
3892acef22Matthew Ahrens    dmu_tx_t *tx)
3902acef22Matthew Ahrens{
3912acef22Matthew Ahrens	uint64_t refcount;
3922acef22Matthew Ahrens	zfeature_info_t *feature = &spa_feature_table[fid];
393ca0cc39Matthew Ahrens	uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
3942acef22Matthew Ahrens	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
3952acef22Matthew Ahrens
39643466aaMax Grossman	ASSERT(VALID_FEATURE_FID(fid));
3972acef22Matthew Ahrens	ASSERT(0 != zapobj);
3982acef22Matthew Ahrens	ASSERT(zfeature_is_valid_guid(feature->fi_guid));
3992acef22Matthew Ahrens
4002acef22Matthew Ahrens	ASSERT(dmu_tx_is_syncing(tx));
4012acef22Matthew Ahrens	ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
4022acef22Matthew Ahrens
40343466aaMax Grossman	VERIFY3U(feature_get_refcount(spa, feature, &refcount), !=, ENOTSUP);
40553089abeschrock	switch (action) {
40653089abeschrock	case FEATURE_ACTION_INCR:
4072acef22Matthew Ahrens		VERIFY3U(refcount, !=, UINT64_MAX);
40853089abeschrock		refcount++;
40953089abeschrock		break;
41053089abeschrock	case FEATURE_ACTION_DECR:
4112acef22Matthew Ahrens		VERIFY3U(refcount, !=, 0);
41253089abeschrock		refcount--;
41353089abeschrock		break;
41453089abeschrock	default:
41553089abeschrock		ASSERT(0);
41653089abeschrock		break;
41753089abeschrock	}
4192acef22Matthew Ahrens	feature_sync(spa, feature, refcount, tx);
42353089abeschrockspa_feature_create_zap_objects(spa_t *spa, dmu_tx_t *tx)
42553089abeschrock	/*
42653089abeschrock	 * We create feature flags ZAP objects in two instances: during pool
42753089abeschrock	 * creation and during pool upgrade.
42853089abeschrock	 */
429eb63303Tom Caputi	ASSERT((!spa->spa_sync_on && tx->tx_txg == TXG_INITIAL) ||
430eb63303Tom Caputi	    dsl_pool_sync_context(spa_get_dsl(spa)));
43253089abeschrock	spa->spa_feat_for_read_obj = zap_create_link(spa->spa_meta_objset,
43453089abeschrock	    DMU_POOL_FEATURES_FOR_READ, tx);
43553089abeschrock	spa->spa_feat_for_write_obj = zap_create_link(spa->spa_meta_objset,
43753089abeschrock	    DMU_POOL_FEATURES_FOR_WRITE, tx);
43853089abeschrock	spa->spa_feat_desc_obj = zap_create_link(spa->spa_meta_objset,
44053089abeschrock	    DMU_POOL_FEATURE_DESCRIPTIONS, tx);
44453089abeschrock * Enable any required dependencies, then enable the requested feature.
44553089abeschrock */
4472acef22Matthew Ahrensspa_feature_enable(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
44953089abeschrock	ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
45043466aaMax Grossman	ASSERT(VALID_FEATURE_FID(fid));
4512acef22Matthew Ahrens	feature_enable_sync(spa, &spa_feature_table[fid], tx);
4552acef22Matthew Ahrensspa_feature_incr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
4572acef22Matthew Ahrens	feature_do_action(spa, fid, FEATURE_ACTION_INCR, tx);
4612acef22Matthew Ahrensspa_feature_decr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
4632acef22Matthew Ahrens	feature_do_action(spa, fid, FEATURE_ACTION_DECR, tx);
4640713e23George Wilson}
4650713e23George Wilson
4672acef22Matthew Ahrensspa_feature_is_enabled(spa_t *spa, spa_feature_t fid)
46953089abeschrock	int err;
47053089abeschrock	uint64_t refcount;
47243466aaMax Grossman	ASSERT(VALID_FEATURE_FID(fid));
47353089abeschrock	if (spa_version(spa) < SPA_VERSION_FEATURES)
47453089abeschrock		return (B_FALSE);
4762acef22Matthew Ahrens	err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount);
47753089abeschrock	ASSERT(err == 0 || err == ENOTSUP);
47853089abeschrock	return (err == 0);
4822acef22Matthew Ahrensspa_feature_is_active(spa_t *spa, spa_feature_t fid)
48453089abeschrock	int err;
48553089abeschrock	uint64_t refcount;
48743466aaMax Grossman	ASSERT(VALID_FEATURE_FID(fid));
48853089abeschrock	if (spa_version(spa) < SPA_VERSION_FEATURES)
48953089abeschrock		return (B_FALSE);
4912acef22Matthew Ahrens	err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount);
49253089abeschrock	ASSERT(err == 0 || err == ENOTSUP);
49353089abeschrock	return (err == 0 && refcount > 0);
49543466aaMax Grossman
49643466aaMax Grossman/*
49743466aaMax Grossman * For the feature specified by fid (which must depend on
49843466aaMax Grossman * SPA_FEATURE_ENABLED_TXG), return the TXG at which it was enabled in the
49943466aaMax Grossman * OUT txg argument.
50043466aaMax Grossman *
50143466aaMax Grossman * Returns B_TRUE if the feature is enabled, in which case txg will be filled
50243466aaMax Grossman * with the transaction group in which the specified feature was enabled.
50343466aaMax Grossman * Returns B_FALSE otherwise (i.e. if the feature is not enabled).
50443466aaMax Grossman */
50543466aaMax Grossmanboolean_t
5069a686fbPaul Dagneliespa_feature_enabled_txg(spa_t *spa, spa_feature_t fid, uint64_t *txg)
5079a686fbPaul Dagnelie{
50843466aaMax Grossman	int err;
50943466aaMax Grossman
51043466aaMax Grossman	ASSERT(VALID_FEATURE_FID(fid));
51143466aaMax Grossman	if (spa_version(spa) < SPA_VERSION_FEATURES)
51243466aaMax Grossman		return (B_FALSE);
51343466aaMax Grossman
51443466aaMax Grossman	err = feature_get_enabled_txg(spa, &spa_feature_table[fid], txg);
51543466aaMax Grossman	ASSERT(err == 0 || err == ENOTSUP);
51643466aaMax Grossman
51743466aaMax Grossman	return (err == 0);
51843466aaMax Grossman}