zfeature.c revision 53089ab7c84db6fb76c16ca50076c147cda11757
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/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Copyright (c) 2012 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_ENABLE,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock FEATURE_ACTION_INCR,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock FEATURE_ACTION_DECR,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock} feature_action_t;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Checks that the features active in the specified object are supported by
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * this software. Adds each unsupported feature (name -> description) to
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * the supplied nvlist.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrockboolean_t
53089ab7c84db6fb76c16ca50076c147cda11757eschrockfeature_is_supported(objset_t *os, uint64_t obj, uint64_t desc_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock nvlist_t *unsup_feat)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock boolean_t supported;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zap_cursor_t zc;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zap_attribute_t za;
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
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (za.za_first_integer != 0 &&
53089ab7c84db6fb76c16ca50076c147cda11757eschrock !zfeature_is_supported(za.za_name)) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock supported = B_FALSE;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (unsup_feat != NULL) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock char *desc = "";
53089ab7c84db6fb76c16ca50076c147cda11757eschrock char buf[MAXPATHLEN];
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (zap_lookup(os, desc_obj, za.za_name,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock 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
53089ab7c84db6fb76c16ca50076c147cda11757eschrockstatic int
53089ab7c84db6fb76c16ca50076c147cda11757eschrockfeature_get_refcount(objset_t *os, uint64_t read_obj, uint64_t write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zfeature_info_t *feature, uint64_t *res)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t zapobj = feature->fi_can_readonly ? write_obj : read_obj;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(0 != zapobj);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = zap_lookup(os, zapobj, feature->fi_guid, sizeof (uint64_t), 1,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock &refcount);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (err != 0) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (err == ENOENT)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock else
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *res = refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockstatic int
53089ab7c84db6fb76c16ca50076c147cda11757eschrockfeature_do_action(objset_t *os, uint64_t read_obj, uint64_t write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t desc_obj, zfeature_info_t *feature, feature_action_t action,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int error;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t zapobj = feature->fi_can_readonly ? write_obj : read_obj;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(0 != zapobj);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(zfeature_is_valid_guid(feature->fi_guid));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock error = zap_lookup(os, zapobj, feature->fi_guid,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock sizeof (uint64_t), 1, &refcount);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * If we can't ascertain the status of the specified feature, an I/O
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * error occurred.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (error != 0 && error != ENOENT)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (error);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock switch (action) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock case FEATURE_ACTION_ENABLE:
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * If the feature is already enabled, ignore the request.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (error == 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock refcount = 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock case FEATURE_ACTION_INCR:
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (error == ENOENT)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (refcount == UINT64_MAX)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (EOVERFLOW);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock refcount++;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock case FEATURE_ACTION_DECR:
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (error == ENOENT)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (refcount == 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (EOVERFLOW);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock refcount--;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock default:
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (action == FEATURE_ACTION_ENABLE) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int i;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock for (i = 0; feature->fi_depends[i] != NULL; i++) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock zfeature_info_t *dep = feature->fi_depends[i];
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock error = feature_do_action(os, read_obj, write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock desc_obj, dep, FEATURE_ACTION_ENABLE, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (error != 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (error);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock error = zap_update(os, zapobj, feature->fi_guid,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock sizeof (uint64_t), 1, &refcount, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (error != 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (error);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (action == FEATURE_ACTION_ENABLE) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock error = zap_update(os, desc_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock feature->fi_guid, 1, strlen(feature->fi_desc) + 1,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock feature->fi_desc, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (error != 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (error);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (action == FEATURE_ACTION_INCR && refcount == 1 && feature->fi_mos) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa_activate_mos_feature(dmu_objset_spa(os), feature->fi_guid);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (action == FEATURE_ACTION_DECR && refcount == 0) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa_deactivate_mos_feature(dmu_objset_spa(os),
53089ab7c84db6fb76c16ca50076c147cda11757eschrock feature->fi_guid);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (0);
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
53089ab7c84db6fb76c16ca50076c147cda11757eschrockspa_feature_enable(spa_t *spa, zfeature_info_t *feature, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock VERIFY3U(0, ==, feature_do_action(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_for_read_obj, spa->spa_feat_for_write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_desc_obj, feature, FEATURE_ACTION_ENABLE, tx));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * If the specified feature has not yet been enabled, this function returns
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * ENOTSUP; otherwise, this function increments the feature's refcount (or
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * returns EOVERFLOW if the refcount cannot be incremented). This function must
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * be called from syncing context.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrockvoid
53089ab7c84db6fb76c16ca50076c147cda11757eschrockspa_feature_incr(spa_t *spa, zfeature_info_t *feature, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock VERIFY3U(0, ==, feature_do_action(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_for_read_obj, spa->spa_feat_for_write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_desc_obj, feature, FEATURE_ACTION_INCR, tx));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * If the specified feature has not yet been enabled, this function returns
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * ENOTSUP; otherwise, this function decrements the feature's refcount (or
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * returns EOVERFLOW if the refcount is already 0). This function must
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * be called from syncing context.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrockvoid
53089ab7c84db6fb76c16ca50076c147cda11757eschrockspa_feature_decr(spa_t *spa, zfeature_info_t *feature, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock VERIFY3U(0, ==, feature_do_action(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_for_read_obj, spa->spa_feat_for_write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_desc_obj, feature, FEATURE_ACTION_DECR, tx));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockboolean_t
53089ab7c84db6fb76c16ca50076c147cda11757eschrockspa_feature_is_enabled(spa_t *spa, zfeature_info_t *feature)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (spa_version(spa) < SPA_VERSION_FEATURES)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (B_FALSE);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = feature_get_refcount(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_for_read_obj, spa->spa_feat_for_write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock feature, &refcount);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(err == 0 || err == ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err == 0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockboolean_t
53089ab7c84db6fb76c16ca50076c147cda11757eschrockspa_feature_is_active(spa_t *spa, zfeature_info_t *feature)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t refcount;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (spa_version(spa) < SPA_VERSION_FEATURES)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (B_FALSE);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = feature_get_refcount(spa->spa_meta_objset,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock spa->spa_feat_for_read_obj, spa->spa_feat_for_write_obj,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock feature, &refcount);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(err == 0 || err == ENOTSUP);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err == 0 && refcount > 0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}