xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfeature.c (revision 43466aae47bfcd2ad9bf501faec8e75c08095e4f)
153089ab7Seschrock /*
253089ab7Seschrock  * CDDL HEADER START
353089ab7Seschrock  *
453089ab7Seschrock  * The contents of this file are subject to the terms of the
553089ab7Seschrock  * Common Development and Distribution License (the "License").
653089ab7Seschrock  * You may not use this file except in compliance with the License.
753089ab7Seschrock  *
853089ab7Seschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
953089ab7Seschrock  * or http://www.opensolaris.org/os/licensing.
1053089ab7Seschrock  * See the License for the specific language governing permissions
1153089ab7Seschrock  * and limitations under the License.
1253089ab7Seschrock  *
1353089ab7Seschrock  * When distributing Covered Code, include this CDDL HEADER in each
1453089ab7Seschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1553089ab7Seschrock  * If applicable, add the following below this CDDL HEADER, with the
1653089ab7Seschrock  * fields enclosed by brackets "[]" replaced with your own identifying
1753089ab7Seschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
1853089ab7Seschrock  *
1953089ab7Seschrock  * CDDL HEADER END
2053089ab7Seschrock  */
2153089ab7Seschrock 
2253089ab7Seschrock /*
23be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
2453089ab7Seschrock  */
2553089ab7Seschrock 
2653089ab7Seschrock #include <sys/zfs_context.h>
2753089ab7Seschrock #include <sys/zfeature.h>
2853089ab7Seschrock #include <sys/dmu.h>
2953089ab7Seschrock #include <sys/nvpair.h>
3053089ab7Seschrock #include <sys/zap.h>
3153089ab7Seschrock #include <sys/dmu_tx.h>
3253089ab7Seschrock #include "zfeature_common.h"
3353089ab7Seschrock #include <sys/spa_impl.h>
3453089ab7Seschrock 
3553089ab7Seschrock /*
3653089ab7Seschrock  * ZFS Feature Flags
3753089ab7Seschrock  * -----------------
3853089ab7Seschrock  *
3953089ab7Seschrock  * ZFS feature flags are used to provide fine-grained versioning to the ZFS
4053089ab7Seschrock  * on-disk format. Once enabled on a pool feature flags replace the old
4153089ab7Seschrock  * spa_version() number.
4253089ab7Seschrock  *
4353089ab7Seschrock  * Each new on-disk format change will be given a uniquely identifying string
4453089ab7Seschrock  * guid rather than a version number. This avoids the problem of different
4553089ab7Seschrock  * organizations creating new on-disk formats with the same version number. To
4653089ab7Seschrock  * keep feature guids unique they should consist of the reverse dns name of the
4753089ab7Seschrock  * organization which implemented the feature and a short name for the feature,
4853089ab7Seschrock  * separated by a colon (e.g. com.delphix:async_destroy).
4953089ab7Seschrock  *
5053089ab7Seschrock  * Reference Counts
5153089ab7Seschrock  * ----------------
5253089ab7Seschrock  *
5353089ab7Seschrock  * Within each pool features can be in one of three states: disabled, enabled,
5453089ab7Seschrock  * or active. These states are differentiated by a reference count stored on
5553089ab7Seschrock  * disk for each feature:
5653089ab7Seschrock  *
5753089ab7Seschrock  *   1) If there is no reference count stored on disk the feature is disabled.
5853089ab7Seschrock  *   2) If the reference count is 0 a system administrator has enabled the
5953089ab7Seschrock  *      feature, but the feature has not been used yet, so no on-disk
6053089ab7Seschrock  *      format changes have been made.
6153089ab7Seschrock  *   3) If the reference count is greater than 0 the feature is active.
6253089ab7Seschrock  *      The format changes required by the feature are currently on disk.
6353089ab7Seschrock  *      Note that if the feature's format changes are reversed the feature
6453089ab7Seschrock  *      may choose to set its reference count back to 0.
6553089ab7Seschrock  *
6653089ab7Seschrock  * Feature flags makes no differentiation between non-zero reference counts
6753089ab7Seschrock  * for an active feature (e.g. a reference count of 1 means the same thing as a
6853089ab7Seschrock  * reference count of 27834721), but feature implementations may choose to use
6953089ab7Seschrock  * the reference count to store meaningful information. For example, a new RAID
7053089ab7Seschrock  * implementation might set the reference count to the number of vdevs using
7153089ab7Seschrock  * it. If all those disks are removed from the pool the feature goes back to
7253089ab7Seschrock  * having a reference count of 0.
7353089ab7Seschrock  *
7453089ab7Seschrock  * It is the responsibility of the individual features to maintain a non-zero
7553089ab7Seschrock  * reference count as long as the feature's format changes are present on disk.
7653089ab7Seschrock  *
7753089ab7Seschrock  * Dependencies
7853089ab7Seschrock  * ------------
7953089ab7Seschrock  *
8053089ab7Seschrock  * Each feature may depend on other features. The only effect of this
8153089ab7Seschrock  * relationship is that when a feature is enabled all of its dependencies are
8253089ab7Seschrock  * automatically enabled as well. Any future work to support disabling of
8353089ab7Seschrock  * features would need to ensure that features cannot be disabled if other
8453089ab7Seschrock  * enabled features depend on them.
8553089ab7Seschrock  *
8653089ab7Seschrock  * On-disk Format
8753089ab7Seschrock  * --------------
8853089ab7Seschrock  *
8953089ab7Seschrock  * When feature flags are enabled spa_version() is set to SPA_VERSION_FEATURES
9053089ab7Seschrock  * (5000). In order for this to work the pool is automatically upgraded to
9153089ab7Seschrock  * SPA_VERSION_BEFORE_FEATURES (28) first, so all pre-feature flags on disk
9253089ab7Seschrock  * format changes will be in use.
9353089ab7Seschrock  *
9453089ab7Seschrock  * Information about features is stored in 3 ZAP objects in the pool's MOS.
9553089ab7Seschrock  * These objects are linked to by the following names in the pool directory
9653089ab7Seschrock  * object:
9753089ab7Seschrock  *
9853089ab7Seschrock  * 1) features_for_read: feature guid -> reference count
9953089ab7Seschrock  *    Features needed to open the pool for reading.
10053089ab7Seschrock  * 2) features_for_write: feature guid -> reference count
10153089ab7Seschrock  *    Features needed to open the pool for writing.
10253089ab7Seschrock  * 3) feature_descriptions: feature guid -> descriptive string
10353089ab7Seschrock  *    A human readable string.
10453089ab7Seschrock  *
10553089ab7Seschrock  * All enabled features appear in either features_for_read or
10653089ab7Seschrock  * features_for_write, but not both.
10753089ab7Seschrock  *
10853089ab7Seschrock  * To open a pool in read-only mode only the features listed in
10953089ab7Seschrock  * features_for_read need to be supported.
11053089ab7Seschrock  *
11153089ab7Seschrock  * To open the pool in read-write mode features in both features_for_read and
11253089ab7Seschrock  * features_for_write need to be supported.
11353089ab7Seschrock  *
11453089ab7Seschrock  * Some features may be required to read the ZAP objects containing feature
11553089ab7Seschrock  * information. To allow software to check for compatibility with these features
11653089ab7Seschrock  * before the pool is opened their names must be stored in the label in a
11753089ab7Seschrock  * new "features_for_read" entry (note that features that are only required
11853089ab7Seschrock  * to write to a pool never need to be stored in the label since the
11953089ab7Seschrock  * features_for_write ZAP object can be read before the pool is written to).
12053089ab7Seschrock  * To save space in the label features must be explicitly marked as needing to
12153089ab7Seschrock  * be written to the label. Also, reference counts are not stored in the label,
12253089ab7Seschrock  * instead any feature whose reference count drops to 0 is removed from the
12353089ab7Seschrock  * label.
12453089ab7Seschrock  *
12553089ab7Seschrock  * Adding New Features
12653089ab7Seschrock  * -------------------
12753089ab7Seschrock  *
12853089ab7Seschrock  * Features must be registered in zpool_feature_init() function in
12953089ab7Seschrock  * zfeature_common.c using the zfeature_register() function. This function
13053089ab7Seschrock  * has arguments to specify if the feature should be stored in the
13153089ab7Seschrock  * features_for_read or features_for_write ZAP object and if it needs to be
13253089ab7Seschrock  * written to the label when active.
13353089ab7Seschrock  *
13453089ab7Seschrock  * Once a feature is registered it will appear as a "feature@<feature name>"
13553089ab7Seschrock  * property which can be set by an administrator. Feature implementors should
13653089ab7Seschrock  * use the spa_feature_is_enabled() and spa_feature_is_active() functions to
13753089ab7Seschrock  * query the state of a feature and the spa_feature_incr() and
13853089ab7Seschrock  * spa_feature_decr() functions to change an enabled feature's reference count.
13953089ab7Seschrock  * Reference counts may only be updated in the syncing context.
14053089ab7Seschrock  *
14153089ab7Seschrock  * Features may not perform enable-time initialization. Instead, any such
14253089ab7Seschrock  * initialization should occur when the feature is first used. This design
14353089ab7Seschrock  * enforces that on-disk changes be made only when features are used. Code
14453089ab7Seschrock  * should only check if a feature is enabled using spa_feature_is_enabled(),
14553089ab7Seschrock  * not by relying on any feature specific metadata existing. If a feature is
14653089ab7Seschrock  * enabled, but the feature's metadata is not on disk yet then it should be
14753089ab7Seschrock  * created as needed.
14853089ab7Seschrock  *
14953089ab7Seschrock  * As an example, consider the com.delphix:async_destroy feature. This feature
15053089ab7Seschrock  * relies on the existence of a bptree in the MOS that store blocks for
15153089ab7Seschrock  * asynchronous freeing. This bptree is not created when async_destroy is
15253089ab7Seschrock  * enabled. Instead, when a dataset is destroyed spa_feature_is_enabled() is
15353089ab7Seschrock  * called to check if async_destroy is enabled. If it is and the bptree object
15453089ab7Seschrock  * does not exist yet, the bptree object is created as part of the dataset
15553089ab7Seschrock  * destroy and async_destroy's reference count is incremented to indicate it
15653089ab7Seschrock  * has made an on-disk format change. Later, after the destroyed dataset's
15753089ab7Seschrock  * blocks have all been asynchronously freed there is no longer any use for the
15853089ab7Seschrock  * bptree object, so it is destroyed and async_destroy's reference count is
15953089ab7Seschrock  * decremented back to 0 to indicate that it has undone its on-disk format
16053089ab7Seschrock  * changes.
16153089ab7Seschrock  */
16253089ab7Seschrock 
16353089ab7Seschrock typedef enum {
16453089ab7Seschrock 	FEATURE_ACTION_INCR,
16553089ab7Seschrock 	FEATURE_ACTION_DECR,
16653089ab7Seschrock } feature_action_t;
16753089ab7Seschrock 
16853089ab7Seschrock /*
1692acef22dSMatthew Ahrens  * Checks that the active features in the pool are supported by
17053089ab7Seschrock  * this software.  Adds each unsupported feature (name -> description) to
17153089ab7Seschrock  * the supplied nvlist.
17253089ab7Seschrock  */
17353089ab7Seschrock boolean_t
1742acef22dSMatthew Ahrens spa_features_check(spa_t *spa, boolean_t for_write,
17557221772SChristopher Siden     nvlist_t *unsup_feat, nvlist_t *enabled_feat)
17653089ab7Seschrock {
1772acef22dSMatthew Ahrens 	objset_t *os = spa->spa_meta_objset;
17853089ab7Seschrock 	boolean_t supported;
17953089ab7Seschrock 	zap_cursor_t zc;
18053089ab7Seschrock 	zap_attribute_t za;
1812acef22dSMatthew Ahrens 	uint64_t obj = for_write ?
1822acef22dSMatthew Ahrens 	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
18353089ab7Seschrock 
18453089ab7Seschrock 	supported = B_TRUE;
18553089ab7Seschrock 	for (zap_cursor_init(&zc, os, obj);
18653089ab7Seschrock 	    zap_cursor_retrieve(&zc, &za) == 0;
18753089ab7Seschrock 	    zap_cursor_advance(&zc)) {
18853089ab7Seschrock 		ASSERT(za.za_integer_length == sizeof (uint64_t) &&
18953089ab7Seschrock 		    za.za_num_integers == 1);
19053089ab7Seschrock 
19157221772SChristopher Siden 		if (NULL != enabled_feat) {
19257221772SChristopher Siden 			fnvlist_add_uint64(enabled_feat, za.za_name,
19357221772SChristopher Siden 			    za.za_first_integer);
19457221772SChristopher Siden 		}
19557221772SChristopher Siden 
19653089ab7Seschrock 		if (za.za_first_integer != 0 &&
19753089ab7Seschrock 		    !zfeature_is_supported(za.za_name)) {
19853089ab7Seschrock 			supported = B_FALSE;
19953089ab7Seschrock 
20057221772SChristopher Siden 			if (NULL != unsup_feat) {
20153089ab7Seschrock 				char *desc = "";
20253089ab7Seschrock 				char buf[MAXPATHLEN];
20353089ab7Seschrock 
2042acef22dSMatthew Ahrens 				if (zap_lookup(os, spa->spa_feat_desc_obj,
2052acef22dSMatthew Ahrens 				    za.za_name, 1, sizeof (buf), buf) == 0)
20653089ab7Seschrock 					desc = buf;
20753089ab7Seschrock 
20853089ab7Seschrock 				VERIFY(nvlist_add_string(unsup_feat, za.za_name,
20953089ab7Seschrock 				    desc) == 0);
21053089ab7Seschrock 			}
21153089ab7Seschrock 		}
21253089ab7Seschrock 	}
21353089ab7Seschrock 	zap_cursor_fini(&zc);
21453089ab7Seschrock 
21553089ab7Seschrock 	return (supported);
21653089ab7Seschrock }
21753089ab7Seschrock 
2182acef22dSMatthew Ahrens /*
219*43466aaeSMax Grossman  * Use an in-memory cache of feature refcounts for quick retrieval.
220*43466aaeSMax Grossman  *
2212acef22dSMatthew Ahrens  * Note: well-designed features will not need to use this; they should
2222acef22dSMatthew Ahrens  * use spa_feature_is_enabled() and spa_feature_is_active() instead.
2232acef22dSMatthew Ahrens  * However, this is non-static for zdb and zhack.
2242acef22dSMatthew Ahrens  */
2252acef22dSMatthew Ahrens int
2262acef22dSMatthew Ahrens feature_get_refcount(spa_t *spa, zfeature_info_t *feature, uint64_t *res)
227*43466aaeSMax Grossman {
228*43466aaeSMax Grossman 	ASSERT(VALID_FEATURE_FID(feature->fi_feature));
229*43466aaeSMax Grossman 	if (spa->spa_feat_refcount_cache[feature->fi_feature] ==
230*43466aaeSMax Grossman 	    SPA_FEATURE_DISABLED) {
231*43466aaeSMax Grossman 		return (SET_ERROR(ENOTSUP));
232*43466aaeSMax Grossman 	}
233*43466aaeSMax Grossman 	*res = spa->spa_feat_refcount_cache[feature->fi_feature];
234*43466aaeSMax Grossman 	return (0);
235*43466aaeSMax Grossman }
236*43466aaeSMax Grossman 
237*43466aaeSMax Grossman /*
238*43466aaeSMax Grossman  * Note: well-designed features will not need to use this; they should
239*43466aaeSMax Grossman  * use spa_feature_is_enabled() and spa_feature_is_active() instead.
240*43466aaeSMax Grossman  * However, this is non-static for zdb and zhack.
241*43466aaeSMax Grossman  */
242*43466aaeSMax Grossman int
243*43466aaeSMax Grossman feature_get_refcount_from_disk(spa_t *spa, zfeature_info_t *feature,
244*43466aaeSMax Grossman     uint64_t *res)
24553089ab7Seschrock {
24653089ab7Seschrock 	int err;
24753089ab7Seschrock 	uint64_t refcount;
2482acef22dSMatthew Ahrens 	uint64_t zapobj = feature->fi_can_readonly ?
2492acef22dSMatthew Ahrens 	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
25053089ab7Seschrock 
251f1745736SMatthew Ahrens 	/*
252f1745736SMatthew Ahrens 	 * If the pool is currently being created, the feature objects may not
253f1745736SMatthew Ahrens 	 * have been allocated yet.  Act as though all features are disabled.
254f1745736SMatthew Ahrens 	 */
255f1745736SMatthew Ahrens 	if (zapobj == 0)
256be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
25753089ab7Seschrock 
2582acef22dSMatthew Ahrens 	err = zap_lookup(spa->spa_meta_objset, zapobj,
2592acef22dSMatthew Ahrens 	    feature->fi_guid, sizeof (uint64_t), 1, &refcount);
26053089ab7Seschrock 	if (err != 0) {
26153089ab7Seschrock 		if (err == ENOENT)
262be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
26353089ab7Seschrock 		else
26453089ab7Seschrock 			return (err);
26553089ab7Seschrock 	}
26653089ab7Seschrock 	*res = refcount;
26753089ab7Seschrock 	return (0);
26853089ab7Seschrock }
26953089ab7Seschrock 
270*43466aaeSMax Grossman 
271*43466aaeSMax Grossman static int
272*43466aaeSMax Grossman feature_get_enabled_txg(spa_t *spa, zfeature_info_t *feature, uint64_t *res) {
273*43466aaeSMax Grossman 	uint64_t enabled_txg_obj = spa->spa_feat_enabled_txg_obj;
274*43466aaeSMax Grossman 
275*43466aaeSMax Grossman 	ASSERT(zfeature_depends_on(feature->fi_feature,
276*43466aaeSMax Grossman 	    SPA_FEATURE_ENABLED_TXG));
277*43466aaeSMax Grossman 
278*43466aaeSMax Grossman 	if (!spa_feature_is_enabled(spa, feature->fi_feature)) {
279*43466aaeSMax Grossman 		return (SET_ERROR(ENOTSUP));
280*43466aaeSMax Grossman 	}
281*43466aaeSMax Grossman 
282*43466aaeSMax Grossman 	ASSERT(enabled_txg_obj != 0);
283*43466aaeSMax Grossman 
284*43466aaeSMax Grossman 	VERIFY0(zap_lookup(spa->spa_meta_objset, spa->spa_feat_enabled_txg_obj,
285*43466aaeSMax Grossman 	    feature->fi_guid, sizeof (uint64_t), 1, res));
286*43466aaeSMax Grossman 
287*43466aaeSMax Grossman 	return (0);
288*43466aaeSMax Grossman }
289*43466aaeSMax Grossman 
2902acef22dSMatthew Ahrens /*
2912acef22dSMatthew Ahrens  * This function is non-static for zhack; it should otherwise not be used
2922acef22dSMatthew Ahrens  * outside this file.
2932acef22dSMatthew Ahrens  */
2942acef22dSMatthew Ahrens void
2952acef22dSMatthew Ahrens feature_sync(spa_t *spa, zfeature_info_t *feature, uint64_t refcount,
29653089ab7Seschrock     dmu_tx_t *tx)
29753089ab7Seschrock {
298*43466aaeSMax Grossman 	ASSERT(VALID_FEATURE_OR_NONE(feature->fi_feature));
2992acef22dSMatthew Ahrens 	uint64_t zapobj = feature->fi_can_readonly ?
3002acef22dSMatthew Ahrens 	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
3012acef22dSMatthew Ahrens 
3022acef22dSMatthew Ahrens 	VERIFY0(zap_update(spa->spa_meta_objset, zapobj, feature->fi_guid,
3032acef22dSMatthew Ahrens 	    sizeof (uint64_t), 1, &refcount, tx));
3042acef22dSMatthew Ahrens 
305*43466aaeSMax Grossman 	/*
306*43466aaeSMax Grossman 	 * feature_sync is called directly from zhack, allowing the
307*43466aaeSMax Grossman 	 * creation of arbitrary features whose fi_feature field may
308*43466aaeSMax Grossman 	 * be greater than SPA_FEATURES. When called from zhack, the
309*43466aaeSMax Grossman 	 * zfeature_info_t object's fi_feature field will be set to
310*43466aaeSMax Grossman 	 * SPA_FEATURE_NONE.
311*43466aaeSMax Grossman 	 */
312*43466aaeSMax Grossman 	if (feature->fi_feature != SPA_FEATURE_NONE) {
313*43466aaeSMax Grossman 		uint64_t *refcount_cache =
314*43466aaeSMax Grossman 		    &spa->spa_feat_refcount_cache[feature->fi_feature];
315*43466aaeSMax Grossman 		VERIFY3U(*refcount_cache, ==,
316*43466aaeSMax Grossman 		    atomic_swap_64(refcount_cache, refcount));
317*43466aaeSMax Grossman 	}
318*43466aaeSMax Grossman 
3192acef22dSMatthew Ahrens 	if (refcount == 0)
3202acef22dSMatthew Ahrens 		spa_deactivate_mos_feature(spa, feature->fi_guid);
3212acef22dSMatthew Ahrens 	else if (feature->fi_mos)
322*43466aaeSMax Grossman 		spa_activate_mos_feature(spa, feature->fi_guid, tx);
3232acef22dSMatthew Ahrens }
3242acef22dSMatthew Ahrens 
3252acef22dSMatthew Ahrens /*
3262acef22dSMatthew Ahrens  * This function is non-static for zhack; it should otherwise not be used
3272acef22dSMatthew Ahrens  * outside this file.
3282acef22dSMatthew Ahrens  */
3292acef22dSMatthew Ahrens void
3302acef22dSMatthew Ahrens feature_enable_sync(spa_t *spa, zfeature_info_t *feature, dmu_tx_t *tx)
3312acef22dSMatthew Ahrens {
332*43466aaeSMax Grossman 	uint64_t initial_refcount = feature->fi_activate_on_enable ? 1 : 0;
3332acef22dSMatthew Ahrens 	uint64_t zapobj = feature->fi_can_readonly ?
3342acef22dSMatthew Ahrens 	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
33553089ab7Seschrock 
33653089ab7Seschrock 	ASSERT(0 != zapobj);
33753089ab7Seschrock 	ASSERT(zfeature_is_valid_guid(feature->fi_guid));
3382acef22dSMatthew Ahrens 	ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
33953089ab7Seschrock 
34053089ab7Seschrock 	/*
3412acef22dSMatthew Ahrens 	 * If the feature is already enabled, ignore the request.
34253089ab7Seschrock 	 */
3432acef22dSMatthew Ahrens 	if (zap_contains(spa->spa_meta_objset, zapobj, feature->fi_guid) == 0)
3442acef22dSMatthew Ahrens 		return;
3452acef22dSMatthew Ahrens 
3462acef22dSMatthew Ahrens 	for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++)
3472acef22dSMatthew Ahrens 		spa_feature_enable(spa, feature->fi_depends[i], tx);
3482acef22dSMatthew Ahrens 
3492acef22dSMatthew Ahrens 	VERIFY0(zap_update(spa->spa_meta_objset, spa->spa_feat_desc_obj,
3502acef22dSMatthew Ahrens 	    feature->fi_guid, 1, strlen(feature->fi_desc) + 1,
3512acef22dSMatthew Ahrens 	    feature->fi_desc, tx));
352*43466aaeSMax Grossman 
353*43466aaeSMax Grossman 	feature_sync(spa, feature, initial_refcount, tx);
354*43466aaeSMax Grossman 
355*43466aaeSMax Grossman 	if (spa_feature_is_enabled(spa, SPA_FEATURE_ENABLED_TXG)) {
356*43466aaeSMax Grossman 		uint64_t enabling_txg = dmu_tx_get_txg(tx);
357*43466aaeSMax Grossman 
358*43466aaeSMax Grossman 		if (spa->spa_feat_enabled_txg_obj == 0ULL) {
359*43466aaeSMax Grossman 			spa->spa_feat_enabled_txg_obj =
360*43466aaeSMax Grossman 			    zap_create_link(spa->spa_meta_objset,
361*43466aaeSMax Grossman 			    DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
362*43466aaeSMax Grossman 			    DMU_POOL_FEATURE_ENABLED_TXG, tx);
363*43466aaeSMax Grossman 		}
364*43466aaeSMax Grossman 		spa_feature_incr(spa, SPA_FEATURE_ENABLED_TXG, tx);
365*43466aaeSMax Grossman 
366*43466aaeSMax Grossman 		VERIFY0(zap_add(spa->spa_meta_objset,
367*43466aaeSMax Grossman 		    spa->spa_feat_enabled_txg_obj, feature->fi_guid,
368*43466aaeSMax Grossman 		    sizeof (uint64_t), 1, &enabling_txg, tx));
369*43466aaeSMax Grossman 	}
3702acef22dSMatthew Ahrens }
3712acef22dSMatthew Ahrens 
3722acef22dSMatthew Ahrens static void
3732acef22dSMatthew Ahrens feature_do_action(spa_t *spa, spa_feature_t fid, feature_action_t action,
3742acef22dSMatthew Ahrens     dmu_tx_t *tx)
3752acef22dSMatthew Ahrens {
3762acef22dSMatthew Ahrens 	uint64_t refcount;
3772acef22dSMatthew Ahrens 	zfeature_info_t *feature = &spa_feature_table[fid];
3782acef22dSMatthew Ahrens 	uint64_t zapobj = feature->fi_can_readonly ?
3792acef22dSMatthew Ahrens 	    spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
3802acef22dSMatthew Ahrens 
381*43466aaeSMax Grossman 	ASSERT(VALID_FEATURE_FID(fid));
3822acef22dSMatthew Ahrens 	ASSERT(0 != zapobj);
3832acef22dSMatthew Ahrens 	ASSERT(zfeature_is_valid_guid(feature->fi_guid));
3842acef22dSMatthew Ahrens 
3852acef22dSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
3862acef22dSMatthew Ahrens 	ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
3872acef22dSMatthew Ahrens 
388*43466aaeSMax Grossman 	VERIFY3U(feature_get_refcount(spa, feature, &refcount), !=, ENOTSUP);
38953089ab7Seschrock 
39053089ab7Seschrock 	switch (action) {
39153089ab7Seschrock 	case FEATURE_ACTION_INCR:
3922acef22dSMatthew Ahrens 		VERIFY3U(refcount, !=, UINT64_MAX);
39353089ab7Seschrock 		refcount++;
39453089ab7Seschrock 		break;
39553089ab7Seschrock 	case FEATURE_ACTION_DECR:
3962acef22dSMatthew Ahrens 		VERIFY3U(refcount, !=, 0);
39753089ab7Seschrock 		refcount--;
39853089ab7Seschrock 		break;
39953089ab7Seschrock 	default:
40053089ab7Seschrock 		ASSERT(0);
40153089ab7Seschrock 		break;
40253089ab7Seschrock 	}
40353089ab7Seschrock 
4042acef22dSMatthew Ahrens 	feature_sync(spa, feature, refcount, tx);
40553089ab7Seschrock }
40653089ab7Seschrock 
40753089ab7Seschrock void
40853089ab7Seschrock spa_feature_create_zap_objects(spa_t *spa, dmu_tx_t *tx)
40953089ab7Seschrock {
41053089ab7Seschrock 	/*
41153089ab7Seschrock 	 * We create feature flags ZAP objects in two instances: during pool
41253089ab7Seschrock 	 * creation and during pool upgrade.
41353089ab7Seschrock 	 */
41453089ab7Seschrock 	ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)) || (!spa->spa_sync_on &&
41553089ab7Seschrock 	    tx->tx_txg == TXG_INITIAL));
41653089ab7Seschrock 
41753089ab7Seschrock 	spa->spa_feat_for_read_obj = zap_create_link(spa->spa_meta_objset,
41853089ab7Seschrock 	    DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
41953089ab7Seschrock 	    DMU_POOL_FEATURES_FOR_READ, tx);
42053089ab7Seschrock 	spa->spa_feat_for_write_obj = zap_create_link(spa->spa_meta_objset,
42153089ab7Seschrock 	    DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
42253089ab7Seschrock 	    DMU_POOL_FEATURES_FOR_WRITE, tx);
42353089ab7Seschrock 	spa->spa_feat_desc_obj = zap_create_link(spa->spa_meta_objset,
42453089ab7Seschrock 	    DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
42553089ab7Seschrock 	    DMU_POOL_FEATURE_DESCRIPTIONS, tx);
42653089ab7Seschrock }
42753089ab7Seschrock 
42853089ab7Seschrock /*
42953089ab7Seschrock  * Enable any required dependencies, then enable the requested feature.
43053089ab7Seschrock  */
43153089ab7Seschrock void
4322acef22dSMatthew Ahrens spa_feature_enable(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
43353089ab7Seschrock {
43453089ab7Seschrock 	ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
435*43466aaeSMax Grossman 	ASSERT(VALID_FEATURE_FID(fid));
4362acef22dSMatthew Ahrens 	feature_enable_sync(spa, &spa_feature_table[fid], tx);
43753089ab7Seschrock }
43853089ab7Seschrock 
43953089ab7Seschrock void
4402acef22dSMatthew Ahrens spa_feature_incr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
44153089ab7Seschrock {
4422acef22dSMatthew Ahrens 	feature_do_action(spa, fid, FEATURE_ACTION_INCR, tx);
44353089ab7Seschrock }
44453089ab7Seschrock 
44553089ab7Seschrock void
4462acef22dSMatthew Ahrens spa_feature_decr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
44753089ab7Seschrock {
4482acef22dSMatthew Ahrens 	feature_do_action(spa, fid, FEATURE_ACTION_DECR, tx);
4490713e232SGeorge Wilson }
4500713e232SGeorge Wilson 
45153089ab7Seschrock boolean_t
4522acef22dSMatthew Ahrens spa_feature_is_enabled(spa_t *spa, spa_feature_t fid)
45353089ab7Seschrock {
45453089ab7Seschrock 	int err;
45553089ab7Seschrock 	uint64_t refcount;
45653089ab7Seschrock 
457*43466aaeSMax Grossman 	ASSERT(VALID_FEATURE_FID(fid));
45853089ab7Seschrock 	if (spa_version(spa) < SPA_VERSION_FEATURES)
45953089ab7Seschrock 		return (B_FALSE);
46053089ab7Seschrock 
4612acef22dSMatthew Ahrens 	err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount);
46253089ab7Seschrock 	ASSERT(err == 0 || err == ENOTSUP);
46353089ab7Seschrock 	return (err == 0);
46453089ab7Seschrock }
46553089ab7Seschrock 
46653089ab7Seschrock boolean_t
4672acef22dSMatthew Ahrens spa_feature_is_active(spa_t *spa, spa_feature_t fid)
46853089ab7Seschrock {
46953089ab7Seschrock 	int err;
47053089ab7Seschrock 	uint64_t refcount;
47153089ab7Seschrock 
472*43466aaeSMax Grossman 	ASSERT(VALID_FEATURE_FID(fid));
47353089ab7Seschrock 	if (spa_version(spa) < SPA_VERSION_FEATURES)
47453089ab7Seschrock 		return (B_FALSE);
47553089ab7Seschrock 
4762acef22dSMatthew Ahrens 	err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount);
47753089ab7Seschrock 	ASSERT(err == 0 || err == ENOTSUP);
47853089ab7Seschrock 	return (err == 0 && refcount > 0);
47953089ab7Seschrock }
480*43466aaeSMax Grossman 
481*43466aaeSMax Grossman /*
482*43466aaeSMax Grossman  * For the feature specified by fid (which must depend on
483*43466aaeSMax Grossman  * SPA_FEATURE_ENABLED_TXG), return the TXG at which it was enabled in the
484*43466aaeSMax Grossman  * OUT txg argument.
485*43466aaeSMax Grossman  *
486*43466aaeSMax Grossman  * Returns B_TRUE if the feature is enabled, in which case txg will be filled
487*43466aaeSMax Grossman  * with the transaction group in which the specified feature was enabled.
488*43466aaeSMax Grossman  * Returns B_FALSE otherwise (i.e. if the feature is not enabled).
489*43466aaeSMax Grossman  */
490*43466aaeSMax Grossman boolean_t
491*43466aaeSMax Grossman spa_feature_enabled_txg(spa_t *spa, spa_feature_t fid, uint64_t *txg) {
492*43466aaeSMax Grossman 	int err;
493*43466aaeSMax Grossman 
494*43466aaeSMax Grossman 	ASSERT(VALID_FEATURE_FID(fid));
495*43466aaeSMax Grossman 	if (spa_version(spa) < SPA_VERSION_FEATURES)
496*43466aaeSMax Grossman 		return (B_FALSE);
497*43466aaeSMax Grossman 
498*43466aaeSMax Grossman 	err = feature_get_enabled_txg(spa, &spa_feature_table[fid], txg);
499*43466aaeSMax Grossman 	ASSERT(err == 0 || err == ENOTSUP);
500*43466aaeSMax Grossman 
501*43466aaeSMax Grossman 	return (err == 0);
502*43466aaeSMax Grossman }
503