]> git.proxmox.com Git - mirror_zfs.git/blob - module/zcommon/zfeature_common.c
Detect and prevent mixed raw and non-raw sends
[mirror_zfs.git] / module / zcommon / zfeature_common.c
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) 2017, Intel Corporation.
28 */
29
30 #ifndef _KERNEL
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #endif
35 #include <sys/debug.h>
36 #include <sys/fs/zfs.h>
37 #include <sys/inttypes.h>
38 #include <sys/types.h>
39 #include <sys/zfs_sysfs.h>
40 #include "zfeature_common.h"
41
42 /*
43 * Set to disable all feature checks while opening pools, allowing pools with
44 * unsupported features to be opened. Set for testing only.
45 */
46 boolean_t zfeature_checks_disable = B_FALSE;
47
48 zfeature_info_t spa_feature_table[SPA_FEATURES];
49
50 /*
51 * Valid characters for feature guids. This list is mainly for aesthetic
52 * purposes and could be expanded in the future. There are different allowed
53 * characters in the guids reverse dns portion (before the colon) and its
54 * short name (after the colon).
55 */
56 static int
57 valid_char(char c, boolean_t after_colon)
58 {
59 return ((c >= 'a' && c <= 'z') ||
60 (c >= '0' && c <= '9') ||
61 (after_colon && c == '_') ||
62 (!after_colon && (c == '.' || c == '-')));
63 }
64
65 /*
66 * Every feature guid must contain exactly one colon which separates a reverse
67 * dns organization name from the feature's "short" name (e.g.
68 * "com.company:feature_name").
69 */
70 boolean_t
71 zfeature_is_valid_guid(const char *name)
72 {
73 int i;
74 boolean_t has_colon = B_FALSE;
75
76 i = 0;
77 while (name[i] != '\0') {
78 char c = name[i++];
79 if (c == ':') {
80 if (has_colon)
81 return (B_FALSE);
82 has_colon = B_TRUE;
83 continue;
84 }
85 if (!valid_char(c, has_colon))
86 return (B_FALSE);
87 }
88
89 return (has_colon);
90 }
91
92 boolean_t
93 zfeature_is_supported(const char *guid)
94 {
95 if (zfeature_checks_disable)
96 return (B_TRUE);
97
98 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
99 zfeature_info_t *feature = &spa_feature_table[i];
100 if (strcmp(guid, feature->fi_guid) == 0)
101 return (B_TRUE);
102 }
103 return (B_FALSE);
104 }
105
106 int
107 zfeature_lookup_guid(const char *guid, spa_feature_t *res)
108 {
109 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
110 zfeature_info_t *feature = &spa_feature_table[i];
111 if (!feature->fi_zfs_mod_supported)
112 continue;
113 if (strcmp(guid, feature->fi_guid) == 0) {
114 if (res != NULL)
115 *res = i;
116 return (0);
117 }
118 }
119
120 return (ENOENT);
121 }
122
123 int
124 zfeature_lookup_name(const char *name, spa_feature_t *res)
125 {
126 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
127 zfeature_info_t *feature = &spa_feature_table[i];
128 if (!feature->fi_zfs_mod_supported)
129 continue;
130 if (strcmp(name, feature->fi_uname) == 0) {
131 if (res != NULL)
132 *res = i;
133 return (0);
134 }
135 }
136
137 return (ENOENT);
138 }
139
140 boolean_t
141 zfeature_depends_on(spa_feature_t fid, spa_feature_t check)
142 {
143 zfeature_info_t *feature = &spa_feature_table[fid];
144
145 for (int i = 0; feature->fi_depends[i] != SPA_FEATURE_NONE; i++) {
146 if (feature->fi_depends[i] == check)
147 return (B_TRUE);
148 }
149 return (B_FALSE);
150 }
151
152 static boolean_t
153 deps_contains_feature(const spa_feature_t *deps, const spa_feature_t feature)
154 {
155 for (int i = 0; deps[i] != SPA_FEATURE_NONE; i++)
156 if (deps[i] == feature)
157 return (B_TRUE);
158
159 return (B_FALSE);
160 }
161
162 #if !defined(_KERNEL) && !defined(LIB_ZPOOL_BUILD)
163 static boolean_t
164 zfs_mod_supported_impl(const char *scope, const char *name, const char *sysfs)
165 {
166 boolean_t supported = B_FALSE;
167 char *path;
168
169 int len = asprintf(&path, "%s%s%s%s%s", sysfs,
170 scope == NULL ? "" : "/", scope == NULL ? "" : scope,
171 name == NULL ? "" : "/", name == NULL ? "" : name);
172 if (len > 0) {
173 struct stat64 statbuf;
174 supported = !!(stat64(path, &statbuf) == 0);
175 free(path);
176 }
177
178 return (supported);
179 }
180
181 boolean_t
182 zfs_mod_supported(const char *scope, const char *name)
183 {
184 boolean_t supported;
185
186 /*
187 * Check both the primary and alternate sysfs locations to determine
188 * if the required functionality is supported.
189 */
190 supported = (zfs_mod_supported_impl(scope, name, ZFS_SYSFS_DIR) ||
191 zfs_mod_supported_impl(scope, name, ZFS_SYSFS_ALT_DIR));
192
193 /*
194 * For backwards compatibility with kernel modules that predate
195 * supported feature/property checking. Report the feature/property
196 * as supported if the kernel module is loaded but the requested
197 * scope directory does not exist.
198 */
199 if (supported == B_FALSE) {
200 struct stat64 statbuf;
201 if ((stat64(ZFS_SYSFS_DIR, &statbuf) == 0) &&
202 !zfs_mod_supported_impl(scope, NULL, ZFS_SYSFS_DIR) &&
203 !zfs_mod_supported_impl(scope, NULL, ZFS_SYSFS_ALT_DIR)) {
204 supported = B_TRUE;
205 }
206 }
207
208 return (supported);
209 }
210 #endif
211
212 static boolean_t
213 zfs_mod_supported_feature(const char *name)
214 {
215 /*
216 * The zfs module spa_feature_table[], whether in-kernel or in
217 * libzpool, always supports all the features. libzfs needs to
218 * query the running module, via sysfs, to determine which
219 * features are supported.
220 */
221 #if defined(_KERNEL) || defined(LIB_ZPOOL_BUILD)
222 return (B_TRUE);
223 #else
224 return (zfs_mod_supported(ZFS_SYSFS_POOL_FEATURES, name));
225 #endif
226 }
227
228 static void
229 zfeature_register(spa_feature_t fid, const char *guid, const char *name,
230 const char *desc, zfeature_flags_t flags, zfeature_type_t type,
231 const spa_feature_t *deps)
232 {
233 zfeature_info_t *feature = &spa_feature_table[fid];
234 static spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
235
236 ASSERT(name != NULL);
237 ASSERT(desc != NULL);
238 ASSERT((flags & ZFEATURE_FLAG_READONLY_COMPAT) == 0 ||
239 (flags & ZFEATURE_FLAG_MOS) == 0);
240 ASSERT3U(fid, <, SPA_FEATURES);
241 ASSERT(zfeature_is_valid_guid(guid));
242
243 if (deps == NULL)
244 deps = nodeps;
245
246 VERIFY(((flags & ZFEATURE_FLAG_PER_DATASET) == 0) ||
247 (deps_contains_feature(deps, SPA_FEATURE_EXTENSIBLE_DATASET)));
248
249 feature->fi_feature = fid;
250 feature->fi_guid = guid;
251 feature->fi_uname = name;
252 feature->fi_desc = desc;
253 feature->fi_flags = flags;
254 feature->fi_type = type;
255 feature->fi_depends = deps;
256 feature->fi_zfs_mod_supported = zfs_mod_supported_feature(guid);
257 }
258
259 void
260 zpool_feature_init(void)
261 {
262 zfeature_register(SPA_FEATURE_ASYNC_DESTROY,
263 "com.delphix:async_destroy", "async_destroy",
264 "Destroy filesystems asynchronously.",
265 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN, NULL);
266
267 zfeature_register(SPA_FEATURE_EMPTY_BPOBJ,
268 "com.delphix:empty_bpobj", "empty_bpobj",
269 "Snapshots use less space.",
270 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN, NULL);
271
272 zfeature_register(SPA_FEATURE_LZ4_COMPRESS,
273 "org.illumos:lz4_compress", "lz4_compress",
274 "LZ4 compression algorithm support.",
275 ZFEATURE_FLAG_ACTIVATE_ON_ENABLE, ZFEATURE_TYPE_BOOLEAN, NULL);
276
277 zfeature_register(SPA_FEATURE_MULTI_VDEV_CRASH_DUMP,
278 "com.joyent:multi_vdev_crash_dump", "multi_vdev_crash_dump",
279 "Crash dumps to multiple vdev pools.",
280 0, ZFEATURE_TYPE_BOOLEAN, NULL);
281
282 zfeature_register(SPA_FEATURE_SPACEMAP_HISTOGRAM,
283 "com.delphix:spacemap_histogram", "spacemap_histogram",
284 "Spacemaps maintain space histograms.",
285 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN, NULL);
286
287 zfeature_register(SPA_FEATURE_ENABLED_TXG,
288 "com.delphix:enabled_txg", "enabled_txg",
289 "Record txg at which a feature is enabled",
290 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN, NULL);
291
292 {
293 static const spa_feature_t hole_birth_deps[] = {
294 SPA_FEATURE_ENABLED_TXG,
295 SPA_FEATURE_NONE
296 };
297 zfeature_register(SPA_FEATURE_HOLE_BIRTH,
298 "com.delphix:hole_birth", "hole_birth",
299 "Retain hole birth txg for more precise zfs send",
300 ZFEATURE_FLAG_MOS | ZFEATURE_FLAG_ACTIVATE_ON_ENABLE,
301 ZFEATURE_TYPE_BOOLEAN, hole_birth_deps);
302 }
303
304 zfeature_register(SPA_FEATURE_POOL_CHECKPOINT,
305 "com.delphix:zpool_checkpoint", "zpool_checkpoint",
306 "Pool state can be checkpointed, allowing rewind later.",
307 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN, NULL);
308
309 zfeature_register(SPA_FEATURE_SPACEMAP_V2,
310 "com.delphix:spacemap_v2", "spacemap_v2",
311 "Space maps representing large segments are more efficient.",
312 ZFEATURE_FLAG_READONLY_COMPAT | ZFEATURE_FLAG_ACTIVATE_ON_ENABLE,
313 ZFEATURE_TYPE_BOOLEAN, NULL);
314
315 zfeature_register(SPA_FEATURE_EXTENSIBLE_DATASET,
316 "com.delphix:extensible_dataset", "extensible_dataset",
317 "Enhanced dataset functionality, used by other features.",
318 0, ZFEATURE_TYPE_BOOLEAN, NULL);
319
320 {
321 static const spa_feature_t bookmarks_deps[] = {
322 SPA_FEATURE_EXTENSIBLE_DATASET,
323 SPA_FEATURE_NONE
324 };
325
326 zfeature_register(SPA_FEATURE_BOOKMARKS,
327 "com.delphix:bookmarks", "bookmarks",
328 "\"zfs bookmark\" command",
329 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN,
330 bookmarks_deps);
331 }
332
333 {
334 static const spa_feature_t filesystem_limits_deps[] = {
335 SPA_FEATURE_EXTENSIBLE_DATASET,
336 SPA_FEATURE_NONE
337 };
338 zfeature_register(SPA_FEATURE_FS_SS_LIMIT,
339 "com.joyent:filesystem_limits", "filesystem_limits",
340 "Filesystem and snapshot limits.",
341 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN,
342 filesystem_limits_deps);
343 }
344
345 zfeature_register(SPA_FEATURE_EMBEDDED_DATA,
346 "com.delphix:embedded_data", "embedded_data",
347 "Blocks which compress very well use even less space.",
348 ZFEATURE_FLAG_MOS | ZFEATURE_FLAG_ACTIVATE_ON_ENABLE,
349 ZFEATURE_TYPE_BOOLEAN, NULL);
350
351 {
352 static const spa_feature_t large_blocks_deps[] = {
353 SPA_FEATURE_EXTENSIBLE_DATASET,
354 SPA_FEATURE_NONE
355 };
356 zfeature_register(SPA_FEATURE_LARGE_BLOCKS,
357 "org.open-zfs:large_blocks", "large_blocks",
358 "Support for blocks larger than 128KB.",
359 ZFEATURE_FLAG_PER_DATASET, ZFEATURE_TYPE_BOOLEAN,
360 large_blocks_deps);
361 }
362
363 {
364 static const spa_feature_t large_dnode_deps[] = {
365 SPA_FEATURE_EXTENSIBLE_DATASET,
366 SPA_FEATURE_NONE
367 };
368 zfeature_register(SPA_FEATURE_LARGE_DNODE,
369 "org.zfsonlinux:large_dnode", "large_dnode",
370 "Variable on-disk size of dnodes.",
371 ZFEATURE_FLAG_PER_DATASET, ZFEATURE_TYPE_BOOLEAN,
372 large_dnode_deps);
373 }
374
375 {
376 static const spa_feature_t sha512_deps[] = {
377 SPA_FEATURE_EXTENSIBLE_DATASET,
378 SPA_FEATURE_NONE
379 };
380 zfeature_register(SPA_FEATURE_SHA512,
381 "org.illumos:sha512", "sha512",
382 "SHA-512/256 hash algorithm.",
383 ZFEATURE_FLAG_PER_DATASET, ZFEATURE_TYPE_BOOLEAN,
384 sha512_deps);
385 }
386
387 {
388 static const spa_feature_t skein_deps[] = {
389 SPA_FEATURE_EXTENSIBLE_DATASET,
390 SPA_FEATURE_NONE
391 };
392 zfeature_register(SPA_FEATURE_SKEIN,
393 "org.illumos:skein", "skein",
394 "Skein hash algorithm.",
395 ZFEATURE_FLAG_PER_DATASET, ZFEATURE_TYPE_BOOLEAN,
396 skein_deps);
397 }
398
399 {
400 static const spa_feature_t edonr_deps[] = {
401 SPA_FEATURE_EXTENSIBLE_DATASET,
402 SPA_FEATURE_NONE
403 };
404 zfeature_register(SPA_FEATURE_EDONR,
405 "org.illumos:edonr", "edonr",
406 "Edon-R hash algorithm.",
407 ZFEATURE_FLAG_PER_DATASET, ZFEATURE_TYPE_BOOLEAN,
408 edonr_deps);
409 }
410
411 zfeature_register(SPA_FEATURE_DEVICE_REMOVAL,
412 "com.delphix:device_removal", "device_removal",
413 "Top-level vdevs can be removed, reducing logical pool size.",
414 ZFEATURE_FLAG_MOS, ZFEATURE_TYPE_BOOLEAN, NULL);
415
416 {
417 static const spa_feature_t obsolete_counts_deps[] = {
418 SPA_FEATURE_EXTENSIBLE_DATASET,
419 SPA_FEATURE_DEVICE_REMOVAL,
420 SPA_FEATURE_NONE
421 };
422 zfeature_register(SPA_FEATURE_OBSOLETE_COUNTS,
423 "com.delphix:obsolete_counts", "obsolete_counts",
424 "Reduce memory used by removed devices when their blocks are "
425 "freed or remapped.",
426 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN,
427 obsolete_counts_deps);
428 }
429
430 {
431 static const spa_feature_t userobj_accounting_deps[] = {
432 SPA_FEATURE_EXTENSIBLE_DATASET,
433 SPA_FEATURE_NONE
434 };
435 zfeature_register(SPA_FEATURE_USEROBJ_ACCOUNTING,
436 "org.zfsonlinux:userobj_accounting", "userobj_accounting",
437 "User/Group object accounting.",
438 ZFEATURE_FLAG_READONLY_COMPAT | ZFEATURE_FLAG_PER_DATASET,
439 ZFEATURE_TYPE_BOOLEAN, userobj_accounting_deps);
440 }
441
442 {
443 static const spa_feature_t bookmark_v2_deps[] = {
444 SPA_FEATURE_EXTENSIBLE_DATASET,
445 SPA_FEATURE_BOOKMARKS,
446 SPA_FEATURE_NONE
447 };
448 zfeature_register(SPA_FEATURE_BOOKMARK_V2,
449 "com.datto:bookmark_v2", "bookmark_v2",
450 "Support for larger bookmarks",
451 0, ZFEATURE_TYPE_BOOLEAN, bookmark_v2_deps);
452 }
453
454 {
455 static const spa_feature_t encryption_deps[] = {
456 SPA_FEATURE_EXTENSIBLE_DATASET,
457 SPA_FEATURE_BOOKMARK_V2,
458 SPA_FEATURE_NONE
459 };
460 zfeature_register(SPA_FEATURE_ENCRYPTION,
461 "com.datto:encryption", "encryption",
462 "Support for dataset level encryption",
463 ZFEATURE_FLAG_PER_DATASET, ZFEATURE_TYPE_BOOLEAN,
464 encryption_deps);
465 }
466
467 {
468 static const spa_feature_t project_quota_deps[] = {
469 SPA_FEATURE_EXTENSIBLE_DATASET,
470 SPA_FEATURE_NONE
471 };
472 zfeature_register(SPA_FEATURE_PROJECT_QUOTA,
473 "org.zfsonlinux:project_quota", "project_quota",
474 "space/object accounting based on project ID.",
475 ZFEATURE_FLAG_READONLY_COMPAT | ZFEATURE_FLAG_PER_DATASET,
476 ZFEATURE_TYPE_BOOLEAN, project_quota_deps);
477 }
478
479 {
480 zfeature_register(SPA_FEATURE_ALLOCATION_CLASSES,
481 "org.zfsonlinux:allocation_classes", "allocation_classes",
482 "Support for separate allocation classes.",
483 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN, NULL);
484 }
485
486 zfeature_register(SPA_FEATURE_RESILVER_DEFER,
487 "com.datto:resilver_defer", "resilver_defer",
488 "Support for defering new resilvers when one is already running.",
489 ZFEATURE_FLAG_READONLY_COMPAT, ZFEATURE_TYPE_BOOLEAN, NULL);
490 }
491
492 #if defined(_KERNEL)
493 EXPORT_SYMBOL(zfeature_lookup_guid);
494 EXPORT_SYMBOL(zfeature_lookup_name);
495 EXPORT_SYMBOL(zfeature_is_supported);
496 EXPORT_SYMBOL(zfeature_is_valid_guid);
497 EXPORT_SYMBOL(zfeature_depends_on);
498 EXPORT_SYMBOL(zpool_feature_init);
499 EXPORT_SYMBOL(spa_feature_table);
500 #endif