53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * CDDL HEADER START
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * The contents of this file are subject to the terms of the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Common Development and Distribution License (the "License").
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * You may not use this file except in compliance with the License.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * or http://www.opensolaris.org/os/licensing.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * See the License for the specific language governing permissions
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * and limitations under the License.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * When distributing Covered Code, include this CDDL HEADER in each
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * If applicable, add the following below this CDDL HEADER, with the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * fields enclosed by brackets "[]" replaced with your own identifying
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * information: Portions Copyright [yyyy] [name of copyright owner]
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * CDDL HEADER END
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/zfs_context.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/zfeature.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dmu.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/nvpair.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/zap.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dmu_tx.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include "zfeature_common.h"
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/spa_impl.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * ZFS Feature Flags
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * -----------------
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * ZFS feature flags are used to provide fine-grained versioning to the ZFS
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * on-disk format. Once enabled on a pool feature flags replace the old
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * spa_version() number.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Each new on-disk format change will be given a uniquely identifying string
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * guid rather than a version number. This avoids the problem of different
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * organizations creating new on-disk formats with the same version number. To
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * keep feature guids unique they should consist of the reverse dns name of the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * organization which implemented the feature and a short name for the feature,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * separated by a colon (e.g. com.delphix:async_destroy).
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Reference Counts
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * ----------------
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Within each pool features can be in one of three states: disabled, enabled,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * or active. These states are differentiated by a reference count stored on
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * disk for each feature:
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * 1) If there is no reference count stored on disk the feature is disabled.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * 2) If the reference count is 0 a system administrator has enabled the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * feature, but the feature has not been used yet, so no on-disk
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * format changes have been made.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * 3) If the reference count is greater than 0 the feature is active.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * The format changes required by the feature are currently on disk.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Note that if the feature's format changes are reversed the feature
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * may choose to set its reference count back to 0.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Feature flags makes no differentiation between non-zero reference counts
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * for an active feature (e.g. a reference count of 1 means the same thing as a
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * reference count of 27834721), but feature implementations may choose to use
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * the reference count to store meaningful information. For example, a new RAID
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * implementation might set the reference count to the number of vdevs using
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * it. If all those disks are removed from the pool the feature goes back to
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * having a reference count of 0.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * It is the responsibility of the individual features to maintain a non-zero
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * reference count as long as the feature's format changes are present on disk.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Dependencies
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * ------------
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Each feature may depend on other features. The only effect of this
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * relationship is that when a feature is enabled all of its dependencies are
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * automatically enabled as well. Any future work to support disabling of
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * features would need to ensure that features cannot be disabled if other
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * enabled features depend on them.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * On-disk Format
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * --------------
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * When feature flags are enabled spa_version() is set to SPA_VERSION_FEATURES
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * (5000). In order for this to work the pool is automatically upgraded to
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * SPA_VERSION_BEFORE_FEATURES (28) first, so all pre-feature flags on disk
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * format changes will be in use.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Information about features is stored in 3 ZAP objects in the pool's MOS.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * These objects are linked to by the following names in the pool directory
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * object:
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * 1) features_for_read: feature guid -> reference count
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Features needed to open the pool for reading.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * 2) features_for_write: feature guid -> reference count
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Features needed to open the pool for writing.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * 3) feature_descriptions: feature guid -> descriptive string
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * A human readable string.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * All enabled features appear in either features_for_read or
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * features_for_write, but not both.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * To open a pool in read-only mode only the features listed in
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * features_for_read need to be supported.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * To open the pool in read-write mode features in both features_for_read and
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * features_for_write need to be supported.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Some features may be required to read the ZAP objects containing feature
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * information. To allow software to check for compatibility with these features
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * before the pool is opened their names must be stored in the label in a
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * new "features_for_read" entry (note that features that are only required
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * to write to a pool never need to be stored in the label since the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * features_for_write ZAP object can be read before the pool is written to).
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * To save space in the label features must be explicitly marked as needing to
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * be written to the label. Also, reference counts are not stored in the label,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * instead any feature whose reference count drops to 0 is removed from the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * label.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Adding New Features
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * -------------------
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Features must be registered in zpool_feature_init() function in
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * zfeature_common.c using the zfeature_register() function. This function
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * has arguments to specify if the feature should be stored in the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * features_for_read or features_for_write ZAP object and if it needs to be
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * written to the label when active.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Once a feature is registered it will appear as a "feature@<feature name>"
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * property which can be set by an administrator. Feature implementors should
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * use the spa_feature_is_enabled() and spa_feature_is_active() functions to
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * query the state of a feature and the spa_feature_incr() and
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * spa_feature_decr() functions to change an enabled feature's reference count.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Reference counts may only be updated in the syncing context.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Features may not perform enable-time initialization. Instead, any such
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * initialization should occur when the feature is first used. This design
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * enforces that on-disk changes be made only when features are used. Code
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * should only check if a feature is enabled using spa_feature_is_enabled(),
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * not by relying on any feature specific metadata existing. If a feature is
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * enabled, but the feature's metadata is not on disk yet then it should be
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * created as needed.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * As an example, consider the com.delphix:async_destroy feature. This feature
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * relies on the existence of a bptree in the MOS that store blocks for
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * asynchronous freeing. This bptree is not created when async_destroy is
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * enabled. Instead, when a dataset is destroyed spa_feature_is_enabled() is
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * called to check if async_destroy is enabled. If it is and the bptree object
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * does not exist yet, the bptree object is created as part of the dataset
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * destroy and async_destroy's reference count is incremented to indicate it
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * has made an on-disk format change. Later, after the destroyed dataset's
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * blocks have all been asynchronously freed there is no longer any use for the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * bptree object, so it is destroyed and async_destroy's reference count is
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * decremented back to 0 to indicate that it has undone its on-disk format
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * changes.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrocktypedef enum {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock FEATURE_ACTION_INCR,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock FEATURE_ACTION_DECR,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock} feature_action_t;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * Checks that the active features in the pool are supported by
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * this software. Adds each unsupported feature (name -> description) to
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * the supplied nvlist.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrockboolean_t
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensspa_features_check(spa_t *spa, boolean_t for_write,
57221772c3fc05faba04bf48ddff45abf2bbf2bdChristopher Siden nvlist_t *unsup_feat, nvlist_t *enabled_feat)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens objset_t *os = spa->spa_meta_objset;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock boolean_t supported;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zap_cursor_t zc;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zap_attribute_t za;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens uint64_t obj = for_write ?
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock supported = B_TRUE;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock for (zap_cursor_init(&zc, os, obj);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zap_cursor_retrieve(&zc, &za) == 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zap_cursor_advance(&zc)) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(za.za_integer_length == sizeof (uint64_t) &&
53089ab7c84db6fb76c16ca50076c147cda11757eschrock za.za_num_integers == 1);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
57221772c3fc05faba04bf48ddff45abf2bbf2bdChristopher Siden if (NULL != enabled_feat) {
57221772c3fc05faba04bf48ddff45abf2bbf2bdChristopher Siden fnvlist_add_uint64(enabled_feat, za.za_name,
57221772c3fc05faba04bf48ddff45abf2bbf2bdChristopher Siden za.za_first_integer);
57221772c3fc05faba04bf48ddff45abf2bbf2bdChristopher Siden }
57221772c3fc05faba04bf48ddff45abf2bbf2bdChristopher Siden
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (za.za_first_integer != 0 &&
53089ab7c84db6fb76c16ca50076c147cda11757eschrock !zfeature_is_supported(za.za_name)) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock supported = B_FALSE;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
57221772c3fc05faba04bf48ddff45abf2bbf2bdChristopher Siden if (NULL != unsup_feat) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock char *desc = "";
53089ab7c84db6fb76c16ca50076c147cda11757eschrock char buf[MAXPATHLEN];
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (zap_lookup(os, spa->spa_feat_desc_obj,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens za.za_name, 1, sizeof (buf), buf) == 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock desc = buf;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock VERIFY(nvlist_add_string(unsup_feat, za.za_name,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock desc) == 0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zap_cursor_fini(&zc);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (supported);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens/*
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * Use an in-memory cache of feature refcounts for quick retrieval.
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman *
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * Note: well-designed features will not need to use this; they should
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * use spa_feature_is_enabled() and spa_feature_is_active() instead.
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * However, this is non-static for zdb and zhack.
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens */
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensint
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensfeature_get_refcount(spa_t *spa, zfeature_info_t *feature, uint64_t *res)
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman{
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(VALID_FEATURE_FID(feature->fi_feature));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (spa->spa_feat_refcount_cache[feature->fi_feature] ==
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman SPA_FEATURE_DISABLED) {
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman return (SET_ERROR(ENOTSUP));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman }
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman *res = spa->spa_feat_refcount_cache[feature->fi_feature];
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman return (0);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman}
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman/*
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * Note: well-designed features will not need to use this; they should
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * use spa_feature_is_enabled() and spa_feature_is_active() instead.
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * However, this is non-static for zdb and zhack.
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman */
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossmanint
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossmanfeature_get_refcount_from_disk(spa_t *spa, zfeature_info_t *feature,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman uint64_t *res)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t refcount;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens /*
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens * If the pool is currently being created, the feature objects may not
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens * have been allocated yet. Act as though all features are disabled.
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens */
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (zapobj == 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens err = zap_lookup(spa->spa_meta_objset, zapobj,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens feature->fi_guid, sizeof (uint64_t), 1, &refcount);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (err != 0) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (err == ENOENT)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock else
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *res = refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossmanstatic int
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagneliefeature_get_enabled_txg(spa_t *spa, zfeature_info_t *feature, uint64_t *res)
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagnelie{
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman uint64_t enabled_txg_obj = spa->spa_feat_enabled_txg_obj;
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(zfeature_depends_on(feature->fi_feature,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman SPA_FEATURE_ENABLED_TXG));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (!spa_feature_is_enabled(spa, feature->fi_feature)) {
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman return (SET_ERROR(ENOTSUP));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman }
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(enabled_txg_obj != 0);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman VERIFY0(zap_lookup(spa->spa_meta_objset, spa->spa_feat_enabled_txg_obj,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman feature->fi_guid, sizeof (uint64_t), 1, res));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman return (0);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman}
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens/*
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * This function is non-static for zhack; it should otherwise not be used
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * outside this file.
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens */
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensfeature_sync(spa_t *spa, zfeature_info_t *feature, uint64_t refcount,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(VALID_FEATURE_OR_NONE(feature->fi_feature));
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens VERIFY0(zap_update(spa->spa_meta_objset, zapobj, feature->fi_guid,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens sizeof (uint64_t), 1, &refcount, tx));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman /*
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * feature_sync is called directly from zhack, allowing the
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * creation of arbitrary features whose fi_feature field may
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * be greater than SPA_FEATURES. When called from zhack, the
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * zfeature_info_t object's fi_feature field will be set to
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * SPA_FEATURE_NONE.
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman */
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (feature->fi_feature != SPA_FEATURE_NONE) {
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman uint64_t *refcount_cache =
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman &spa->spa_feat_refcount_cache[feature->fi_feature];
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman VERIFY3U(*refcount_cache, ==,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman atomic_swap_64(refcount_cache, refcount));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman }
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (refcount == 0)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa_deactivate_mos_feature(spa, feature->fi_guid);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens else if (feature->fi_flags & ZFEATURE_FLAG_MOS)
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman spa_activate_mos_feature(spa, feature->fi_guid, tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens}
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens/*
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * This function is non-static for zhack; it should otherwise not be used
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * outside this file.
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens */
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensfeature_enable_sync(spa_t *spa, zfeature_info_t *feature, dmu_tx_t *tx)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens{
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens uint64_t initial_refcount =
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens (feature->fi_flags & ZFEATURE_FLAG_ACTIVATE_ON_ENABLE) ? 1 : 0;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(0 != zapobj);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(zfeature_is_valid_guid(feature->fi_guid));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /*
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * If the feature is already enabled, ignore the request.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (zap_contains(spa->spa_meta_objset, zapobj, feature->fi_guid) == 0)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens return;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa_feature_enable(spa, feature->fi_depends[i], tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens VERIFY0(zap_update(spa->spa_meta_objset, spa->spa_feat_desc_obj,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens feature->fi_guid, 1, strlen(feature->fi_desc) + 1,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens feature->fi_desc, tx));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman feature_sync(spa, feature, initial_refcount, tx);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (spa_feature_is_enabled(spa, SPA_FEATURE_ENABLED_TXG)) {
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman uint64_t enabling_txg = dmu_tx_get_txg(tx);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (spa->spa_feat_enabled_txg_obj == 0ULL) {
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman spa->spa_feat_enabled_txg_obj =
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman zap_create_link(spa->spa_meta_objset,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman DMU_POOL_FEATURE_ENABLED_TXG, tx);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman }
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman spa_feature_incr(spa, SPA_FEATURE_ENABLED_TXG, tx);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman VERIFY0(zap_add(spa->spa_meta_objset,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman spa->spa_feat_enabled_txg_obj, feature->fi_guid,
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman sizeof (uint64_t), 1, &enabling_txg, tx));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman }
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens}
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensstatic void
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensfeature_do_action(spa_t *spa, spa_feature_t fid, feature_action_t action,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dmu_tx_t *tx)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens{
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens uint64_t refcount;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens zfeature_info_t *feature = &spa_feature_table[fid];
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(VALID_FEATURE_FID(fid));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT(0 != zapobj);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT(zfeature_is_valid_guid(feature->fi_guid));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman VERIFY3U(feature_get_refcount(spa, feature, &refcount), !=, ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock switch (action) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock case FEATURE_ACTION_INCR:
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens VERIFY3U(refcount, !=, UINT64_MAX);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock refcount++;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock case FEATURE_ACTION_DECR:
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens VERIFY3U(refcount, !=, 0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock refcount--;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock default:
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens feature_sync(spa, feature, refcount, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockvoid
53089ab7c84db6fb76c16ca50076c147cda11757eschrockspa_feature_create_zap_objects(spa_t *spa, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * We create feature flags ZAP objects in two instances: during pool
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * creation and during pool upgrade.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)) || (!spa->spa_sync_on &&
53089ab7c84db6fb76c16ca50076c147cda11757eschrock tx->tx_txg == TXG_INITIAL));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_for_read_obj = zap_create_link(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock DMU_POOL_FEATURES_FOR_READ, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_for_write_obj = zap_create_link(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock DMU_POOL_FEATURES_FOR_WRITE, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_desc_obj = zap_create_link(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock DMU_POOL_FEATURE_DESCRIPTIONS, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Enable any required dependencies, then enable the requested feature.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrockvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensspa_feature_enable(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(VALID_FEATURE_FID(fid));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens feature_enable_sync(spa, &spa_feature_table[fid], tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensspa_feature_incr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens feature_do_action(spa, fid, FEATURE_ACTION_INCR, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensspa_feature_decr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens feature_do_action(spa, fid, FEATURE_ACTION_DECR, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
53089ab7c84db6fb76c16ca50076c147cda11757eschrockboolean_t
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensspa_feature_is_enabled(spa_t *spa, spa_feature_t fid)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(VALID_FEATURE_FID(fid));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (spa_version(spa) < SPA_VERSION_FEATURES)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (B_FALSE);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(err == 0 || err == ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err == 0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockboolean_t
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensspa_feature_is_active(spa_t *spa, spa_feature_t fid)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(VALID_FEATURE_FID(fid));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (spa_version(spa) < SPA_VERSION_FEATURES)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (B_FALSE);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(err == 0 || err == ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err == 0 && refcount > 0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman/*
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * For the feature specified by fid (which must depend on
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * SPA_FEATURE_ENABLED_TXG), return the TXG at which it was enabled in the
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * OUT txg argument.
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman *
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * Returns B_TRUE if the feature is enabled, in which case txg will be filled
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * with the transaction group in which the specified feature was enabled.
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman * Returns B_FALSE otherwise (i.e. if the feature is not enabled).
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman */
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossmanboolean_t
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagneliespa_feature_enabled_txg(spa_t *spa, spa_feature_t fid, uint64_t *txg)
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagnelie{
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman int err;
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(VALID_FEATURE_FID(fid));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (spa_version(spa) < SPA_VERSION_FEATURES)
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman return (B_FALSE);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman err = feature_get_enabled_txg(spa, &spa_feature_table[fid], txg);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(err == 0 || err == ENOTSUP);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman return (err == 0);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman}