xref: /titanic_52/usr/src/common/zfs/zfs_prop.c (revision 39c23413b8df94a95f67b34cfd4a4dfc3fd0b48d)
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 /*
22*39c23413Seschrock  * 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;
81fa9e4066Sahrens } prop_desc_t;
82fa9e4066Sahrens 
83fa9e4066Sahrens static prop_desc_t zfs_prop_table[ZFS_NPROP_ALL] = {
84fa9e4066Sahrens 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
85e9dbad6fSeschrock 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE },
86fa9e4066Sahrens 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
87e9dbad6fSeschrock 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE },
88fa9e4066Sahrens 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
89e9dbad6fSeschrock 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE },
90fa9e4066Sahrens 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
91e9dbad6fSeschrock 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE },
92fa9e4066Sahrens 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
934f75c27eSeschrock 	    ZFS_TYPE_ANY,
94e9dbad6fSeschrock 	    "<size>", "REFER", B_TRUE },
95fa9e4066Sahrens 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
96e9dbad6fSeschrock 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE },
97fa9e4066Sahrens 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
98e9dbad6fSeschrock 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE },
99fa9e4066Sahrens 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
100101f5531Slling 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
101e9dbad6fSeschrock 	    B_FALSE },
102fa9e4066Sahrens 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
103e9dbad6fSeschrock 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE },
104fa9e4066Sahrens 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
105fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
106e9dbad6fSeschrock 	    "<size> | none", "RESERV", B_TRUE },
107fa9e4066Sahrens 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
108e9dbad6fSeschrock 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE },
1094f75c27eSeschrock 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
110e9dbad6fSeschrock 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE },
111fa9e4066Sahrens 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
112fa9e4066Sahrens 	    prop_inherit,
1134f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
114e9dbad6fSeschrock 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE },
115fa9e4066Sahrens 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
116fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
117e9dbad6fSeschrock 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE },
118fa9e4066Sahrens 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
119fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
120e9dbad6fSeschrock 	    "on | off | share(1M) options", "SHARENFS", B_FALSE },
121f3861e1aSahl 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
122f3861e1aSahl 	    ZFS_TYPE_ANY,
123f3861e1aSahl 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE },
124101f5531Slling 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
1254f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
126e9dbad6fSeschrock 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE },
127101f5531Slling 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
1284f75c27eSeschrock 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
129e9dbad6fSeschrock 	    "on | off | lzjb", "COMPRESS", B_TRUE },
130fa9e4066Sahrens 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
131fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
132e9dbad6fSeschrock 	    "on | off", "ATIME", B_TRUE },
133fa9e4066Sahrens 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
13412054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
135e9dbad6fSeschrock 	    "on | off", "DEVICES", B_TRUE },
136fa9e4066Sahrens 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
13712054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
138e9dbad6fSeschrock 	    "on | off", "EXEC", B_TRUE },
139fa9e4066Sahrens 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
14012054bfcSnd150628 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
141e9dbad6fSeschrock 	    B_TRUE },
142fa9e4066Sahrens 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
1434f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
144e9dbad6fSeschrock 	    "on | off", "RDONLY", B_TRUE },
145fa9e4066Sahrens 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
1464f75c27eSeschrock 	    ZFS_TYPE_FILESYSTEM,
147e9dbad6fSeschrock 	    "on | off", "ZONED", B_TRUE },
148906d120cSlling 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
149a0965f35Sbonwick 	    prop_inherit,
150fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
151e9dbad6fSeschrock 	    "hidden | visible", "SNAPDIR", B_TRUE },
152e9dbad6fSeschrock 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
153e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
154e9dbad6fSeschrock 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE },
155e9dbad6fSeschrock 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
156e9dbad6fSeschrock 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
157e9dbad6fSeschrock 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE },
158e9dbad6fSeschrock 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
159fa9e4066Sahrens 	    ZFS_TYPE_FILESYSTEM,
160e9dbad6fSeschrock 	    "on | off", "CANMOUNT", B_TRUE },
1617b55fa8eSck153898 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
1627b55fa8eSck153898 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
1637b55fa8eSck153898 	    "on | off", "XATTR", B_TRUE },
164fa9e4066Sahrens 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
165a2eea2e1Sahrens 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE},
166fa9e4066Sahrens 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
167a2eea2e1Sahrens 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE },
168f3861e1aSahl 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
169f3861e1aSahl 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE },
170*39c23413Seschrock 	{ "numclones", prop_type_number,	0,	NULL,	prop_readonly,
171*39c23413Seschrock 	    ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE },
172fa9e4066Sahrens };
173fa9e4066Sahrens 
174fa9e4066Sahrens zfs_proptype_t
175fa9e4066Sahrens zfs_prop_get_type(zfs_prop_t prop)
176fa9e4066Sahrens {
177fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype);
178fa9e4066Sahrens }
179fa9e4066Sahrens 
180e9dbad6fSeschrock static boolean_t
181e9dbad6fSeschrock propname_match(const char *p, zfs_prop_t prop, size_t len)
1825c709891Seschrock {
1835c709891Seschrock 	const char *propname = zfs_prop_table[prop].pd_name;
1845c709891Seschrock #ifndef _KERNEL
1855c709891Seschrock 	const char *colname = zfs_prop_table[prop].pd_colname;
1865c709891Seschrock 	int c;
1875c709891Seschrock #endif
1885c709891Seschrock 
1893bb79becSeschrock #ifndef _KERNEL
1903bb79becSeschrock 	if (colname == NULL)
191e9dbad6fSeschrock 		return (B_FALSE);
1923bb79becSeschrock #endif
1933bb79becSeschrock 
1945c709891Seschrock 	if (len == strlen(propname) &&
1955c709891Seschrock 	    strncmp(p, propname, len) == 0)
196e9dbad6fSeschrock 		return (B_TRUE);
1975c709891Seschrock 
1985c709891Seschrock #ifndef _KERNEL
1993bb79becSeschrock 	if (len != strlen(colname))
200e9dbad6fSeschrock 		return (B_FALSE);
2015c709891Seschrock 
2025c709891Seschrock 	for (c = 0; c < len; c++)
2035c709891Seschrock 		if (p[c] != tolower(colname[c]))
2045c709891Seschrock 			break;
2055c709891Seschrock 
2065c709891Seschrock 	return (colname[c] == '\0');
2075c709891Seschrock #else
208e9dbad6fSeschrock 	return (B_FALSE);
2095c709891Seschrock #endif
2105c709891Seschrock }
2115c709891Seschrock 
2125c709891Seschrock 
213fa9e4066Sahrens /*
214fa9e4066Sahrens  * Given a property name, returns the corresponding property ID.
215fa9e4066Sahrens  */
216fa9e4066Sahrens zfs_prop_t
217fa9e4066Sahrens zfs_name_to_prop(const char *propname)
218fa9e4066Sahrens {
219fa9e4066Sahrens 	int i;
220fa9e4066Sahrens 
221fa9e4066Sahrens 	for (i = 0; i < ZFS_NPROP_ALL; i++) {
2225c709891Seschrock 		if (propname_match(propname, i, strlen(propname)))
223fa9e4066Sahrens 			return (i);
224fa9e4066Sahrens 	}
225fa9e4066Sahrens 
226fa9e4066Sahrens 	return (ZFS_PROP_INVAL);
227fa9e4066Sahrens }
228fa9e4066Sahrens 
229fa9e4066Sahrens /*
230e9dbad6fSeschrock  * For user property names, we allow all lowercase alphanumeric characters, plus
231e9dbad6fSeschrock  * a few useful punctuation characters.
232e9dbad6fSeschrock  */
233e9dbad6fSeschrock static int
234e9dbad6fSeschrock valid_char(char c)
235e9dbad6fSeschrock {
236e9dbad6fSeschrock 	return ((c >= 'a' && c <= 'z') ||
237e9dbad6fSeschrock 	    (c >= '0' && c <= '9') ||
238e9dbad6fSeschrock 	    c == '-' || c == '_' || c == '.' || c == ':');
239e9dbad6fSeschrock }
240e9dbad6fSeschrock 
241e9dbad6fSeschrock /*
242e9dbad6fSeschrock  * Returns true if this is a valid user-defined property (one with a ':').
243e9dbad6fSeschrock  */
244e9dbad6fSeschrock boolean_t
245e9dbad6fSeschrock zfs_prop_user(const char *name)
246e9dbad6fSeschrock {
247e9dbad6fSeschrock 	int i;
248e9dbad6fSeschrock 	char c;
249e9dbad6fSeschrock 	boolean_t foundsep = B_FALSE;
250e9dbad6fSeschrock 
251e9dbad6fSeschrock 	for (i = 0; i < strlen(name); i++) {
252e9dbad6fSeschrock 		c = name[i];
253e9dbad6fSeschrock 		if (!valid_char(c))
254e9dbad6fSeschrock 			return (B_FALSE);
255e9dbad6fSeschrock 		if (c == ':')
256e9dbad6fSeschrock 			foundsep = B_TRUE;
257e9dbad6fSeschrock 	}
258e9dbad6fSeschrock 
259e9dbad6fSeschrock 	if (!foundsep)
260e9dbad6fSeschrock 		return (B_FALSE);
261e9dbad6fSeschrock 
262e9dbad6fSeschrock 	return (B_TRUE);
263e9dbad6fSeschrock }
264e9dbad6fSeschrock 
265e9dbad6fSeschrock /*
266fa9e4066Sahrens  * Return the default value for the given property.
267fa9e4066Sahrens  */
2687f7322feSeschrock const char *
2697f7322feSeschrock zfs_prop_default_string(zfs_prop_t prop)
270fa9e4066Sahrens {
2717f7322feSeschrock 	return (zfs_prop_table[prop].pd_strdefault);
272fa9e4066Sahrens }
273fa9e4066Sahrens 
274fa9e4066Sahrens uint64_t
275fa9e4066Sahrens zfs_prop_default_numeric(zfs_prop_t prop)
276fa9e4066Sahrens {
277fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_numdefault);
278fa9e4066Sahrens }
279fa9e4066Sahrens 
280fa9e4066Sahrens /*
281fa9e4066Sahrens  * Returns TRUE if the property is readonly.
282fa9e4066Sahrens  */
283fa9e4066Sahrens int
284fa9e4066Sahrens zfs_prop_readonly(zfs_prop_t prop)
285fa9e4066Sahrens {
286fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
287fa9e4066Sahrens }
288fa9e4066Sahrens 
289fa9e4066Sahrens /*
290fa9e4066Sahrens  * Given a property ID, returns the corresponding name.
291fa9e4066Sahrens  */
292fa9e4066Sahrens const char *
293fa9e4066Sahrens zfs_prop_to_name(zfs_prop_t prop)
294fa9e4066Sahrens {
295fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_name);
296fa9e4066Sahrens }
297fa9e4066Sahrens 
298fa9e4066Sahrens /*
299fa9e4066Sahrens  * Returns TRUE if the property is inheritable.
300fa9e4066Sahrens  */
301fa9e4066Sahrens int
302fa9e4066Sahrens zfs_prop_inheritable(zfs_prop_t prop)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
305fa9e4066Sahrens }
306fa9e4066Sahrens 
307e9dbad6fSeschrock typedef struct zfs_index {
308e9dbad6fSeschrock 	const char *name;
309e9dbad6fSeschrock 	uint64_t index;
310e9dbad6fSeschrock } zfs_index_t;
311e9dbad6fSeschrock 
312e9dbad6fSeschrock static zfs_index_t checksum_table[] = {
313e9dbad6fSeschrock 	{ "on",		ZIO_CHECKSUM_ON },
314e9dbad6fSeschrock 	{ "off",	ZIO_CHECKSUM_OFF },
315e9dbad6fSeschrock 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
316e9dbad6fSeschrock 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
317e9dbad6fSeschrock 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
318e9dbad6fSeschrock 	{ NULL }
319e9dbad6fSeschrock };
320e9dbad6fSeschrock 
321e9dbad6fSeschrock static zfs_index_t compress_table[] = {
322e9dbad6fSeschrock 	{ "on",		ZIO_COMPRESS_ON },
323e9dbad6fSeschrock 	{ "off",	ZIO_COMPRESS_OFF },
324e9dbad6fSeschrock 	{ "lzjb",	ZIO_COMPRESS_LZJB },
325e9dbad6fSeschrock 	{ NULL }
326e9dbad6fSeschrock };
327e9dbad6fSeschrock 
328e9dbad6fSeschrock static zfs_index_t snapdir_table[] = {
329e9dbad6fSeschrock 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
330e9dbad6fSeschrock 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
331e9dbad6fSeschrock 	{ NULL }
332e9dbad6fSeschrock };
333e9dbad6fSeschrock 
334e9dbad6fSeschrock static zfs_index_t acl_mode_table[] = {
335e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
336e9dbad6fSeschrock 	{ "groupmask",	ZFS_ACL_GROUPMASK },
337e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
338e9dbad6fSeschrock 	{ NULL }
339e9dbad6fSeschrock };
340e9dbad6fSeschrock 
341e9dbad6fSeschrock static zfs_index_t acl_inherit_table[] = {
342e9dbad6fSeschrock 	{ "discard",	ZFS_ACL_DISCARD },
343e9dbad6fSeschrock 	{ "noallow",	ZFS_ACL_NOALLOW },
344e9dbad6fSeschrock 	{ "secure",	ZFS_ACL_SECURE },
345e9dbad6fSeschrock 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
346e9dbad6fSeschrock 	{ NULL }
347e9dbad6fSeschrock };
348e9dbad6fSeschrock 
349e9dbad6fSeschrock static zfs_index_t *
350e9dbad6fSeschrock zfs_prop_index_table(zfs_prop_t prop)
351e9dbad6fSeschrock {
352e9dbad6fSeschrock 	switch (prop) {
353e9dbad6fSeschrock 	case ZFS_PROP_CHECKSUM:
354e9dbad6fSeschrock 		return (checksum_table);
355e9dbad6fSeschrock 	case ZFS_PROP_COMPRESSION:
356e9dbad6fSeschrock 		return (compress_table);
357e9dbad6fSeschrock 	case ZFS_PROP_SNAPDIR:
358e9dbad6fSeschrock 		return (snapdir_table);
359e9dbad6fSeschrock 	case ZFS_PROP_ACLMODE:
360e9dbad6fSeschrock 		return (acl_mode_table);
361e9dbad6fSeschrock 	case ZFS_PROP_ACLINHERIT:
362e9dbad6fSeschrock 		return (acl_inherit_table);
363e9dbad6fSeschrock 	default:
364e9dbad6fSeschrock 		return (NULL);
365e9dbad6fSeschrock 	}
366e9dbad6fSeschrock }
367e9dbad6fSeschrock 
368e9dbad6fSeschrock 
369e9dbad6fSeschrock /*
370e9dbad6fSeschrock  * Tables of index types, plus functions to convert between the user view
371e9dbad6fSeschrock  * (strings) and internal representation (uint64_t).
372e9dbad6fSeschrock  */
373e9dbad6fSeschrock int
374e9dbad6fSeschrock zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
375e9dbad6fSeschrock {
376e9dbad6fSeschrock 	zfs_index_t *table;
377e9dbad6fSeschrock 	int i;
378e9dbad6fSeschrock 
379e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
380e9dbad6fSeschrock 		return (-1);
381e9dbad6fSeschrock 
382e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
383e9dbad6fSeschrock 		if (strcmp(string, table[i].name) == 0) {
384e9dbad6fSeschrock 			*index = table[i].index;
385e9dbad6fSeschrock 			return (0);
386e9dbad6fSeschrock 		}
387e9dbad6fSeschrock 	}
388e9dbad6fSeschrock 
389e9dbad6fSeschrock 	return (-1);
390e9dbad6fSeschrock }
391e9dbad6fSeschrock 
392e9dbad6fSeschrock int
393e9dbad6fSeschrock zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
394e9dbad6fSeschrock {
395e9dbad6fSeschrock 	zfs_index_t *table;
396e9dbad6fSeschrock 	int i;
397e9dbad6fSeschrock 
398e9dbad6fSeschrock 	if ((table = zfs_prop_index_table(prop)) == NULL)
399e9dbad6fSeschrock 		return (-1);
400e9dbad6fSeschrock 
401e9dbad6fSeschrock 	for (i = 0; table[i].name != NULL; i++) {
402e9dbad6fSeschrock 		if (table[i].index == index) {
403e9dbad6fSeschrock 			*string = table[i].name;
404e9dbad6fSeschrock 			return (0);
405e9dbad6fSeschrock 		}
406e9dbad6fSeschrock 	}
407e9dbad6fSeschrock 
408e9dbad6fSeschrock 	return (-1);
409e9dbad6fSeschrock }
410e9dbad6fSeschrock 
411acd76fe5Seschrock #ifndef _KERNEL
412acd76fe5Seschrock 
413fa9e4066Sahrens /*
414fa9e4066Sahrens  * Returns TRUE if the property applies to the given dataset types.
415fa9e4066Sahrens  */
416fa9e4066Sahrens int
417fa9e4066Sahrens zfs_prop_valid_for_type(zfs_prop_t prop, int types)
418fa9e4066Sahrens {
419fa9e4066Sahrens 	return ((zfs_prop_table[prop].pd_types & types) != 0);
420fa9e4066Sahrens }
421fa9e4066Sahrens 
422fa9e4066Sahrens /*
423fa9e4066Sahrens  * Returns a string describing the set of acceptable values for the given
424fa9e4066Sahrens  * property, or NULL if it cannot be set.
425fa9e4066Sahrens  */
426fa9e4066Sahrens const char *
427fa9e4066Sahrens zfs_prop_values(zfs_prop_t prop)
428fa9e4066Sahrens {
429fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_values);
430fa9e4066Sahrens }
431fa9e4066Sahrens 
432fa9e4066Sahrens /*
433fa9e4066Sahrens  * Returns TRUE if this property is a string type.  Note that index types
434fa9e4066Sahrens  * (compression, checksum) are treated as strings in userland, even though they
435fa9e4066Sahrens  * are stored numerically on disk.
436fa9e4066Sahrens  */
437fa9e4066Sahrens int
438fa9e4066Sahrens zfs_prop_is_string(zfs_prop_t prop)
439fa9e4066Sahrens {
440fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
441fa9e4066Sahrens 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
442fa9e4066Sahrens }
443fa9e4066Sahrens 
444fa9e4066Sahrens /*
445fa9e4066Sahrens  * Returns the column header for the given property.  Used only in
446fa9e4066Sahrens  * 'zfs list -o', but centralized here with the other property information.
447fa9e4066Sahrens  */
448fa9e4066Sahrens const char *
449fa9e4066Sahrens zfs_prop_column_name(zfs_prop_t prop)
450fa9e4066Sahrens {
451fa9e4066Sahrens 	return (zfs_prop_table[prop].pd_colname);
452fa9e4066Sahrens }
453fa9e4066Sahrens 
454fa9e4066Sahrens /*
455e9dbad6fSeschrock  * Returns whether the given property should be displayed right-justified for
456e9dbad6fSeschrock  * 'zfs list'.
457fa9e4066Sahrens  */
458e9dbad6fSeschrock boolean_t
459e9dbad6fSeschrock zfs_prop_align_right(zfs_prop_t prop)
460fa9e4066Sahrens {
461e9dbad6fSeschrock 	return (zfs_prop_table[prop].pd_rightalign);
462fa9e4066Sahrens }
463fa9e4066Sahrens 
464fa9e4066Sahrens /*
465e9dbad6fSeschrock  * Determines the minimum width for the column, and indicates whether it's fixed
466e9dbad6fSeschrock  * or not.  Only string columns are non-fixed.
467fa9e4066Sahrens  */
468e9dbad6fSeschrock size_t
469e9dbad6fSeschrock zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
470fa9e4066Sahrens {
471e9dbad6fSeschrock 	prop_desc_t *pd = &zfs_prop_table[prop];
472e9dbad6fSeschrock 	zfs_index_t *idx;
473e9dbad6fSeschrock 	size_t ret;
474fa9e4066Sahrens 	int i;
475fa9e4066Sahrens 
476e9dbad6fSeschrock 	*fixed = B_TRUE;
477fa9e4066Sahrens 
47807ba0419Seschrock 	/*
479e9dbad6fSeschrock 	 * Start with the width of the column name.
48007ba0419Seschrock 	 */
481e9dbad6fSeschrock 	ret = strlen(pd->pd_colname);
482fa9e4066Sahrens 
483e9dbad6fSeschrock 	/*
484e9dbad6fSeschrock 	 * For fixed-width values, make sure the width is large enough to hold
485e9dbad6fSeschrock 	 * any possible value.
486e9dbad6fSeschrock 	 */
487e9dbad6fSeschrock 	switch (pd->pd_proptype) {
488e9dbad6fSeschrock 	case prop_type_number:
489e9dbad6fSeschrock 		/*
490e9dbad6fSeschrock 		 * The maximum length of a human-readable number is 5 characters
491e9dbad6fSeschrock 		 * ("20.4M", for example).
492e9dbad6fSeschrock 		 */
493e9dbad6fSeschrock 		if (ret < 5)
494e9dbad6fSeschrock 			ret = 5;
495e9dbad6fSeschrock 		/*
496e9dbad6fSeschrock 		 * 'creation' is handled specially because it's a number
497e9dbad6fSeschrock 		 * internally, but displayed as a date string.
498e9dbad6fSeschrock 		 */
499e9dbad6fSeschrock 		if (prop == ZFS_PROP_CREATION)
500e9dbad6fSeschrock 			*fixed = B_FALSE;
501e9dbad6fSeschrock 		break;
502e9dbad6fSeschrock 	case prop_type_boolean:
503e9dbad6fSeschrock 		/*
504e9dbad6fSeschrock 		 * The maximum length of a boolean value is 3 characters, for
505e9dbad6fSeschrock 		 * "off".
506e9dbad6fSeschrock 		 */
507e9dbad6fSeschrock 		if (ret < 3)
508e9dbad6fSeschrock 			ret = 3;
509e9dbad6fSeschrock 		break;
510e9dbad6fSeschrock 	case prop_type_index:
511e9dbad6fSeschrock 		idx = zfs_prop_index_table(prop);
512e9dbad6fSeschrock 		for (i = 0; idx[i].name != NULL; i++) {
513e9dbad6fSeschrock 			if (strlen(idx[i].name) > ret)
514e9dbad6fSeschrock 				ret = strlen(idx[i].name);
515fa9e4066Sahrens 		}
516e9dbad6fSeschrock 		break;
517fa9e4066Sahrens 
518e9dbad6fSeschrock 	case prop_type_string:
519e9dbad6fSeschrock 		*fixed = B_FALSE;
52007ba0419Seschrock 		break;
52107ba0419Seschrock 	}
52207ba0419Seschrock 
523e9dbad6fSeschrock 	return (ret);
524fa9e4066Sahrens }
525fa9e4066Sahrens 
526fa9e4066Sahrens #endif
527