Illumos #2619 and #2747
[zfs.git] / module / zfs / 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) 2012 by Delphix. All rights reserved.
24  */
25
26 #ifdef _KERNEL
27 #include <sys/systm.h>
28 #else
29 #include <errno.h>
30 #include <string.h>
31 #endif
32 #include <sys/debug.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/inttypes.h>
35 #include <sys/types.h>
36 #include "zfeature_common.h"
37
38 /*
39  * Set to disable all feature checks while opening pools, allowing pools with
40  * unsupported features to be opened. Set for testing only.
41  */
42 boolean_t zfeature_checks_disable = B_FALSE;
43
44 zfeature_info_t spa_feature_table[SPA_FEATURES];
45
46 /*
47  * Valid characters for feature guids. This list is mainly for aesthetic
48  * purposes and could be expanded in the future. There are different allowed
49  * characters in the guids reverse dns portion (before the colon) and its
50  * short name (after the colon).
51  */
52 static int
53 valid_char(char c, boolean_t after_colon)
54 {
55         return ((c >= 'a' && c <= 'z') ||
56             (c >= '0' && c <= '9') ||
57             c == (after_colon ? '_' : '.'));
58 }
59
60 /*
61  * Every feature guid must contain exactly one colon which separates a reverse
62  * dns organization name from the feature's "short" name (e.g.
63  * "com.company:feature_name").
64  */
65 boolean_t
66 zfeature_is_valid_guid(const char *name)
67 {
68         int i;
69         boolean_t has_colon = B_FALSE;
70
71         i = 0;
72         while (name[i] != '\0') {
73                 char c = name[i++];
74                 if (c == ':') {
75                         if (has_colon)
76                                 return (B_FALSE);
77                         has_colon = B_TRUE;
78                         continue;
79                 }
80                 if (!valid_char(c, has_colon))
81                         return (B_FALSE);
82         }
83
84         return (has_colon);
85 }
86
87 boolean_t
88 zfeature_is_supported(const char *guid)
89 {
90         if (zfeature_checks_disable)
91                 return (B_TRUE);
92
93         return (0 == zfeature_lookup_guid(guid, NULL));
94 }
95
96 int
97 zfeature_lookup_guid(const char *guid, zfeature_info_t **res)
98 {
99         int i;
100
101         for (i = 0; i < SPA_FEATURES; i++) {
102                 zfeature_info_t *feature = &spa_feature_table[i];
103                 if (strcmp(guid, feature->fi_guid) == 0) {
104                         if (res != NULL)
105                                 *res = feature;
106                         return (0);
107                 }
108         }
109
110         return (ENOENT);
111 }
112
113 int
114 zfeature_lookup_name(const char *name, zfeature_info_t **res)
115 {
116         int i;
117
118         for (i = 0; i < SPA_FEATURES; i++) {
119                 zfeature_info_t *feature = &spa_feature_table[i];
120                 if (strcmp(name, feature->fi_uname) == 0) {
121                         if (res != NULL)
122                                 *res = feature;
123                         return (0);
124                 }
125         }
126
127         return (ENOENT);
128 }
129
130 static void
131 zfeature_register(int fid, const char *guid, const char *name, const char *desc,
132     boolean_t readonly, boolean_t mos, zfeature_info_t **deps)
133 {
134         zfeature_info_t *feature = &spa_feature_table[fid];
135         static zfeature_info_t *nodeps[] = { NULL };
136
137         ASSERT(name != NULL);
138         ASSERT(desc != NULL);
139         ASSERT(!readonly || !mos);
140         ASSERT3U(fid, <, SPA_FEATURES);
141         ASSERT(zfeature_is_valid_guid(guid));
142
143         if (deps == NULL)
144                 deps = nodeps;
145
146         feature->fi_guid = guid;
147         feature->fi_uname = name;
148         feature->fi_desc = desc;
149         feature->fi_can_readonly = readonly;
150         feature->fi_mos = mos;
151         feature->fi_depends = deps;
152 }
153
154 void
155 zpool_feature_init(void)
156 {
157         zfeature_register(SPA_FEATURE_ASYNC_DESTROY,
158             "com.delphix:async_destroy", "async_destroy",
159             "Destroy filesystems asynchronously.", B_TRUE, B_FALSE, NULL);
160 }