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
94263391Sdelphij	for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
95236884Smm		zfeature_info_t *feature = &spa_feature_table[i];
96263391Sdelphij		if (strcmp(guid, feature->fi_guid) == 0)
97263391Sdelphij			return (B_TRUE);
98236884Smm	}
99263391Sdelphij	return (B_FALSE);
100236884Smm}
101236884Smm
102236884Smmint
103263391Sdelphijzfeature_lookup_name(const char *name, spa_feature_t *res)
104236884Smm{
105263391Sdelphij	for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
106236884Smm		zfeature_info_t *feature = &spa_feature_table[i];
107236884Smm		if (strcmp(name, feature->fi_uname) == 0) {
108236884Smm			if (res != NULL)
109263391Sdelphij				*res = i;
110236884Smm			return (0);
111236884Smm		}
112236884Smm	}
113236884Smm
114236884Smm	return (ENOENT);
115236884Smm}
116236884Smm
117263398Sdelphijboolean_t
118263398Sdelphijzfeature_depends_on(spa_feature_t fid, spa_feature_t check) {
119263398Sdelphij	zfeature_info_t *feature = &spa_feature_table[fid];
120263398Sdelphij
121263398Sdelphij	for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++) {
122263398Sdelphij		if (feature->fi_depends[i] == check)
123263398Sdelphij			return (B_TRUE);
124263398Sdelphij	}
125263398Sdelphij	return (B_FALSE);
126263398Sdelphij}
127263398Sdelphij
128236884Smmstatic void
129263391Sdelphijzfeature_register(spa_feature_t fid, const char *guid, const char *name,
130263391Sdelphij    const char *desc, boolean_t readonly, boolean_t mos,
131263398Sdelphij    boolean_t activate_on_enable, const spa_feature_t *deps)
132236884Smm{
133236884Smm	zfeature_info_t *feature = &spa_feature_table[fid];
134263391Sdelphij	static spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
135236884Smm
136236884Smm	ASSERT(name != NULL);
137236884Smm	ASSERT(desc != NULL);
138236884Smm	ASSERT(!readonly || !mos);
139236884Smm	ASSERT3U(fid, <, SPA_FEATURES);
140236884Smm	ASSERT(zfeature_is_valid_guid(guid));
141236884Smm
142236884Smm	if (deps == NULL)
143236884Smm		deps = nodeps;
144236884Smm
145263391Sdelphij	feature->fi_feature = fid;
146236884Smm	feature->fi_guid = guid;
147236884Smm	feature->fi_uname = name;
148236884Smm	feature->fi_desc = desc;
149236884Smm	feature->fi_can_readonly = readonly;
150236884Smm	feature->fi_mos = mos;
151263398Sdelphij	feature->fi_activate_on_enable = activate_on_enable;
152236884Smm	feature->fi_depends = deps;
153236884Smm}
154236884Smm
155236884Smmvoid
156236884Smmzpool_feature_init(void)
157236884Smm{
158236884Smm	zfeature_register(SPA_FEATURE_ASYNC_DESTROY,
159236884Smm	    "com.delphix:async_destroy", "async_destroy",
160263398Sdelphij	    "Destroy filesystems asynchronously.", B_TRUE, B_FALSE,
161263398Sdelphij	    B_FALSE, NULL);
162263398Sdelphij
163243674Smm	zfeature_register(SPA_FEATURE_EMPTY_BPOBJ,
164243674Smm	    "com.delphix:empty_bpobj", "empty_bpobj",
165263398Sdelphij	    "Snapshots use less space.", B_TRUE, B_FALSE,
166263398Sdelphij	    B_FALSE, NULL);
167263398Sdelphij
168247309Sdelphij	zfeature_register(SPA_FEATURE_LZ4_COMPRESS,
169247309Sdelphij	    "org.illumos:lz4_compress", "lz4_compress",
170263398Sdelphij	    "LZ4 compression algorithm support.", B_FALSE, B_FALSE,
171263398Sdelphij	    B_FALSE, NULL);
172263398Sdelphij
173262089Savg	zfeature_register(SPA_FEATURE_MULTI_VDEV_CRASH_DUMP,
174262089Savg	    "com.joyent:multi_vdev_crash_dump", "multi_vdev_crash_dump",
175263398Sdelphij	    "Crash dumps to multiple vdev pools.", B_FALSE, B_FALSE,
176263398Sdelphij	    B_FALSE, NULL);
177263398Sdelphij
178262094Savg	zfeature_register(SPA_FEATURE_SPACEMAP_HISTOGRAM,
179262094Savg	    "com.delphix:spacemap_histogram", "spacemap_histogram",
180263398Sdelphij	    "Spacemaps maintain space histograms.", B_TRUE, B_FALSE,
181263398Sdelphij	    B_FALSE, NULL);
182263398Sdelphij
183263398Sdelphij	zfeature_register(SPA_FEATURE_ENABLED_TXG,
184263398Sdelphij	    "com.delphix:enabled_txg", "enabled_txg",
185263398Sdelphij	    "Record txg at which a feature is enabled", B_TRUE, B_FALSE,
186263398Sdelphij	    B_FALSE, NULL);
187263398Sdelphij
188263398Sdelphij	static spa_feature_t hole_birth_deps[] = { SPA_FEATURE_ENABLED_TXG,
189263398Sdelphij	    SPA_FEATURE_NONE };
190263398Sdelphij	zfeature_register(SPA_FEATURE_HOLE_BIRTH,
191263398Sdelphij	    "com.delphix:hole_birth", "hole_birth",
192263398Sdelphij	    "Retain hole birth txg for more precise zfs send",
193263398Sdelphij	    B_FALSE, B_TRUE, B_TRUE, hole_birth_deps);
194263398Sdelphij
195263391Sdelphij	zfeature_register(SPA_FEATURE_EXTENSIBLE_DATASET,
196263391Sdelphij	    "com.delphix:extensible_dataset", "extensible_dataset",
197263391Sdelphij	    "Enhanced dataset functionality, used by other features.",
198263398Sdelphij	    B_FALSE, B_FALSE, B_FALSE, NULL);
199263410Sdelphij
200263410Sdelphij	static const spa_feature_t bookmarks_deps[] = {
201263410Sdelphij		SPA_FEATURE_EXTENSIBLE_DATASET,
202263410Sdelphij		SPA_FEATURE_NONE
203263410Sdelphij	};
204263410Sdelphij	zfeature_register(SPA_FEATURE_BOOKMARKS,
205263410Sdelphij	    "com.delphix:bookmarks", "bookmarks",
206263410Sdelphij	    "\"zfs bookmark\" command",
207263410Sdelphij	    B_TRUE, B_FALSE, B_FALSE, bookmarks_deps);
208265754Sdelphij
209265754Sdelphij	static const spa_feature_t filesystem_limits_deps[] = {
210265754Sdelphij	    SPA_FEATURE_EXTENSIBLE_DATASET,
211265754Sdelphij	    SPA_FEATURE_NONE
212265754Sdelphij	};
213265754Sdelphij	zfeature_register(SPA_FEATURE_FS_SS_LIMIT,
214265754Sdelphij	    "com.joyent:filesystem_limits", "filesystem_limits",
215265754Sdelphij	    "Filesystem and snapshot limits.", B_TRUE, B_FALSE, B_FALSE,
216265754Sdelphij	    filesystem_limits_deps);
217236884Smm}
218