xref: /illumos-gate/usr/src/common/zfs/zfs_prop.c (revision b1b8ab34de515a5e83206da22c3d7e563241b021)
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;
8166e2aaccSgw 	boolean_t	pd_visible;
82fa9e4066Sahrens } prop_desc_t;
83fa9e4066Sahrens 
8466e2aaccSgw static prop_desc_t zfs_prop_table[] = {
85fa9e4066Sahrens 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
8666e2aaccSgw 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE,
8766e2aaccSgw 	    B_TRUE },
88fa9e4066Sahrens 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
8966e2aaccSgw 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE },
90fa9e4066Sahrens 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
9166e2aaccSgw 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE, B_TRUE },
92fa9e4066Sahrens 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
9366e2aaccSgw 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE,
9466e2aaccSgw 	    B_TRUE },
95fa9e4066Sahrens 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
964f75c27eSeschrock 	    ZFS_TYPE_ANY,
9766e2aaccSgw 	    "<size>", "REFER", B_TRUE, B_TRUE },
98fa9e4066Sahrens 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
9966e2aaccSgw 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE,
10066e2aaccSgw 	    B_TRUE },
101fa9e4066Sahrens 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
10266e2aaccSgw 	    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",
10566e2aaccSgw 	    B_FALSE, B_TRUE },
106fa9e4066Sahrens 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
10766e2aaccSgw 	    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,
11066e2aaccSgw 	    "<size> | none", "RESERV", B_TRUE, B_TRUE },
111fa9e4066Sahrens 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
11266e2aaccSgw 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE },
1134f75c27eSeschrock 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
11466e2aaccSgw 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE,
11566e2aaccSgw 	    B_TRUE },
116fa9e4066Sahrens 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
117fa9e4066Sahrens 	    prop_inherit,
1184f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
11966e2aaccSgw 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE },
120fa9e4066Sahrens 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
121fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
12266e2aaccSgw 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE },
123fa9e4066Sahrens 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
124fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
12566e2aaccSgw 	    "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,
12866e2aaccSgw 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE,
12966e2aaccSgw 	    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,
13566e2aaccSgw 	    "on | off", "ATIME", B_TRUE, B_TRUE },
136fa9e4066Sahrens 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
13712054bfcSnd 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
13866e2aaccSgw 	    "on | off", "DEVICES", B_TRUE, B_TRUE },
139fa9e4066Sahrens 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
14012054bfcSnd 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
14166e2aaccSgw 	    "on | off", "EXEC", B_TRUE, B_TRUE },
142fa9e4066Sahrens 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
14312054bfcSnd 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
14466e2aaccSgw 	    B_TRUE, B_TRUE },
145fa9e4066Sahrens 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
1464f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
14766e2aaccSgw 	    "on | off", "RDONLY", B_TRUE, B_TRUE },
148fa9e4066Sahrens 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
1494f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
15066e2aaccSgw 	    "on | off", "ZONED", B_TRUE, B_TRUE },
151906d120cSlling 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
152a0965f35Sbonwick 	    prop_inherit,
153fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
15466e2aaccSgw 	    "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE },
155e9dbad6fSeschrock 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
156e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
15766e2aaccSgw 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE, B_TRUE },
158e9dbad6fSeschrock 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
159e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
16066e2aaccSgw 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE,
16166e2aaccSgw 	    B_TRUE },
16266e2aaccSgw 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
16366e2aaccSgw 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE },
16466e2aaccSgw 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
16566e2aaccSgw 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE },
166e9dbad6fSeschrock 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
167fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
16866e2aaccSgw 	    "on | off", "CANMOUNT", B_TRUE, B_TRUE },
16966e2aaccSgw 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
17066e2aaccSgw 	    ZFS_TYPE_ANY,
17166e2aaccSgw 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE },
17266e2aaccSgw 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
17366e2aaccSgw 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE },
1747b55fa8eSck 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
1757b55fa8eSck 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
17666e2aaccSgw 	    "on | off", "XATTR", B_TRUE, B_TRUE },
17739c23413Seschrock 	{ "numclones", prop_type_number,	0,	NULL,	prop_readonly,
17866e2aaccSgw 	    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 },
182*b1b8ab34Slling 	{ "bootfs", prop_type_string,	0,	NULL,	prop_default,
183*b1b8ab34Slling 	    ZFS_TYPE_POOL, "<filesystem>", "BOOTFS", B_FALSE, B_TRUE },
184fa9e4066Sahrens };
185fa9e4066Sahrens 
18666e2aaccSgw #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
18766e2aaccSgw 
188*b1b8ab34Slling /*
189*b1b8ab34Slling  * Returns TRUE if the property applies to the given dataset types.
190*b1b8ab34Slling  */
191*b1b8ab34Slling int
192*b1b8ab34Slling zfs_prop_valid_for_type(zfs_prop_t prop, int types)
193*b1b8ab34Slling {
194*b1b8ab34Slling 	return ((zfs_prop_table[prop].pd_types & types) != 0);
195*b1b8ab34Slling }
196*b1b8ab34Slling 
197*b1b8ab34Slling /*
198*b1b8ab34Slling  * Determine if the specified property is visible or not.
199*b1b8ab34Slling  */
200*b1b8ab34Slling boolean_t
201*b1b8ab34Slling zfs_prop_is_visible(zfs_prop_t prop)
202*b1b8ab34Slling {
203*b1b8ab34Slling 	if (prop < 0)
204*b1b8ab34Slling 		return (B_FALSE);
205*b1b8ab34Slling 
206*b1b8ab34Slling 	return (zfs_prop_table[prop].pd_visible);
207*b1b8ab34Slling }
208*b1b8ab34Slling 
209*b1b8ab34Slling /*
210*b1b8ab34Slling  * Iterate over all properties, calling back into the specified function
211*b1b8ab34Slling  * for each property. We will continue to iterate until we either
212*b1b8ab34Slling  * reach the end or the callback function something other than
213*b1b8ab34Slling  * ZFS_PROP_CONT.
214*b1b8ab34Slling  */
215*b1b8ab34Slling zfs_prop_t
216*b1b8ab34Slling zfs_prop_iter_common(zfs_prop_f func, void *cb, zfs_type_t type,
217*b1b8ab34Slling     boolean_t show_all)
218*b1b8ab34Slling {
219*b1b8ab34Slling 	int i;
220*b1b8ab34Slling 
221*b1b8ab34Slling 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
222*b1b8ab34Slling 		if (zfs_prop_valid_for_type(i, type) &&
223*b1b8ab34Slling 		    (zfs_prop_is_visible(i) || show_all)) {
224*b1b8ab34Slling 			if (func(i, cb) != ZFS_PROP_CONT)
225*b1b8ab34Slling 				return (i);
226*b1b8ab34Slling 		}
227*b1b8ab34Slling 	}
228*b1b8ab34Slling 	return (ZFS_PROP_CONT);
229*b1b8ab34Slling }
230*b1b8ab34Slling 
231*b1b8ab34Slling zfs_prop_t
232*b1b8ab34Slling zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
233*b1b8ab34Slling {
234*b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_ANY, show_all));
235*b1b8ab34Slling }
236*b1b8ab34Slling 
237*b1b8ab34Slling zpool_prop_t
238*b1b8ab34Slling zpool_prop_iter(zpool_prop_f func, void *cb, boolean_t show_all)
239*b1b8ab34Slling {
240*b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_POOL, show_all));
241*b1b8ab34Slling }
242*b1b8ab34Slling 
243fa9e4066Sahrens zfs_proptype_t
244fa9e4066Sahrens zfs_prop_get_type(zfs_prop_t prop)
245fa9e4066Sahrens {
246fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype);
247fa9e4066Sahrens }
248fa9e4066Sahrens 
249e9dbad6fSeschrock static boolean_t
250e9dbad6fSeschrock propname_match(const char *p, zfs_prop_t prop, size_t len)
2515c709891Seschrock {
2525c709891Seschrock 	const char *propname = zfs_prop_table[prop].pd_name;
2535c709891Seschrock #ifndef _KERNEL
2545c709891Seschrock 	const char *colname = zfs_prop_table[prop].pd_colname;
2555c709891Seschrock 	int c;
2565c709891Seschrock #endif
2575c709891Seschrock 
2583bb79becSeschrock #ifndef _KERNEL
2593bb79becSeschrock 	if (colname == NULL)
260e9dbad6fSeschrock 		return (B_FALSE);
2613bb79becSeschrock #endif
2623bb79becSeschrock 
2635c709891Seschrock 	if (len == strlen(propname) &&
2645c709891Seschrock 	    strncmp(p, propname, len) == 0)
265e9dbad6fSeschrock 		return (B_TRUE);
2665c709891Seschrock 
2675c709891Seschrock #ifndef _KERNEL
2683bb79becSeschrock 	if (len != strlen(colname))
269e9dbad6fSeschrock 		return (B_FALSE);
2705c709891Seschrock 
2715c709891Seschrock 	for (c = 0; c < len; c++)
2725c709891Seschrock 		if (p[c] != tolower(colname[c]))
2735c709891Seschrock 			break;
2745c709891Seschrock 
2755c709891Seschrock 	return (colname[c] == '\0');
2765c709891Seschrock #else
277e9dbad6fSeschrock 	return (B_FALSE);
2785c709891Seschrock #endif
2795c709891Seschrock }
2805c709891Seschrock 
28166e2aaccSgw zfs_prop_t
28266e2aaccSgw zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
28366e2aaccSgw {
28466e2aaccSgw 	const char *propname = cb_data;
28566e2aaccSgw 
28666e2aaccSgw 	if (propname_match(propname, prop, strlen(propname)))
28766e2aaccSgw 		return (prop);
28866e2aaccSgw 
28966e2aaccSgw 	return (ZFS_PROP_CONT);
29066e2aaccSgw }
2915c709891Seschrock 
292fa9e4066Sahrens /*
293*b1b8ab34Slling  * Given a property name and its type, returns the corresponding property ID.
294fa9e4066Sahrens  */
295fa9e4066Sahrens zfs_prop_t
296*b1b8ab34Slling zfs_name_to_prop_common(const char *propname, zfs_type_t type)
297fa9e4066Sahrens {
29866e2aaccSgw 	zfs_prop_t prop;
299fa9e4066Sahrens 
300*b1b8ab34Slling 	prop = zfs_prop_iter_common(zfs_name_to_prop_cb, (void *)propname,
301*b1b8ab34Slling 	    type, B_TRUE);
30266e2aaccSgw 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
303fa9e4066Sahrens }
304fa9e4066Sahrens 
305*b1b8ab34Slling /*
306*b1b8ab34Slling  * Given a zfs dataset property name, returns the corresponding property ID.
307*b1b8ab34Slling  */
308*b1b8ab34Slling zfs_prop_t
309*b1b8ab34Slling zfs_name_to_prop(const char *propname)
310*b1b8ab34Slling {
311*b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_ANY));
312*b1b8ab34Slling }
313*b1b8ab34Slling 
314*b1b8ab34Slling /*
315*b1b8ab34Slling  * Given a pool property name, returns the corresponding property ID.
316*b1b8ab34Slling  */
317*b1b8ab34Slling zpool_prop_t
318*b1b8ab34Slling zpool_name_to_prop(const char *propname)
319*b1b8ab34Slling {
320*b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_POOL));
321*b1b8ab34Slling }
322*b1b8ab34Slling 
323e9dbad6fSeschrock /*
324e9dbad6fSeschrock  * For user property names, we allow all lowercase alphanumeric characters, plus
325e9dbad6fSeschrock  * a few useful punctuation characters.
326e9dbad6fSeschrock  */
327e9dbad6fSeschrock static int
328e9dbad6fSeschrock valid_char(char c)
329e9dbad6fSeschrock {
330e9dbad6fSeschrock 	return ((c >= 'a' && c <= 'z') ||
331e9dbad6fSeschrock 	    (c >= '0' && c <= '9') ||
332e9dbad6fSeschrock 	    c == '-' || c == '_' || c == '.' || c == ':');
333e9dbad6fSeschrock }
334e9dbad6fSeschrock 
335e9dbad6fSeschrock /*
336e9dbad6fSeschrock  * Returns true if this is a valid user-defined property (one with a ':').
337e9dbad6fSeschrock  */
338e9dbad6fSeschrock boolean_t
339e9dbad6fSeschrock zfs_prop_user(const char *name)
340e9dbad6fSeschrock {
341e9dbad6fSeschrock 	int i;
342e9dbad6fSeschrock 	char c;
343e9dbad6fSeschrock 	boolean_t foundsep = B_FALSE;
344e9dbad6fSeschrock 
345e9dbad6fSeschrock 	for (i = 0; i < strlen(name); i++) {
346e9dbad6fSeschrock 		c = name[i];
347e9dbad6fSeschrock 		if (!valid_char(c))
348e9dbad6fSeschrock 			return (B_FALSE);
349e9dbad6fSeschrock 		if (c == ':')
350e9dbad6fSeschrock 			foundsep = B_TRUE;
351e9dbad6fSeschrock 	}
352e9dbad6fSeschrock 
353e9dbad6fSeschrock 	if (!foundsep)
354e9dbad6fSeschrock 		return (B_FALSE);
355e9dbad6fSeschrock 
356e9dbad6fSeschrock 	return (B_TRUE);
357e9dbad6fSeschrock }
358e9dbad6fSeschrock 
359fa9e4066Sahrens /*
360fa9e4066Sahrens  * Return the default value for the given property.
361fa9e4066Sahrens  */
3627f7322feSeschrock const char *
3637f7322feSeschrock zfs_prop_default_string(zfs_prop_t prop)
364fa9e4066Sahrens {
3657f7322feSeschrock 	return (zfs_prop_table[prop].pd_strdefault);
366fa9e4066Sahrens }
367fa9e4066Sahrens 
368fa9e4066Sahrens uint64_t
369fa9e4066Sahrens zfs_prop_default_numeric(zfs_prop_t prop)
370fa9e4066Sahrens {
371fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_numdefault);
372fa9e4066Sahrens }
373fa9e4066Sahrens 
374fa9e4066Sahrens /*
375fa9e4066Sahrens  * Returns TRUE if the property is readonly.
376fa9e4066Sahrens  */
377fa9e4066Sahrens int
378fa9e4066Sahrens zfs_prop_readonly(zfs_prop_t prop)
379fa9e4066Sahrens {
380fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
381fa9e4066Sahrens }
382fa9e4066Sahrens 
383fa9e4066Sahrens /*
384*b1b8ab34Slling  * Given a dataset property ID, returns the corresponding name.
385*b1b8ab34Slling  * Assuming the zfs dataset propety ID is valid.
386fa9e4066Sahrens  */
387fa9e4066Sahrens const char *
388fa9e4066Sahrens zfs_prop_to_name(zfs_prop_t prop)
389fa9e4066Sahrens {
390fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_name);
391fa9e4066Sahrens }
392fa9e4066Sahrens 
393*b1b8ab34Slling /*
394*b1b8ab34Slling  * Given a pool property ID, returns the corresponding name.
395*b1b8ab34Slling  * Assuming the pool propety ID is valid.
396*b1b8ab34Slling  */
397*b1b8ab34Slling const char *
398*b1b8ab34Slling zpool_prop_to_name(zpool_prop_t prop)
399*b1b8ab34Slling {
400*b1b8ab34Slling 	return (zfs_prop_table[prop].pd_name);
401*b1b8ab34Slling }
402*b1b8ab34Slling 
403fa9e4066Sahrens /*
404fa9e4066Sahrens  * Returns TRUE if the property is inheritable.
405fa9e4066Sahrens  */
406fa9e4066Sahrens int
407fa9e4066Sahrens zfs_prop_inheritable(zfs_prop_t prop)
408fa9e4066Sahrens {
409fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
410fa9e4066Sahrens }
411fa9e4066Sahrens 
412e9dbad6fSeschrock typedef struct zfs_index {
413e9dbad6fSeschrock 	const char *name;
414e9dbad6fSeschrock 	uint64_t index;
415e9dbad6fSeschrock } zfs_index_t;
416e9dbad6fSeschrock 
417e9dbad6fSeschrock static zfs_index_t checksum_table[] = {
418e9dbad6fSeschrock 	{ "on",		ZIO_CHECKSUM_ON },
419e9dbad6fSeschrock 	{ "off",	ZIO_CHECKSUM_OFF },
420e9dbad6fSeschrock 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
421e9dbad6fSeschrock 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
422e9dbad6fSeschrock 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
423e9dbad6fSeschrock 	{ NULL }
424e9dbad6fSeschrock };
425e9dbad6fSeschrock 
426e9dbad6fSeschrock static zfs_index_t compress_table[] = {
427e9dbad6fSeschrock 	{ "on",		ZIO_COMPRESS_ON },
428e9dbad6fSeschrock 	{ "off",	ZIO_COMPRESS_OFF },
429e9dbad6fSeschrock 	{ "lzjb",	ZIO_COMPRESS_LZJB },
430c9431fa1Sahl 	{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* the default gzip level */
431c9431fa1Sahl 	{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
432c9431fa1Sahl 	{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
433c9431fa1Sahl 	{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
434c9431fa1Sahl 	{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
435c9431fa1Sahl 	{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
436c9431fa1Sahl 	{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
437c9431fa1Sahl 	{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
438c9431fa1Sahl 	{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
439c9431fa1Sahl 	{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
440e9dbad6fSeschrock 	{ NULL }
441e9dbad6fSeschrock };
442e9dbad6fSeschrock 
443e9dbad6fSeschrock static zfs_index_t snapdir_table[] = {
444e9dbad6fSeschrock 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
445e9dbad6fSeschrock 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
446e9dbad6fSeschrock 	{ NULL }
447e9dbad6fSeschrock };
448e9dbad6fSeschrock 
449e9dbad6fSeschrock static zfs_index_t acl_mode_table[] = {
450e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
451e9dbad6fSeschrock 	{ "groupmask",	ZFS_ACL_GROUPMASK },
452e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
453e9dbad6fSeschrock 	{ NULL }
454e9dbad6fSeschrock };
455e9dbad6fSeschrock 
456e9dbad6fSeschrock static zfs_index_t acl_inherit_table[] = {
457e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
458e9dbad6fSeschrock 	{ "noallow",	ZFS_ACL_NOALLOW },
459e9dbad6fSeschrock 	{ "secure",	ZFS_ACL_SECURE },
460e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
461e9dbad6fSeschrock 	{ NULL }
462e9dbad6fSeschrock };
463e9dbad6fSeschrock 
464d0ad202dSahrens static zfs_index_t copies_table[] = {
465d0ad202dSahrens 	{ "1",	1 },
466d0ad202dSahrens 	{ "2",	2 },
467d0ad202dSahrens 	{ "3",	3 },
468d0ad202dSahrens 	{ NULL }
469d0ad202dSahrens };
470d0ad202dSahrens 
471e9dbad6fSeschrock static zfs_index_t *
472e9dbad6fSeschrock zfs_prop_index_table(zfs_prop_t prop)
473e9dbad6fSeschrock {
474e9dbad6fSeschrock 	switch (prop) {
475e9dbad6fSeschrock 	case ZFS_PROP_CHECKSUM:
476e9dbad6fSeschrock 		return (checksum_table);
477e9dbad6fSeschrock 	case ZFS_PROP_COMPRESSION:
478e9dbad6fSeschrock 		return (compress_table);
479e9dbad6fSeschrock 	case ZFS_PROP_SNAPDIR:
480e9dbad6fSeschrock 		return (snapdir_table);
481e9dbad6fSeschrock 	case ZFS_PROP_ACLMODE:
482e9dbad6fSeschrock 		return (acl_mode_table);
483e9dbad6fSeschrock 	case ZFS_PROP_ACLINHERIT:
484e9dbad6fSeschrock 		return (acl_inherit_table);
485d0ad202dSahrens 	case ZFS_PROP_COPIES:
486d0ad202dSahrens 		return (copies_table);
487e9dbad6fSeschrock 	default:
488e9dbad6fSeschrock 		return (NULL);
489e9dbad6fSeschrock 	}
490e9dbad6fSeschrock }
491e9dbad6fSeschrock 
492e9dbad6fSeschrock 
493e9dbad6fSeschrock /*
494e9dbad6fSeschrock  * Tables of index types, plus functions to convert between the user view
495e9dbad6fSeschrock  * (strings) and internal representation (uint64_t).
496e9dbad6fSeschrock  */
497e9dbad6fSeschrock int
498e9dbad6fSeschrock zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
499e9dbad6fSeschrock {
500e9dbad6fSeschrock 	zfs_index_t *table;
501e9dbad6fSeschrock 	int i;
502e9dbad6fSeschrock 
503e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
504e9dbad6fSeschrock 		return (-1);
505e9dbad6fSeschrock 
506e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
507e9dbad6fSeschrock 		if (strcmp(string, table[i].name) == 0) {
508e9dbad6fSeschrock 			*index = table[i].index;
509e9dbad6fSeschrock 			return (0);
510e9dbad6fSeschrock 		}
511e9dbad6fSeschrock 	}
512e9dbad6fSeschrock 
513e9dbad6fSeschrock 	return (-1);
514e9dbad6fSeschrock }
515e9dbad6fSeschrock 
516e9dbad6fSeschrock int
517e9dbad6fSeschrock zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
518e9dbad6fSeschrock {
519e9dbad6fSeschrock 	zfs_index_t *table;
520e9dbad6fSeschrock 	int i;
521e9dbad6fSeschrock 
522e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
523e9dbad6fSeschrock 		return (-1);
524e9dbad6fSeschrock 
525e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
526e9dbad6fSeschrock 		if (table[i].index == index) {
527e9dbad6fSeschrock 			*string = table[i].name;
528e9dbad6fSeschrock 			return (0);
529e9dbad6fSeschrock 		}
530e9dbad6fSeschrock 	}
531e9dbad6fSeschrock 
532e9dbad6fSeschrock 	return (-1);
533e9dbad6fSeschrock }
534e9dbad6fSeschrock 
535acd76fe5Seschrock #ifndef _KERNEL
536acd76fe5Seschrock 
537fa9e4066Sahrens /*
538*b1b8ab34Slling  * Returns a string describing the set of acceptable values for the given
539*b1b8ab34Slling  * zfs property, or NULL if it cannot be set.
540fa9e4066Sahrens  */
541*b1b8ab34Slling const char *
542*b1b8ab34Slling zfs_prop_values(zfs_prop_t prop)
543fa9e4066Sahrens {
544*b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types == ZFS_TYPE_POOL)
545*b1b8ab34Slling 		return (NULL);
546*b1b8ab34Slling 
547*b1b8ab34Slling 	return (zfs_prop_table[prop].pd_values);
548fa9e4066Sahrens }
549fa9e4066Sahrens 
550fa9e4066Sahrens /*
551fa9e4066Sahrens  * Returns a string describing the set of acceptable values for the given
552*b1b8ab34Slling  * zpool property, or NULL if it cannot be set.
553fa9e4066Sahrens  */
554fa9e4066Sahrens const char *
555*b1b8ab34Slling zpool_prop_values(zfs_prop_t prop)
556fa9e4066Sahrens {
557*b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types != ZFS_TYPE_POOL)
558*b1b8ab34Slling 		return (NULL);
559*b1b8ab34Slling 
560fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_values);
561fa9e4066Sahrens }
562fa9e4066Sahrens 
563fa9e4066Sahrens /*
564fa9e4066Sahrens  * Returns TRUE if this property is a string type.  Note that index types
565fa9e4066Sahrens  * (compression, checksum) are treated as strings in userland, even though they
566fa9e4066Sahrens  * are stored numerically on disk.
567fa9e4066Sahrens  */
568fa9e4066Sahrens int
569fa9e4066Sahrens zfs_prop_is_string(zfs_prop_t prop)
570fa9e4066Sahrens {
571fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
572fa9e4066Sahrens 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
573fa9e4066Sahrens }
574fa9e4066Sahrens 
575fa9e4066Sahrens /*
576fa9e4066Sahrens  * Returns the column header for the given property.  Used only in
577fa9e4066Sahrens  * 'zfs list -o', but centralized here with the other property information.
578fa9e4066Sahrens  */
579fa9e4066Sahrens const char *
580fa9e4066Sahrens zfs_prop_column_name(zfs_prop_t prop)
581fa9e4066Sahrens {
582fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_colname);
583fa9e4066Sahrens }
584fa9e4066Sahrens 
585fa9e4066Sahrens /*
586e9dbad6fSeschrock  * Returns whether the given property should be displayed right-justified for
587e9dbad6fSeschrock  * 'zfs list'.
588fa9e4066Sahrens  */
589e9dbad6fSeschrock boolean_t
590e9dbad6fSeschrock zfs_prop_align_right(zfs_prop_t prop)
591fa9e4066Sahrens {
592e9dbad6fSeschrock 	return (zfs_prop_table[prop].pd_rightalign);
593fa9e4066Sahrens }
594fa9e4066Sahrens 
595fa9e4066Sahrens /*
596e9dbad6fSeschrock  * Determines the minimum width for the column, and indicates whether it's fixed
597e9dbad6fSeschrock  * or not.  Only string columns are non-fixed.
598fa9e4066Sahrens  */
599e9dbad6fSeschrock size_t
600e9dbad6fSeschrock zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
601fa9e4066Sahrens {
602e9dbad6fSeschrock 	prop_desc_t *pd = &zfs_prop_table[prop];
603e9dbad6fSeschrock 	zfs_index_t *idx;
604e9dbad6fSeschrock 	size_t ret;
605fa9e4066Sahrens 	int i;
606fa9e4066Sahrens 
607e9dbad6fSeschrock 	*fixed = B_TRUE;
608fa9e4066Sahrens 
60907ba0419Seschrock 	/*
610e9dbad6fSeschrock 	 * Start with the width of the column name.
61107ba0419Seschrock 	 */
612e9dbad6fSeschrock 	ret = strlen(pd->pd_colname);
613fa9e4066Sahrens 
61407ba0419Seschrock 	/*
615e9dbad6fSeschrock 	 * For fixed-width values, make sure the width is large enough to hold
616e9dbad6fSeschrock 	 * any possible value.
61707ba0419Seschrock 	 */
618e9dbad6fSeschrock 	switch (pd->pd_proptype) {
619e9dbad6fSeschrock 	case prop_type_number:
62007ba0419Seschrock 		/*
621e9dbad6fSeschrock 		 * The maximum length of a human-readable number is 5 characters
622e9dbad6fSeschrock 		 * ("20.4M", for example).
62307ba0419Seschrock 		 */
624e9dbad6fSeschrock 		if (ret < 5)
625e9dbad6fSeschrock 			ret = 5;
62607ba0419Seschrock 		/*
627e9dbad6fSeschrock 		 * 'creation' is handled specially because it's a number
628e9dbad6fSeschrock 		 * internally, but displayed as a date string.
62907ba0419Seschrock 		 */
630e9dbad6fSeschrock 		if (prop == ZFS_PROP_CREATION)
631e9dbad6fSeschrock 			*fixed = B_FALSE;
632e9dbad6fSeschrock 		break;
633e9dbad6fSeschrock 	case prop_type_boolean:
63407ba0419Seschrock 		/*
635e9dbad6fSeschrock 		 * The maximum length of a boolean value is 3 characters, for
636e9dbad6fSeschrock 		 * "off".
63707ba0419Seschrock 		 */
638e9dbad6fSeschrock 		if (ret < 3)
639e9dbad6fSeschrock 			ret = 3;
640e9dbad6fSeschrock 		break;
641e9dbad6fSeschrock 	case prop_type_index:
642e9dbad6fSeschrock 		idx = zfs_prop_index_table(prop);
643e9dbad6fSeschrock 		for (i = 0; idx[i].name != NULL; i++) {
644e9dbad6fSeschrock 			if (strlen(idx[i].name) > ret)
645e9dbad6fSeschrock 				ret = strlen(idx[i].name);
64607ba0419Seschrock 		}
647e9dbad6fSeschrock 		break;
64807ba0419Seschrock 
649e9dbad6fSeschrock 	case prop_type_string:
650e9dbad6fSeschrock 		*fixed = B_FALSE;
651e9dbad6fSeschrock 		break;
65207ba0419Seschrock 	}
653fa9e4066Sahrens 
654e9dbad6fSeschrock 	return (ret);
655fa9e4066Sahrens }
656fa9e4066Sahrens 
657fa9e4066Sahrens #endif
658