zfeature_common.c revision 262094
1236884Smm/* 2236884Smm * CDDL HEADER START 3236884Smm * 4236884Smm * The contents of this file are subject to the terms of the 5236884Smm * Common Development and Distribution License (the "License"). 6236884Smm * You may not use this file except in compliance with the License. 7236884Smm * 8236884Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9236884Smm * or http://www.opensolaris.org/os/licensing. 10236884Smm * See the License for the specific language governing permissions 11236884Smm * and limitations under the License. 12236884Smm * 13236884Smm * When distributing Covered Code, include this CDDL HEADER in each 14236884Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15236884Smm * If applicable, add the following below this CDDL HEADER, with the 16236884Smm * fields enclosed by brackets "[]" replaced with your own identifying 17236884Smm * information: Portions Copyright [yyyy] [name of copyright owner] 18236884Smm * 19236884Smm * CDDL HEADER END 20236884Smm */ 21236884Smm 22236884Smm/* 23262094Savg * Copyright (c) 2013 by Delphix. All rights reserved. 24247309Sdelphij * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 25262089Savg * Copyright (c) 2013, Joyent, Inc. All rights reserved. 26236884Smm */ 27236884Smm 28236884Smm#ifdef _KERNEL 29236884Smm#include <sys/systm.h> 30236884Smm#else 31236884Smm#include <errno.h> 32236884Smm#include <string.h> 33236884Smm#endif 34236884Smm#include <sys/debug.h> 35236884Smm#include <sys/fs/zfs.h> 36236884Smm#include <sys/types.h> 37236884Smm#include "zfeature_common.h" 38236884Smm 39236884Smm/* 40236884Smm * Set to disable all feature checks while opening pools, allowing pools with 41236884Smm * unsupported features to be opened. Set for testing only. 42236884Smm */ 43236884Smmboolean_t zfeature_checks_disable = B_FALSE; 44236884Smm 45236884Smmzfeature_info_t spa_feature_table[SPA_FEATURES]; 46236884Smm 47236884Smm/* 48236884Smm * Valid characters for feature guids. This list is mainly for aesthetic 49236884Smm * purposes and could be expanded in the future. There are different allowed 50236884Smm * characters in the guids reverse dns portion (before the colon) and its 51236884Smm * short name (after the colon). 52236884Smm */ 53236884Smmstatic int 54236884Smmvalid_char(char c, boolean_t after_colon) 55236884Smm{ 56236884Smm return ((c >= 'a' && c <= 'z') || 57236884Smm (c >= '0' && c <= '9') || 58236884Smm c == (after_colon ? '_' : '.')); 59236884Smm} 60236884Smm 61236884Smm/* 62236884Smm * Every feature guid must contain exactly one colon which separates a reverse 63236884Smm * dns organization name from the feature's "short" name (e.g. 64236884Smm * "com.company:feature_name"). 65236884Smm */ 66236884Smmboolean_t 67236884Smmzfeature_is_valid_guid(const char *name) 68236884Smm{ 69236884Smm int i; 70236884Smm boolean_t has_colon = B_FALSE; 71236884Smm 72236884Smm i = 0; 73236884Smm while (name[i] != '\0') { 74236884Smm char c = name[i++]; 75236884Smm if (c == ':') { 76236884Smm if (has_colon) 77236884Smm return (B_FALSE); 78236884Smm has_colon = B_TRUE; 79236884Smm continue; 80236884Smm } 81236884Smm if (!valid_char(c, has_colon)) 82236884Smm return (B_FALSE); 83236884Smm } 84236884Smm 85236884Smm return (has_colon); 86236884Smm} 87236884Smm 88236884Smmboolean_t 89236884Smmzfeature_is_supported(const char *guid) 90236884Smm{ 91236884Smm if (zfeature_checks_disable) 92236884Smm return (B_TRUE); 93236884Smm 94236884Smm return (0 == zfeature_lookup_guid(guid, NULL)); 95236884Smm} 96236884Smm 97236884Smmint 98236884Smmzfeature_lookup_guid(const char *guid, zfeature_info_t **res) 99236884Smm{ 100236884Smm for (int i = 0; i < SPA_FEATURES; i++) { 101236884Smm zfeature_info_t *feature = &spa_feature_table[i]; 102236884Smm if (strcmp(guid, feature->fi_guid) == 0) { 103236884Smm if (res != NULL) 104236884Smm *res = feature; 105236884Smm return (0); 106236884Smm } 107236884Smm } 108236884Smm 109236884Smm return (ENOENT); 110236884Smm} 111236884Smm 112236884Smmint 113236884Smmzfeature_lookup_name(const char *name, zfeature_info_t **res) 114236884Smm{ 115236884Smm for (int i = 0; i < SPA_FEATURES; i++) { 116236884Smm zfeature_info_t *feature = &spa_feature_table[i]; 117236884Smm if (strcmp(name, feature->fi_uname) == 0) { 118236884Smm if (res != NULL) 119236884Smm *res = feature; 120236884Smm return (0); 121236884Smm } 122236884Smm } 123236884Smm 124236884Smm return (ENOENT); 125236884Smm} 126236884Smm 127236884Smmstatic void 128236884Smmzfeature_register(int fid, const char *guid, const char *name, const char *desc, 129236884Smm boolean_t readonly, boolean_t mos, zfeature_info_t **deps) 130236884Smm{ 131236884Smm zfeature_info_t *feature = &spa_feature_table[fid]; 132236884Smm static zfeature_info_t *nodeps[] = { NULL }; 133236884Smm 134236884Smm ASSERT(name != NULL); 135236884Smm ASSERT(desc != NULL); 136236884Smm ASSERT(!readonly || !mos); 137236884Smm ASSERT3U(fid, <, SPA_FEATURES); 138236884Smm ASSERT(zfeature_is_valid_guid(guid)); 139236884Smm 140236884Smm if (deps == NULL) 141236884Smm deps = nodeps; 142236884Smm 143236884Smm feature->fi_guid = guid; 144236884Smm feature->fi_uname = name; 145236884Smm feature->fi_desc = desc; 146236884Smm feature->fi_can_readonly = readonly; 147236884Smm feature->fi_mos = mos; 148236884Smm feature->fi_depends = deps; 149236884Smm} 150236884Smm 151236884Smmvoid 152236884Smmzpool_feature_init(void) 153236884Smm{ 154236884Smm zfeature_register(SPA_FEATURE_ASYNC_DESTROY, 155236884Smm "com.delphix:async_destroy", "async_destroy", 156236884Smm "Destroy filesystems asynchronously.", B_TRUE, B_FALSE, NULL); 157243674Smm zfeature_register(SPA_FEATURE_EMPTY_BPOBJ, 158243674Smm "com.delphix:empty_bpobj", "empty_bpobj", 159243674Smm "Snapshots use less space.", B_TRUE, B_FALSE, NULL); 160247309Sdelphij zfeature_register(SPA_FEATURE_LZ4_COMPRESS, 161247309Sdelphij "org.illumos:lz4_compress", "lz4_compress", 162247309Sdelphij "LZ4 compression algorithm support.", B_FALSE, B_FALSE, NULL); 163262089Savg zfeature_register(SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, 164262089Savg "com.joyent:multi_vdev_crash_dump", "multi_vdev_crash_dump", 165262089Savg "Crash dumps to multiple vdev pools.", B_FALSE, B_FALSE, NULL); 166262094Savg zfeature_register(SPA_FEATURE_SPACEMAP_HISTOGRAM, 167262094Savg "com.delphix:spacemap_histogram", "spacemap_histogram", 168262094Savg "Spacemaps maintain space histograms.", B_TRUE, B_FALSE, NULL); 169236884Smm} 170