xref: /titanic_44/usr/src/common/zfs/zfeature_common.c (revision a6f561b4aee75d0d028e7b36b151c8ed8a86bc76)
153089ab7Seschrock /*
253089ab7Seschrock  * CDDL HEADER START
353089ab7Seschrock  *
453089ab7Seschrock  * The contents of this file are subject to the terms of the
553089ab7Seschrock  * Common Development and Distribution License (the "License").
653089ab7Seschrock  * You may not use this file except in compliance with the License.
753089ab7Seschrock  *
853089ab7Seschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
953089ab7Seschrock  * or http://www.opensolaris.org/os/licensing.
1053089ab7Seschrock  * See the License for the specific language governing permissions
1153089ab7Seschrock  * and limitations under the License.
1253089ab7Seschrock  *
1353089ab7Seschrock  * When distributing Covered Code, include this CDDL HEADER in each
1453089ab7Seschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1553089ab7Seschrock  * If applicable, add the following below this CDDL HEADER, with the
1653089ab7Seschrock  * fields enclosed by brackets "[]" replaced with your own identifying
1753089ab7Seschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
1853089ab7Seschrock  *
1953089ab7Seschrock  * CDDL HEADER END
2053089ab7Seschrock  */
2153089ab7Seschrock 
2253089ab7Seschrock /*
2353089ab7Seschrock  * Copyright (c) 2012 by Delphix. All rights reserved.
24*a6f561b4SSašo Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
2553089ab7Seschrock  */
2653089ab7Seschrock 
2753089ab7Seschrock #ifdef _KERNEL
2853089ab7Seschrock #include <sys/systm.h>
2953089ab7Seschrock #else
3053089ab7Seschrock #include <errno.h>
3153089ab7Seschrock #include <string.h>
3253089ab7Seschrock #endif
3353089ab7Seschrock #include <sys/debug.h>
3453089ab7Seschrock #include <sys/fs/zfs.h>
3553089ab7Seschrock #include <sys/inttypes.h>
3653089ab7Seschrock #include <sys/types.h>
3753089ab7Seschrock #include "zfeature_common.h"
3853089ab7Seschrock 
3953089ab7Seschrock /*
4053089ab7Seschrock  * Set to disable all feature checks while opening pools, allowing pools with
4153089ab7Seschrock  * unsupported features to be opened. Set for testing only.
4253089ab7Seschrock  */
4353089ab7Seschrock boolean_t zfeature_checks_disable = B_FALSE;
4453089ab7Seschrock 
4553089ab7Seschrock zfeature_info_t spa_feature_table[SPA_FEATURES];
4653089ab7Seschrock 
4753089ab7Seschrock /*
4853089ab7Seschrock  * Valid characters for feature guids. This list is mainly for aesthetic
4953089ab7Seschrock  * purposes and could be expanded in the future. There are different allowed
5053089ab7Seschrock  * characters in the guids reverse dns portion (before the colon) and its
5153089ab7Seschrock  * short name (after the colon).
5253089ab7Seschrock  */
5353089ab7Seschrock static int
5453089ab7Seschrock valid_char(char c, boolean_t after_colon)
5553089ab7Seschrock {
5653089ab7Seschrock 	return ((c >= 'a' && c <= 'z') ||
5753089ab7Seschrock 	    (c >= '0' && c <= '9') ||
5853089ab7Seschrock 	    c == (after_colon ? '_' : '.'));
5953089ab7Seschrock }
6053089ab7Seschrock 
6153089ab7Seschrock /*
6253089ab7Seschrock  * Every feature guid must contain exactly one colon which separates a reverse
6353089ab7Seschrock  * dns organization name from the feature's "short" name (e.g.
6453089ab7Seschrock  * "com.company:feature_name").
6553089ab7Seschrock  */
6653089ab7Seschrock boolean_t
6753089ab7Seschrock zfeature_is_valid_guid(const char *name)
6853089ab7Seschrock {
6953089ab7Seschrock 	int i;
7053089ab7Seschrock 	boolean_t has_colon = B_FALSE;
7153089ab7Seschrock 
7253089ab7Seschrock 	i = 0;
7353089ab7Seschrock 	while (name[i] != '\0') {
7453089ab7Seschrock 		char c = name[i++];
7553089ab7Seschrock 		if (c == ':') {
7653089ab7Seschrock 			if (has_colon)
7753089ab7Seschrock 				return (B_FALSE);
7853089ab7Seschrock 			has_colon = B_TRUE;
7953089ab7Seschrock 			continue;
8053089ab7Seschrock 		}
8153089ab7Seschrock 		if (!valid_char(c, has_colon))
8253089ab7Seschrock 			return (B_FALSE);
8353089ab7Seschrock 	}
8453089ab7Seschrock 
8553089ab7Seschrock 	return (has_colon);
8653089ab7Seschrock }
8753089ab7Seschrock 
8853089ab7Seschrock boolean_t
8953089ab7Seschrock zfeature_is_supported(const char *guid)
9053089ab7Seschrock {
9153089ab7Seschrock 	if (zfeature_checks_disable)
9253089ab7Seschrock 		return (B_TRUE);
9353089ab7Seschrock 
9453089ab7Seschrock 	return (0 == zfeature_lookup_guid(guid, NULL));
9553089ab7Seschrock }
9653089ab7Seschrock 
9753089ab7Seschrock int
9853089ab7Seschrock zfeature_lookup_guid(const char *guid, zfeature_info_t **res)
9953089ab7Seschrock {
10053089ab7Seschrock 	for (int i = 0; i < SPA_FEATURES; i++) {
10153089ab7Seschrock 		zfeature_info_t *feature = &spa_feature_table[i];
10253089ab7Seschrock 		if (strcmp(guid, feature->fi_guid) == 0) {
10353089ab7Seschrock 			if (res != NULL)
10453089ab7Seschrock 				*res = feature;
10553089ab7Seschrock 			return (0);
10653089ab7Seschrock 		}
10753089ab7Seschrock 	}
10853089ab7Seschrock 
10953089ab7Seschrock 	return (ENOENT);
11053089ab7Seschrock }
11153089ab7Seschrock 
11253089ab7Seschrock int
11353089ab7Seschrock zfeature_lookup_name(const char *name, zfeature_info_t **res)
11453089ab7Seschrock {
11553089ab7Seschrock 	for (int i = 0; i < SPA_FEATURES; i++) {
11653089ab7Seschrock 		zfeature_info_t *feature = &spa_feature_table[i];
11753089ab7Seschrock 		if (strcmp(name, feature->fi_uname) == 0) {
11853089ab7Seschrock 			if (res != NULL)
11953089ab7Seschrock 				*res = feature;
12053089ab7Seschrock 			return (0);
12153089ab7Seschrock 		}
12253089ab7Seschrock 	}
12353089ab7Seschrock 
12453089ab7Seschrock 	return (ENOENT);
12553089ab7Seschrock }
12653089ab7Seschrock 
12753089ab7Seschrock static void
12853089ab7Seschrock zfeature_register(int fid, const char *guid, const char *name, const char *desc,
12953089ab7Seschrock     boolean_t readonly, boolean_t mos, zfeature_info_t **deps)
13053089ab7Seschrock {
13153089ab7Seschrock 	zfeature_info_t *feature = &spa_feature_table[fid];
13253089ab7Seschrock 	static zfeature_info_t *nodeps[] = { NULL };
13353089ab7Seschrock 
13453089ab7Seschrock 	ASSERT(name != NULL);
13553089ab7Seschrock 	ASSERT(desc != NULL);
13653089ab7Seschrock 	ASSERT(!readonly || !mos);
13753089ab7Seschrock 	ASSERT3U(fid, <, SPA_FEATURES);
13853089ab7Seschrock 	ASSERT(zfeature_is_valid_guid(guid));
13953089ab7Seschrock 
14053089ab7Seschrock 	if (deps == NULL)
14153089ab7Seschrock 		deps = nodeps;
14253089ab7Seschrock 
14353089ab7Seschrock 	feature->fi_guid = guid;
14453089ab7Seschrock 	feature->fi_uname = name;
14553089ab7Seschrock 	feature->fi_desc = desc;
14653089ab7Seschrock 	feature->fi_can_readonly = readonly;
14753089ab7Seschrock 	feature->fi_mos = mos;
14853089ab7Seschrock 	feature->fi_depends = deps;
14953089ab7Seschrock }
15053089ab7Seschrock 
15153089ab7Seschrock void
15253089ab7Seschrock zpool_feature_init(void)
15353089ab7Seschrock {
15453089ab7Seschrock 	zfeature_register(SPA_FEATURE_ASYNC_DESTROY,
15553089ab7Seschrock 	    "com.delphix:async_destroy", "async_destroy",
15653089ab7Seschrock 	    "Destroy filesystems asynchronously.", B_TRUE, B_FALSE, NULL);
157f1745736SMatthew Ahrens 	zfeature_register(SPA_FEATURE_EMPTY_BPOBJ,
158f1745736SMatthew Ahrens 	    "com.delphix:empty_bpobj", "empty_bpobj",
159f1745736SMatthew Ahrens 	    "Snapshots use less space.", B_TRUE, B_FALSE, NULL);
160*a6f561b4SSašo Kiselkov 	zfeature_register(SPA_FEATURE_LZ4_COMPRESS,
161*a6f561b4SSašo Kiselkov 	    "org.illumos:lz4_compress", "lz4_compress",
162*a6f561b4SSašo Kiselkov 	    "LZ4 compression algorithm support.", B_FALSE, B_FALSE, NULL);
16353089ab7Seschrock }
164