xref: /illumos-gate/usr/src/common/zfs/zfeature_common.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26  * Copyright (c) 2014, Nexenta Systems, Inc. All rights reserved.
27  * Copyright (c) 2014 Integros [integros.com]
28  * Copyright (c) 2017, Intel Corporation.
29  */
30 
31 #ifdef _KERNEL
32 #include <sys/systm.h>
33 #else
34 #include <errno.h>
35 #include <string.h>
36 #endif
37 #include <sys/debug.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/inttypes.h>
40 #include <sys/types.h>
41 #include "zfeature_common.h"
42 
43 /*
44  * Set to disable all feature checks while opening pools, allowing pools with
45  * unsupported features to be opened. Set for testing only.
46  */
47 boolean_t zfeature_checks_disable = B_FALSE;
48 
49 zfeature_info_t spa_feature_table[SPA_FEATURES];
50 
51 /*
52  * Valid characters for feature guids. This list is mainly for aesthetic
53  * purposes and could be expanded in the future. There are different allowed
54  * characters in the guids reverse dns portion (before the colon) and its
55  * short name (after the colon).
56  */
57 static int
58 valid_char(char c, boolean_t after_colon)
59 {
60 	return ((c >= 'a' && c <= 'z') ||
61 	    (c >= '0' && c <= '9') ||
62 	    (after_colon && c == '_') ||
63 	    (!after_colon && (c == '.' || c == '-')));
64 }
65 
66 /*
67  * Every feature guid must contain exactly one colon which separates a reverse
68  * dns organization name from the feature's "short" name (e.g.
69  * "com.company:feature_name").
70  */
71 boolean_t
72 zfeature_is_valid_guid(const char *name)
73 {
74 	int i;
75 	boolean_t has_colon = B_FALSE;
76 
77 	i = 0;
78 	while (name[i] != '\0') {
79 		char c = name[i++];
80 		if (c == ':') {
81 			if (has_colon)
82 				return (B_FALSE);
83 			has_colon = B_TRUE;
84 			continue;
85 		}
86 		if (!valid_char(c, has_colon))
87 			return (B_FALSE);
88 	}
89 
90 	return (has_colon);
91 }
92 
93 boolean_t
94 zfeature_is_supported(const char *guid)
95 {
96 	if (zfeature_checks_disable)
97 		return (B_TRUE);
98 
99 	for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
100 		zfeature_info_t *feature = &spa_feature_table[i];
101 		if (strcmp(guid, feature->fi_guid) == 0)
102 			return (B_TRUE);
103 	}
104 	return (B_FALSE);
105 }
106 
107 int
108 zfeature_lookup_name(const char *name, spa_feature_t *res)
109 {
110 	for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
111 		zfeature_info_t *feature = &spa_feature_table[i];
112 		if (strcmp(name, feature->fi_uname) == 0) {
113 			if (res != NULL)
114 				*res = i;
115 			return (0);
116 		}
117 	}
118 
119 	return (ENOENT);
120 }
121 
122 boolean_t
123 zfeature_depends_on(spa_feature_t fid, spa_feature_t check)
124 {
125 	zfeature_info_t *feature = &spa_feature_table[fid];
126 
127 	for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++) {
128 		if (feature->fi_depends[i] == check)
129 			return (B_TRUE);
130 	}
131 	return (B_FALSE);
132 }
133 
134 static boolean_t
135 deps_contains_feature(const spa_feature_t *deps, const spa_feature_t feature)
136 {
137 	for (int i = 0; deps[i] != SPA_FEATURE_NONE; i++)
138 		if (deps[i] == feature)
139 			return (B_TRUE);
140 
141 	return (B_FALSE);
142 }
143 
144 static void
145 zfeature_register(spa_feature_t fid, const char *guid, const char *name,
146     const char *desc, zfeature_flags_t flags, const spa_feature_t *deps)
147 {
148 	zfeature_info_t *feature = &spa_feature_table[fid];
149 	static spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
150 
151 	ASSERT(name != NULL);
152 	ASSERT(desc != NULL);
153 	ASSERT((flags & ZFEATURE_FLAG_READONLY_COMPAT) == 0 ||
154 	    (flags & ZFEATURE_FLAG_MOS) == 0);
155 	ASSERT3U(fid, <, SPA_FEATURES);
156 	ASSERT(zfeature_is_valid_guid(guid));
157 
158 	if (deps == NULL)
159 		deps = nodeps;
160 
161 	VERIFY(((flags & ZFEATURE_FLAG_PER_DATASET) == 0) ||
162 	    (deps_contains_feature(deps, SPA_FEATURE_EXTENSIBLE_DATASET)));
163 
164 	feature->fi_feature = fid;
165 	feature->fi_guid = guid;
166 	feature->fi_uname = name;
167 	feature->fi_desc = desc;
168 	feature->fi_flags = flags;
169 	feature->fi_depends = deps;
170 }
171 
172 void
173 zpool_feature_init(void)
174 {
175 	zfeature_register(SPA_FEATURE_ASYNC_DESTROY,
176 	    "com.delphix:async_destroy", "async_destroy",
177 	    "Destroy filesystems asynchronously.",
178 	    ZFEATURE_FLAG_READONLY_COMPAT, NULL);
179 
180 	zfeature_register(SPA_FEATURE_EMPTY_BPOBJ,
181 	    "com.delphix:empty_bpobj", "empty_bpobj",
182 	    "Snapshots use less space.",
183 	    ZFEATURE_FLAG_READONLY_COMPAT, NULL);
184 
185 	zfeature_register(SPA_FEATURE_LZ4_COMPRESS,
186 	    "org.illumos:lz4_compress", "lz4_compress",
187 	    "LZ4 compression algorithm support.",
188 	    ZFEATURE_FLAG_ACTIVATE_ON_ENABLE, NULL);
189 
190 	zfeature_register(SPA_FEATURE_MULTI_VDEV_CRASH_DUMP,
191 	    "com.joyent:multi_vdev_crash_dump", "multi_vdev_crash_dump",
192 	    "Crash dumps to multiple vdev pools.",
193 	    0, NULL);
194 
195 	zfeature_register(SPA_FEATURE_SPACEMAP_HISTOGRAM,
196 	    "com.delphix:spacemap_histogram", "spacemap_histogram",
197 	    "Spacemaps maintain space histograms.",
198 	    ZFEATURE_FLAG_READONLY_COMPAT, NULL);
199 
200 	zfeature_register(SPA_FEATURE_ENABLED_TXG,
201 	    "com.delphix:enabled_txg", "enabled_txg",
202 	    "Record txg at which a feature is enabled",
203 	    ZFEATURE_FLAG_READONLY_COMPAT, NULL);
204 
205 	static spa_feature_t hole_birth_deps[] = {
206 		SPA_FEATURE_ENABLED_TXG,
207 		SPA_FEATURE_NONE
208 	};
209 	zfeature_register(SPA_FEATURE_HOLE_BIRTH,
210 	    "com.delphix:hole_birth", "hole_birth",
211 	    "Retain hole birth txg for more precise zfs send",
212 	    ZFEATURE_FLAG_MOS | ZFEATURE_FLAG_ACTIVATE_ON_ENABLE,
213 	    hole_birth_deps);
214 
215 	zfeature_register(SPA_FEATURE_EXTENSIBLE_DATASET,
216 	    "com.delphix:extensible_dataset", "extensible_dataset",
217 	    "Enhanced dataset functionality, used by other features.",
218 	    0, NULL);
219 
220 	static const spa_feature_t bookmarks_deps[] = {
221 		SPA_FEATURE_EXTENSIBLE_DATASET,
222 		SPA_FEATURE_NONE
223 	};
224 	zfeature_register(SPA_FEATURE_BOOKMARKS,
225 	    "com.delphix:bookmarks", "bookmarks",
226 	    "\"zfs bookmark\" command",
227 	    ZFEATURE_FLAG_READONLY_COMPAT, bookmarks_deps);
228 
229 	static const spa_feature_t filesystem_limits_deps[] = {
230 		SPA_FEATURE_EXTENSIBLE_DATASET,
231 		SPA_FEATURE_NONE
232 	};
233 	zfeature_register(SPA_FEATURE_FS_SS_LIMIT,
234 	    "com.joyent:filesystem_limits", "filesystem_limits",
235 	    "Filesystem and snapshot limits.",
236 	    ZFEATURE_FLAG_READONLY_COMPAT, filesystem_limits_deps);
237 
238 	zfeature_register(SPA_FEATURE_EMBEDDED_DATA,
239 	    "com.delphix:embedded_data", "embedded_data",
240 	    "Blocks which compress very well use even less space.",
241 	    ZFEATURE_FLAG_MOS | ZFEATURE_FLAG_ACTIVATE_ON_ENABLE,
242 	    NULL);
243 
244 	zfeature_register(SPA_FEATURE_POOL_CHECKPOINT,
245 	    "com.delphix:zpool_checkpoint", "zpool_checkpoint",
246 	    "Pool state can be checkpointed, allowing rewind later.",
247 	    ZFEATURE_FLAG_READONLY_COMPAT, NULL);
248 
249 	zfeature_register(SPA_FEATURE_SPACEMAP_V2,
250 	    "com.delphix:spacemap_v2", "spacemap_v2",
251 	    "Space maps representing large segments are more efficient.",
252 	    ZFEATURE_FLAG_READONLY_COMPAT | ZFEATURE_FLAG_ACTIVATE_ON_ENABLE,
253 	    NULL);
254 
255 	static const spa_feature_t large_blocks_deps[] = {
256 		SPA_FEATURE_EXTENSIBLE_DATASET,
257 		SPA_FEATURE_NONE
258 	};
259 	zfeature_register(SPA_FEATURE_LARGE_BLOCKS,
260 	    "org.open-zfs:large_blocks", "large_blocks",
261 	    "Support for blocks larger than 128KB.",
262 	    ZFEATURE_FLAG_PER_DATASET, large_blocks_deps);
263 
264 	{
265 	static const spa_feature_t bookmark_v2_deps[] = {
266 		SPA_FEATURE_EXTENSIBLE_DATASET,
267 		SPA_FEATURE_BOOKMARKS,
268 		SPA_FEATURE_NONE
269 	};
270 	zfeature_register(SPA_FEATURE_BOOKMARK_V2,
271 	    "com.datto:bookmark_v2", "bookmark_v2",
272 	    "Support for larger bookmarks",
273 	    ZFEATURE_FLAG_PER_DATASET, bookmark_v2_deps);
274 	}
275 
276 	{
277 	static const spa_feature_t large_dnode_deps[] = {
278 		SPA_FEATURE_EXTENSIBLE_DATASET,
279 		SPA_FEATURE_NONE
280 	};
281 	zfeature_register(SPA_FEATURE_LARGE_DNODE,
282 	    "org.zfsonlinux:large_dnode", "large_dnode",
283 	    "Variable on-disk size of dnodes.",
284 	    ZFEATURE_FLAG_PER_DATASET, large_dnode_deps);
285 	}
286 
287 	static const spa_feature_t sha512_deps[] = {
288 		SPA_FEATURE_EXTENSIBLE_DATASET,
289 		SPA_FEATURE_NONE
290 	};
291 	zfeature_register(SPA_FEATURE_SHA512,
292 	    "org.illumos:sha512", "sha512",
293 	    "SHA-512/256 hash algorithm.",
294 	    ZFEATURE_FLAG_PER_DATASET, sha512_deps);
295 
296 	static const spa_feature_t skein_deps[] = {
297 		SPA_FEATURE_EXTENSIBLE_DATASET,
298 		SPA_FEATURE_NONE
299 	};
300 	zfeature_register(SPA_FEATURE_SKEIN,
301 	    "org.illumos:skein", "skein",
302 	    "Skein hash algorithm.",
303 	    ZFEATURE_FLAG_PER_DATASET, skein_deps);
304 
305 	static const spa_feature_t edonr_deps[] = {
306 		SPA_FEATURE_EXTENSIBLE_DATASET,
307 		SPA_FEATURE_NONE
308 	};
309 	zfeature_register(SPA_FEATURE_EDONR,
310 	    "org.illumos:edonr", "edonr",
311 	    "Edon-R hash algorithm.",
312 	    ZFEATURE_FLAG_PER_DATASET, edonr_deps);
313 
314 	zfeature_register(SPA_FEATURE_DEVICE_REMOVAL,
315 	    "com.delphix:device_removal", "device_removal",
316 	    "Top-level vdevs can be removed, reducing logical pool size.",
317 	    ZFEATURE_FLAG_MOS, NULL);
318 
319 	static const spa_feature_t obsolete_counts_deps[] = {
320 		SPA_FEATURE_EXTENSIBLE_DATASET,
321 		SPA_FEATURE_DEVICE_REMOVAL,
322 		SPA_FEATURE_NONE
323 	};
324 	zfeature_register(SPA_FEATURE_OBSOLETE_COUNTS,
325 	    "com.delphix:obsolete_counts", "obsolete_counts",
326 	    "Reduce memory used by removed devices when their blocks are "
327 	    "freed or remapped.",
328 	    ZFEATURE_FLAG_READONLY_COMPAT, obsolete_counts_deps);
329 
330 	zfeature_register(SPA_FEATURE_ALLOCATION_CLASSES,
331 	    "org.zfsonlinux:allocation_classes", "allocation_classes",
332 	    "Support for separate allocation classes.",
333 	    ZFEATURE_FLAG_READONLY_COMPAT, NULL);
334 
335 	zfeature_register(SPA_FEATURE_RESILVER_DEFER,
336 	    "com.datto:resilver_defer", "resilver_defer",
337 	    "Support for defering new resilvers when one is already running.",
338 	    ZFEATURE_FLAG_READONLY_COMPAT, NULL);
339 
340 	static const spa_feature_t encryption_deps[] = {
341 		SPA_FEATURE_EXTENSIBLE_DATASET,
342 		SPA_FEATURE_BOOKMARK_V2,
343 		SPA_FEATURE_NONE
344 	};
345 	zfeature_register(SPA_FEATURE_ENCRYPTION,
346 	    "com.datto:encryption", "encryption",
347 	    "Support for dataset level encryption",
348 	    ZFEATURE_FLAG_PER_DATASET, encryption_deps);
349 
350 	static const spa_feature_t userobj_accounting_deps[] = {
351 		SPA_FEATURE_EXTENSIBLE_DATASET,
352 		SPA_FEATURE_NONE
353 	};
354 	zfeature_register(SPA_FEATURE_USEROBJ_ACCOUNTING,
355 	    "org.zfsonlinux:userobj_accounting", "userobj_accounting",
356 	    "User/Group object accounting.",
357 	    ZFEATURE_FLAG_READONLY_COMPAT | ZFEATURE_FLAG_PER_DATASET,
358 	    userobj_accounting_deps);
359 
360 	static const spa_feature_t project_quota_deps[] = {
361 		SPA_FEATURE_EXTENSIBLE_DATASET,
362 		SPA_FEATURE_NONE
363 	};
364 	zfeature_register(SPA_FEATURE_PROJECT_QUOTA,
365 	    "org.zfsonlinux:project_quota", "project_quota",
366 	    "space/object accounting based on project ID.",
367 	    ZFEATURE_FLAG_READONLY_COMPAT | ZFEATURE_FLAG_PER_DATASET,
368 	    project_quota_deps);
369 
370 	static const spa_feature_t log_spacemap_deps[] = {
371 		SPA_FEATURE_SPACEMAP_V2,
372 		SPA_FEATURE_NONE
373 	};
374 	zfeature_register(SPA_FEATURE_LOG_SPACEMAP,
375 	    "com.delphix:log_spacemap", "log_spacemap",
376 	    "Log metaslab changes on a single spacemap and "
377 	    "flush them periodically.",
378 	    ZFEATURE_FLAG_READONLY_COMPAT,
379 	    log_spacemap_deps);
380 }
381