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