zfeature_common.c revision 246586
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/*
23236884Smm * Copyright (c) 2012 by Delphix. All rights reserved.
24246586Sdelphij * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25236884Smm */
26236884Smm
27236884Smm#ifdef _KERNEL
28236884Smm#include <sys/systm.h>
29236884Smm#else
30236884Smm#include <errno.h>
31236884Smm#include <string.h>
32236884Smm#endif
33236884Smm#include <sys/debug.h>
34236884Smm#include <sys/fs/zfs.h>
35236884Smm#include <sys/types.h>
36236884Smm#include "zfeature_common.h"
37236884Smm
38236884Smm/*
39236884Smm * Set to disable all feature checks while opening pools, allowing pools with
40236884Smm * unsupported features to be opened. Set for testing only.
41236884Smm */
42236884Smmboolean_t zfeature_checks_disable = B_FALSE;
43236884Smm
44236884Smmzfeature_info_t spa_feature_table[SPA_FEATURES];
45236884Smm
46236884Smm/*
47236884Smm * Valid characters for feature guids. This list is mainly for aesthetic
48236884Smm * purposes and could be expanded in the future. There are different allowed
49236884Smm * characters in the guids reverse dns portion (before the colon) and its
50236884Smm * short name (after the colon).
51236884Smm */
52236884Smmstatic int
53236884Smmvalid_char(char c, boolean_t after_colon)
54236884Smm{
55236884Smm	return ((c >= 'a' && c <= 'z') ||
56236884Smm	    (c >= '0' && c <= '9') ||
57236884Smm	    c == (after_colon ? '_' : '.'));
58236884Smm}
59236884Smm
60236884Smm/*
61236884Smm * Every feature guid must contain exactly one colon which separates a reverse
62236884Smm * dns organization name from the feature's "short" name (e.g.
63236884Smm * "com.company:feature_name").
64236884Smm */
65236884Smmboolean_t
66236884Smmzfeature_is_valid_guid(const char *name)
67236884Smm{
68236884Smm	int i;
69236884Smm	boolean_t has_colon = B_FALSE;
70236884Smm
71236884Smm	i = 0;
72236884Smm	while (name[i] != '\0') {
73236884Smm		char c = name[i++];
74236884Smm		if (c == ':') {
75236884Smm			if (has_colon)
76236884Smm				return (B_FALSE);
77236884Smm			has_colon = B_TRUE;
78236884Smm			continue;
79236884Smm		}
80236884Smm		if (!valid_char(c, has_colon))
81236884Smm			return (B_FALSE);
82236884Smm	}
83236884Smm
84236884Smm	return (has_colon);
85236884Smm}
86236884Smm
87236884Smmboolean_t
88236884Smmzfeature_is_supported(const char *guid)
89236884Smm{
90236884Smm	if (zfeature_checks_disable)
91236884Smm		return (B_TRUE);
92236884Smm
93236884Smm	return (0 == zfeature_lookup_guid(guid, NULL));
94236884Smm}
95236884Smm
96236884Smmint
97236884Smmzfeature_lookup_guid(const char *guid, zfeature_info_t **res)
98236884Smm{
99236884Smm	for (int i = 0; i < SPA_FEATURES; i++) {
100236884Smm		zfeature_info_t *feature = &spa_feature_table[i];
101236884Smm		if (strcmp(guid, feature->fi_guid) == 0) {
102236884Smm			if (res != NULL)
103236884Smm				*res = feature;
104236884Smm			return (0);
105236884Smm		}
106236884Smm	}
107236884Smm
108236884Smm	return (ENOENT);
109236884Smm}
110236884Smm
111236884Smmint
112236884Smmzfeature_lookup_name(const char *name, zfeature_info_t **res)
113236884Smm{
114236884Smm	for (int i = 0; i < SPA_FEATURES; i++) {
115236884Smm		zfeature_info_t *feature = &spa_feature_table[i];
116236884Smm		if (strcmp(name, feature->fi_uname) == 0) {
117236884Smm			if (res != NULL)
118236884Smm				*res = feature;
119236884Smm			return (0);
120236884Smm		}
121236884Smm	}
122236884Smm
123236884Smm	return (ENOENT);
124236884Smm}
125236884Smm
126236884Smmstatic void
127236884Smmzfeature_register(int fid, const char *guid, const char *name, const char *desc,
128236884Smm    boolean_t readonly, boolean_t mos, zfeature_info_t **deps)
129236884Smm{
130236884Smm	zfeature_info_t *feature = &spa_feature_table[fid];
131236884Smm	static zfeature_info_t *nodeps[] = { NULL };
132236884Smm
133236884Smm	ASSERT(name != NULL);
134236884Smm	ASSERT(desc != NULL);
135236884Smm	ASSERT(!readonly || !mos);
136236884Smm	ASSERT3U(fid, <, SPA_FEATURES);
137236884Smm	ASSERT(zfeature_is_valid_guid(guid));
138236884Smm
139236884Smm	if (deps == NULL)
140236884Smm		deps = nodeps;
141236884Smm
142236884Smm	feature->fi_guid = guid;
143236884Smm	feature->fi_uname = name;
144236884Smm	feature->fi_desc = desc;
145236884Smm	feature->fi_can_readonly = readonly;
146236884Smm	feature->fi_mos = mos;
147236884Smm	feature->fi_depends = deps;
148236884Smm}
149236884Smm
150236884Smmvoid
151236884Smmzpool_feature_init(void)
152236884Smm{
153236884Smm	zfeature_register(SPA_FEATURE_ASYNC_DESTROY,
154236884Smm	    "com.delphix:async_destroy", "async_destroy",
155236884Smm	    "Destroy filesystems asynchronously.", B_TRUE, B_FALSE, NULL);
156239774Smm	zfeature_register(SPA_FEATURE_EMPTY_BPOBJ,
157239774Smm	    "com.delphix:empty_bpobj", "empty_bpobj",
158239774Smm	    "Snapshots use less space.", B_TRUE, B_FALSE, NULL);
159246586Sdelphij	zfeature_register(SPA_FEATURE_LZ4_COMPRESS,
160246586Sdelphij	    "org.illumos:lz4_compress", "lz4_compress",
161246586Sdelphij	    "LZ4 compression algorithm support.", B_FALSE, B_FALSE, NULL);
162236884Smm}
163