xref: /titanic_53/usr/src/common/zfs/zfs_prop.c (revision 66e2aacc02a4625d105fb249ad16c27e79604ff2)
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;
81*66e2aaccSgw25295 	boolean_t	pd_visible;
82fa9e4066Sahrens } prop_desc_t;
83fa9e4066Sahrens 
84*66e2aaccSgw25295 static prop_desc_t zfs_prop_table[] = {
85fa9e4066Sahrens 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
86*66e2aaccSgw25295 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE,
87*66e2aaccSgw25295 	    B_TRUE },
88fa9e4066Sahrens 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
89*66e2aaccSgw25295 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE },
90fa9e4066Sahrens 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
91*66e2aaccSgw25295 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE, B_TRUE },
92fa9e4066Sahrens 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
93*66e2aaccSgw25295 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE,
94*66e2aaccSgw25295 	    B_TRUE },
95fa9e4066Sahrens 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
964f75c27eSeschrock 	    ZFS_TYPE_ANY,
97*66e2aaccSgw25295 	    "<size>", "REFER", B_TRUE, B_TRUE },
98fa9e4066Sahrens 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
99*66e2aaccSgw25295 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE,
100*66e2aaccSgw25295 	    B_TRUE },
101fa9e4066Sahrens 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
102*66e2aaccSgw25295 	    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",
105*66e2aaccSgw25295 	    B_FALSE, B_TRUE },
106fa9e4066Sahrens 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
107*66e2aaccSgw25295 	    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,
110*66e2aaccSgw25295 	    "<size> | none", "RESERV", B_TRUE, B_TRUE },
111fa9e4066Sahrens 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
112*66e2aaccSgw25295 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE },
1134f75c27eSeschrock 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
114*66e2aaccSgw25295 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE,
115*66e2aaccSgw25295 	    B_TRUE },
116fa9e4066Sahrens 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
117fa9e4066Sahrens 	    prop_inherit,
1184f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
119*66e2aaccSgw25295 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE },
120fa9e4066Sahrens 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
121fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
122*66e2aaccSgw25295 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE },
123fa9e4066Sahrens 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
124fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
125*66e2aaccSgw25295 	    "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,
128*66e2aaccSgw25295 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE,
129*66e2aaccSgw25295 	    B_TRUE },
130101f5531Slling 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
1314f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
132*66e2aaccSgw25295 	    "on | off | lzjb", "COMPRESS", B_TRUE, B_TRUE },
133fa9e4066Sahrens 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
134fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
135*66e2aaccSgw25295 	    "on | off", "ATIME", B_TRUE, B_TRUE },
136fa9e4066Sahrens 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
13712054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
138*66e2aaccSgw25295 	    "on | off", "DEVICES", B_TRUE, B_TRUE },
139fa9e4066Sahrens 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
14012054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
141*66e2aaccSgw25295 	    "on | off", "EXEC", B_TRUE, B_TRUE },
142fa9e4066Sahrens 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
14312054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
144*66e2aaccSgw25295 	    B_TRUE, B_TRUE },
145fa9e4066Sahrens 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
1464f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
147*66e2aaccSgw25295 	    "on | off", "RDONLY", B_TRUE, B_TRUE },
148fa9e4066Sahrens 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
1494f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
150*66e2aaccSgw25295 	    "on | off", "ZONED", B_TRUE, B_TRUE },
151906d120cSlling 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
152a0965f35Sbonwick 	    prop_inherit,
153fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
154*66e2aaccSgw25295 	    "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE },
155e9dbad6fSeschrock 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
156e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
157*66e2aaccSgw25295 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE, B_TRUE },
158e9dbad6fSeschrock 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
159e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
160*66e2aaccSgw25295 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE,
161*66e2aaccSgw25295 	    B_TRUE },
162*66e2aaccSgw25295 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
163*66e2aaccSgw25295 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE },
164*66e2aaccSgw25295 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
165*66e2aaccSgw25295 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE },
166e9dbad6fSeschrock 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
167fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
168*66e2aaccSgw25295 	    "on | off", "CANMOUNT", B_TRUE, B_TRUE },
169*66e2aaccSgw25295 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
170*66e2aaccSgw25295 	    ZFS_TYPE_ANY,
171*66e2aaccSgw25295 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE },
172*66e2aaccSgw25295 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
173*66e2aaccSgw25295 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE },
1747b55fa8eSck153898 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
1757b55fa8eSck153898 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
176*66e2aaccSgw25295 	    "on | off", "XATTR", B_TRUE, B_TRUE },
17739c23413Seschrock 	{ "numclones", prop_type_number,	0,	NULL,	prop_readonly,
178*66e2aaccSgw25295 	    ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE, B_FALSE },
179fa9e4066Sahrens };
180fa9e4066Sahrens 
181*66e2aaccSgw25295 #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
182*66e2aaccSgw25295 
183fa9e4066Sahrens zfs_proptype_t
184fa9e4066Sahrens zfs_prop_get_type(zfs_prop_t prop)
185fa9e4066Sahrens {
186fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype);
187fa9e4066Sahrens }
188fa9e4066Sahrens 
189e9dbad6fSeschrock static boolean_t
190e9dbad6fSeschrock propname_match(const char *p, zfs_prop_t prop, size_t len)
1915c709891Seschrock {
1925c709891Seschrock 	const char *propname = zfs_prop_table[prop].pd_name;
1935c709891Seschrock #ifndef _KERNEL
1945c709891Seschrock 	const char *colname = zfs_prop_table[prop].pd_colname;
1955c709891Seschrock 	int c;
1965c709891Seschrock #endif
1975c709891Seschrock 
1983bb79becSeschrock #ifndef _KERNEL
1993bb79becSeschrock 	if (colname == NULL)
200e9dbad6fSeschrock 		return (B_FALSE);
2013bb79becSeschrock #endif
2023bb79becSeschrock 
2035c709891Seschrock 	if (len == strlen(propname) &&
2045c709891Seschrock 	    strncmp(p, propname, len) == 0)
205e9dbad6fSeschrock 		return (B_TRUE);
2065c709891Seschrock 
2075c709891Seschrock #ifndef _KERNEL
2083bb79becSeschrock 	if (len != strlen(colname))
209e9dbad6fSeschrock 		return (B_FALSE);
2105c709891Seschrock 
2115c709891Seschrock 	for (c = 0; c < len; c++)
2125c709891Seschrock 		if (p[c] != tolower(colname[c]))
2135c709891Seschrock 			break;
2145c709891Seschrock 
2155c709891Seschrock 	return (colname[c] == '\0');
2165c709891Seschrock #else
217e9dbad6fSeschrock 	return (B_FALSE);
2185c709891Seschrock #endif
2195c709891Seschrock }
2205c709891Seschrock 
221*66e2aaccSgw25295 zfs_prop_t
222*66e2aaccSgw25295 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
223*66e2aaccSgw25295 {
224*66e2aaccSgw25295 	const char *propname = cb_data;
225*66e2aaccSgw25295 
226*66e2aaccSgw25295 	if (propname_match(propname, prop, strlen(propname)))
227*66e2aaccSgw25295 		return (prop);
228*66e2aaccSgw25295 
229*66e2aaccSgw25295 	return (ZFS_PROP_CONT);
230*66e2aaccSgw25295 }
2315c709891Seschrock 
232fa9e4066Sahrens /*
233fa9e4066Sahrens  * Given a property name, returns the corresponding property ID.
234fa9e4066Sahrens  */
235fa9e4066Sahrens zfs_prop_t
236fa9e4066Sahrens zfs_name_to_prop(const char *propname)
237fa9e4066Sahrens {
238*66e2aaccSgw25295 	zfs_prop_t prop;
239fa9e4066Sahrens 
240*66e2aaccSgw25295 	prop = zfs_prop_iter(zfs_name_to_prop_cb, (void *)propname, B_TRUE);
241*66e2aaccSgw25295 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
242fa9e4066Sahrens }
243fa9e4066Sahrens 
244fa9e4066Sahrens /*
245e9dbad6fSeschrock  * For user property names, we allow all lowercase alphanumeric characters, plus
246e9dbad6fSeschrock  * a few useful punctuation characters.
247e9dbad6fSeschrock  */
248e9dbad6fSeschrock static int
249e9dbad6fSeschrock valid_char(char c)
250e9dbad6fSeschrock {
251e9dbad6fSeschrock 	return ((c >= 'a' && c <= 'z') ||
252e9dbad6fSeschrock 	    (c >= '0' && c <= '9') ||
253e9dbad6fSeschrock 	    c == '-' || c == '_' || c == '.' || c == ':');
254e9dbad6fSeschrock }
255e9dbad6fSeschrock 
256e9dbad6fSeschrock /*
257e9dbad6fSeschrock  * Returns true if this is a valid user-defined property (one with a ':').
258e9dbad6fSeschrock  */
259e9dbad6fSeschrock boolean_t
260e9dbad6fSeschrock zfs_prop_user(const char *name)
261e9dbad6fSeschrock {
262e9dbad6fSeschrock 	int i;
263e9dbad6fSeschrock 	char c;
264e9dbad6fSeschrock 	boolean_t foundsep = B_FALSE;
265e9dbad6fSeschrock 
266e9dbad6fSeschrock 	for (i = 0; i < strlen(name); i++) {
267e9dbad6fSeschrock 		c = name[i];
268e9dbad6fSeschrock 		if (!valid_char(c))
269e9dbad6fSeschrock 			return (B_FALSE);
270e9dbad6fSeschrock 		if (c == ':')
271e9dbad6fSeschrock 			foundsep = B_TRUE;
272e9dbad6fSeschrock 	}
273e9dbad6fSeschrock 
274e9dbad6fSeschrock 	if (!foundsep)
275e9dbad6fSeschrock 		return (B_FALSE);
276e9dbad6fSeschrock 
277e9dbad6fSeschrock 	return (B_TRUE);
278e9dbad6fSeschrock }
279e9dbad6fSeschrock 
280e9dbad6fSeschrock /*
281fa9e4066Sahrens  * Return the default value for the given property.
282fa9e4066Sahrens  */
2837f7322feSeschrock const char *
2847f7322feSeschrock zfs_prop_default_string(zfs_prop_t prop)
285fa9e4066Sahrens {
2867f7322feSeschrock 	return (zfs_prop_table[prop].pd_strdefault);
287fa9e4066Sahrens }
288fa9e4066Sahrens 
289fa9e4066Sahrens uint64_t
290fa9e4066Sahrens zfs_prop_default_numeric(zfs_prop_t prop)
291fa9e4066Sahrens {
292fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_numdefault);
293fa9e4066Sahrens }
294fa9e4066Sahrens 
295fa9e4066Sahrens /*
296fa9e4066Sahrens  * Returns TRUE if the property is readonly.
297fa9e4066Sahrens  */
298fa9e4066Sahrens int
299fa9e4066Sahrens zfs_prop_readonly(zfs_prop_t prop)
300fa9e4066Sahrens {
301fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
302fa9e4066Sahrens }
303fa9e4066Sahrens 
304fa9e4066Sahrens /*
305fa9e4066Sahrens  * Given a property ID, returns the corresponding name.
306fa9e4066Sahrens  */
307fa9e4066Sahrens const char *
308fa9e4066Sahrens zfs_prop_to_name(zfs_prop_t prop)
309fa9e4066Sahrens {
310fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_name);
311fa9e4066Sahrens }
312fa9e4066Sahrens 
313fa9e4066Sahrens /*
314fa9e4066Sahrens  * Returns TRUE if the property is inheritable.
315fa9e4066Sahrens  */
316fa9e4066Sahrens int
317fa9e4066Sahrens zfs_prop_inheritable(zfs_prop_t prop)
318fa9e4066Sahrens {
319fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
320fa9e4066Sahrens }
321fa9e4066Sahrens 
322e9dbad6fSeschrock typedef struct zfs_index {
323e9dbad6fSeschrock 	const char *name;
324e9dbad6fSeschrock 	uint64_t index;
325e9dbad6fSeschrock } zfs_index_t;
326e9dbad6fSeschrock 
327e9dbad6fSeschrock static zfs_index_t checksum_table[] = {
328e9dbad6fSeschrock 	{ "on",		ZIO_CHECKSUM_ON },
329e9dbad6fSeschrock 	{ "off",	ZIO_CHECKSUM_OFF },
330e9dbad6fSeschrock 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
331e9dbad6fSeschrock 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
332e9dbad6fSeschrock 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
333e9dbad6fSeschrock 	{ NULL }
334e9dbad6fSeschrock };
335e9dbad6fSeschrock 
336e9dbad6fSeschrock static zfs_index_t compress_table[] = {
337e9dbad6fSeschrock 	{ "on",		ZIO_COMPRESS_ON },
338e9dbad6fSeschrock 	{ "off",	ZIO_COMPRESS_OFF },
339e9dbad6fSeschrock 	{ "lzjb",	ZIO_COMPRESS_LZJB },
340e9dbad6fSeschrock 	{ NULL }
341e9dbad6fSeschrock };
342e9dbad6fSeschrock 
343e9dbad6fSeschrock static zfs_index_t snapdir_table[] = {
344e9dbad6fSeschrock 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
345e9dbad6fSeschrock 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
346e9dbad6fSeschrock 	{ NULL }
347e9dbad6fSeschrock };
348e9dbad6fSeschrock 
349e9dbad6fSeschrock static zfs_index_t acl_mode_table[] = {
350e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
351e9dbad6fSeschrock 	{ "groupmask",	ZFS_ACL_GROUPMASK },
352e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
353e9dbad6fSeschrock 	{ NULL }
354e9dbad6fSeschrock };
355e9dbad6fSeschrock 
356e9dbad6fSeschrock static zfs_index_t acl_inherit_table[] = {
357e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
358e9dbad6fSeschrock 	{ "noallow",	ZFS_ACL_NOALLOW },
359e9dbad6fSeschrock 	{ "secure",	ZFS_ACL_SECURE },
360e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
361e9dbad6fSeschrock 	{ NULL }
362e9dbad6fSeschrock };
363e9dbad6fSeschrock 
364e9dbad6fSeschrock static zfs_index_t *
365e9dbad6fSeschrock zfs_prop_index_table(zfs_prop_t prop)
366e9dbad6fSeschrock {
367e9dbad6fSeschrock 	switch (prop) {
368e9dbad6fSeschrock 	case ZFS_PROP_CHECKSUM:
369e9dbad6fSeschrock 		return (checksum_table);
370e9dbad6fSeschrock 	case ZFS_PROP_COMPRESSION:
371e9dbad6fSeschrock 		return (compress_table);
372e9dbad6fSeschrock 	case ZFS_PROP_SNAPDIR:
373e9dbad6fSeschrock 		return (snapdir_table);
374e9dbad6fSeschrock 	case ZFS_PROP_ACLMODE:
375e9dbad6fSeschrock 		return (acl_mode_table);
376e9dbad6fSeschrock 	case ZFS_PROP_ACLINHERIT:
377e9dbad6fSeschrock 		return (acl_inherit_table);
378e9dbad6fSeschrock 	default:
379e9dbad6fSeschrock 		return (NULL);
380e9dbad6fSeschrock 	}
381e9dbad6fSeschrock }
382e9dbad6fSeschrock 
383e9dbad6fSeschrock 
384e9dbad6fSeschrock /*
385e9dbad6fSeschrock  * Tables of index types, plus functions to convert between the user view
386e9dbad6fSeschrock  * (strings) and internal representation (uint64_t).
387e9dbad6fSeschrock  */
388e9dbad6fSeschrock int
389e9dbad6fSeschrock zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
390e9dbad6fSeschrock {
391e9dbad6fSeschrock 	zfs_index_t *table;
392e9dbad6fSeschrock 	int i;
393e9dbad6fSeschrock 
394e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
395e9dbad6fSeschrock 		return (-1);
396e9dbad6fSeschrock 
397e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
398e9dbad6fSeschrock 		if (strcmp(string, table[i].name) == 0) {
399e9dbad6fSeschrock 			*index = table[i].index;
400e9dbad6fSeschrock 			return (0);
401e9dbad6fSeschrock 		}
402e9dbad6fSeschrock 	}
403e9dbad6fSeschrock 
404e9dbad6fSeschrock 	return (-1);
405e9dbad6fSeschrock }
406e9dbad6fSeschrock 
407e9dbad6fSeschrock int
408e9dbad6fSeschrock zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
409e9dbad6fSeschrock {
410e9dbad6fSeschrock 	zfs_index_t *table;
411e9dbad6fSeschrock 	int i;
412e9dbad6fSeschrock 
413e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
414e9dbad6fSeschrock 		return (-1);
415e9dbad6fSeschrock 
416e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
417e9dbad6fSeschrock 		if (table[i].index == index) {
418e9dbad6fSeschrock 			*string = table[i].name;
419e9dbad6fSeschrock 			return (0);
420e9dbad6fSeschrock 		}
421e9dbad6fSeschrock 	}
422e9dbad6fSeschrock 
423e9dbad6fSeschrock 	return (-1);
424e9dbad6fSeschrock }
425e9dbad6fSeschrock 
426*66e2aaccSgw25295 /*
427*66e2aaccSgw25295  * Determine if the specified property is visible or not.
428*66e2aaccSgw25295  */
429*66e2aaccSgw25295 boolean_t
430*66e2aaccSgw25295 zfs_prop_is_visible(zfs_prop_t prop)
431*66e2aaccSgw25295 {
432*66e2aaccSgw25295 	if (prop < 0)
433*66e2aaccSgw25295 		return (B_FALSE);
434*66e2aaccSgw25295 
435*66e2aaccSgw25295 	return (zfs_prop_table[prop].pd_visible);
436*66e2aaccSgw25295 }
437*66e2aaccSgw25295 
438*66e2aaccSgw25295 /*
439*66e2aaccSgw25295  * Iterate over all properties, calling back into the specified function
440*66e2aaccSgw25295  * for each property. We will continue to iterate until we either
441*66e2aaccSgw25295  * reach the end or the callback function something other than
442*66e2aaccSgw25295  * ZFS_PROP_CONT.
443*66e2aaccSgw25295  */
444*66e2aaccSgw25295 zfs_prop_t
445*66e2aaccSgw25295 zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
446*66e2aaccSgw25295 {
447*66e2aaccSgw25295 	int i;
448*66e2aaccSgw25295 
449*66e2aaccSgw25295 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
450*66e2aaccSgw25295 		if (zfs_prop_is_visible(i) || show_all) {
451*66e2aaccSgw25295 			if (func(i, cb) != ZFS_PROP_CONT)
452*66e2aaccSgw25295 				return (i);
453*66e2aaccSgw25295 		}
454*66e2aaccSgw25295 	}
455*66e2aaccSgw25295 	return (ZFS_PROP_CONT);
456*66e2aaccSgw25295 }
457*66e2aaccSgw25295 
458acd76fe5Seschrock #ifndef _KERNEL
459acd76fe5Seschrock 
460fa9e4066Sahrens /*
461fa9e4066Sahrens  * Returns TRUE if the property applies to the given dataset types.
462fa9e4066Sahrens  */
463fa9e4066Sahrens int
464fa9e4066Sahrens zfs_prop_valid_for_type(zfs_prop_t prop, int types)
465fa9e4066Sahrens {
466fa9e4066Sahrens 	return ((zfs_prop_table[prop].pd_types & types) != 0);
467fa9e4066Sahrens }
468fa9e4066Sahrens 
469fa9e4066Sahrens /*
470fa9e4066Sahrens  * Returns a string describing the set of acceptable values for the given
471fa9e4066Sahrens  * property, or NULL if it cannot be set.
472fa9e4066Sahrens  */
473fa9e4066Sahrens const char *
474fa9e4066Sahrens zfs_prop_values(zfs_prop_t prop)
475fa9e4066Sahrens {
476fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_values);
477fa9e4066Sahrens }
478fa9e4066Sahrens 
479fa9e4066Sahrens /*
480fa9e4066Sahrens  * Returns TRUE if this property is a string type.  Note that index types
481fa9e4066Sahrens  * (compression, checksum) are treated as strings in userland, even though they
482fa9e4066Sahrens  * are stored numerically on disk.
483fa9e4066Sahrens  */
484fa9e4066Sahrens int
485fa9e4066Sahrens zfs_prop_is_string(zfs_prop_t prop)
486fa9e4066Sahrens {
487fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
488fa9e4066Sahrens 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
489fa9e4066Sahrens }
490fa9e4066Sahrens 
491fa9e4066Sahrens /*
492fa9e4066Sahrens  * Returns the column header for the given property.  Used only in
493fa9e4066Sahrens  * 'zfs list -o', but centralized here with the other property information.
494fa9e4066Sahrens  */
495fa9e4066Sahrens const char *
496fa9e4066Sahrens zfs_prop_column_name(zfs_prop_t prop)
497fa9e4066Sahrens {
498fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_colname);
499fa9e4066Sahrens }
500fa9e4066Sahrens 
501fa9e4066Sahrens /*
502e9dbad6fSeschrock  * Returns whether the given property should be displayed right-justified for
503e9dbad6fSeschrock  * 'zfs list'.
504fa9e4066Sahrens  */
505e9dbad6fSeschrock boolean_t
506e9dbad6fSeschrock zfs_prop_align_right(zfs_prop_t prop)
507fa9e4066Sahrens {
508e9dbad6fSeschrock 	return (zfs_prop_table[prop].pd_rightalign);
509fa9e4066Sahrens }
510fa9e4066Sahrens 
511fa9e4066Sahrens /*
512e9dbad6fSeschrock  * Determines the minimum width for the column, and indicates whether it's fixed
513e9dbad6fSeschrock  * or not.  Only string columns are non-fixed.
514fa9e4066Sahrens  */
515e9dbad6fSeschrock size_t
516e9dbad6fSeschrock zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
517fa9e4066Sahrens {
518e9dbad6fSeschrock 	prop_desc_t *pd = &zfs_prop_table[prop];
519e9dbad6fSeschrock 	zfs_index_t *idx;
520e9dbad6fSeschrock 	size_t ret;
521fa9e4066Sahrens 	int i;
522fa9e4066Sahrens 
523e9dbad6fSeschrock 	*fixed = B_TRUE;
524fa9e4066Sahrens 
52507ba0419Seschrock 	/*
526e9dbad6fSeschrock 	 * Start with the width of the column name.
52707ba0419Seschrock 	 */
528e9dbad6fSeschrock 	ret = strlen(pd->pd_colname);
529fa9e4066Sahrens 
530e9dbad6fSeschrock 	/*
531e9dbad6fSeschrock 	 * For fixed-width values, make sure the width is large enough to hold
532e9dbad6fSeschrock 	 * any possible value.
533e9dbad6fSeschrock 	 */
534e9dbad6fSeschrock 	switch (pd->pd_proptype) {
535e9dbad6fSeschrock 	case prop_type_number:
536e9dbad6fSeschrock 		/*
537e9dbad6fSeschrock 		 * The maximum length of a human-readable number is 5 characters
538e9dbad6fSeschrock 		 * ("20.4M", for example).
539e9dbad6fSeschrock 		 */
540e9dbad6fSeschrock 		if (ret < 5)
541e9dbad6fSeschrock 			ret = 5;
542e9dbad6fSeschrock 		/*
543e9dbad6fSeschrock 		 * 'creation' is handled specially because it's a number
544e9dbad6fSeschrock 		 * internally, but displayed as a date string.
545e9dbad6fSeschrock 		 */
546e9dbad6fSeschrock 		if (prop == ZFS_PROP_CREATION)
547e9dbad6fSeschrock 			*fixed = B_FALSE;
548e9dbad6fSeschrock 		break;
549e9dbad6fSeschrock 	case prop_type_boolean:
550e9dbad6fSeschrock 		/*
551e9dbad6fSeschrock 		 * The maximum length of a boolean value is 3 characters, for
552e9dbad6fSeschrock 		 * "off".
553e9dbad6fSeschrock 		 */
554e9dbad6fSeschrock 		if (ret < 3)
555e9dbad6fSeschrock 			ret = 3;
556e9dbad6fSeschrock 		break;
557e9dbad6fSeschrock 	case prop_type_index:
558e9dbad6fSeschrock 		idx = zfs_prop_index_table(prop);
559e9dbad6fSeschrock 		for (i = 0; idx[i].name != NULL; i++) {
560e9dbad6fSeschrock 			if (strlen(idx[i].name) > ret)
561e9dbad6fSeschrock 				ret = strlen(idx[i].name);
562fa9e4066Sahrens 		}
563e9dbad6fSeschrock 		break;
564fa9e4066Sahrens 
565e9dbad6fSeschrock 	case prop_type_string:
566e9dbad6fSeschrock 		*fixed = B_FALSE;
56707ba0419Seschrock 		break;
56807ba0419Seschrock 	}
56907ba0419Seschrock 
570e9dbad6fSeschrock 	return (ret);
571fa9e4066Sahrens }
572fa9e4066Sahrens 
573fa9e4066Sahrens #endif
574