xref: /titanic_53/usr/src/common/zfs/zfs_prop.c (revision e7437265dc2a4920c197ed4337665539d358b22c)
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>
54*e7437265Sahrens #include <sys/zfs_znode.h>
55fa9e4066Sahrens 
56fa9e4066Sahrens #include "zfs_prop.h"
57ecd6cf80Smarks #include "zfs_deleg.h"
58fa9e4066Sahrens 
59fa9e4066Sahrens #if defined(_KERNEL)
60fa9e4066Sahrens #include <sys/systm.h>
61fa9e4066Sahrens #else
62fa9e4066Sahrens #include <stdlib.h>
63fa9e4066Sahrens #include <string.h>
64fa9e4066Sahrens #include <ctype.h>
65fa9e4066Sahrens #endif
66fa9e4066Sahrens 
67fa9e4066Sahrens typedef enum {
68fa9e4066Sahrens 	prop_default,
69fa9e4066Sahrens 	prop_readonly,
70fa9e4066Sahrens 	prop_inherit
71fa9e4066Sahrens } prop_attr_t;
72fa9e4066Sahrens 
73fa9e4066Sahrens typedef struct {
74fa9e4066Sahrens 	const char	*pd_name;
75fa9e4066Sahrens 	zfs_proptype_t	pd_proptype;
76fa9e4066Sahrens 	uint64_t	pd_numdefault;
77fa9e4066Sahrens 	const char	*pd_strdefault;
78fa9e4066Sahrens 	prop_attr_t	pd_attr;
79fa9e4066Sahrens 	int		pd_types;
80fa9e4066Sahrens 	const char	*pd_values;
81fa9e4066Sahrens 	const char	*pd_colname;
82e9dbad6fSeschrock 	boolean_t	pd_rightalign;
8366e2aaccSgw25295 	boolean_t	pd_visible;
84ecd6cf80Smarks 	const char	*pd_perm;
85fa9e4066Sahrens } prop_desc_t;
86fa9e4066Sahrens 
8766e2aaccSgw25295 static prop_desc_t zfs_prop_table[] = {
88fa9e4066Sahrens 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
8966e2aaccSgw25295 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE,
90ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
91fa9e4066Sahrens 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
92ecd6cf80Smarks 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE,
93ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
94fa9e4066Sahrens 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
95ecd6cf80Smarks 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE, B_TRUE,
96ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
97fa9e4066Sahrens 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
9866e2aaccSgw25295 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE,
99ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
100fa9e4066Sahrens 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
1014f75c27eSeschrock 	    ZFS_TYPE_ANY,
102ecd6cf80Smarks 	    "<size>", "REFER", B_TRUE, B_TRUE, ZFS_DELEG_PERM_NONE },
103fa9e4066Sahrens 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
10466e2aaccSgw25295 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE,
105ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
106fa9e4066Sahrens 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
107ecd6cf80Smarks 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE, B_TRUE,
108ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
109fa9e4066Sahrens 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
110101f5531Slling 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
111ecd6cf80Smarks 	    B_FALSE, B_TRUE, ZFS_DELEG_PERM_NONE },
112fa9e4066Sahrens 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
113ecd6cf80Smarks 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE, B_TRUE,
114ecd6cf80Smarks 	    ZFS_DELEG_PERM_QUOTA },
115fa9e4066Sahrens 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
116fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
117ecd6cf80Smarks 	    "<size> | none", "RESERV", B_TRUE, B_TRUE,
118ecd6cf80Smarks 	    ZFS_DELEG_PERM_RESERVATION },
119fa9e4066Sahrens 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
120ecd6cf80Smarks 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE,
121ecd6cf80Smarks 	    ZFS_DELEG_PERM_VOLSIZE },
1224f75c27eSeschrock 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
12366e2aaccSgw25295 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE,
124ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
125fa9e4066Sahrens 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
126fa9e4066Sahrens 	    prop_inherit,
1274f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
128ecd6cf80Smarks 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE,
129ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECORDSIZE },
130fa9e4066Sahrens 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
131fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
132ecd6cf80Smarks 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE,
133ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNTPOINT },
134fa9e4066Sahrens 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
135fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
136ecd6cf80Smarks 	    "on | off | share(1M) options", "SHARENFS", B_FALSE, B_TRUE,
137ecd6cf80Smarks 	    ZFS_DELEG_PERM_SHARENFS },
138101f5531Slling 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
1394f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
14066e2aaccSgw25295 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE,
141ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_CHECKSUM },
142101f5531Slling 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
1434f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
144ecd6cf80Smarks 	    "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", B_TRUE, B_TRUE,
145ecd6cf80Smarks 	    ZFS_DELEG_PERM_COMPRESSION },
146fa9e4066Sahrens 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
147fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
148ecd6cf80Smarks 	    "on | off", "ATIME", B_TRUE, B_TRUE, ZFS_DELEG_PERM_ATIME },
149fa9e4066Sahrens 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
15012054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
151ecd6cf80Smarks 	    "on | off", "DEVICES", B_TRUE, B_TRUE, ZFS_DELEG_PERM_DEVICES },
152fa9e4066Sahrens 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
15312054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
154ecd6cf80Smarks 	    "on | off", "EXEC", B_TRUE, B_TRUE, ZFS_DELEG_PERM_EXEC },
155fa9e4066Sahrens 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
15612054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
157ecd6cf80Smarks 	    B_TRUE, B_TRUE, ZFS_DELEG_PERM_SETUID },
158fa9e4066Sahrens 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
1594f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
160ecd6cf80Smarks 	    "on | off", "RDONLY", B_TRUE, B_TRUE, ZFS_DELEG_PERM_READONLY },
161fa9e4066Sahrens 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
1624f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
163ecd6cf80Smarks 	    "on | off", "ZONED", B_TRUE, B_TRUE, ZFS_DELEG_PERM_ZONED },
164906d120cSlling 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
165a0965f35Sbonwick 	    prop_inherit,
166fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
167ecd6cf80Smarks 	    "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE,
168ecd6cf80Smarks 	    ZFS_DELEG_PERM_SNAPDIR },
169e9dbad6fSeschrock 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
170e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
171ecd6cf80Smarks 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE,
172ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_ACLMODE },
173e9dbad6fSeschrock 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
174e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
17566e2aaccSgw25295 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE,
176ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_ACLINHERIT },
17766e2aaccSgw25295 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
178ecd6cf80Smarks 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE, ZFS_DELEG_PERM_NONE },
17966e2aaccSgw25295 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
180ecd6cf80Smarks 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE, ZFS_DELEG_PERM_NONE },
181e9dbad6fSeschrock 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
182fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
183ecd6cf80Smarks 	    "on | off", "CANMOUNT", B_TRUE, B_TRUE, ZFS_DELEG_PERM_CANMOUNT },
18466e2aaccSgw25295 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
18566e2aaccSgw25295 	    ZFS_TYPE_ANY,
186ecd6cf80Smarks 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE,
187ecd6cf80Smarks 	    ZFS_DELEG_PERM_SHAREISCSI },
18866e2aaccSgw25295 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
189ecd6cf80Smarks 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE,
190ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
1917b55fa8eSck153898 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
1927b55fa8eSck153898 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
193ecd6cf80Smarks 	    "on | off", "XATTR", B_TRUE, B_TRUE, ZFS_DELEG_PERM_XATTR },
19439c23413Seschrock 	{ "numclones", prop_type_number,	0,	NULL,	prop_readonly,
195ecd6cf80Smarks 	    ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE, B_FALSE,
196ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
197d0ad202dSahrens 	{ "copies",	prop_type_index,	1,	"1",	prop_inherit,
198d0ad202dSahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
199ecd6cf80Smarks 	    "1 | 2 | 3", "COPIES", B_TRUE, B_TRUE, ZFS_DELEG_PERM_COPIES },
200b1b8ab34Slling 	{ "bootfs", prop_type_string,	0,	NULL,	prop_default,
201ecd6cf80Smarks 	    ZFS_TYPE_POOL, "<filesystem>", "BOOTFS", B_FALSE,
202ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
2033d7072f8Seschrock 	{ "autoreplace", prop_type_boolean,	0,	NULL, prop_default,
204ecd6cf80Smarks 	    ZFS_TYPE_POOL, "on | off", "REPLACE", B_FALSE, B_TRUE,
205ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
206ecd6cf80Smarks 	{ "delegation", prop_type_boolean,	1,	NULL,	prop_default,
207ecd6cf80Smarks 	    ZFS_TYPE_POOL, "on | off", "DELEGATION", B_TRUE,
208*e7437265Sahrens 	    B_TRUE, ZFS_DELEG_PERM_NONE },
209*e7437265Sahrens 	{ "version",	prop_type_index,	0,	NULL,	prop_default,
210*e7437265Sahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "1 | 2 | current",
211*e7437265Sahrens 	    "VERSION", B_TRUE, B_TRUE, ZFS_DELEG_PERM_VERSION },
212fa9e4066Sahrens };
213fa9e4066Sahrens 
21466e2aaccSgw25295 #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
21566e2aaccSgw25295 
216b1b8ab34Slling /*
217b1b8ab34Slling  * Returns TRUE if the property applies to the given dataset types.
218b1b8ab34Slling  */
219b1b8ab34Slling int
220b1b8ab34Slling zfs_prop_valid_for_type(zfs_prop_t prop, int types)
221b1b8ab34Slling {
222b1b8ab34Slling 	return ((zfs_prop_table[prop].pd_types & types) != 0);
223b1b8ab34Slling }
224b1b8ab34Slling 
225b1b8ab34Slling /*
226b1b8ab34Slling  * Determine if the specified property is visible or not.
227b1b8ab34Slling  */
228b1b8ab34Slling boolean_t
229b1b8ab34Slling zfs_prop_is_visible(zfs_prop_t prop)
230b1b8ab34Slling {
231b1b8ab34Slling 	if (prop < 0)
232b1b8ab34Slling 		return (B_FALSE);
233b1b8ab34Slling 
234b1b8ab34Slling 	return (zfs_prop_table[prop].pd_visible);
235b1b8ab34Slling }
236b1b8ab34Slling 
237b1b8ab34Slling /*
238b1b8ab34Slling  * Iterate over all properties, calling back into the specified function
239b1b8ab34Slling  * for each property. We will continue to iterate until we either
240b1b8ab34Slling  * reach the end or the callback function something other than
241b1b8ab34Slling  * ZFS_PROP_CONT.
242b1b8ab34Slling  */
243b1b8ab34Slling zfs_prop_t
244b1b8ab34Slling zfs_prop_iter_common(zfs_prop_f func, void *cb, zfs_type_t type,
245b1b8ab34Slling     boolean_t show_all)
246b1b8ab34Slling {
247b1b8ab34Slling 	int i;
248b1b8ab34Slling 
249b1b8ab34Slling 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
250b1b8ab34Slling 		if (zfs_prop_valid_for_type(i, type) &&
251b1b8ab34Slling 		    (zfs_prop_is_visible(i) || show_all)) {
252b1b8ab34Slling 			if (func(i, cb) != ZFS_PROP_CONT)
253b1b8ab34Slling 				return (i);
254b1b8ab34Slling 		}
255b1b8ab34Slling 	}
256b1b8ab34Slling 	return (ZFS_PROP_CONT);
257b1b8ab34Slling }
258b1b8ab34Slling 
259b1b8ab34Slling zfs_prop_t
260b1b8ab34Slling zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
261b1b8ab34Slling {
262b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_ANY, show_all));
263b1b8ab34Slling }
264b1b8ab34Slling 
265b1b8ab34Slling zpool_prop_t
266b1b8ab34Slling zpool_prop_iter(zpool_prop_f func, void *cb, boolean_t show_all)
267b1b8ab34Slling {
268b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_POOL, show_all));
269b1b8ab34Slling }
270b1b8ab34Slling 
271fa9e4066Sahrens zfs_proptype_t
272fa9e4066Sahrens zfs_prop_get_type(zfs_prop_t prop)
273fa9e4066Sahrens {
274fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype);
275fa9e4066Sahrens }
276fa9e4066Sahrens 
2773d7072f8Seschrock zfs_proptype_t
2783d7072f8Seschrock zpool_prop_get_type(zfs_prop_t prop)
2793d7072f8Seschrock {
2803d7072f8Seschrock 	return (zfs_prop_table[prop].pd_proptype);
2813d7072f8Seschrock }
2823d7072f8Seschrock 
283e9dbad6fSeschrock static boolean_t
284e9dbad6fSeschrock propname_match(const char *p, zfs_prop_t prop, size_t len)
2855c709891Seschrock {
2865c709891Seschrock 	const char *propname = zfs_prop_table[prop].pd_name;
2875c709891Seschrock #ifndef _KERNEL
2885c709891Seschrock 	const char *colname = zfs_prop_table[prop].pd_colname;
2895c709891Seschrock 	int c;
2905c709891Seschrock #endif
2915c709891Seschrock 
2923bb79becSeschrock #ifndef _KERNEL
2933bb79becSeschrock 	if (colname == NULL)
294e9dbad6fSeschrock 		return (B_FALSE);
2953bb79becSeschrock #endif
2963bb79becSeschrock 
2975c709891Seschrock 	if (len == strlen(propname) &&
2985c709891Seschrock 	    strncmp(p, propname, len) == 0)
299e9dbad6fSeschrock 		return (B_TRUE);
3005c709891Seschrock 
3015c709891Seschrock #ifndef _KERNEL
3023bb79becSeschrock 	if (len != strlen(colname))
303e9dbad6fSeschrock 		return (B_FALSE);
3045c709891Seschrock 
3055c709891Seschrock 	for (c = 0; c < len; c++)
3065c709891Seschrock 		if (p[c] != tolower(colname[c]))
3075c709891Seschrock 			break;
3085c709891Seschrock 
3095c709891Seschrock 	return (colname[c] == '\0');
3105c709891Seschrock #else
311e9dbad6fSeschrock 	return (B_FALSE);
3125c709891Seschrock #endif
3135c709891Seschrock }
3145c709891Seschrock 
31566e2aaccSgw25295 zfs_prop_t
31666e2aaccSgw25295 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
31766e2aaccSgw25295 {
31866e2aaccSgw25295 	const char *propname = cb_data;
31966e2aaccSgw25295 
320ecd6cf80Smarks 	if (propname_match(propname, prop, strlen(propname))) {
32166e2aaccSgw25295 		return (prop);
322ecd6cf80Smarks 	}
32366e2aaccSgw25295 
32466e2aaccSgw25295 	return (ZFS_PROP_CONT);
32566e2aaccSgw25295 }
3265c709891Seschrock 
327fa9e4066Sahrens /*
328b1b8ab34Slling  * Given a property name and its type, returns the corresponding property ID.
329b1b8ab34Slling  */
330b1b8ab34Slling zfs_prop_t
331b1b8ab34Slling zfs_name_to_prop_common(const char *propname, zfs_type_t type)
332b1b8ab34Slling {
333b1b8ab34Slling 	zfs_prop_t prop;
334b1b8ab34Slling 
335b1b8ab34Slling 	prop = zfs_prop_iter_common(zfs_name_to_prop_cb, (void *)propname,
336b1b8ab34Slling 	    type, B_TRUE);
337b1b8ab34Slling 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
338b1b8ab34Slling }
339b1b8ab34Slling 
340b1b8ab34Slling /*
341b1b8ab34Slling  * Given a zfs dataset property name, returns the corresponding property ID.
342fa9e4066Sahrens  */
343fa9e4066Sahrens zfs_prop_t
344fa9e4066Sahrens zfs_name_to_prop(const char *propname)
345fa9e4066Sahrens {
346b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_ANY));
347b1b8ab34Slling }
348fa9e4066Sahrens 
349b1b8ab34Slling /*
350b1b8ab34Slling  * Given a pool property name, returns the corresponding property ID.
351b1b8ab34Slling  */
352b1b8ab34Slling zpool_prop_t
353b1b8ab34Slling zpool_name_to_prop(const char *propname)
354b1b8ab34Slling {
355b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_POOL));
356fa9e4066Sahrens }
357fa9e4066Sahrens 
358ecd6cf80Smarks const char *
359ecd6cf80Smarks zfs_prop_perm(zfs_prop_t prop)
360ecd6cf80Smarks {
361ecd6cf80Smarks 	return (zfs_prop_table[prop].pd_perm);
362ecd6cf80Smarks }
363ecd6cf80Smarks 
364fa9e4066Sahrens /*
365e9dbad6fSeschrock  * For user property names, we allow all lowercase alphanumeric characters, plus
366e9dbad6fSeschrock  * a few useful punctuation characters.
367e9dbad6fSeschrock  */
368e9dbad6fSeschrock static int
369e9dbad6fSeschrock valid_char(char c)
370e9dbad6fSeschrock {
371e9dbad6fSeschrock 	return ((c >= 'a' && c <= 'z') ||
372e9dbad6fSeschrock 	    (c >= '0' && c <= '9') ||
373e9dbad6fSeschrock 	    c == '-' || c == '_' || c == '.' || c == ':');
374e9dbad6fSeschrock }
375e9dbad6fSeschrock 
376e9dbad6fSeschrock /*
377e9dbad6fSeschrock  * Returns true if this is a valid user-defined property (one with a ':').
378e9dbad6fSeschrock  */
379e9dbad6fSeschrock boolean_t
380e9dbad6fSeschrock zfs_prop_user(const char *name)
381e9dbad6fSeschrock {
382e9dbad6fSeschrock 	int i;
383e9dbad6fSeschrock 	char c;
384e9dbad6fSeschrock 	boolean_t foundsep = B_FALSE;
385e9dbad6fSeschrock 
386e9dbad6fSeschrock 	for (i = 0; i < strlen(name); i++) {
387e9dbad6fSeschrock 		c = name[i];
388e9dbad6fSeschrock 		if (!valid_char(c))
389e9dbad6fSeschrock 			return (B_FALSE);
390e9dbad6fSeschrock 		if (c == ':')
391e9dbad6fSeschrock 			foundsep = B_TRUE;
392e9dbad6fSeschrock 	}
393e9dbad6fSeschrock 
394e9dbad6fSeschrock 	if (!foundsep)
395e9dbad6fSeschrock 		return (B_FALSE);
396e9dbad6fSeschrock 
397e9dbad6fSeschrock 	return (B_TRUE);
398e9dbad6fSeschrock }
399e9dbad6fSeschrock 
400e9dbad6fSeschrock /*
401fa9e4066Sahrens  * Return the default value for the given property.
402fa9e4066Sahrens  */
4037f7322feSeschrock const char *
4047f7322feSeschrock zfs_prop_default_string(zfs_prop_t prop)
405fa9e4066Sahrens {
4067f7322feSeschrock 	return (zfs_prop_table[prop].pd_strdefault);
407fa9e4066Sahrens }
408fa9e4066Sahrens 
4093d7072f8Seschrock const char *
4103d7072f8Seschrock zpool_prop_default_string(zpool_prop_t prop)
4113d7072f8Seschrock {
4123d7072f8Seschrock 	return (zfs_prop_table[prop].pd_strdefault);
4133d7072f8Seschrock }
4143d7072f8Seschrock 
415fa9e4066Sahrens uint64_t
416fa9e4066Sahrens zfs_prop_default_numeric(zfs_prop_t prop)
417fa9e4066Sahrens {
418fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_numdefault);
419fa9e4066Sahrens }
420fa9e4066Sahrens 
4213d7072f8Seschrock uint64_t
4223d7072f8Seschrock zpool_prop_default_numeric(zpool_prop_t prop)
4233d7072f8Seschrock {
4243d7072f8Seschrock 	return (zfs_prop_table[prop].pd_numdefault);
4253d7072f8Seschrock }
4263d7072f8Seschrock 
427fa9e4066Sahrens /*
428fa9e4066Sahrens  * Returns TRUE if the property is readonly.
429fa9e4066Sahrens  */
430fa9e4066Sahrens int
431fa9e4066Sahrens zfs_prop_readonly(zfs_prop_t prop)
432fa9e4066Sahrens {
433fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
434fa9e4066Sahrens }
435fa9e4066Sahrens 
436fa9e4066Sahrens /*
437b1b8ab34Slling  * Given a dataset property ID, returns the corresponding name.
4383d7072f8Seschrock  * Assuming the zfs dataset property ID is valid.
439fa9e4066Sahrens  */
440fa9e4066Sahrens const char *
441fa9e4066Sahrens zfs_prop_to_name(zfs_prop_t prop)
442fa9e4066Sahrens {
443fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_name);
444fa9e4066Sahrens }
445fa9e4066Sahrens 
446fa9e4066Sahrens /*
447b1b8ab34Slling  * Given a pool property ID, returns the corresponding name.
4483d7072f8Seschrock  * Assuming the pool property ID is valid.
449b1b8ab34Slling  */
450b1b8ab34Slling const char *
451b1b8ab34Slling zpool_prop_to_name(zpool_prop_t prop)
452b1b8ab34Slling {
453b1b8ab34Slling 	return (zfs_prop_table[prop].pd_name);
454b1b8ab34Slling }
455b1b8ab34Slling 
456b1b8ab34Slling /*
457fa9e4066Sahrens  * Returns TRUE if the property is inheritable.
458fa9e4066Sahrens  */
459fa9e4066Sahrens int
460fa9e4066Sahrens zfs_prop_inheritable(zfs_prop_t prop)
461fa9e4066Sahrens {
462fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
463fa9e4066Sahrens }
464fa9e4066Sahrens 
465e9dbad6fSeschrock typedef struct zfs_index {
466e9dbad6fSeschrock 	const char *name;
467e9dbad6fSeschrock 	uint64_t index;
468e9dbad6fSeschrock } zfs_index_t;
469e9dbad6fSeschrock 
470e9dbad6fSeschrock static zfs_index_t checksum_table[] = {
471e9dbad6fSeschrock 	{ "on",		ZIO_CHECKSUM_ON },
472e9dbad6fSeschrock 	{ "off",	ZIO_CHECKSUM_OFF },
473e9dbad6fSeschrock 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
474e9dbad6fSeschrock 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
475e9dbad6fSeschrock 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
476e9dbad6fSeschrock 	{ NULL }
477e9dbad6fSeschrock };
478e9dbad6fSeschrock 
479e9dbad6fSeschrock static zfs_index_t compress_table[] = {
480e9dbad6fSeschrock 	{ "on",		ZIO_COMPRESS_ON },
481e9dbad6fSeschrock 	{ "off",	ZIO_COMPRESS_OFF },
482e9dbad6fSeschrock 	{ "lzjb",	ZIO_COMPRESS_LZJB },
483c9431fa1Sahl 	{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* the default gzip level */
484c9431fa1Sahl 	{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
485c9431fa1Sahl 	{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
486c9431fa1Sahl 	{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
487c9431fa1Sahl 	{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
488c9431fa1Sahl 	{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
489c9431fa1Sahl 	{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
490c9431fa1Sahl 	{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
491c9431fa1Sahl 	{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
492c9431fa1Sahl 	{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
493e9dbad6fSeschrock 	{ NULL }
494e9dbad6fSeschrock };
495e9dbad6fSeschrock 
496e9dbad6fSeschrock static zfs_index_t snapdir_table[] = {
497e9dbad6fSeschrock 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
498e9dbad6fSeschrock 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
499e9dbad6fSeschrock 	{ NULL }
500e9dbad6fSeschrock };
501e9dbad6fSeschrock 
502e9dbad6fSeschrock static zfs_index_t acl_mode_table[] = {
503e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
504e9dbad6fSeschrock 	{ "groupmask",	ZFS_ACL_GROUPMASK },
505e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
506e9dbad6fSeschrock 	{ NULL }
507e9dbad6fSeschrock };
508e9dbad6fSeschrock 
509e9dbad6fSeschrock static zfs_index_t acl_inherit_table[] = {
510e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
511e9dbad6fSeschrock 	{ "noallow",	ZFS_ACL_NOALLOW },
512e9dbad6fSeschrock 	{ "secure",	ZFS_ACL_SECURE },
513e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
514e9dbad6fSeschrock 	{ NULL }
515e9dbad6fSeschrock };
516e9dbad6fSeschrock 
517d0ad202dSahrens static zfs_index_t copies_table[] = {
518d0ad202dSahrens 	{ "1",	1 },
519d0ad202dSahrens 	{ "2",	2 },
520d0ad202dSahrens 	{ "3",	3 },
521d0ad202dSahrens 	{ NULL }
522d0ad202dSahrens };
523d0ad202dSahrens 
524*e7437265Sahrens static zfs_index_t version_table[] = {
525*e7437265Sahrens 	{ "1",		1 },
526*e7437265Sahrens 	{ "2",		2 },
527*e7437265Sahrens 	{ "current",	ZPL_VERSION },
528*e7437265Sahrens 	{ NULL }
529*e7437265Sahrens };
530*e7437265Sahrens 
531e9dbad6fSeschrock static zfs_index_t *
532e9dbad6fSeschrock zfs_prop_index_table(zfs_prop_t prop)
533e9dbad6fSeschrock {
534e9dbad6fSeschrock 	switch (prop) {
535e9dbad6fSeschrock 	case ZFS_PROP_CHECKSUM:
536e9dbad6fSeschrock 		return (checksum_table);
537e9dbad6fSeschrock 	case ZFS_PROP_COMPRESSION:
538e9dbad6fSeschrock 		return (compress_table);
539e9dbad6fSeschrock 	case ZFS_PROP_SNAPDIR:
540e9dbad6fSeschrock 		return (snapdir_table);
541e9dbad6fSeschrock 	case ZFS_PROP_ACLMODE:
542e9dbad6fSeschrock 		return (acl_mode_table);
543e9dbad6fSeschrock 	case ZFS_PROP_ACLINHERIT:
544e9dbad6fSeschrock 		return (acl_inherit_table);
545d0ad202dSahrens 	case ZFS_PROP_COPIES:
546d0ad202dSahrens 		return (copies_table);
547*e7437265Sahrens 	case ZFS_PROP_VERSION:
548*e7437265Sahrens 		return (version_table);
549e9dbad6fSeschrock 	default:
550e9dbad6fSeschrock 		return (NULL);
551e9dbad6fSeschrock 	}
552e9dbad6fSeschrock }
553e9dbad6fSeschrock 
554e9dbad6fSeschrock /*
555e9dbad6fSeschrock  * Tables of index types, plus functions to convert between the user view
556e9dbad6fSeschrock  * (strings) and internal representation (uint64_t).
557e9dbad6fSeschrock  */
558e9dbad6fSeschrock int
559e9dbad6fSeschrock zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
560e9dbad6fSeschrock {
561e9dbad6fSeschrock 	zfs_index_t *table;
562e9dbad6fSeschrock 	int i;
563e9dbad6fSeschrock 
564e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
565e9dbad6fSeschrock 		return (-1);
566e9dbad6fSeschrock 
567e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
568e9dbad6fSeschrock 		if (strcmp(string, table[i].name) == 0) {
569e9dbad6fSeschrock 			*index = table[i].index;
570e9dbad6fSeschrock 			return (0);
571e9dbad6fSeschrock 		}
572e9dbad6fSeschrock 	}
573e9dbad6fSeschrock 
574e9dbad6fSeschrock 	return (-1);
575e9dbad6fSeschrock }
576e9dbad6fSeschrock 
577e9dbad6fSeschrock int
578e9dbad6fSeschrock zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
579e9dbad6fSeschrock {
580e9dbad6fSeschrock 	zfs_index_t *table;
581e9dbad6fSeschrock 	int i;
582e9dbad6fSeschrock 
583e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
584e9dbad6fSeschrock 		return (-1);
585e9dbad6fSeschrock 
586e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
587e9dbad6fSeschrock 		if (table[i].index == index) {
588e9dbad6fSeschrock 			*string = table[i].name;
589e9dbad6fSeschrock 			return (0);
590e9dbad6fSeschrock 		}
591e9dbad6fSeschrock 	}
592e9dbad6fSeschrock 
593e9dbad6fSeschrock 	return (-1);
594e9dbad6fSeschrock }
595e9dbad6fSeschrock 
596acd76fe5Seschrock #ifndef _KERNEL
597acd76fe5Seschrock 
598fa9e4066Sahrens /*
599fa9e4066Sahrens  * Returns a string describing the set of acceptable values for the given
600b1b8ab34Slling  * zfs property, or NULL if it cannot be set.
601fa9e4066Sahrens  */
602fa9e4066Sahrens const char *
603fa9e4066Sahrens zfs_prop_values(zfs_prop_t prop)
604fa9e4066Sahrens {
605b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types == ZFS_TYPE_POOL)
606b1b8ab34Slling 		return (NULL);
607b1b8ab34Slling 
608b1b8ab34Slling 	return (zfs_prop_table[prop].pd_values);
609b1b8ab34Slling }
610b1b8ab34Slling 
611b1b8ab34Slling /*
612b1b8ab34Slling  * Returns a string describing the set of acceptable values for the given
613b1b8ab34Slling  * zpool property, or NULL if it cannot be set.
614b1b8ab34Slling  */
615b1b8ab34Slling const char *
616b1b8ab34Slling zpool_prop_values(zfs_prop_t prop)
617b1b8ab34Slling {
618b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types != ZFS_TYPE_POOL)
619b1b8ab34Slling 		return (NULL);
620b1b8ab34Slling 
621fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_values);
622fa9e4066Sahrens }
623fa9e4066Sahrens 
624fa9e4066Sahrens /*
625fa9e4066Sahrens  * Returns TRUE if this property is a string type.  Note that index types
626fa9e4066Sahrens  * (compression, checksum) are treated as strings in userland, even though they
627fa9e4066Sahrens  * are stored numerically on disk.
628fa9e4066Sahrens  */
629fa9e4066Sahrens int
630fa9e4066Sahrens zfs_prop_is_string(zfs_prop_t prop)
631fa9e4066Sahrens {
632fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
633fa9e4066Sahrens 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
634fa9e4066Sahrens }
635fa9e4066Sahrens 
636fa9e4066Sahrens /*
637fa9e4066Sahrens  * Returns the column header for the given property.  Used only in
638fa9e4066Sahrens  * 'zfs list -o', but centralized here with the other property information.
639fa9e4066Sahrens  */
640fa9e4066Sahrens const char *
641fa9e4066Sahrens zfs_prop_column_name(zfs_prop_t prop)
642fa9e4066Sahrens {
643fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_colname);
644fa9e4066Sahrens }
645fa9e4066Sahrens 
646fa9e4066Sahrens /*
647e9dbad6fSeschrock  * Returns whether the given property should be displayed right-justified for
648e9dbad6fSeschrock  * 'zfs list'.
649fa9e4066Sahrens  */
650e9dbad6fSeschrock boolean_t
651e9dbad6fSeschrock zfs_prop_align_right(zfs_prop_t prop)
652fa9e4066Sahrens {
653e9dbad6fSeschrock 	return (zfs_prop_table[prop].pd_rightalign);
654fa9e4066Sahrens }
655fa9e4066Sahrens 
656fa9e4066Sahrens /*
657e9dbad6fSeschrock  * Determines the minimum width for the column, and indicates whether it's fixed
658e9dbad6fSeschrock  * or not.  Only string columns are non-fixed.
659fa9e4066Sahrens  */
660e9dbad6fSeschrock size_t
661e9dbad6fSeschrock zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
662fa9e4066Sahrens {
663e9dbad6fSeschrock 	prop_desc_t *pd = &zfs_prop_table[prop];
664e9dbad6fSeschrock 	zfs_index_t *idx;
665e9dbad6fSeschrock 	size_t ret;
666fa9e4066Sahrens 	int i;
667fa9e4066Sahrens 
668e9dbad6fSeschrock 	*fixed = B_TRUE;
669fa9e4066Sahrens 
67007ba0419Seschrock 	/*
671e9dbad6fSeschrock 	 * Start with the width of the column name.
67207ba0419Seschrock 	 */
673e9dbad6fSeschrock 	ret = strlen(pd->pd_colname);
674fa9e4066Sahrens 
675e9dbad6fSeschrock 	/*
676e9dbad6fSeschrock 	 * For fixed-width values, make sure the width is large enough to hold
677e9dbad6fSeschrock 	 * any possible value.
678e9dbad6fSeschrock 	 */
679e9dbad6fSeschrock 	switch (pd->pd_proptype) {
680e9dbad6fSeschrock 	case prop_type_number:
681e9dbad6fSeschrock 		/*
682e9dbad6fSeschrock 		 * The maximum length of a human-readable number is 5 characters
683e9dbad6fSeschrock 		 * ("20.4M", for example).
684e9dbad6fSeschrock 		 */
685e9dbad6fSeschrock 		if (ret < 5)
686e9dbad6fSeschrock 			ret = 5;
687e9dbad6fSeschrock 		/*
688e9dbad6fSeschrock 		 * 'creation' is handled specially because it's a number
689e9dbad6fSeschrock 		 * internally, but displayed as a date string.
690e9dbad6fSeschrock 		 */
691e9dbad6fSeschrock 		if (prop == ZFS_PROP_CREATION)
692e9dbad6fSeschrock 			*fixed = B_FALSE;
693e9dbad6fSeschrock 		break;
694e9dbad6fSeschrock 	case prop_type_boolean:
695e9dbad6fSeschrock 		/*
696e9dbad6fSeschrock 		 * The maximum length of a boolean value is 3 characters, for
697e9dbad6fSeschrock 		 * "off".
698e9dbad6fSeschrock 		 */
699e9dbad6fSeschrock 		if (ret < 3)
700e9dbad6fSeschrock 			ret = 3;
701e9dbad6fSeschrock 		break;
702e9dbad6fSeschrock 	case prop_type_index:
703e9dbad6fSeschrock 		idx = zfs_prop_index_table(prop);
704e9dbad6fSeschrock 		for (i = 0; idx[i].name != NULL; i++) {
705e9dbad6fSeschrock 			if (strlen(idx[i].name) > ret)
706e9dbad6fSeschrock 				ret = strlen(idx[i].name);
707fa9e4066Sahrens 		}
708e9dbad6fSeschrock 		break;
709fa9e4066Sahrens 
710e9dbad6fSeschrock 	case prop_type_string:
711e9dbad6fSeschrock 		*fixed = B_FALSE;
71207ba0419Seschrock 		break;
71307ba0419Seschrock 	}
71407ba0419Seschrock 
715e9dbad6fSeschrock 	return (ret);
716fa9e4066Sahrens }
717fa9e4066Sahrens 
718fa9e4066Sahrens #endif
719