1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6*eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10*eda14cbcSMatt Macy * See the License for the specific language governing permissions 11*eda14cbcSMatt Macy * and limitations under the License. 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * CDDL HEADER END 20*eda14cbcSMatt Macy */ 21*eda14cbcSMatt Macy 22*eda14cbcSMatt Macy /* 23*eda14cbcSMatt Macy * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 24*eda14cbcSMatt Macy */ 25*eda14cbcSMatt Macy 26*eda14cbcSMatt Macy #include <sys/zfs_context.h> 27*eda14cbcSMatt Macy #include <sys/zfeature.h> 28*eda14cbcSMatt Macy #include <sys/dmu.h> 29*eda14cbcSMatt Macy #include <sys/nvpair.h> 30*eda14cbcSMatt Macy #include <sys/zap.h> 31*eda14cbcSMatt Macy #include <sys/dmu_tx.h> 32*eda14cbcSMatt Macy #include "zfeature_common.h" 33*eda14cbcSMatt Macy #include <sys/spa_impl.h> 34*eda14cbcSMatt Macy 35*eda14cbcSMatt Macy /* 36*eda14cbcSMatt Macy * ZFS Feature Flags 37*eda14cbcSMatt Macy * ----------------- 38*eda14cbcSMatt Macy * 39*eda14cbcSMatt Macy * ZFS feature flags are used to provide fine-grained versioning to the ZFS 40*eda14cbcSMatt Macy * on-disk format. Once enabled on a pool feature flags replace the old 41*eda14cbcSMatt Macy * spa_version() number. 42*eda14cbcSMatt Macy * 43*eda14cbcSMatt Macy * Each new on-disk format change will be given a uniquely identifying string 44*eda14cbcSMatt Macy * GUID rather than a version number. This avoids the problem of different 45*eda14cbcSMatt Macy * organizations creating new on-disk formats with the same version number. To 46*eda14cbcSMatt Macy * keep feature GUIDs unique they should consist of the reverse dns name of the 47*eda14cbcSMatt Macy * organization which implemented the feature and a short name for the feature, 48*eda14cbcSMatt Macy * separated by a colon (e.g. com.delphix:async_destroy). 49*eda14cbcSMatt Macy * 50*eda14cbcSMatt Macy * Reference Counts 51*eda14cbcSMatt Macy * ---------------- 52*eda14cbcSMatt Macy * 53*eda14cbcSMatt Macy * Within each pool features can be in one of three states: disabled, enabled, 54*eda14cbcSMatt Macy * or active. These states are differentiated by a reference count stored on 55*eda14cbcSMatt Macy * disk for each feature: 56*eda14cbcSMatt Macy * 57*eda14cbcSMatt Macy * 1) If there is no reference count stored on disk the feature is disabled. 58*eda14cbcSMatt Macy * 2) If the reference count is 0 a system administrator has enabled the 59*eda14cbcSMatt Macy * feature, but the feature has not been used yet, so no on-disk 60*eda14cbcSMatt Macy * format changes have been made. 61*eda14cbcSMatt Macy * 3) If the reference count is greater than 0 the feature is active. 62*eda14cbcSMatt Macy * The format changes required by the feature are currently on disk. 63*eda14cbcSMatt Macy * Note that if the feature's format changes are reversed the feature 64*eda14cbcSMatt Macy * may choose to set its reference count back to 0. 65*eda14cbcSMatt Macy * 66*eda14cbcSMatt Macy * Feature flags makes no differentiation between non-zero reference counts 67*eda14cbcSMatt Macy * for an active feature (e.g. a reference count of 1 means the same thing as a 68*eda14cbcSMatt Macy * reference count of 27834721), but feature implementations may choose to use 69*eda14cbcSMatt Macy * the reference count to store meaningful information. For example, a new RAID 70*eda14cbcSMatt Macy * implementation might set the reference count to the number of vdevs using 71*eda14cbcSMatt Macy * it. If all those disks are removed from the pool the feature goes back to 72*eda14cbcSMatt Macy * having a reference count of 0. 73*eda14cbcSMatt Macy * 74*eda14cbcSMatt Macy * It is the responsibility of the individual features to maintain a non-zero 75*eda14cbcSMatt Macy * reference count as long as the feature's format changes are present on disk. 76*eda14cbcSMatt Macy * 77*eda14cbcSMatt Macy * Dependencies 78*eda14cbcSMatt Macy * ------------ 79*eda14cbcSMatt Macy * 80*eda14cbcSMatt Macy * Each feature may depend on other features. The only effect of this 81*eda14cbcSMatt Macy * relationship is that when a feature is enabled all of its dependencies are 82*eda14cbcSMatt Macy * automatically enabled as well. Any future work to support disabling of 83*eda14cbcSMatt Macy * features would need to ensure that features cannot be disabled if other 84*eda14cbcSMatt Macy * enabled features depend on them. 85*eda14cbcSMatt Macy * 86*eda14cbcSMatt Macy * On-disk Format 87*eda14cbcSMatt Macy * -------------- 88*eda14cbcSMatt Macy * 89*eda14cbcSMatt Macy * When feature flags are enabled spa_version() is set to SPA_VERSION_FEATURES 90*eda14cbcSMatt Macy * (5000). In order for this to work the pool is automatically upgraded to 91*eda14cbcSMatt Macy * SPA_VERSION_BEFORE_FEATURES (28) first, so all pre-feature flags on disk 92*eda14cbcSMatt Macy * format changes will be in use. 93*eda14cbcSMatt Macy * 94*eda14cbcSMatt Macy * Information about features is stored in 3 ZAP objects in the pool's MOS. 95*eda14cbcSMatt Macy * These objects are linked to by the following names in the pool directory 96*eda14cbcSMatt Macy * object: 97*eda14cbcSMatt Macy * 98*eda14cbcSMatt Macy * 1) features_for_read: feature GUID -> reference count 99*eda14cbcSMatt Macy * Features needed to open the pool for reading. 100*eda14cbcSMatt Macy * 2) features_for_write: feature GUID -> reference count 101*eda14cbcSMatt Macy * Features needed to open the pool for writing. 102*eda14cbcSMatt Macy * 3) feature_descriptions: feature GUID -> descriptive string 103*eda14cbcSMatt Macy * A human readable string. 104*eda14cbcSMatt Macy * 105*eda14cbcSMatt Macy * All enabled features appear in either features_for_read or 106*eda14cbcSMatt Macy * features_for_write, but not both. 107*eda14cbcSMatt Macy * 108*eda14cbcSMatt Macy * To open a pool in read-only mode only the features listed in 109*eda14cbcSMatt Macy * features_for_read need to be supported. 110*eda14cbcSMatt Macy * 111*eda14cbcSMatt Macy * To open the pool in read-write mode features in both features_for_read and 112*eda14cbcSMatt Macy * features_for_write need to be supported. 113*eda14cbcSMatt Macy * 114*eda14cbcSMatt Macy * Some features may be required to read the ZAP objects containing feature 115*eda14cbcSMatt Macy * information. To allow software to check for compatibility with these features 116*eda14cbcSMatt Macy * before the pool is opened their names must be stored in the label in a 117*eda14cbcSMatt Macy * new "features_for_read" entry (note that features that are only required 118*eda14cbcSMatt Macy * to write to a pool never need to be stored in the label since the 119*eda14cbcSMatt Macy * features_for_write ZAP object can be read before the pool is written to). 120*eda14cbcSMatt Macy * To save space in the label features must be explicitly marked as needing to 121*eda14cbcSMatt Macy * be written to the label. Also, reference counts are not stored in the label, 122*eda14cbcSMatt Macy * instead any feature whose reference count drops to 0 is removed from the 123*eda14cbcSMatt Macy * label. 124*eda14cbcSMatt Macy * 125*eda14cbcSMatt Macy * Adding New Features 126*eda14cbcSMatt Macy * ------------------- 127*eda14cbcSMatt Macy * 128*eda14cbcSMatt Macy * Features must be registered in zpool_feature_init() function in 129*eda14cbcSMatt Macy * zfeature_common.c using the zfeature_register() function. This function 130*eda14cbcSMatt Macy * has arguments to specify if the feature should be stored in the 131*eda14cbcSMatt Macy * features_for_read or features_for_write ZAP object and if it needs to be 132*eda14cbcSMatt Macy * written to the label when active. 133*eda14cbcSMatt Macy * 134*eda14cbcSMatt Macy * Once a feature is registered it will appear as a "feature@<feature name>" 135*eda14cbcSMatt Macy * property which can be set by an administrator. Feature implementors should 136*eda14cbcSMatt Macy * use the spa_feature_is_enabled() and spa_feature_is_active() functions to 137*eda14cbcSMatt Macy * query the state of a feature and the spa_feature_incr() and 138*eda14cbcSMatt Macy * spa_feature_decr() functions to change an enabled feature's reference count. 139*eda14cbcSMatt Macy * Reference counts may only be updated in the syncing context. 140*eda14cbcSMatt Macy * 141*eda14cbcSMatt Macy * Features may not perform enable-time initialization. Instead, any such 142*eda14cbcSMatt Macy * initialization should occur when the feature is first used. This design 143*eda14cbcSMatt Macy * enforces that on-disk changes be made only when features are used. Code 144*eda14cbcSMatt Macy * should only check if a feature is enabled using spa_feature_is_enabled(), 145*eda14cbcSMatt Macy * not by relying on any feature specific metadata existing. If a feature is 146*eda14cbcSMatt Macy * enabled, but the feature's metadata is not on disk yet then it should be 147*eda14cbcSMatt Macy * created as needed. 148*eda14cbcSMatt Macy * 149*eda14cbcSMatt Macy * As an example, consider the com.delphix:async_destroy feature. This feature 150*eda14cbcSMatt Macy * relies on the existence of a bptree in the MOS that store blocks for 151*eda14cbcSMatt Macy * asynchronous freeing. This bptree is not created when async_destroy is 152*eda14cbcSMatt Macy * enabled. Instead, when a dataset is destroyed spa_feature_is_enabled() is 153*eda14cbcSMatt Macy * called to check if async_destroy is enabled. If it is and the bptree object 154*eda14cbcSMatt Macy * does not exist yet, the bptree object is created as part of the dataset 155*eda14cbcSMatt Macy * destroy and async_destroy's reference count is incremented to indicate it 156*eda14cbcSMatt Macy * has made an on-disk format change. Later, after the destroyed dataset's 157*eda14cbcSMatt Macy * blocks have all been asynchronously freed there is no longer any use for the 158*eda14cbcSMatt Macy * bptree object, so it is destroyed and async_destroy's reference count is 159*eda14cbcSMatt Macy * decremented back to 0 to indicate that it has undone its on-disk format 160*eda14cbcSMatt Macy * changes. 161*eda14cbcSMatt Macy */ 162*eda14cbcSMatt Macy 163*eda14cbcSMatt Macy typedef enum { 164*eda14cbcSMatt Macy FEATURE_ACTION_INCR, 165*eda14cbcSMatt Macy FEATURE_ACTION_DECR, 166*eda14cbcSMatt Macy } feature_action_t; 167*eda14cbcSMatt Macy 168*eda14cbcSMatt Macy /* 169*eda14cbcSMatt Macy * Checks that the active features in the pool are supported by 170*eda14cbcSMatt Macy * this software. Adds each unsupported feature (name -> description) to 171*eda14cbcSMatt Macy * the supplied nvlist. 172*eda14cbcSMatt Macy */ 173*eda14cbcSMatt Macy boolean_t 174*eda14cbcSMatt Macy spa_features_check(spa_t *spa, boolean_t for_write, 175*eda14cbcSMatt Macy nvlist_t *unsup_feat, nvlist_t *enabled_feat) 176*eda14cbcSMatt Macy { 177*eda14cbcSMatt Macy objset_t *os = spa->spa_meta_objset; 178*eda14cbcSMatt Macy boolean_t supported; 179*eda14cbcSMatt Macy zap_cursor_t *zc; 180*eda14cbcSMatt Macy zap_attribute_t *za; 181*eda14cbcSMatt Macy uint64_t obj = for_write ? 182*eda14cbcSMatt Macy spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj; 183*eda14cbcSMatt Macy char *buf; 184*eda14cbcSMatt Macy 185*eda14cbcSMatt Macy zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 186*eda14cbcSMatt Macy za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 187*eda14cbcSMatt Macy buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 188*eda14cbcSMatt Macy 189*eda14cbcSMatt Macy supported = B_TRUE; 190*eda14cbcSMatt Macy for (zap_cursor_init(zc, os, obj); 191*eda14cbcSMatt Macy zap_cursor_retrieve(zc, za) == 0; 192*eda14cbcSMatt Macy zap_cursor_advance(zc)) { 193*eda14cbcSMatt Macy ASSERT(za->za_integer_length == sizeof (uint64_t) && 194*eda14cbcSMatt Macy za->za_num_integers == 1); 195*eda14cbcSMatt Macy 196*eda14cbcSMatt Macy if (NULL != enabled_feat) { 197*eda14cbcSMatt Macy fnvlist_add_uint64(enabled_feat, za->za_name, 198*eda14cbcSMatt Macy za->za_first_integer); 199*eda14cbcSMatt Macy } 200*eda14cbcSMatt Macy 201*eda14cbcSMatt Macy if (za->za_first_integer != 0 && 202*eda14cbcSMatt Macy !zfeature_is_supported(za->za_name)) { 203*eda14cbcSMatt Macy supported = B_FALSE; 204*eda14cbcSMatt Macy 205*eda14cbcSMatt Macy if (NULL != unsup_feat) { 206*eda14cbcSMatt Macy char *desc = ""; 207*eda14cbcSMatt Macy 208*eda14cbcSMatt Macy if (zap_lookup(os, spa->spa_feat_desc_obj, 209*eda14cbcSMatt Macy za->za_name, 1, MAXPATHLEN, buf) == 0) 210*eda14cbcSMatt Macy desc = buf; 211*eda14cbcSMatt Macy 212*eda14cbcSMatt Macy VERIFY(nvlist_add_string(unsup_feat, 213*eda14cbcSMatt Macy za->za_name, desc) == 0); 214*eda14cbcSMatt Macy } 215*eda14cbcSMatt Macy } 216*eda14cbcSMatt Macy } 217*eda14cbcSMatt Macy zap_cursor_fini(zc); 218*eda14cbcSMatt Macy 219*eda14cbcSMatt Macy kmem_free(buf, MAXPATHLEN); 220*eda14cbcSMatt Macy kmem_free(za, sizeof (zap_attribute_t)); 221*eda14cbcSMatt Macy kmem_free(zc, sizeof (zap_cursor_t)); 222*eda14cbcSMatt Macy 223*eda14cbcSMatt Macy return (supported); 224*eda14cbcSMatt Macy } 225*eda14cbcSMatt Macy 226*eda14cbcSMatt Macy /* 227*eda14cbcSMatt Macy * Use an in-memory cache of feature refcounts for quick retrieval. 228*eda14cbcSMatt Macy * 229*eda14cbcSMatt Macy * Note: well-designed features will not need to use this; they should 230*eda14cbcSMatt Macy * use spa_feature_is_enabled() and spa_feature_is_active() instead. 231*eda14cbcSMatt Macy * However, this is non-static for zdb, zhack, and spa_add_feature_stats(). 232*eda14cbcSMatt Macy */ 233*eda14cbcSMatt Macy int 234*eda14cbcSMatt Macy feature_get_refcount(spa_t *spa, zfeature_info_t *feature, uint64_t *res) 235*eda14cbcSMatt Macy { 236*eda14cbcSMatt Macy ASSERT(VALID_FEATURE_FID(feature->fi_feature)); 237*eda14cbcSMatt Macy if (spa->spa_feat_refcount_cache[feature->fi_feature] == 238*eda14cbcSMatt Macy SPA_FEATURE_DISABLED) { 239*eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP)); 240*eda14cbcSMatt Macy } 241*eda14cbcSMatt Macy *res = spa->spa_feat_refcount_cache[feature->fi_feature]; 242*eda14cbcSMatt Macy return (0); 243*eda14cbcSMatt Macy } 244*eda14cbcSMatt Macy 245*eda14cbcSMatt Macy /* 246*eda14cbcSMatt Macy * Note: well-designed features will not need to use this; they should 247*eda14cbcSMatt Macy * use spa_feature_is_enabled() and spa_feature_is_active() instead. 248*eda14cbcSMatt Macy * However, this is non-static for zdb and zhack. 249*eda14cbcSMatt Macy */ 250*eda14cbcSMatt Macy int 251*eda14cbcSMatt Macy feature_get_refcount_from_disk(spa_t *spa, zfeature_info_t *feature, 252*eda14cbcSMatt Macy uint64_t *res) 253*eda14cbcSMatt Macy { 254*eda14cbcSMatt Macy int err; 255*eda14cbcSMatt Macy uint64_t refcount; 256*eda14cbcSMatt Macy uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 257*eda14cbcSMatt Macy spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj; 258*eda14cbcSMatt Macy 259*eda14cbcSMatt Macy /* 260*eda14cbcSMatt Macy * If the pool is currently being created, the feature objects may not 261*eda14cbcSMatt Macy * have been allocated yet. Act as though all features are disabled. 262*eda14cbcSMatt Macy */ 263*eda14cbcSMatt Macy if (zapobj == 0) 264*eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP)); 265*eda14cbcSMatt Macy 266*eda14cbcSMatt Macy err = zap_lookup(spa->spa_meta_objset, zapobj, 267*eda14cbcSMatt Macy feature->fi_guid, sizeof (uint64_t), 1, &refcount); 268*eda14cbcSMatt Macy if (err != 0) { 269*eda14cbcSMatt Macy if (err == ENOENT) 270*eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP)); 271*eda14cbcSMatt Macy else 272*eda14cbcSMatt Macy return (err); 273*eda14cbcSMatt Macy } 274*eda14cbcSMatt Macy *res = refcount; 275*eda14cbcSMatt Macy return (0); 276*eda14cbcSMatt Macy } 277*eda14cbcSMatt Macy 278*eda14cbcSMatt Macy 279*eda14cbcSMatt Macy static int 280*eda14cbcSMatt Macy feature_get_enabled_txg(spa_t *spa, zfeature_info_t *feature, uint64_t *res) 281*eda14cbcSMatt Macy { 282*eda14cbcSMatt Macy uint64_t enabled_txg_obj __maybe_unused = spa->spa_feat_enabled_txg_obj; 283*eda14cbcSMatt Macy 284*eda14cbcSMatt Macy ASSERT(zfeature_depends_on(feature->fi_feature, 285*eda14cbcSMatt Macy SPA_FEATURE_ENABLED_TXG)); 286*eda14cbcSMatt Macy 287*eda14cbcSMatt Macy if (!spa_feature_is_enabled(spa, feature->fi_feature)) { 288*eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP)); 289*eda14cbcSMatt Macy } 290*eda14cbcSMatt Macy 291*eda14cbcSMatt Macy ASSERT(enabled_txg_obj != 0); 292*eda14cbcSMatt Macy 293*eda14cbcSMatt Macy VERIFY0(zap_lookup(spa->spa_meta_objset, spa->spa_feat_enabled_txg_obj, 294*eda14cbcSMatt Macy feature->fi_guid, sizeof (uint64_t), 1, res)); 295*eda14cbcSMatt Macy 296*eda14cbcSMatt Macy return (0); 297*eda14cbcSMatt Macy } 298*eda14cbcSMatt Macy 299*eda14cbcSMatt Macy /* 300*eda14cbcSMatt Macy * This function is non-static for zhack; it should otherwise not be used 301*eda14cbcSMatt Macy * outside this file. 302*eda14cbcSMatt Macy */ 303*eda14cbcSMatt Macy void 304*eda14cbcSMatt Macy feature_sync(spa_t *spa, zfeature_info_t *feature, uint64_t refcount, 305*eda14cbcSMatt Macy dmu_tx_t *tx) 306*eda14cbcSMatt Macy { 307*eda14cbcSMatt Macy ASSERT(VALID_FEATURE_OR_NONE(feature->fi_feature)); 308*eda14cbcSMatt Macy uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 309*eda14cbcSMatt Macy spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj; 310*eda14cbcSMatt Macy VERIFY0(zap_update(spa->spa_meta_objset, zapobj, feature->fi_guid, 311*eda14cbcSMatt Macy sizeof (uint64_t), 1, &refcount, tx)); 312*eda14cbcSMatt Macy 313*eda14cbcSMatt Macy /* 314*eda14cbcSMatt Macy * feature_sync is called directly from zhack, allowing the 315*eda14cbcSMatt Macy * creation of arbitrary features whose fi_feature field may 316*eda14cbcSMatt Macy * be greater than SPA_FEATURES. When called from zhack, the 317*eda14cbcSMatt Macy * zfeature_info_t object's fi_feature field will be set to 318*eda14cbcSMatt Macy * SPA_FEATURE_NONE. 319*eda14cbcSMatt Macy */ 320*eda14cbcSMatt Macy if (feature->fi_feature != SPA_FEATURE_NONE) { 321*eda14cbcSMatt Macy uint64_t *refcount_cache = 322*eda14cbcSMatt Macy &spa->spa_feat_refcount_cache[feature->fi_feature]; 323*eda14cbcSMatt Macy VERIFY3U(*refcount_cache, ==, 324*eda14cbcSMatt Macy atomic_swap_64(refcount_cache, refcount)); 325*eda14cbcSMatt Macy } 326*eda14cbcSMatt Macy 327*eda14cbcSMatt Macy if (refcount == 0) 328*eda14cbcSMatt Macy spa_deactivate_mos_feature(spa, feature->fi_guid); 329*eda14cbcSMatt Macy else if (feature->fi_flags & ZFEATURE_FLAG_MOS) 330*eda14cbcSMatt Macy spa_activate_mos_feature(spa, feature->fi_guid, tx); 331*eda14cbcSMatt Macy } 332*eda14cbcSMatt Macy 333*eda14cbcSMatt Macy /* 334*eda14cbcSMatt Macy * This function is non-static for zhack; it should otherwise not be used 335*eda14cbcSMatt Macy * outside this file. 336*eda14cbcSMatt Macy */ 337*eda14cbcSMatt Macy void 338*eda14cbcSMatt Macy feature_enable_sync(spa_t *spa, zfeature_info_t *feature, dmu_tx_t *tx) 339*eda14cbcSMatt Macy { 340*eda14cbcSMatt Macy uint64_t initial_refcount = 341*eda14cbcSMatt Macy (feature->fi_flags & ZFEATURE_FLAG_ACTIVATE_ON_ENABLE) ? 1 : 0; 342*eda14cbcSMatt Macy uint64_t zapobj = (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 343*eda14cbcSMatt Macy spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj; 344*eda14cbcSMatt Macy 345*eda14cbcSMatt Macy ASSERT(0 != zapobj); 346*eda14cbcSMatt Macy ASSERT(zfeature_is_valid_guid(feature->fi_guid)); 347*eda14cbcSMatt Macy ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES); 348*eda14cbcSMatt Macy 349*eda14cbcSMatt Macy /* 350*eda14cbcSMatt Macy * If the feature is already enabled, ignore the request. 351*eda14cbcSMatt Macy */ 352*eda14cbcSMatt Macy if (zap_contains(spa->spa_meta_objset, zapobj, feature->fi_guid) == 0) 353*eda14cbcSMatt Macy return; 354*eda14cbcSMatt Macy 355*eda14cbcSMatt Macy for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++) 356*eda14cbcSMatt Macy spa_feature_enable(spa, feature->fi_depends[i], tx); 357*eda14cbcSMatt Macy 358*eda14cbcSMatt Macy VERIFY0(zap_update(spa->spa_meta_objset, spa->spa_feat_desc_obj, 359*eda14cbcSMatt Macy feature->fi_guid, 1, strlen(feature->fi_desc) + 1, 360*eda14cbcSMatt Macy feature->fi_desc, tx)); 361*eda14cbcSMatt Macy 362*eda14cbcSMatt Macy feature_sync(spa, feature, initial_refcount, tx); 363*eda14cbcSMatt Macy 364*eda14cbcSMatt Macy if (spa_feature_is_enabled(spa, SPA_FEATURE_ENABLED_TXG)) { 365*eda14cbcSMatt Macy uint64_t enabling_txg = dmu_tx_get_txg(tx); 366*eda14cbcSMatt Macy 367*eda14cbcSMatt Macy if (spa->spa_feat_enabled_txg_obj == 0ULL) { 368*eda14cbcSMatt Macy spa->spa_feat_enabled_txg_obj = 369*eda14cbcSMatt Macy zap_create_link(spa->spa_meta_objset, 370*eda14cbcSMatt Macy DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 371*eda14cbcSMatt Macy DMU_POOL_FEATURE_ENABLED_TXG, tx); 372*eda14cbcSMatt Macy } 373*eda14cbcSMatt Macy spa_feature_incr(spa, SPA_FEATURE_ENABLED_TXG, tx); 374*eda14cbcSMatt Macy 375*eda14cbcSMatt Macy VERIFY0(zap_add(spa->spa_meta_objset, 376*eda14cbcSMatt Macy spa->spa_feat_enabled_txg_obj, feature->fi_guid, 377*eda14cbcSMatt Macy sizeof (uint64_t), 1, &enabling_txg, tx)); 378*eda14cbcSMatt Macy } 379*eda14cbcSMatt Macy 380*eda14cbcSMatt Macy /* 381*eda14cbcSMatt Macy * Errata #4 is mostly a problem with encrypted datasets, but it 382*eda14cbcSMatt Macy * is also a problem where the old encryption feature did not 383*eda14cbcSMatt Macy * depend on the bookmark_v2 feature. If the pool does not have 384*eda14cbcSMatt Macy * any encrypted datasets we can resolve this issue simply by 385*eda14cbcSMatt Macy * enabling this dependency. 386*eda14cbcSMatt Macy */ 387*eda14cbcSMatt Macy if (spa->spa_errata == ZPOOL_ERRATA_ZOL_8308_ENCRYPTION && 388*eda14cbcSMatt Macy spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && 389*eda14cbcSMatt Macy !spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION) && 390*eda14cbcSMatt Macy feature->fi_feature == SPA_FEATURE_BOOKMARK_V2) 391*eda14cbcSMatt Macy spa->spa_errata = 0; 392*eda14cbcSMatt Macy } 393*eda14cbcSMatt Macy 394*eda14cbcSMatt Macy static void 395*eda14cbcSMatt Macy feature_do_action(spa_t *spa, spa_feature_t fid, feature_action_t action, 396*eda14cbcSMatt Macy dmu_tx_t *tx) 397*eda14cbcSMatt Macy { 398*eda14cbcSMatt Macy uint64_t refcount = 0; 399*eda14cbcSMatt Macy zfeature_info_t *feature = &spa_feature_table[fid]; 400*eda14cbcSMatt Macy uint64_t zapobj __maybe_unused = 401*eda14cbcSMatt Macy (feature->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ? 402*eda14cbcSMatt Macy spa->spa_feat_for_write_obj : spa->spa_feat_for_read_obj; 403*eda14cbcSMatt Macy 404*eda14cbcSMatt Macy ASSERT(VALID_FEATURE_FID(fid)); 405*eda14cbcSMatt Macy ASSERT(0 != zapobj); 406*eda14cbcSMatt Macy ASSERT(zfeature_is_valid_guid(feature->fi_guid)); 407*eda14cbcSMatt Macy 408*eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx)); 409*eda14cbcSMatt Macy ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES); 410*eda14cbcSMatt Macy 411*eda14cbcSMatt Macy VERIFY3U(feature_get_refcount(spa, feature, &refcount), !=, ENOTSUP); 412*eda14cbcSMatt Macy 413*eda14cbcSMatt Macy switch (action) { 414*eda14cbcSMatt Macy case FEATURE_ACTION_INCR: 415*eda14cbcSMatt Macy VERIFY3U(refcount, !=, UINT64_MAX); 416*eda14cbcSMatt Macy refcount++; 417*eda14cbcSMatt Macy break; 418*eda14cbcSMatt Macy case FEATURE_ACTION_DECR: 419*eda14cbcSMatt Macy VERIFY3U(refcount, !=, 0); 420*eda14cbcSMatt Macy refcount--; 421*eda14cbcSMatt Macy break; 422*eda14cbcSMatt Macy default: 423*eda14cbcSMatt Macy ASSERT(0); 424*eda14cbcSMatt Macy break; 425*eda14cbcSMatt Macy } 426*eda14cbcSMatt Macy 427*eda14cbcSMatt Macy feature_sync(spa, feature, refcount, tx); 428*eda14cbcSMatt Macy } 429*eda14cbcSMatt Macy 430*eda14cbcSMatt Macy void 431*eda14cbcSMatt Macy spa_feature_create_zap_objects(spa_t *spa, dmu_tx_t *tx) 432*eda14cbcSMatt Macy { 433*eda14cbcSMatt Macy /* 434*eda14cbcSMatt Macy * We create feature flags ZAP objects in two instances: during pool 435*eda14cbcSMatt Macy * creation and during pool upgrade. 436*eda14cbcSMatt Macy */ 437*eda14cbcSMatt Macy ASSERT((!spa->spa_sync_on && tx->tx_txg == TXG_INITIAL) || 438*eda14cbcSMatt Macy dsl_pool_sync_context(spa_get_dsl(spa))); 439*eda14cbcSMatt Macy 440*eda14cbcSMatt Macy spa->spa_feat_for_read_obj = zap_create_link(spa->spa_meta_objset, 441*eda14cbcSMatt Macy DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 442*eda14cbcSMatt Macy DMU_POOL_FEATURES_FOR_READ, tx); 443*eda14cbcSMatt Macy spa->spa_feat_for_write_obj = zap_create_link(spa->spa_meta_objset, 444*eda14cbcSMatt Macy DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 445*eda14cbcSMatt Macy DMU_POOL_FEATURES_FOR_WRITE, tx); 446*eda14cbcSMatt Macy spa->spa_feat_desc_obj = zap_create_link(spa->spa_meta_objset, 447*eda14cbcSMatt Macy DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 448*eda14cbcSMatt Macy DMU_POOL_FEATURE_DESCRIPTIONS, tx); 449*eda14cbcSMatt Macy } 450*eda14cbcSMatt Macy 451*eda14cbcSMatt Macy /* 452*eda14cbcSMatt Macy * Enable any required dependencies, then enable the requested feature. 453*eda14cbcSMatt Macy */ 454*eda14cbcSMatt Macy void 455*eda14cbcSMatt Macy spa_feature_enable(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx) 456*eda14cbcSMatt Macy { 457*eda14cbcSMatt Macy ASSERT3U(spa_version(spa), >=, SPA_VERSION_FEATURES); 458*eda14cbcSMatt Macy ASSERT(VALID_FEATURE_FID(fid)); 459*eda14cbcSMatt Macy feature_enable_sync(spa, &spa_feature_table[fid], tx); 460*eda14cbcSMatt Macy } 461*eda14cbcSMatt Macy 462*eda14cbcSMatt Macy void 463*eda14cbcSMatt Macy spa_feature_incr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx) 464*eda14cbcSMatt Macy { 465*eda14cbcSMatt Macy feature_do_action(spa, fid, FEATURE_ACTION_INCR, tx); 466*eda14cbcSMatt Macy } 467*eda14cbcSMatt Macy 468*eda14cbcSMatt Macy void 469*eda14cbcSMatt Macy spa_feature_decr(spa_t *spa, spa_feature_t fid, dmu_tx_t *tx) 470*eda14cbcSMatt Macy { 471*eda14cbcSMatt Macy feature_do_action(spa, fid, FEATURE_ACTION_DECR, tx); 472*eda14cbcSMatt Macy } 473*eda14cbcSMatt Macy 474*eda14cbcSMatt Macy boolean_t 475*eda14cbcSMatt Macy spa_feature_is_enabled(spa_t *spa, spa_feature_t fid) 476*eda14cbcSMatt Macy { 477*eda14cbcSMatt Macy int err; 478*eda14cbcSMatt Macy uint64_t refcount = 0; 479*eda14cbcSMatt Macy 480*eda14cbcSMatt Macy ASSERT(VALID_FEATURE_FID(fid)); 481*eda14cbcSMatt Macy if (spa_version(spa) < SPA_VERSION_FEATURES) 482*eda14cbcSMatt Macy return (B_FALSE); 483*eda14cbcSMatt Macy 484*eda14cbcSMatt Macy err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount); 485*eda14cbcSMatt Macy ASSERT(err == 0 || err == ENOTSUP); 486*eda14cbcSMatt Macy return (err == 0); 487*eda14cbcSMatt Macy } 488*eda14cbcSMatt Macy 489*eda14cbcSMatt Macy boolean_t 490*eda14cbcSMatt Macy spa_feature_is_active(spa_t *spa, spa_feature_t fid) 491*eda14cbcSMatt Macy { 492*eda14cbcSMatt Macy int err; 493*eda14cbcSMatt Macy uint64_t refcount = 0; 494*eda14cbcSMatt Macy 495*eda14cbcSMatt Macy ASSERT(VALID_FEATURE_FID(fid)); 496*eda14cbcSMatt Macy if (spa_version(spa) < SPA_VERSION_FEATURES) 497*eda14cbcSMatt Macy return (B_FALSE); 498*eda14cbcSMatt Macy 499*eda14cbcSMatt Macy err = feature_get_refcount(spa, &spa_feature_table[fid], &refcount); 500*eda14cbcSMatt Macy ASSERT(err == 0 || err == ENOTSUP); 501*eda14cbcSMatt Macy return (err == 0 && refcount > 0); 502*eda14cbcSMatt Macy } 503*eda14cbcSMatt Macy 504*eda14cbcSMatt Macy /* 505*eda14cbcSMatt Macy * For the feature specified by fid (which must depend on 506*eda14cbcSMatt Macy * SPA_FEATURE_ENABLED_TXG), return the TXG at which it was enabled in the 507*eda14cbcSMatt Macy * OUT txg argument. 508*eda14cbcSMatt Macy * 509*eda14cbcSMatt Macy * Returns B_TRUE if the feature is enabled, in which case txg will be filled 510*eda14cbcSMatt Macy * with the transaction group in which the specified feature was enabled. 511*eda14cbcSMatt Macy * Returns B_FALSE otherwise (i.e. if the feature is not enabled). 512*eda14cbcSMatt Macy */ 513*eda14cbcSMatt Macy boolean_t 514*eda14cbcSMatt Macy spa_feature_enabled_txg(spa_t *spa, spa_feature_t fid, uint64_t *txg) 515*eda14cbcSMatt Macy { 516*eda14cbcSMatt Macy int err; 517*eda14cbcSMatt Macy 518*eda14cbcSMatt Macy ASSERT(VALID_FEATURE_FID(fid)); 519*eda14cbcSMatt Macy if (spa_version(spa) < SPA_VERSION_FEATURES) 520*eda14cbcSMatt Macy return (B_FALSE); 521*eda14cbcSMatt Macy 522*eda14cbcSMatt Macy err = feature_get_enabled_txg(spa, &spa_feature_table[fid], txg); 523*eda14cbcSMatt Macy ASSERT(err == 0 || err == ENOTSUP); 524*eda14cbcSMatt Macy 525*eda14cbcSMatt Macy return (err == 0); 526*eda14cbcSMatt Macy } 527