xref: /titanic_53/usr/src/common/zfs/zfs_prop.c (revision ecd6cf800b63704be73fb264c3f5b6e0dafc068d)
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"
56*ecd6cf80Smarks #include "zfs_deleg.h"
57fa9e4066Sahrens 
58fa9e4066Sahrens #if defined(_KERNEL)
59fa9e4066Sahrens #include <sys/systm.h>
60fa9e4066Sahrens #else
61fa9e4066Sahrens #include <stdlib.h>
62fa9e4066Sahrens #include <string.h>
63fa9e4066Sahrens #include <ctype.h>
64fa9e4066Sahrens #endif
65fa9e4066Sahrens 
66fa9e4066Sahrens typedef enum {
67fa9e4066Sahrens 	prop_default,
68fa9e4066Sahrens 	prop_readonly,
69fa9e4066Sahrens 	prop_inherit
70fa9e4066Sahrens } prop_attr_t;
71fa9e4066Sahrens 
72fa9e4066Sahrens typedef struct {
73fa9e4066Sahrens 	const char	*pd_name;
74fa9e4066Sahrens 	zfs_proptype_t	pd_proptype;
75fa9e4066Sahrens 	uint64_t	pd_numdefault;
76fa9e4066Sahrens 	const char	*pd_strdefault;
77fa9e4066Sahrens 	prop_attr_t	pd_attr;
78fa9e4066Sahrens 	int		pd_types;
79fa9e4066Sahrens 	const char	*pd_values;
80fa9e4066Sahrens 	const char	*pd_colname;
81e9dbad6fSeschrock 	boolean_t	pd_rightalign;
8266e2aaccSgw25295 	boolean_t	pd_visible;
83*ecd6cf80Smarks 	const char	*pd_perm;
84fa9e4066Sahrens } prop_desc_t;
85fa9e4066Sahrens 
8666e2aaccSgw25295 static prop_desc_t zfs_prop_table[] = {
87fa9e4066Sahrens 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
8866e2aaccSgw25295 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE,
89*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
90fa9e4066Sahrens 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
91*ecd6cf80Smarks 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE,
92*ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
93fa9e4066Sahrens 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
94*ecd6cf80Smarks 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE, B_TRUE,
95*ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
96fa9e4066Sahrens 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
9766e2aaccSgw25295 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE,
98*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
99fa9e4066Sahrens 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
1004f75c27eSeschrock 	    ZFS_TYPE_ANY,
101*ecd6cf80Smarks 	    "<size>", "REFER", B_TRUE, B_TRUE, ZFS_DELEG_PERM_NONE },
102fa9e4066Sahrens 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
10366e2aaccSgw25295 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE,
104*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
105fa9e4066Sahrens 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
106*ecd6cf80Smarks 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE, B_TRUE,
107*ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
108fa9e4066Sahrens 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
109101f5531Slling 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
110*ecd6cf80Smarks 	    B_FALSE, B_TRUE, ZFS_DELEG_PERM_NONE },
111fa9e4066Sahrens 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
112*ecd6cf80Smarks 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE, B_TRUE,
113*ecd6cf80Smarks 	    ZFS_DELEG_PERM_QUOTA },
114fa9e4066Sahrens 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
115fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
116*ecd6cf80Smarks 	    "<size> | none", "RESERV", B_TRUE, B_TRUE,
117*ecd6cf80Smarks 	    ZFS_DELEG_PERM_RESERVATION },
118fa9e4066Sahrens 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
119*ecd6cf80Smarks 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE,
120*ecd6cf80Smarks 	    ZFS_DELEG_PERM_VOLSIZE },
1214f75c27eSeschrock 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
12266e2aaccSgw25295 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE,
123*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
124fa9e4066Sahrens 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
125fa9e4066Sahrens 	    prop_inherit,
1264f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
127*ecd6cf80Smarks 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE,
128*ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECORDSIZE },
129fa9e4066Sahrens 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
130fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
131*ecd6cf80Smarks 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE,
132*ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNTPOINT },
133fa9e4066Sahrens 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
134fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
135*ecd6cf80Smarks 	    "on | off | share(1M) options", "SHARENFS", B_FALSE, B_TRUE,
136*ecd6cf80Smarks 	    ZFS_DELEG_PERM_SHARENFS },
137101f5531Slling 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
1384f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
13966e2aaccSgw25295 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE,
140*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_CHECKSUM },
141101f5531Slling 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
1424f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
143*ecd6cf80Smarks 	    "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", B_TRUE, B_TRUE,
144*ecd6cf80Smarks 	    ZFS_DELEG_PERM_COMPRESSION },
145fa9e4066Sahrens 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
146fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
147*ecd6cf80Smarks 	    "on | off", "ATIME", B_TRUE, B_TRUE, ZFS_DELEG_PERM_ATIME },
148fa9e4066Sahrens 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
14912054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
150*ecd6cf80Smarks 	    "on | off", "DEVICES", B_TRUE, B_TRUE, ZFS_DELEG_PERM_DEVICES },
151fa9e4066Sahrens 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
15212054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
153*ecd6cf80Smarks 	    "on | off", "EXEC", B_TRUE, B_TRUE, ZFS_DELEG_PERM_EXEC },
154fa9e4066Sahrens 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
15512054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
156*ecd6cf80Smarks 	    B_TRUE, B_TRUE, ZFS_DELEG_PERM_SETUID },
157fa9e4066Sahrens 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
1584f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
159*ecd6cf80Smarks 	    "on | off", "RDONLY", B_TRUE, B_TRUE, ZFS_DELEG_PERM_READONLY },
160fa9e4066Sahrens 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
1614f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
162*ecd6cf80Smarks 	    "on | off", "ZONED", B_TRUE, B_TRUE, ZFS_DELEG_PERM_ZONED },
163906d120cSlling 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
164a0965f35Sbonwick 	    prop_inherit,
165fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
166*ecd6cf80Smarks 	    "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE,
167*ecd6cf80Smarks 	    ZFS_DELEG_PERM_SNAPDIR },
168e9dbad6fSeschrock 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
169e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
170*ecd6cf80Smarks 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE,
171*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_ACLMODE },
172e9dbad6fSeschrock 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
173e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
17466e2aaccSgw25295 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE,
175*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_ACLINHERIT },
17666e2aaccSgw25295 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
177*ecd6cf80Smarks 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE, ZFS_DELEG_PERM_NONE },
17866e2aaccSgw25295 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
179*ecd6cf80Smarks 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE, ZFS_DELEG_PERM_NONE },
180e9dbad6fSeschrock 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
181fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
182*ecd6cf80Smarks 	    "on | off", "CANMOUNT", B_TRUE, B_TRUE, ZFS_DELEG_PERM_CANMOUNT },
18366e2aaccSgw25295 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
18466e2aaccSgw25295 	    ZFS_TYPE_ANY,
185*ecd6cf80Smarks 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE,
186*ecd6cf80Smarks 	    ZFS_DELEG_PERM_SHAREISCSI },
18766e2aaccSgw25295 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
188*ecd6cf80Smarks 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE,
189*ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
1907b55fa8eSck153898 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
1917b55fa8eSck153898 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
192*ecd6cf80Smarks 	    "on | off", "XATTR", B_TRUE, B_TRUE, ZFS_DELEG_PERM_XATTR },
19339c23413Seschrock 	{ "numclones", prop_type_number,	0,	NULL,	prop_readonly,
194*ecd6cf80Smarks 	    ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE, B_FALSE,
195*ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
196d0ad202dSahrens 	{ "copies",	prop_type_index,	1,	"1",	prop_inherit,
197d0ad202dSahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
198*ecd6cf80Smarks 	    "1 | 2 | 3", "COPIES", B_TRUE, B_TRUE, ZFS_DELEG_PERM_COPIES },
199b1b8ab34Slling 	{ "bootfs", prop_type_string,	0,	NULL,	prop_default,
200*ecd6cf80Smarks 	    ZFS_TYPE_POOL, "<filesystem>", "BOOTFS", B_FALSE,
201*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE },
2023d7072f8Seschrock 	{ "autoreplace", prop_type_boolean,	0,	NULL, prop_default,
203*ecd6cf80Smarks 	    ZFS_TYPE_POOL, "on | off", "REPLACE", B_FALSE, B_TRUE,
204*ecd6cf80Smarks 	    ZFS_DELEG_PERM_NONE },
205*ecd6cf80Smarks 	{ "delegation", prop_type_boolean,	1,	NULL,	prop_default,
206*ecd6cf80Smarks 	    ZFS_TYPE_POOL, "on | off", "DELEGATION", B_TRUE,
207*ecd6cf80Smarks 	    B_TRUE, ZFS_DELEG_PERM_NONE }
208fa9e4066Sahrens };
209fa9e4066Sahrens 
21066e2aaccSgw25295 #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
21166e2aaccSgw25295 
212b1b8ab34Slling /*
213b1b8ab34Slling  * Returns TRUE if the property applies to the given dataset types.
214b1b8ab34Slling  */
215b1b8ab34Slling int
216b1b8ab34Slling zfs_prop_valid_for_type(zfs_prop_t prop, int types)
217b1b8ab34Slling {
218b1b8ab34Slling 	return ((zfs_prop_table[prop].pd_types & types) != 0);
219b1b8ab34Slling }
220b1b8ab34Slling 
221b1b8ab34Slling /*
222b1b8ab34Slling  * Determine if the specified property is visible or not.
223b1b8ab34Slling  */
224b1b8ab34Slling boolean_t
225b1b8ab34Slling zfs_prop_is_visible(zfs_prop_t prop)
226b1b8ab34Slling {
227b1b8ab34Slling 	if (prop < 0)
228b1b8ab34Slling 		return (B_FALSE);
229b1b8ab34Slling 
230b1b8ab34Slling 	return (zfs_prop_table[prop].pd_visible);
231b1b8ab34Slling }
232b1b8ab34Slling 
233b1b8ab34Slling /*
234b1b8ab34Slling  * Iterate over all properties, calling back into the specified function
235b1b8ab34Slling  * for each property. We will continue to iterate until we either
236b1b8ab34Slling  * reach the end or the callback function something other than
237b1b8ab34Slling  * ZFS_PROP_CONT.
238b1b8ab34Slling  */
239b1b8ab34Slling zfs_prop_t
240b1b8ab34Slling zfs_prop_iter_common(zfs_prop_f func, void *cb, zfs_type_t type,
241b1b8ab34Slling     boolean_t show_all)
242b1b8ab34Slling {
243b1b8ab34Slling 	int i;
244b1b8ab34Slling 
245b1b8ab34Slling 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
246b1b8ab34Slling 		if (zfs_prop_valid_for_type(i, type) &&
247b1b8ab34Slling 		    (zfs_prop_is_visible(i) || show_all)) {
248b1b8ab34Slling 			if (func(i, cb) != ZFS_PROP_CONT)
249b1b8ab34Slling 				return (i);
250b1b8ab34Slling 		}
251b1b8ab34Slling 	}
252b1b8ab34Slling 	return (ZFS_PROP_CONT);
253b1b8ab34Slling }
254b1b8ab34Slling 
255b1b8ab34Slling zfs_prop_t
256b1b8ab34Slling zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
257b1b8ab34Slling {
258b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_ANY, show_all));
259b1b8ab34Slling }
260b1b8ab34Slling 
261b1b8ab34Slling zpool_prop_t
262b1b8ab34Slling zpool_prop_iter(zpool_prop_f func, void *cb, boolean_t show_all)
263b1b8ab34Slling {
264b1b8ab34Slling 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_POOL, show_all));
265b1b8ab34Slling }
266b1b8ab34Slling 
267fa9e4066Sahrens zfs_proptype_t
268fa9e4066Sahrens zfs_prop_get_type(zfs_prop_t prop)
269fa9e4066Sahrens {
270fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype);
271fa9e4066Sahrens }
272fa9e4066Sahrens 
2733d7072f8Seschrock zfs_proptype_t
2743d7072f8Seschrock zpool_prop_get_type(zfs_prop_t prop)
2753d7072f8Seschrock {
2763d7072f8Seschrock 	return (zfs_prop_table[prop].pd_proptype);
2773d7072f8Seschrock }
2783d7072f8Seschrock 
279e9dbad6fSeschrock static boolean_t
280e9dbad6fSeschrock propname_match(const char *p, zfs_prop_t prop, size_t len)
2815c709891Seschrock {
2825c709891Seschrock 	const char *propname = zfs_prop_table[prop].pd_name;
2835c709891Seschrock #ifndef _KERNEL
2845c709891Seschrock 	const char *colname = zfs_prop_table[prop].pd_colname;
2855c709891Seschrock 	int c;
2865c709891Seschrock #endif
2875c709891Seschrock 
2883bb79becSeschrock #ifndef _KERNEL
2893bb79becSeschrock 	if (colname == NULL)
290e9dbad6fSeschrock 		return (B_FALSE);
2913bb79becSeschrock #endif
2923bb79becSeschrock 
2935c709891Seschrock 	if (len == strlen(propname) &&
2945c709891Seschrock 	    strncmp(p, propname, len) == 0)
295e9dbad6fSeschrock 		return (B_TRUE);
2965c709891Seschrock 
2975c709891Seschrock #ifndef _KERNEL
2983bb79becSeschrock 	if (len != strlen(colname))
299e9dbad6fSeschrock 		return (B_FALSE);
3005c709891Seschrock 
3015c709891Seschrock 	for (c = 0; c < len; c++)
3025c709891Seschrock 		if (p[c] != tolower(colname[c]))
3035c709891Seschrock 			break;
3045c709891Seschrock 
3055c709891Seschrock 	return (colname[c] == '\0');
3065c709891Seschrock #else
307e9dbad6fSeschrock 	return (B_FALSE);
3085c709891Seschrock #endif
3095c709891Seschrock }
3105c709891Seschrock 
31166e2aaccSgw25295 zfs_prop_t
31266e2aaccSgw25295 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
31366e2aaccSgw25295 {
31466e2aaccSgw25295 	const char *propname = cb_data;
31566e2aaccSgw25295 
316*ecd6cf80Smarks 	if (propname_match(propname, prop, strlen(propname))) {
31766e2aaccSgw25295 		return (prop);
318*ecd6cf80Smarks 	}
31966e2aaccSgw25295 
32066e2aaccSgw25295 	return (ZFS_PROP_CONT);
32166e2aaccSgw25295 }
3225c709891Seschrock 
323fa9e4066Sahrens /*
324b1b8ab34Slling  * Given a property name and its type, returns the corresponding property ID.
325b1b8ab34Slling  */
326b1b8ab34Slling zfs_prop_t
327b1b8ab34Slling zfs_name_to_prop_common(const char *propname, zfs_type_t type)
328b1b8ab34Slling {
329b1b8ab34Slling 	zfs_prop_t prop;
330b1b8ab34Slling 
331b1b8ab34Slling 	prop = zfs_prop_iter_common(zfs_name_to_prop_cb, (void *)propname,
332b1b8ab34Slling 	    type, B_TRUE);
333b1b8ab34Slling 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
334b1b8ab34Slling }
335b1b8ab34Slling 
336b1b8ab34Slling /*
337b1b8ab34Slling  * Given a zfs dataset property name, returns the corresponding property ID.
338fa9e4066Sahrens  */
339fa9e4066Sahrens zfs_prop_t
340fa9e4066Sahrens zfs_name_to_prop(const char *propname)
341fa9e4066Sahrens {
342b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_ANY));
343b1b8ab34Slling }
344fa9e4066Sahrens 
345b1b8ab34Slling /*
346b1b8ab34Slling  * Given a pool property name, returns the corresponding property ID.
347b1b8ab34Slling  */
348b1b8ab34Slling zpool_prop_t
349b1b8ab34Slling zpool_name_to_prop(const char *propname)
350b1b8ab34Slling {
351b1b8ab34Slling 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_POOL));
352fa9e4066Sahrens }
353fa9e4066Sahrens 
354*ecd6cf80Smarks const char *
355*ecd6cf80Smarks zfs_prop_perm(zfs_prop_t prop)
356*ecd6cf80Smarks {
357*ecd6cf80Smarks 	return (zfs_prop_table[prop].pd_perm);
358*ecd6cf80Smarks }
359*ecd6cf80Smarks 
360fa9e4066Sahrens /*
361e9dbad6fSeschrock  * For user property names, we allow all lowercase alphanumeric characters, plus
362e9dbad6fSeschrock  * a few useful punctuation characters.
363e9dbad6fSeschrock  */
364e9dbad6fSeschrock static int
365e9dbad6fSeschrock valid_char(char c)
366e9dbad6fSeschrock {
367e9dbad6fSeschrock 	return ((c >= 'a' && c <= 'z') ||
368e9dbad6fSeschrock 	    (c >= '0' && c <= '9') ||
369e9dbad6fSeschrock 	    c == '-' || c == '_' || c == '.' || c == ':');
370e9dbad6fSeschrock }
371e9dbad6fSeschrock 
372e9dbad6fSeschrock /*
373e9dbad6fSeschrock  * Returns true if this is a valid user-defined property (one with a ':').
374e9dbad6fSeschrock  */
375e9dbad6fSeschrock boolean_t
376e9dbad6fSeschrock zfs_prop_user(const char *name)
377e9dbad6fSeschrock {
378e9dbad6fSeschrock 	int i;
379e9dbad6fSeschrock 	char c;
380e9dbad6fSeschrock 	boolean_t foundsep = B_FALSE;
381e9dbad6fSeschrock 
382e9dbad6fSeschrock 	for (i = 0; i < strlen(name); i++) {
383e9dbad6fSeschrock 		c = name[i];
384e9dbad6fSeschrock 		if (!valid_char(c))
385e9dbad6fSeschrock 			return (B_FALSE);
386e9dbad6fSeschrock 		if (c == ':')
387e9dbad6fSeschrock 			foundsep = B_TRUE;
388e9dbad6fSeschrock 	}
389e9dbad6fSeschrock 
390e9dbad6fSeschrock 	if (!foundsep)
391e9dbad6fSeschrock 		return (B_FALSE);
392e9dbad6fSeschrock 
393e9dbad6fSeschrock 	return (B_TRUE);
394e9dbad6fSeschrock }
395e9dbad6fSeschrock 
396e9dbad6fSeschrock /*
397fa9e4066Sahrens  * Return the default value for the given property.
398fa9e4066Sahrens  */
3997f7322feSeschrock const char *
4007f7322feSeschrock zfs_prop_default_string(zfs_prop_t prop)
401fa9e4066Sahrens {
4027f7322feSeschrock 	return (zfs_prop_table[prop].pd_strdefault);
403fa9e4066Sahrens }
404fa9e4066Sahrens 
4053d7072f8Seschrock const char *
4063d7072f8Seschrock zpool_prop_default_string(zpool_prop_t prop)
4073d7072f8Seschrock {
4083d7072f8Seschrock 	return (zfs_prop_table[prop].pd_strdefault);
4093d7072f8Seschrock }
4103d7072f8Seschrock 
411fa9e4066Sahrens uint64_t
412fa9e4066Sahrens zfs_prop_default_numeric(zfs_prop_t prop)
413fa9e4066Sahrens {
414fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_numdefault);
415fa9e4066Sahrens }
416fa9e4066Sahrens 
4173d7072f8Seschrock uint64_t
4183d7072f8Seschrock zpool_prop_default_numeric(zpool_prop_t prop)
4193d7072f8Seschrock {
4203d7072f8Seschrock 	return (zfs_prop_table[prop].pd_numdefault);
4213d7072f8Seschrock }
4223d7072f8Seschrock 
423fa9e4066Sahrens /*
424fa9e4066Sahrens  * Returns TRUE if the property is readonly.
425fa9e4066Sahrens  */
426fa9e4066Sahrens int
427fa9e4066Sahrens zfs_prop_readonly(zfs_prop_t prop)
428fa9e4066Sahrens {
429fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
430fa9e4066Sahrens }
431fa9e4066Sahrens 
432fa9e4066Sahrens /*
433b1b8ab34Slling  * Given a dataset property ID, returns the corresponding name.
4343d7072f8Seschrock  * Assuming the zfs dataset property ID is valid.
435fa9e4066Sahrens  */
436fa9e4066Sahrens const char *
437fa9e4066Sahrens zfs_prop_to_name(zfs_prop_t prop)
438fa9e4066Sahrens {
439fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_name);
440fa9e4066Sahrens }
441fa9e4066Sahrens 
442fa9e4066Sahrens /*
443b1b8ab34Slling  * Given a pool property ID, returns the corresponding name.
4443d7072f8Seschrock  * Assuming the pool property ID is valid.
445b1b8ab34Slling  */
446b1b8ab34Slling const char *
447b1b8ab34Slling zpool_prop_to_name(zpool_prop_t prop)
448b1b8ab34Slling {
449b1b8ab34Slling 	return (zfs_prop_table[prop].pd_name);
450b1b8ab34Slling }
451b1b8ab34Slling 
452b1b8ab34Slling /*
453fa9e4066Sahrens  * Returns TRUE if the property is inheritable.
454fa9e4066Sahrens  */
455fa9e4066Sahrens int
456fa9e4066Sahrens zfs_prop_inheritable(zfs_prop_t prop)
457fa9e4066Sahrens {
458fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
459fa9e4066Sahrens }
460fa9e4066Sahrens 
461e9dbad6fSeschrock typedef struct zfs_index {
462e9dbad6fSeschrock 	const char *name;
463e9dbad6fSeschrock 	uint64_t index;
464e9dbad6fSeschrock } zfs_index_t;
465e9dbad6fSeschrock 
466e9dbad6fSeschrock static zfs_index_t checksum_table[] = {
467e9dbad6fSeschrock 	{ "on",		ZIO_CHECKSUM_ON },
468e9dbad6fSeschrock 	{ "off",	ZIO_CHECKSUM_OFF },
469e9dbad6fSeschrock 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
470e9dbad6fSeschrock 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
471e9dbad6fSeschrock 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
472e9dbad6fSeschrock 	{ NULL }
473e9dbad6fSeschrock };
474e9dbad6fSeschrock 
475e9dbad6fSeschrock static zfs_index_t compress_table[] = {
476e9dbad6fSeschrock 	{ "on",		ZIO_COMPRESS_ON },
477e9dbad6fSeschrock 	{ "off",	ZIO_COMPRESS_OFF },
478e9dbad6fSeschrock 	{ "lzjb",	ZIO_COMPRESS_LZJB },
479c9431fa1Sahl 	{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* the default gzip level */
480c9431fa1Sahl 	{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
481c9431fa1Sahl 	{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
482c9431fa1Sahl 	{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
483c9431fa1Sahl 	{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
484c9431fa1Sahl 	{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
485c9431fa1Sahl 	{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
486c9431fa1Sahl 	{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
487c9431fa1Sahl 	{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
488c9431fa1Sahl 	{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
489e9dbad6fSeschrock 	{ NULL }
490e9dbad6fSeschrock };
491e9dbad6fSeschrock 
492e9dbad6fSeschrock static zfs_index_t snapdir_table[] = {
493e9dbad6fSeschrock 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
494e9dbad6fSeschrock 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
495e9dbad6fSeschrock 	{ NULL }
496e9dbad6fSeschrock };
497e9dbad6fSeschrock 
498e9dbad6fSeschrock static zfs_index_t acl_mode_table[] = {
499e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
500e9dbad6fSeschrock 	{ "groupmask",	ZFS_ACL_GROUPMASK },
501e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
502e9dbad6fSeschrock 	{ NULL }
503e9dbad6fSeschrock };
504e9dbad6fSeschrock 
505e9dbad6fSeschrock static zfs_index_t acl_inherit_table[] = {
506e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
507e9dbad6fSeschrock 	{ "noallow",	ZFS_ACL_NOALLOW },
508e9dbad6fSeschrock 	{ "secure",	ZFS_ACL_SECURE },
509e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
510e9dbad6fSeschrock 	{ NULL }
511e9dbad6fSeschrock };
512e9dbad6fSeschrock 
513d0ad202dSahrens static zfs_index_t copies_table[] = {
514d0ad202dSahrens 	{ "1",	1 },
515d0ad202dSahrens 	{ "2",	2 },
516d0ad202dSahrens 	{ "3",	3 },
517d0ad202dSahrens 	{ NULL }
518d0ad202dSahrens };
519d0ad202dSahrens 
520e9dbad6fSeschrock static zfs_index_t *
521e9dbad6fSeschrock zfs_prop_index_table(zfs_prop_t prop)
522e9dbad6fSeschrock {
523e9dbad6fSeschrock 	switch (prop) {
524e9dbad6fSeschrock 	case ZFS_PROP_CHECKSUM:
525e9dbad6fSeschrock 		return (checksum_table);
526e9dbad6fSeschrock 	case ZFS_PROP_COMPRESSION:
527e9dbad6fSeschrock 		return (compress_table);
528e9dbad6fSeschrock 	case ZFS_PROP_SNAPDIR:
529e9dbad6fSeschrock 		return (snapdir_table);
530e9dbad6fSeschrock 	case ZFS_PROP_ACLMODE:
531e9dbad6fSeschrock 		return (acl_mode_table);
532e9dbad6fSeschrock 	case ZFS_PROP_ACLINHERIT:
533e9dbad6fSeschrock 		return (acl_inherit_table);
534d0ad202dSahrens 	case ZFS_PROP_COPIES:
535d0ad202dSahrens 		return (copies_table);
536e9dbad6fSeschrock 	default:
537e9dbad6fSeschrock 		return (NULL);
538e9dbad6fSeschrock 	}
539e9dbad6fSeschrock }
540e9dbad6fSeschrock 
541e9dbad6fSeschrock 
542e9dbad6fSeschrock /*
543e9dbad6fSeschrock  * Tables of index types, plus functions to convert between the user view
544e9dbad6fSeschrock  * (strings) and internal representation (uint64_t).
545e9dbad6fSeschrock  */
546e9dbad6fSeschrock int
547e9dbad6fSeschrock zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
548e9dbad6fSeschrock {
549e9dbad6fSeschrock 	zfs_index_t *table;
550e9dbad6fSeschrock 	int i;
551e9dbad6fSeschrock 
552e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
553e9dbad6fSeschrock 		return (-1);
554e9dbad6fSeschrock 
555e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
556e9dbad6fSeschrock 		if (strcmp(string, table[i].name) == 0) {
557e9dbad6fSeschrock 			*index = table[i].index;
558e9dbad6fSeschrock 			return (0);
559e9dbad6fSeschrock 		}
560e9dbad6fSeschrock 	}
561e9dbad6fSeschrock 
562e9dbad6fSeschrock 	return (-1);
563e9dbad6fSeschrock }
564e9dbad6fSeschrock 
565e9dbad6fSeschrock int
566e9dbad6fSeschrock zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
567e9dbad6fSeschrock {
568e9dbad6fSeschrock 	zfs_index_t *table;
569e9dbad6fSeschrock 	int i;
570e9dbad6fSeschrock 
571e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
572e9dbad6fSeschrock 		return (-1);
573e9dbad6fSeschrock 
574e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
575e9dbad6fSeschrock 		if (table[i].index == index) {
576e9dbad6fSeschrock 			*string = table[i].name;
577e9dbad6fSeschrock 			return (0);
578e9dbad6fSeschrock 		}
579e9dbad6fSeschrock 	}
580e9dbad6fSeschrock 
581e9dbad6fSeschrock 	return (-1);
582e9dbad6fSeschrock }
583e9dbad6fSeschrock 
584acd76fe5Seschrock #ifndef _KERNEL
585acd76fe5Seschrock 
586fa9e4066Sahrens /*
587fa9e4066Sahrens  * Returns a string describing the set of acceptable values for the given
588b1b8ab34Slling  * zfs property, or NULL if it cannot be set.
589fa9e4066Sahrens  */
590fa9e4066Sahrens const char *
591fa9e4066Sahrens zfs_prop_values(zfs_prop_t prop)
592fa9e4066Sahrens {
593b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types == ZFS_TYPE_POOL)
594b1b8ab34Slling 		return (NULL);
595b1b8ab34Slling 
596b1b8ab34Slling 	return (zfs_prop_table[prop].pd_values);
597b1b8ab34Slling }
598b1b8ab34Slling 
599b1b8ab34Slling /*
600b1b8ab34Slling  * Returns a string describing the set of acceptable values for the given
601b1b8ab34Slling  * zpool property, or NULL if it cannot be set.
602b1b8ab34Slling  */
603b1b8ab34Slling const char *
604b1b8ab34Slling zpool_prop_values(zfs_prop_t prop)
605b1b8ab34Slling {
606b1b8ab34Slling 	if (zfs_prop_table[prop].pd_types != ZFS_TYPE_POOL)
607b1b8ab34Slling 		return (NULL);
608b1b8ab34Slling 
609fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_values);
610fa9e4066Sahrens }
611fa9e4066Sahrens 
612fa9e4066Sahrens /*
613fa9e4066Sahrens  * Returns TRUE if this property is a string type.  Note that index types
614fa9e4066Sahrens  * (compression, checksum) are treated as strings in userland, even though they
615fa9e4066Sahrens  * are stored numerically on disk.
616fa9e4066Sahrens  */
617fa9e4066Sahrens int
618fa9e4066Sahrens zfs_prop_is_string(zfs_prop_t prop)
619fa9e4066Sahrens {
620fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
621fa9e4066Sahrens 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
622fa9e4066Sahrens }
623fa9e4066Sahrens 
624fa9e4066Sahrens /*
625fa9e4066Sahrens  * Returns the column header for the given property.  Used only in
626fa9e4066Sahrens  * 'zfs list -o', but centralized here with the other property information.
627fa9e4066Sahrens  */
628fa9e4066Sahrens const char *
629fa9e4066Sahrens zfs_prop_column_name(zfs_prop_t prop)
630fa9e4066Sahrens {
631fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_colname);
632fa9e4066Sahrens }
633fa9e4066Sahrens 
634fa9e4066Sahrens /*
635e9dbad6fSeschrock  * Returns whether the given property should be displayed right-justified for
636e9dbad6fSeschrock  * 'zfs list'.
637fa9e4066Sahrens  */
638e9dbad6fSeschrock boolean_t
639e9dbad6fSeschrock zfs_prop_align_right(zfs_prop_t prop)
640fa9e4066Sahrens {
641e9dbad6fSeschrock 	return (zfs_prop_table[prop].pd_rightalign);
642fa9e4066Sahrens }
643fa9e4066Sahrens 
644fa9e4066Sahrens /*
645e9dbad6fSeschrock  * Determines the minimum width for the column, and indicates whether it's fixed
646e9dbad6fSeschrock  * or not.  Only string columns are non-fixed.
647fa9e4066Sahrens  */
648e9dbad6fSeschrock size_t
649e9dbad6fSeschrock zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
650fa9e4066Sahrens {
651e9dbad6fSeschrock 	prop_desc_t *pd = &zfs_prop_table[prop];
652e9dbad6fSeschrock 	zfs_index_t *idx;
653e9dbad6fSeschrock 	size_t ret;
654fa9e4066Sahrens 	int i;
655fa9e4066Sahrens 
656e9dbad6fSeschrock 	*fixed = B_TRUE;
657fa9e4066Sahrens 
65807ba0419Seschrock 	/*
659e9dbad6fSeschrock 	 * Start with the width of the column name.
66007ba0419Seschrock 	 */
661e9dbad6fSeschrock 	ret = strlen(pd->pd_colname);
662fa9e4066Sahrens 
663e9dbad6fSeschrock 	/*
664e9dbad6fSeschrock 	 * For fixed-width values, make sure the width is large enough to hold
665e9dbad6fSeschrock 	 * any possible value.
666e9dbad6fSeschrock 	 */
667e9dbad6fSeschrock 	switch (pd->pd_proptype) {
668e9dbad6fSeschrock 	case prop_type_number:
669e9dbad6fSeschrock 		/*
670e9dbad6fSeschrock 		 * The maximum length of a human-readable number is 5 characters
671e9dbad6fSeschrock 		 * ("20.4M", for example).
672e9dbad6fSeschrock 		 */
673e9dbad6fSeschrock 		if (ret < 5)
674e9dbad6fSeschrock 			ret = 5;
675e9dbad6fSeschrock 		/*
676e9dbad6fSeschrock 		 * 'creation' is handled specially because it's a number
677e9dbad6fSeschrock 		 * internally, but displayed as a date string.
678e9dbad6fSeschrock 		 */
679e9dbad6fSeschrock 		if (prop == ZFS_PROP_CREATION)
680e9dbad6fSeschrock 			*fixed = B_FALSE;
681e9dbad6fSeschrock 		break;
682e9dbad6fSeschrock 	case prop_type_boolean:
683e9dbad6fSeschrock 		/*
684e9dbad6fSeschrock 		 * The maximum length of a boolean value is 3 characters, for
685e9dbad6fSeschrock 		 * "off".
686e9dbad6fSeschrock 		 */
687e9dbad6fSeschrock 		if (ret < 3)
688e9dbad6fSeschrock 			ret = 3;
689e9dbad6fSeschrock 		break;
690e9dbad6fSeschrock 	case prop_type_index:
691e9dbad6fSeschrock 		idx = zfs_prop_index_table(prop);
692e9dbad6fSeschrock 		for (i = 0; idx[i].name != NULL; i++) {
693e9dbad6fSeschrock 			if (strlen(idx[i].name) > ret)
694e9dbad6fSeschrock 				ret = strlen(idx[i].name);
695fa9e4066Sahrens 		}
696e9dbad6fSeschrock 		break;
697fa9e4066Sahrens 
698e9dbad6fSeschrock 	case prop_type_string:
699e9dbad6fSeschrock 		*fixed = B_FALSE;
70007ba0419Seschrock 		break;
70107ba0419Seschrock 	}
70207ba0419Seschrock 
703e9dbad6fSeschrock 	return (ret);
704fa9e4066Sahrens }
705fa9e4066Sahrens 
706fa9e4066Sahrens #endif
707