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/*
23262093Savg * Copyright (c) 2013 by Delphix. All rights reserved.
24246586Sdelphij * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25255750Sdelphij * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26268658Sdelphij * Copyright (c) 2014, Nexenta Systems, Inc. All rights reserved.
27236884Smm */
28236884Smm
29236884Smm#ifdef _KERNEL
30236884Smm#include <sys/systm.h>
31236884Smm#else
32236884Smm#include <errno.h>
33236884Smm#include <string.h>
34236884Smm#endif
35236884Smm#include <sys/debug.h>
36236884Smm#include <sys/fs/zfs.h>
37236884Smm#include <sys/types.h>
38236884Smm#include "zfeature_common.h"
39236884Smm
40236884Smm/*
41236884Smm * Set to disable all feature checks while opening pools, allowing pools with
42236884Smm * unsupported features to be opened. Set for testing only.
43236884Smm */
44236884Smmboolean_t zfeature_checks_disable = B_FALSE;
45236884Smm
46236884Smmzfeature_info_t spa_feature_table[SPA_FEATURES];
47236884Smm
48236884Smm/*
49236884Smm * Valid characters for feature guids. This list is mainly for aesthetic
50236884Smm * purposes and could be expanded in the future. There are different allowed
51236884Smm * characters in the guids reverse dns portion (before the colon) and its
52236884Smm * short name (after the colon).
53236884Smm */
54236884Smmstatic int
55236884Smmvalid_char(char c, boolean_t after_colon)
56236884Smm{
57236884Smm	return ((c >= 'a' && c <= 'z') ||
58236884Smm	    (c >= '0' && c <= '9') ||
59236884Smm	    c == (after_colon ? '_' : '.'));
60236884Smm}
61236884Smm
62236884Smm/*
63236884Smm * Every feature guid must contain exactly one colon which separates a reverse
64236884Smm * dns organization name from the feature's "short" name (e.g.
65236884Smm * "com.company:feature_name").
66236884Smm */
67236884Smmboolean_t
68236884Smmzfeature_is_valid_guid(const char *name)
69236884Smm{
70236884Smm	int i;
71236884Smm	boolean_t has_colon = B_FALSE;
72236884Smm
73236884Smm	i = 0;
74236884Smm	while (name[i] != '\0') {
75236884Smm		char c = name[i++];
76236884Smm		if (c == ':') {
77236884Smm			if (has_colon)
78236884Smm				return (B_FALSE);
79236884Smm			has_colon = B_TRUE;
80236884Smm			continue;
81236884Smm		}
82236884Smm		if (!valid_char(c, has_colon))
83236884Smm			return (B_FALSE);
84236884Smm	}
85236884Smm
86236884Smm	return (has_colon);
87236884Smm}
88236884Smm
89236884Smmboolean_t
90236884Smmzfeature_is_supported(const char *guid)
91236884Smm{
92236884Smm	if (zfeature_checks_disable)
93236884Smm		return (B_TRUE);
94236884Smm
95263390Sdelphij	for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
96236884Smm		zfeature_info_t *feature = &spa_feature_table[i];
97263390Sdelphij		if (strcmp(guid, feature->fi_guid) == 0)
98263390Sdelphij			return (B_TRUE);
99236884Smm	}
100263390Sdelphij	return (B_FALSE);
101236884Smm}
102236884Smm
103236884Smmint
104263390Sdelphijzfeature_lookup_name(const char *name, spa_feature_t *res)
105236884Smm{
106263390Sdelphij	for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
107236884Smm		zfeature_info_t *feature = &spa_feature_table[i];
108236884Smm		if (strcmp(name, feature->fi_uname) == 0) {
109236884Smm			if (res != NULL)
110263390Sdelphij				*res = i;
111236884Smm			return (0);
112236884Smm		}
113236884Smm	}
114236884Smm
115236884Smm	return (ENOENT);
116236884Smm}
117236884Smm
118263397Sdelphijboolean_t
119263397Sdelphijzfeature_depends_on(spa_feature_t fid, spa_feature_t check) {
120263397Sdelphij	zfeature_info_t *feature = &spa_feature_table[fid];
121263397Sdelphij
122263397Sdelphij	for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++) {
123263397Sdelphij		if (feature->fi_depends[i] == check)
124263397Sdelphij			return (B_TRUE);
125263397Sdelphij	}
126263397Sdelphij	return (B_FALSE);
127263397Sdelphij}
128263397Sdelphij
129236884Smmstatic void
130263390Sdelphijzfeature_register(spa_feature_t fid, const char *guid, const char *name,
131263390Sdelphij    const char *desc, boolean_t readonly, boolean_t mos,
132263397Sdelphij    boolean_t activate_on_enable, const spa_feature_t *deps)
133236884Smm{
134236884Smm	zfeature_info_t *feature = &spa_feature_table[fid];
135263390Sdelphij	static spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
136236884Smm
137236884Smm	ASSERT(name != NULL);
138236884Smm	ASSERT(desc != NULL);
139236884Smm	ASSERT(!readonly || !mos);
140236884Smm	ASSERT3U(fid, <, SPA_FEATURES);
141236884Smm	ASSERT(zfeature_is_valid_guid(guid));
142236884Smm
143236884Smm	if (deps == NULL)
144236884Smm		deps = nodeps;
145236884Smm
146263390Sdelphij	feature->fi_feature = fid;
147236884Smm	feature->fi_guid = guid;
148236884Smm	feature->fi_uname = name;
149236884Smm	feature->fi_desc = desc;
150236884Smm	feature->fi_can_readonly = readonly;
151236884Smm	feature->fi_mos = mos;
152263397Sdelphij	feature->fi_activate_on_enable = activate_on_enable;
153236884Smm	feature->fi_depends = deps;
154236884Smm}
155236884Smm
156236884Smmvoid
157236884Smmzpool_feature_init(void)
158236884Smm{
159236884Smm	zfeature_register(SPA_FEATURE_ASYNC_DESTROY,
160236884Smm	    "com.delphix:async_destroy", "async_destroy",
161263397Sdelphij	    "Destroy filesystems asynchronously.", B_TRUE, B_FALSE,
162263397Sdelphij	    B_FALSE, NULL);
163263397Sdelphij
164239774Smm	zfeature_register(SPA_FEATURE_EMPTY_BPOBJ,
165239774Smm	    "com.delphix:empty_bpobj", "empty_bpobj",
166263397Sdelphij	    "Snapshots use less space.", B_TRUE, B_FALSE,
167263397Sdelphij	    B_FALSE, NULL);
168263397Sdelphij
169246586Sdelphij	zfeature_register(SPA_FEATURE_LZ4_COMPRESS,
170246586Sdelphij	    "org.illumos:lz4_compress", "lz4_compress",
171263397Sdelphij	    "LZ4 compression algorithm support.", B_FALSE, B_FALSE,
172268658Sdelphij	    B_TRUE, NULL);
173263397Sdelphij
174255750Sdelphij	zfeature_register(SPA_FEATURE_MULTI_VDEV_CRASH_DUMP,
175255750Sdelphij	    "com.joyent:multi_vdev_crash_dump", "multi_vdev_crash_dump",
176263397Sdelphij	    "Crash dumps to multiple vdev pools.", B_FALSE, B_FALSE,
177263397Sdelphij	    B_FALSE, NULL);
178263397Sdelphij
179262093Savg	zfeature_register(SPA_FEATURE_SPACEMAP_HISTOGRAM,
180262093Savg	    "com.delphix:spacemap_histogram", "spacemap_histogram",
181263397Sdelphij	    "Spacemaps maintain space histograms.", B_TRUE, B_FALSE,
182263397Sdelphij	    B_FALSE, NULL);
183263397Sdelphij
184263397Sdelphij	zfeature_register(SPA_FEATURE_ENABLED_TXG,
185263397Sdelphij	    "com.delphix:enabled_txg", "enabled_txg",
186263397Sdelphij	    "Record txg at which a feature is enabled", B_TRUE, B_FALSE,
187263397Sdelphij	    B_FALSE, NULL);
188263397Sdelphij
189263397Sdelphij	static spa_feature_t hole_birth_deps[] = { SPA_FEATURE_ENABLED_TXG,
190263397Sdelphij	    SPA_FEATURE_NONE };
191263397Sdelphij	zfeature_register(SPA_FEATURE_HOLE_BIRTH,
192263397Sdelphij	    "com.delphix:hole_birth", "hole_birth",
193263397Sdelphij	    "Retain hole birth txg for more precise zfs send",
194263397Sdelphij	    B_FALSE, B_TRUE, B_TRUE, hole_birth_deps);
195263397Sdelphij
196263390Sdelphij	zfeature_register(SPA_FEATURE_EXTENSIBLE_DATASET,
197263390Sdelphij	    "com.delphix:extensible_dataset", "extensible_dataset",
198263390Sdelphij	    "Enhanced dataset functionality, used by other features.",
199263397Sdelphij	    B_FALSE, B_FALSE, B_FALSE, NULL);
200263407Sdelphij
201263407Sdelphij	static const spa_feature_t bookmarks_deps[] = {
202263407Sdelphij		SPA_FEATURE_EXTENSIBLE_DATASET,
203263407Sdelphij		SPA_FEATURE_NONE
204263407Sdelphij	};
205263407Sdelphij	zfeature_register(SPA_FEATURE_BOOKMARKS,
206263407Sdelphij	    "com.delphix:bookmarks", "bookmarks",
207263407Sdelphij	    "\"zfs bookmark\" command",
208263407Sdelphij	    B_TRUE, B_FALSE, B_FALSE, bookmarks_deps);
209265744Sdelphij
210265744Sdelphij	static const spa_feature_t filesystem_limits_deps[] = {
211265744Sdelphij	    SPA_FEATURE_EXTENSIBLE_DATASET,
212265744Sdelphij	    SPA_FEATURE_NONE
213265744Sdelphij	};
214265744Sdelphij	zfeature_register(SPA_FEATURE_FS_SS_LIMIT,
215265744Sdelphij	    "com.joyent:filesystem_limits", "filesystem_limits",
216265744Sdelphij	    "Filesystem and snapshot limits.", B_TRUE, B_FALSE, B_FALSE,
217265744Sdelphij	    filesystem_limits_deps);
218268649Sdelphij
219268649Sdelphij	zfeature_register(SPA_FEATURE_EMBEDDED_DATA,
220268649Sdelphij	    "com.delphix:embedded_data", "embedded_data",
221268649Sdelphij	    "Blocks which compress very well use even less space.",
222268649Sdelphij	    B_FALSE, B_TRUE, B_TRUE, NULL);
223236884Smm}
224