xref: /titanic_52/usr/src/common/zfs/zfs_prop.c (revision 3d7072f8bd27709dba14f6fe336f149d25d9e207)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5906d120cSlling  * Common Development and Distribution License (the "License").
6906d120cSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2239c23413Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * Master property table.
30fa9e4066Sahrens  *
31fa9e4066Sahrens  * This table keeps track of all the properties supported by ZFS, and their
32fa9e4066Sahrens  * various attributes.  Not all of these are needed by the kernel, and several
33fa9e4066Sahrens  * are only used by a single libzfs client.  But having them here centralizes
34fa9e4066Sahrens  * all property information in one location.
35fa9e4066Sahrens  *
36fa9e4066Sahrens  * 	name		The human-readable string representing this property
37fa9e4066Sahrens  * 	proptype	Basic type (string, boolean, number)
38fa9e4066Sahrens  * 	default		Default value for the property.  Sadly, C only allows
39fa9e4066Sahrens  * 			you to initialize the first member of a union, so we
40fa9e4066Sahrens  * 			have two default members for each property.
41fa9e4066Sahrens  * 	attr		Attributes (readonly, inheritable) for the property
42fa9e4066Sahrens  * 	types		Valid dataset types to which this applies
43fa9e4066Sahrens  * 	values		String describing acceptable values for the property
44fa9e4066Sahrens  * 	colname		The column header for 'zfs list'
45fa9e4066Sahrens  *	colfmt		The column formatting for 'zfs list'
46fa9e4066Sahrens  *
47fa9e4066Sahrens  * This table must match the order of property types in libzfs.h.
48fa9e4066Sahrens  */
49fa9e4066Sahrens 
50fa9e4066Sahrens #include <sys/zio.h>
51fa9e4066Sahrens #include <sys/spa.h>
52fa9e4066Sahrens #include <sys/zfs_acl.h>
53fa9e4066Sahrens #include <sys/zfs_ioctl.h>
54fa9e4066Sahrens 
55fa9e4066Sahrens #include "zfs_prop.h"
56fa9e4066Sahrens 
57fa9e4066Sahrens #if defined(_KERNEL)
58fa9e4066Sahrens #include <sys/systm.h>
59fa9e4066Sahrens #else
60fa9e4066Sahrens #include <stdlib.h>
61fa9e4066Sahrens #include <string.h>
62fa9e4066Sahrens #include <ctype.h>
63fa9e4066Sahrens #endif
64fa9e4066Sahrens 
65fa9e4066Sahrens typedef enum {
66fa9e4066Sahrens 	prop_default,
67fa9e4066Sahrens 	prop_readonly,
68fa9e4066Sahrens 	prop_inherit
69fa9e4066Sahrens } prop_attr_t;
70fa9e4066Sahrens 
71fa9e4066Sahrens typedef struct {
72fa9e4066Sahrens 	const char	*pd_name;
73fa9e4066Sahrens 	zfs_proptype_t	pd_proptype;
74fa9e4066Sahrens 	uint64_t	pd_numdefault;
75fa9e4066Sahrens 	const char	*pd_strdefault;
76fa9e4066Sahrens 	prop_attr_t	pd_attr;
77fa9e4066Sahrens 	int		pd_types;
78fa9e4066Sahrens 	const char	*pd_values;
79fa9e4066Sahrens 	const char	*pd_colname;
80e9dbad6fSeschrock 	boolean_t	pd_rightalign;
8166e2aaccSgw25295 	boolean_t	pd_visible;
82fa9e4066Sahrens } prop_desc_t;
83fa9e4066Sahrens 
8466e2aaccSgw25295 static prop_desc_t zfs_prop_table[] = {
85fa9e4066Sahrens 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
8666e2aaccSgw25295 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE,
8766e2aaccSgw25295 	    B_TRUE },
88fa9e4066Sahrens 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
8966e2aaccSgw25295 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE },
90fa9e4066Sahrens 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
9166e2aaccSgw25295 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE, B_TRUE },
92fa9e4066Sahrens 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
9366e2aaccSgw25295 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE,
9466e2aaccSgw25295 	    B_TRUE },
95fa9e4066Sahrens 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
964f75c27eSeschrock 	    ZFS_TYPE_ANY,
9766e2aaccSgw25295 	    "<size>", "REFER", B_TRUE, B_TRUE },
98fa9e4066Sahrens 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
9966e2aaccSgw25295 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE,
10066e2aaccSgw25295 	    B_TRUE },
101fa9e4066Sahrens 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
10266e2aaccSgw25295 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE, B_TRUE },
103fa9e4066Sahrens 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
104101f5531Slling 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
10566e2aaccSgw25295 	    B_FALSE, B_TRUE },
106fa9e4066Sahrens 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
10766e2aaccSgw25295 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE, B_TRUE },
108fa9e4066Sahrens 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
109fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
11066e2aaccSgw25295 	    "<size> | none", "RESERV", B_TRUE, B_TRUE },
111fa9e4066Sahrens 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
11266e2aaccSgw25295 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE },
1134f75c27eSeschrock 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
11466e2aaccSgw25295 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE,
11566e2aaccSgw25295 	    B_TRUE },
116fa9e4066Sahrens 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
117fa9e4066Sahrens 	    prop_inherit,
1184f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
11966e2aaccSgw25295 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE },
120fa9e4066Sahrens 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
121fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
12266e2aaccSgw25295 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE },
123fa9e4066Sahrens 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
124fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
12566e2aaccSgw25295 	    "on | off | share(1M) options", "SHARENFS", B_FALSE, B_TRUE },
126101f5531Slling 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
1274f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
12866e2aaccSgw25295 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE,
12966e2aaccSgw25295 	    B_TRUE },
130101f5531Slling 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
1314f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
132c9431fa1Sahl 	    "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", B_TRUE, B_TRUE },
133fa9e4066Sahrens 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
134fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
13566e2aaccSgw25295 	    "on | off", "ATIME", B_TRUE, B_TRUE },
136fa9e4066Sahrens 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
13712054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
13866e2aaccSgw25295 	    "on | off", "DEVICES", B_TRUE, B_TRUE },
139fa9e4066Sahrens 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
14012054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
14166e2aaccSgw25295 	    "on | off", "EXEC", B_TRUE, B_TRUE },
142fa9e4066Sahrens 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
14312054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
14466e2aaccSgw25295 	    B_TRUE, B_TRUE },
145fa9e4066Sahrens 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
1464f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
14766e2aaccSgw25295 	    "on | off", "RDONLY", B_TRUE, B_TRUE },
148fa9e4066Sahrens 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
1494f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
15066e2aaccSgw25295 	    "on | off", "ZONED", B_TRUE, B_TRUE },
151906d120cSlling 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
152a0965f35Sbonwick 	    prop_inherit,
153fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
15466e2aaccSgw25295 	    "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE },
155e9dbad6fSeschrock 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
156e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
15766e2aaccSgw25295 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE, B_TRUE },
158e9dbad6fSeschrock 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
159e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
16066e2aaccSgw25295 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE,
16166e2aaccSgw25295 	    B_TRUE },
16266e2aaccSgw25295 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
16366e2aaccSgw25295 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE },
16466e2aaccSgw25295 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
16566e2aaccSgw25295 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE },
166e9dbad6fSeschrock 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
167fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
16866e2aaccSgw25295 	    "on | off", "CANMOUNT", B_TRUE, B_TRUE },
16966e2aaccSgw25295 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
17066e2aaccSgw25295 	    ZFS_TYPE_ANY,
17166e2aaccSgw25295 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE },
17266e2aaccSgw25295 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
17366e2aaccSgw25295 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE },
1747b55fa8eSck153898 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
1757b55fa8eSck153898 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
17666e2aaccSgw25295 	    "on | off", "XATTR", B_TRUE, B_TRUE },
17739c23413Seschrock 	{ "numclones", prop_type_number,	0,	NULL,	prop_readonly,
17866e2aaccSgw25295 	    ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE, B_FALSE },
179d0ad202dSahrens 	{ "copies",	prop_type_index,	1,	"1",	prop_inherit,
180d0ad202dSahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
181d0ad202dSahrens 	    "1 | 2 | 3", "COPIES", B_TRUE, B_TRUE },
182b1b8ab34Slling 	{ "bootfs", prop_type_string,	0,	NULL,	prop_default,
183b1b8ab34Slling 	    ZFS_TYPE_POOL, "<filesystem>", "BOOTFS", B_FALSE, B_TRUE },
184*3d7072f8Seschrock 	{ "autoreplace", prop_type_boolean,	0,	NULL, prop_default,
185*3d7072f8Seschrock 	    ZFS_TYPE_POOL, "on | off", "REPLACE", B_FALSE, B_TRUE },
186fa9e4066Sahrens };
187fa9e4066Sahrens 
18866e2aaccSgw25295 #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
18966e2aaccSgw25295 
190b1b8ab34Slling /*
191b1b8ab34Slling  * Returns TRUE if the property applies to the given dataset types.
192b1b8ab34Slling  */
193b1b8ab34Slling int
194b1b8ab34Slling zfs_prop_valid_for_type(zfs_prop_t prop, int types)
195b1b8ab34Slling {
196b1b8ab34Slling 	return ((zfs_prop_table[prop].pd_types & types) != 0);
197b1b8ab34Slling }
198b1b8ab34Slling 
199b1b8ab34Slling /*
200b1b8ab34Slling  * Determine if the specified property is visible or not.
201b1b8ab34Slling  */
202b1b8ab34Slling boolean_t
203b1b8ab34Slling zfs_prop_is_visible(zfs_prop_t prop)
204b1b8ab34Slling {
205b1b8ab34Slling 	if (prop < 0)
206b1b8ab34Slling 		return (B_FALSE);
207b1b8ab34Slling 
208b1b8ab34Slling 	return (zfs_prop_table[prop].pd_visible);
209b1b8ab34Slling }
210b1b8ab34Slling 
211b1b8ab34Slling /*
212b1b8ab34Slling  * Iterate over all properties, calling back into the specified function
213b1b8ab34Slling  * for each property. We will continue to iterate until we either
214b1b8ab34Slling  * reach the end or the callback function something other than
215b1b8ab34Slling  * ZFS_PROP_CONT.
216b1b8ab34Slling  */
217b1b8ab34Slling zfs_prop_t
218b1b8ab34Slling zfs_prop_iter_common(zfs_prop_f func, void *cb, zfs_type_t type,
219b1b8ab34Slling     boolean_t show_all)
220b1b8ab34Slling {
221b1b8ab34Slling 	int i;
222b1b8ab34Slling 
223b1b8ab34Slling 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
224b1b8ab34Slling 		if (zfs_prop_valid_for_type(i, type) &&
225b1b8ab34Slling 		    (zfs_prop_is_visible(i) || show_all)) {
226b1b8ab34Slling 			if (func(i, cb) != ZFS_PROP_CONT)
227b1b8ab34Slling 				return (i);
228b1b8ab34Slling 		}
229b1b8ab34Slling 	}
230b1b8ab34Slling 	return (ZFS_PROP_CONT);
231b1b8ab34Slling }
232b1b8ab34Slling 
233b1b8ab34Slling zfs_prop_t
234b1b8ab34Slling zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
235b1b8ab34Slling {
236b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_ANY, show_all));
237b1b8ab34Slling }
238b1b8ab34Slling 
239b1b8ab34Slling zpool_prop_t
240b1b8ab34Slling zpool_prop_iter(zpool_prop_f func, void *cb, boolean_t show_all)
241b1b8ab34Slling {
242b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_POOL, show_all));
243b1b8ab34Slling }
244b1b8ab34Slling 
245fa9e4066Sahrens zfs_proptype_t
246fa9e4066Sahrens zfs_prop_get_type(zfs_prop_t prop)
247fa9e4066Sahrens {
248fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype);
249fa9e4066Sahrens }
250fa9e4066Sahrens 
251*3d7072f8Seschrock zfs_proptype_t
252*3d7072f8Seschrock zpool_prop_get_type(zfs_prop_t prop)
253*3d7072f8Seschrock {
254*3d7072f8Seschrock 	return (zfs_prop_table[prop].pd_proptype);
255*3d7072f8Seschrock }
256*3d7072f8Seschrock 
257e9dbad6fSeschrock static boolean_t
258e9dbad6fSeschrock propname_match(const char *p, zfs_prop_t prop, size_t len)
2595c709891Seschrock {
2605c709891Seschrock 	const char *propname = zfs_prop_table[prop].pd_name;
2615c709891Seschrock #ifndef _KERNEL
2625c709891Seschrock 	const char *colname = zfs_prop_table[prop].pd_colname;
2635c709891Seschrock 	int c;
2645c709891Seschrock #endif
2655c709891Seschrock 
2663bb79becSeschrock #ifndef _KERNEL
2673bb79becSeschrock 	if (colname == NULL)
268e9dbad6fSeschrock 		return (B_FALSE);
2693bb79becSeschrock #endif
2703bb79becSeschrock 
2715c709891Seschrock 	if (len == strlen(propname) &&
2725c709891Seschrock 	    strncmp(p, propname, len) == 0)
273e9dbad6fSeschrock 		return (B_TRUE);
2745c709891Seschrock 
2755c709891Seschrock #ifndef _KERNEL
2763bb79becSeschrock 	if (len != strlen(colname))
277e9dbad6fSeschrock 		return (B_FALSE);
2785c709891Seschrock 
2795c709891Seschrock 	for (c = 0; c < len; c++)
2805c709891Seschrock 		if (p[c] != tolower(colname[c]))
2815c709891Seschrock 			break;
2825c709891Seschrock 
2835c709891Seschrock 	return (colname[c] == '\0');
2845c709891Seschrock #else
285e9dbad6fSeschrock 	return (B_FALSE);
2865c709891Seschrock #endif
2875c709891Seschrock }
2885c709891Seschrock 
28966e2aaccSgw25295 zfs_prop_t
29066e2aaccSgw25295 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
29166e2aaccSgw25295 {
29266e2aaccSgw25295 	const char *propname = cb_data;
29366e2aaccSgw25295 
29466e2aaccSgw25295 	if (propname_match(propname, prop, strlen(propname)))
29566e2aaccSgw25295 		return (prop);
29666e2aaccSgw25295 
29766e2aaccSgw25295 	return (ZFS_PROP_CONT);
29866e2aaccSgw25295 }
2995c709891Seschrock 
300fa9e4066Sahrens /*
301b1b8ab34Slling  * Given a property name and its type, returns the corresponding property ID.
302b1b8ab34Slling  */
303b1b8ab34Slling zfs_prop_t
304b1b8ab34Slling zfs_name_to_prop_common(const char *propname, zfs_type_t type)
305b1b8ab34Slling {
306b1b8ab34Slling 	zfs_prop_t prop;
307b1b8ab34Slling 
308b1b8ab34Slling 	prop = zfs_prop_iter_common(zfs_name_to_prop_cb, (void *)propname,
309b1b8ab34Slling 	    type, B_TRUE);
310b1b8ab34Slling 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
311b1b8ab34Slling }
312b1b8ab34Slling 
313b1b8ab34Slling /*
314b1b8ab34Slling  * Given a zfs dataset property name, returns the corresponding property ID.
315fa9e4066Sahrens  */
316fa9e4066Sahrens zfs_prop_t
317fa9e4066Sahrens zfs_name_to_prop(const char *propname)
318fa9e4066Sahrens {
319b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_ANY));
320b1b8ab34Slling }
321fa9e4066Sahrens 
322b1b8ab34Slling /*
323b1b8ab34Slling  * Given a pool property name, returns the corresponding property ID.
324b1b8ab34Slling  */
325b1b8ab34Slling zpool_prop_t
326b1b8ab34Slling zpool_name_to_prop(const char *propname)
327b1b8ab34Slling {
328b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_POOL));
329fa9e4066Sahrens }
330fa9e4066Sahrens 
331fa9e4066Sahrens /*
332e9dbad6fSeschrock  * For user property names, we allow all lowercase alphanumeric characters, plus
333e9dbad6fSeschrock  * a few useful punctuation characters.
334e9dbad6fSeschrock  */
335e9dbad6fSeschrock static int
336e9dbad6fSeschrock valid_char(char c)
337e9dbad6fSeschrock {
338e9dbad6fSeschrock 	return ((c >= 'a' && c <= 'z') ||
339e9dbad6fSeschrock 	    (c >= '0' && c <= '9') ||
340e9dbad6fSeschrock 	    c == '-' || c == '_' || c == '.' || c == ':');
341e9dbad6fSeschrock }
342e9dbad6fSeschrock 
343e9dbad6fSeschrock /*
344e9dbad6fSeschrock  * Returns true if this is a valid user-defined property (one with a ':').
345e9dbad6fSeschrock  */
346e9dbad6fSeschrock boolean_t
347e9dbad6fSeschrock zfs_prop_user(const char *name)
348e9dbad6fSeschrock {
349e9dbad6fSeschrock 	int i;
350e9dbad6fSeschrock 	char c;
351e9dbad6fSeschrock 	boolean_t foundsep = B_FALSE;
352e9dbad6fSeschrock 
353e9dbad6fSeschrock 	for (i = 0; i < strlen(name); i++) {
354e9dbad6fSeschrock 		c = name[i];
355e9dbad6fSeschrock 		if (!valid_char(c))
356e9dbad6fSeschrock 			return (B_FALSE);
357e9dbad6fSeschrock 		if (c == ':')
358e9dbad6fSeschrock 			foundsep = B_TRUE;
359e9dbad6fSeschrock 	}
360e9dbad6fSeschrock 
361e9dbad6fSeschrock 	if (!foundsep)
362e9dbad6fSeschrock 		return (B_FALSE);
363e9dbad6fSeschrock 
364e9dbad6fSeschrock 	return (B_TRUE);
365e9dbad6fSeschrock }
366e9dbad6fSeschrock 
367e9dbad6fSeschrock /*
368fa9e4066Sahrens  * Return the default value for the given property.
369fa9e4066Sahrens  */
3707f7322feSeschrock const char *
3717f7322feSeschrock zfs_prop_default_string(zfs_prop_t prop)
372fa9e4066Sahrens {
3737f7322feSeschrock 	return (zfs_prop_table[prop].pd_strdefault);
374fa9e4066Sahrens }
375fa9e4066Sahrens 
376*3d7072f8Seschrock const char *
377*3d7072f8Seschrock zpool_prop_default_string(zpool_prop_t prop)
378*3d7072f8Seschrock {
379*3d7072f8Seschrock 	return (zfs_prop_table[prop].pd_strdefault);
380*3d7072f8Seschrock }
381*3d7072f8Seschrock 
382fa9e4066Sahrens uint64_t
383fa9e4066Sahrens zfs_prop_default_numeric(zfs_prop_t prop)
384fa9e4066Sahrens {
385fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_numdefault);
386fa9e4066Sahrens }
387fa9e4066Sahrens 
388*3d7072f8Seschrock uint64_t
389*3d7072f8Seschrock zpool_prop_default_numeric(zpool_prop_t prop)
390*3d7072f8Seschrock {
391*3d7072f8Seschrock 	return (zfs_prop_table[prop].pd_numdefault);
392*3d7072f8Seschrock }
393*3d7072f8Seschrock 
394fa9e4066Sahrens /*
395fa9e4066Sahrens  * Returns TRUE if the property is readonly.
396fa9e4066Sahrens  */
397fa9e4066Sahrens int
398fa9e4066Sahrens zfs_prop_readonly(zfs_prop_t prop)
399fa9e4066Sahrens {
400fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
401fa9e4066Sahrens }
402fa9e4066Sahrens 
403fa9e4066Sahrens /*
404b1b8ab34Slling  * Given a dataset property ID, returns the corresponding name.
405*3d7072f8Seschrock  * Assuming the zfs dataset property ID is valid.
406fa9e4066Sahrens  */
407fa9e4066Sahrens const char *
408fa9e4066Sahrens zfs_prop_to_name(zfs_prop_t prop)
409fa9e4066Sahrens {
410fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_name);
411fa9e4066Sahrens }
412fa9e4066Sahrens 
413fa9e4066Sahrens /*
414b1b8ab34Slling  * Given a pool property ID, returns the corresponding name.
415*3d7072f8Seschrock  * Assuming the pool property ID is valid.
416b1b8ab34Slling  */
417b1b8ab34Slling const char *
418b1b8ab34Slling zpool_prop_to_name(zpool_prop_t prop)
419b1b8ab34Slling {
420b1b8ab34Slling 	return (zfs_prop_table[prop].pd_name);
421b1b8ab34Slling }
422b1b8ab34Slling 
423b1b8ab34Slling /*
424fa9e4066Sahrens  * Returns TRUE if the property is inheritable.
425fa9e4066Sahrens  */
426fa9e4066Sahrens int
427fa9e4066Sahrens zfs_prop_inheritable(zfs_prop_t prop)
428fa9e4066Sahrens {
429fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
430fa9e4066Sahrens }
431fa9e4066Sahrens 
432e9dbad6fSeschrock typedef struct zfs_index {
433e9dbad6fSeschrock 	const char *name;
434e9dbad6fSeschrock 	uint64_t index;
435e9dbad6fSeschrock } zfs_index_t;
436e9dbad6fSeschrock 
437e9dbad6fSeschrock static zfs_index_t checksum_table[] = {
438e9dbad6fSeschrock 	{ "on",		ZIO_CHECKSUM_ON },
439e9dbad6fSeschrock 	{ "off",	ZIO_CHECKSUM_OFF },
440e9dbad6fSeschrock 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
441e9dbad6fSeschrock 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
442e9dbad6fSeschrock 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
443e9dbad6fSeschrock 	{ NULL }
444e9dbad6fSeschrock };
445e9dbad6fSeschrock 
446e9dbad6fSeschrock static zfs_index_t compress_table[] = {
447e9dbad6fSeschrock 	{ "on",		ZIO_COMPRESS_ON },
448e9dbad6fSeschrock 	{ "off",	ZIO_COMPRESS_OFF },
449e9dbad6fSeschrock 	{ "lzjb",	ZIO_COMPRESS_LZJB },
450c9431fa1Sahl 	{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* the default gzip level */
451c9431fa1Sahl 	{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
452c9431fa1Sahl 	{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
453c9431fa1Sahl 	{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
454c9431fa1Sahl 	{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
455c9431fa1Sahl 	{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
456c9431fa1Sahl 	{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
457c9431fa1Sahl 	{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
458c9431fa1Sahl 	{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
459c9431fa1Sahl 	{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
460e9dbad6fSeschrock 	{ NULL }
461e9dbad6fSeschrock };
462e9dbad6fSeschrock 
463e9dbad6fSeschrock static zfs_index_t snapdir_table[] = {
464e9dbad6fSeschrock 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
465e9dbad6fSeschrock 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
466e9dbad6fSeschrock 	{ NULL }
467e9dbad6fSeschrock };
468e9dbad6fSeschrock 
469e9dbad6fSeschrock static zfs_index_t acl_mode_table[] = {
470e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
471e9dbad6fSeschrock 	{ "groupmask",	ZFS_ACL_GROUPMASK },
472e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
473e9dbad6fSeschrock 	{ NULL }
474e9dbad6fSeschrock };
475e9dbad6fSeschrock 
476e9dbad6fSeschrock static zfs_index_t acl_inherit_table[] = {
477e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
478e9dbad6fSeschrock 	{ "noallow",	ZFS_ACL_NOALLOW },
479e9dbad6fSeschrock 	{ "secure",	ZFS_ACL_SECURE },
480e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
481e9dbad6fSeschrock 	{ NULL }
482e9dbad6fSeschrock };
483e9dbad6fSeschrock 
484d0ad202dSahrens static zfs_index_t copies_table[] = {
485d0ad202dSahrens 	{ "1",	1 },
486d0ad202dSahrens 	{ "2",	2 },
487d0ad202dSahrens 	{ "3",	3 },
488d0ad202dSahrens 	{ NULL }
489d0ad202dSahrens };
490d0ad202dSahrens 
491e9dbad6fSeschrock static zfs_index_t *
492e9dbad6fSeschrock zfs_prop_index_table(zfs_prop_t prop)
493e9dbad6fSeschrock {
494e9dbad6fSeschrock 	switch (prop) {
495e9dbad6fSeschrock 	case ZFS_PROP_CHECKSUM:
496e9dbad6fSeschrock 		return (checksum_table);
497e9dbad6fSeschrock 	case ZFS_PROP_COMPRESSION:
498e9dbad6fSeschrock 		return (compress_table);
499e9dbad6fSeschrock 	case ZFS_PROP_SNAPDIR:
500e9dbad6fSeschrock 		return (snapdir_table);
501e9dbad6fSeschrock 	case ZFS_PROP_ACLMODE:
502e9dbad6fSeschrock 		return (acl_mode_table);
503e9dbad6fSeschrock 	case ZFS_PROP_ACLINHERIT:
504e9dbad6fSeschrock 		return (acl_inherit_table);
505d0ad202dSahrens 	case ZFS_PROP_COPIES:
506d0ad202dSahrens 		return (copies_table);
507e9dbad6fSeschrock 	default:
508e9dbad6fSeschrock 		return (NULL);
509e9dbad6fSeschrock 	}
510e9dbad6fSeschrock }
511e9dbad6fSeschrock 
512e9dbad6fSeschrock 
513e9dbad6fSeschrock /*
514e9dbad6fSeschrock  * Tables of index types, plus functions to convert between the user view
515e9dbad6fSeschrock  * (strings) and internal representation (uint64_t).
516e9dbad6fSeschrock  */
517e9dbad6fSeschrock int
518e9dbad6fSeschrock zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
519e9dbad6fSeschrock {
520e9dbad6fSeschrock 	zfs_index_t *table;
521e9dbad6fSeschrock 	int i;
522e9dbad6fSeschrock 
523e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
524e9dbad6fSeschrock 		return (-1);
525e9dbad6fSeschrock 
526e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
527e9dbad6fSeschrock 		if (strcmp(string, table[i].name) == 0) {
528e9dbad6fSeschrock 			*index = table[i].index;
529e9dbad6fSeschrock 			return (0);
530e9dbad6fSeschrock 		}
531e9dbad6fSeschrock 	}
532e9dbad6fSeschrock 
533e9dbad6fSeschrock 	return (-1);
534e9dbad6fSeschrock }
535e9dbad6fSeschrock 
536e9dbad6fSeschrock int
537e9dbad6fSeschrock zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
538e9dbad6fSeschrock {
539e9dbad6fSeschrock 	zfs_index_t *table;
540e9dbad6fSeschrock 	int i;
541e9dbad6fSeschrock 
542e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
543e9dbad6fSeschrock 		return (-1);
544e9dbad6fSeschrock 
545e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
546e9dbad6fSeschrock 		if (table[i].index == index) {
547e9dbad6fSeschrock 			*string = table[i].name;
548e9dbad6fSeschrock 			return (0);
549e9dbad6fSeschrock 		}
550e9dbad6fSeschrock 	}
551e9dbad6fSeschrock 
552e9dbad6fSeschrock 	return (-1);
553e9dbad6fSeschrock }
554e9dbad6fSeschrock 
555acd76fe5Seschrock #ifndef _KERNEL
556acd76fe5Seschrock 
557fa9e4066Sahrens /*
558fa9e4066Sahrens  * Returns a string describing the set of acceptable values for the given
559b1b8ab34Slling  * zfs property, or NULL if it cannot be set.
560fa9e4066Sahrens  */
561fa9e4066Sahrens const char *
562fa9e4066Sahrens zfs_prop_values(zfs_prop_t prop)
563fa9e4066Sahrens {
564b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types == ZFS_TYPE_POOL)
565b1b8ab34Slling 		return (NULL);
566b1b8ab34Slling 
567b1b8ab34Slling 	return (zfs_prop_table[prop].pd_values);
568b1b8ab34Slling }
569b1b8ab34Slling 
570b1b8ab34Slling /*
571b1b8ab34Slling  * Returns a string describing the set of acceptable values for the given
572b1b8ab34Slling  * zpool property, or NULL if it cannot be set.
573b1b8ab34Slling  */
574b1b8ab34Slling const char *
575b1b8ab34Slling zpool_prop_values(zfs_prop_t prop)
576b1b8ab34Slling {
577b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types != ZFS_TYPE_POOL)
578b1b8ab34Slling 		return (NULL);
579b1b8ab34Slling 
580fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_values);
581fa9e4066Sahrens }
582fa9e4066Sahrens 
583fa9e4066Sahrens /*
584fa9e4066Sahrens  * Returns TRUE if this property is a string type.  Note that index types
585fa9e4066Sahrens  * (compression, checksum) are treated as strings in userland, even though they
586fa9e4066Sahrens  * are stored numerically on disk.
587fa9e4066Sahrens  */
588fa9e4066Sahrens int
589fa9e4066Sahrens zfs_prop_is_string(zfs_prop_t prop)
590fa9e4066Sahrens {
591fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
592fa9e4066Sahrens 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
593fa9e4066Sahrens }
594fa9e4066Sahrens 
595fa9e4066Sahrens /*
596fa9e4066Sahrens  * Returns the column header for the given property.  Used only in
597fa9e4066Sahrens  * 'zfs list -o', but centralized here with the other property information.
598fa9e4066Sahrens  */
599fa9e4066Sahrens const char *
600fa9e4066Sahrens zfs_prop_column_name(zfs_prop_t prop)
601fa9e4066Sahrens {
602fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_colname);
603fa9e4066Sahrens }
604fa9e4066Sahrens 
605fa9e4066Sahrens /*
606e9dbad6fSeschrock  * Returns whether the given property should be displayed right-justified for
607e9dbad6fSeschrock  * 'zfs list'.
608fa9e4066Sahrens  */
609e9dbad6fSeschrock boolean_t
610e9dbad6fSeschrock zfs_prop_align_right(zfs_prop_t prop)
611fa9e4066Sahrens {
612e9dbad6fSeschrock 	return (zfs_prop_table[prop].pd_rightalign);
613fa9e4066Sahrens }
614fa9e4066Sahrens 
615fa9e4066Sahrens /*
616e9dbad6fSeschrock  * Determines the minimum width for the column, and indicates whether it's fixed
617e9dbad6fSeschrock  * or not.  Only string columns are non-fixed.
618fa9e4066Sahrens  */
619e9dbad6fSeschrock size_t
620e9dbad6fSeschrock zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
621fa9e4066Sahrens {
622e9dbad6fSeschrock 	prop_desc_t *pd = &zfs_prop_table[prop];
623e9dbad6fSeschrock 	zfs_index_t *idx;
624e9dbad6fSeschrock 	size_t ret;
625fa9e4066Sahrens 	int i;
626fa9e4066Sahrens 
627e9dbad6fSeschrock 	*fixed = B_TRUE;
628fa9e4066Sahrens 
62907ba0419Seschrock 	/*
630e9dbad6fSeschrock 	 * Start with the width of the column name.
63107ba0419Seschrock 	 */
632e9dbad6fSeschrock 	ret = strlen(pd->pd_colname);
633fa9e4066Sahrens 
634e9dbad6fSeschrock 	/*
635e9dbad6fSeschrock 	 * For fixed-width values, make sure the width is large enough to hold
636e9dbad6fSeschrock 	 * any possible value.
637e9dbad6fSeschrock 	 */
638e9dbad6fSeschrock 	switch (pd->pd_proptype) {
639e9dbad6fSeschrock 	case prop_type_number:
640e9dbad6fSeschrock 		/*
641e9dbad6fSeschrock 		 * The maximum length of a human-readable number is 5 characters
642e9dbad6fSeschrock 		 * ("20.4M", for example).
643e9dbad6fSeschrock 		 */
644e9dbad6fSeschrock 		if (ret < 5)
645e9dbad6fSeschrock 			ret = 5;
646e9dbad6fSeschrock 		/*
647e9dbad6fSeschrock 		 * 'creation' is handled specially because it's a number
648e9dbad6fSeschrock 		 * internally, but displayed as a date string.
649e9dbad6fSeschrock 		 */
650e9dbad6fSeschrock 		if (prop == ZFS_PROP_CREATION)
651e9dbad6fSeschrock 			*fixed = B_FALSE;
652e9dbad6fSeschrock 		break;
653e9dbad6fSeschrock 	case prop_type_boolean:
654e9dbad6fSeschrock 		/*
655e9dbad6fSeschrock 		 * The maximum length of a boolean value is 3 characters, for
656e9dbad6fSeschrock 		 * "off".
657e9dbad6fSeschrock 		 */
658e9dbad6fSeschrock 		if (ret < 3)
659e9dbad6fSeschrock 			ret = 3;
660e9dbad6fSeschrock 		break;
661e9dbad6fSeschrock 	case prop_type_index:
662e9dbad6fSeschrock 		idx = zfs_prop_index_table(prop);
663e9dbad6fSeschrock 		for (i = 0; idx[i].name != NULL; i++) {
664e9dbad6fSeschrock 			if (strlen(idx[i].name) > ret)
665e9dbad6fSeschrock 				ret = strlen(idx[i].name);
666fa9e4066Sahrens 		}
667e9dbad6fSeschrock 		break;
668fa9e4066Sahrens 
669e9dbad6fSeschrock 	case prop_type_string:
670e9dbad6fSeschrock 		*fixed = B_FALSE;
67107ba0419Seschrock 		break;
67207ba0419Seschrock 	}
67307ba0419Seschrock 
674e9dbad6fSeschrock 	return (ret);
675fa9e4066Sahrens }
676fa9e4066Sahrens 
677fa9e4066Sahrens #endif
678