xref: /freebsd/sys/contrib/openzfs/module/zcommon/zprop_common.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23eda14cbcSMatt Macy  * Use is subject to license terms.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy /*
26eda14cbcSMatt Macy  * Copyright (c) 2012 by Delphix. All rights reserved.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy /*
30eda14cbcSMatt Macy  * Common routines used by zfs and zpool property management.
31eda14cbcSMatt Macy  */
32eda14cbcSMatt Macy 
33eda14cbcSMatt Macy #include <sys/zio.h>
34eda14cbcSMatt Macy #include <sys/spa.h>
35eda14cbcSMatt Macy #include <sys/zfs_acl.h>
36eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
37eda14cbcSMatt Macy #include <sys/zfs_sysfs.h>
38eda14cbcSMatt Macy #include <sys/zfs_znode.h>
39eda14cbcSMatt Macy #include <sys/fs/zfs.h>
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy #include "zfs_prop.h"
42eda14cbcSMatt Macy #include "zfs_deleg.h"
43eda14cbcSMatt Macy 
44eda14cbcSMatt Macy #if !defined(_KERNEL)
45eda14cbcSMatt Macy #include <stdlib.h>
46eda14cbcSMatt Macy #include <string.h>
47eda14cbcSMatt Macy #include <ctype.h>
48eda14cbcSMatt Macy #include <sys/stat.h>
49eda14cbcSMatt Macy #endif
50eda14cbcSMatt Macy 
51eda14cbcSMatt Macy static zprop_desc_t *
zprop_get_proptable(zfs_type_t type)52eda14cbcSMatt Macy zprop_get_proptable(zfs_type_t type)
53eda14cbcSMatt Macy {
54eda14cbcSMatt Macy 	if (type == ZFS_TYPE_POOL)
55eda14cbcSMatt Macy 		return (zpool_prop_get_table());
56681ce946SMartin Matuska 	else if (type == ZFS_TYPE_VDEV)
57681ce946SMartin Matuska 		return (vdev_prop_get_table());
58eda14cbcSMatt Macy 	else
59eda14cbcSMatt Macy 		return (zfs_prop_get_table());
60eda14cbcSMatt Macy }
61eda14cbcSMatt Macy 
62eda14cbcSMatt Macy static int
zprop_get_numprops(zfs_type_t type)63eda14cbcSMatt Macy zprop_get_numprops(zfs_type_t type)
64eda14cbcSMatt Macy {
65eda14cbcSMatt Macy 	if (type == ZFS_TYPE_POOL)
66eda14cbcSMatt Macy 		return (ZPOOL_NUM_PROPS);
67681ce946SMartin Matuska 	else if (type == ZFS_TYPE_VDEV)
68681ce946SMartin Matuska 		return (VDEV_NUM_PROPS);
69eda14cbcSMatt Macy 	else
70eda14cbcSMatt Macy 		return (ZFS_NUM_PROPS);
71eda14cbcSMatt Macy }
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy static boolean_t
zfs_mod_supported_prop(const char * name,zfs_type_t type,const struct zfs_mod_supported_features * sfeatures)74e92ffd9bSMartin Matuska zfs_mod_supported_prop(const char *name, zfs_type_t type,
75e92ffd9bSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
76eda14cbcSMatt Macy {
77eda14cbcSMatt Macy /*
78eda14cbcSMatt Macy  * The zfs module spa_feature_table[], whether in-kernel or in libzpool,
79eda14cbcSMatt Macy  * always supports all the properties. libzfs needs to query the running
80eda14cbcSMatt Macy  * module, via sysfs, to determine which properties are supported.
81eda14cbcSMatt Macy  *
82eda14cbcSMatt Macy  * The equivalent _can_ be done on FreeBSD by way of the sysctl
83eda14cbcSMatt Macy  * tree, but this has not been done yet.
84eda14cbcSMatt Macy  */
85eda14cbcSMatt Macy #if defined(_KERNEL) || defined(LIB_ZPOOL_BUILD) || defined(__FreeBSD__)
86e92ffd9bSMartin Matuska 	(void) name, (void) type, (void) sfeatures;
87eda14cbcSMatt Macy 	return (B_TRUE);
88eda14cbcSMatt Macy #else
89eda14cbcSMatt Macy 	return (zfs_mod_supported(type == ZFS_TYPE_POOL ?
90681ce946SMartin Matuska 	    ZFS_SYSFS_POOL_PROPERTIES : (type == ZFS_TYPE_VDEV ?
91e92ffd9bSMartin Matuska 	    ZFS_SYSFS_VDEV_PROPERTIES : ZFS_SYSFS_DATASET_PROPERTIES),
92e92ffd9bSMartin Matuska 	    name, sfeatures));
93eda14cbcSMatt Macy #endif
94eda14cbcSMatt Macy }
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy void
zprop_register_impl(int prop,const char * name,zprop_type_t type,uint64_t numdefault,const char * strdefault,zprop_attr_t attr,int objset_types,const char * values,const char * colname,boolean_t rightalign,boolean_t visible,boolean_t flex,const zprop_index_t * idx_tbl,const struct zfs_mod_supported_features * sfeatures)97eda14cbcSMatt Macy zprop_register_impl(int prop, const char *name, zprop_type_t type,
98eda14cbcSMatt Macy     uint64_t numdefault, const char *strdefault, zprop_attr_t attr,
99eda14cbcSMatt Macy     int objset_types, const char *values, const char *colname,
100c03c5b1cSMartin Matuska     boolean_t rightalign, boolean_t visible, boolean_t flex,
101c03c5b1cSMartin Matuska     const zprop_index_t *idx_tbl,
102e92ffd9bSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
103eda14cbcSMatt Macy {
104eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types);
105eda14cbcSMatt Macy 	zprop_desc_t *pd;
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy 	pd = &prop_tbl[prop];
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy 	ASSERT(pd->pd_name == NULL || pd->pd_name == name);
110eda14cbcSMatt Macy 	ASSERT(name != NULL);
111eda14cbcSMatt Macy 	ASSERT(colname != NULL);
112eda14cbcSMatt Macy 
113eda14cbcSMatt Macy 	pd->pd_name = name;
114eda14cbcSMatt Macy 	pd->pd_propnum = prop;
115eda14cbcSMatt Macy 	pd->pd_proptype = type;
116eda14cbcSMatt Macy 	pd->pd_numdefault = numdefault;
117eda14cbcSMatt Macy 	pd->pd_strdefault = strdefault;
118eda14cbcSMatt Macy 	pd->pd_attr = attr;
119eda14cbcSMatt Macy 	pd->pd_types = objset_types;
120eda14cbcSMatt Macy 	pd->pd_values = values;
121eda14cbcSMatt Macy 	pd->pd_colname = colname;
122eda14cbcSMatt Macy 	pd->pd_rightalign = rightalign;
123eda14cbcSMatt Macy 	pd->pd_visible = visible;
124e92ffd9bSMartin Matuska 	pd->pd_zfs_mod_supported =
125e92ffd9bSMartin Matuska 	    zfs_mod_supported_prop(name, objset_types, sfeatures);
126c03c5b1cSMartin Matuska 	pd->pd_always_flex = flex;
127eda14cbcSMatt Macy 	pd->pd_table = idx_tbl;
128eda14cbcSMatt Macy 	pd->pd_table_size = 0;
129eda14cbcSMatt Macy 	while (idx_tbl && (idx_tbl++)->pi_name != NULL)
130eda14cbcSMatt Macy 		pd->pd_table_size++;
131eda14cbcSMatt Macy }
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy void
zprop_register_string(int prop,const char * name,const char * def,zprop_attr_t attr,int objset_types,const char * values,const char * colname,const struct zfs_mod_supported_features * sfeatures)134eda14cbcSMatt Macy zprop_register_string(int prop, const char *name, const char *def,
135eda14cbcSMatt Macy     zprop_attr_t attr, int objset_types, const char *values,
136e92ffd9bSMartin Matuska     const char *colname, const struct zfs_mod_supported_features *sfeatures)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy 	zprop_register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr,
139716fd348SMartin Matuska 	    objset_types, values, colname, B_FALSE, B_TRUE, B_TRUE, NULL,
140c03c5b1cSMartin Matuska 	    sfeatures);
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy }
143eda14cbcSMatt Macy 
144eda14cbcSMatt Macy void
zprop_register_number(int prop,const char * name,uint64_t def,zprop_attr_t attr,int objset_types,const char * values,const char * colname,boolean_t flex,const struct zfs_mod_supported_features * sfeatures)145eda14cbcSMatt Macy zprop_register_number(int prop, const char *name, uint64_t def,
146eda14cbcSMatt Macy     zprop_attr_t attr, int objset_types, const char *values,
147c03c5b1cSMartin Matuska     const char *colname, boolean_t flex,
148c03c5b1cSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
149eda14cbcSMatt Macy {
150eda14cbcSMatt Macy 	zprop_register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr,
151c03c5b1cSMartin Matuska 	    objset_types, values, colname, B_TRUE, B_TRUE, flex, NULL,
152c03c5b1cSMartin Matuska 	    sfeatures);
153eda14cbcSMatt Macy }
154eda14cbcSMatt Macy 
155eda14cbcSMatt Macy void
zprop_register_index(int prop,const char * name,uint64_t def,zprop_attr_t attr,int objset_types,const char * values,const char * colname,const zprop_index_t * idx_tbl,const struct zfs_mod_supported_features * sfeatures)156eda14cbcSMatt Macy zprop_register_index(int prop, const char *name, uint64_t def,
157eda14cbcSMatt Macy     zprop_attr_t attr, int objset_types, const char *values,
158e92ffd9bSMartin Matuska     const char *colname, const zprop_index_t *idx_tbl,
159e92ffd9bSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
160eda14cbcSMatt Macy {
161eda14cbcSMatt Macy 	zprop_register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr,
162716fd348SMartin Matuska 	    objset_types, values, colname, B_FALSE, B_TRUE, B_TRUE, idx_tbl,
163c03c5b1cSMartin Matuska 	    sfeatures);
164eda14cbcSMatt Macy }
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy void
zprop_register_hidden(int prop,const char * name,zprop_type_t type,zprop_attr_t attr,int objset_types,const char * colname,boolean_t flex,const struct zfs_mod_supported_features * sfeatures)167eda14cbcSMatt Macy zprop_register_hidden(int prop, const char *name, zprop_type_t type,
168c03c5b1cSMartin Matuska     zprop_attr_t attr, int objset_types, const char *colname, boolean_t flex,
169e92ffd9bSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
170eda14cbcSMatt Macy {
171eda14cbcSMatt Macy 	zprop_register_impl(prop, name, type, 0, NULL, attr,
172eda14cbcSMatt Macy 	    objset_types, NULL, colname,
173c03c5b1cSMartin Matuska 	    type == PROP_TYPE_NUMBER, B_FALSE, flex, NULL, sfeatures);
174eda14cbcSMatt Macy }
175eda14cbcSMatt Macy 
176eda14cbcSMatt Macy 
177eda14cbcSMatt Macy /*
178eda14cbcSMatt Macy  * A comparison function we can use to order indexes into property tables.
179eda14cbcSMatt Macy  */
180eda14cbcSMatt Macy static int
zprop_compare(const void * arg1,const void * arg2)181eda14cbcSMatt Macy zprop_compare(const void *arg1, const void *arg2)
182eda14cbcSMatt Macy {
183eda14cbcSMatt Macy 	const zprop_desc_t *p1 = *((zprop_desc_t **)arg1);
184eda14cbcSMatt Macy 	const zprop_desc_t *p2 = *((zprop_desc_t **)arg2);
185eda14cbcSMatt Macy 	boolean_t p1ro, p2ro;
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 	p1ro = (p1->pd_attr == PROP_READONLY);
188eda14cbcSMatt Macy 	p2ro = (p2->pd_attr == PROP_READONLY);
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy 	if (p1ro == p2ro)
191eda14cbcSMatt Macy 		return (strcmp(p1->pd_name, p2->pd_name));
192eda14cbcSMatt Macy 
193eda14cbcSMatt Macy 	return (p1ro ? -1 : 1);
194eda14cbcSMatt Macy }
195eda14cbcSMatt Macy 
196eda14cbcSMatt Macy /*
197eda14cbcSMatt Macy  * Iterate over all properties in the given property table, calling back
198eda14cbcSMatt Macy  * into the specified function for each property. We will continue to
199eda14cbcSMatt Macy  * iterate until we either reach the end or the callback function returns
200eda14cbcSMatt Macy  * something other than ZPROP_CONT.
201eda14cbcSMatt Macy  */
202eda14cbcSMatt Macy int
zprop_iter_common(zprop_func func,void * cb,boolean_t show_all,boolean_t ordered,zfs_type_t type)203eda14cbcSMatt Macy zprop_iter_common(zprop_func func, void *cb, boolean_t show_all,
204eda14cbcSMatt Macy     boolean_t ordered, zfs_type_t type)
205eda14cbcSMatt Macy {
206eda14cbcSMatt Macy 	int i, num_props, size, prop;
207eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
208eda14cbcSMatt Macy 	zprop_desc_t **order;
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
211eda14cbcSMatt Macy 	num_props = zprop_get_numprops(type);
212eda14cbcSMatt Macy 	size = num_props * sizeof (zprop_desc_t *);
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy #if defined(_KERNEL)
215eda14cbcSMatt Macy 	order = kmem_alloc(size, KM_SLEEP);
216eda14cbcSMatt Macy #else
217eda14cbcSMatt Macy 	if ((order = malloc(size)) == NULL)
218eda14cbcSMatt Macy 		return (ZPROP_CONT);
219eda14cbcSMatt Macy #endif
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy 	for (int j = 0; j < num_props; j++)
222eda14cbcSMatt Macy 		order[j] = &prop_tbl[j];
223eda14cbcSMatt Macy 
224eda14cbcSMatt Macy 	if (ordered) {
225eda14cbcSMatt Macy 		qsort((void *)order, num_props, sizeof (zprop_desc_t *),
226eda14cbcSMatt Macy 		    zprop_compare);
227eda14cbcSMatt Macy 	}
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 	prop = ZPROP_CONT;
230eda14cbcSMatt Macy 	for (i = 0; i < num_props; i++) {
231eda14cbcSMatt Macy 		if ((order[i]->pd_visible || show_all) &&
232eda14cbcSMatt Macy 		    order[i]->pd_zfs_mod_supported &&
233eda14cbcSMatt Macy 		    (func(order[i]->pd_propnum, cb) != ZPROP_CONT)) {
234eda14cbcSMatt Macy 			prop = order[i]->pd_propnum;
235eda14cbcSMatt Macy 			break;
236eda14cbcSMatt Macy 		}
237eda14cbcSMatt Macy 	}
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy #if defined(_KERNEL)
240eda14cbcSMatt Macy 	kmem_free(order, size);
241eda14cbcSMatt Macy #else
242eda14cbcSMatt Macy 	free(order);
243eda14cbcSMatt Macy #endif
244eda14cbcSMatt Macy 	return (prop);
245eda14cbcSMatt Macy }
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy static boolean_t
propname_match(const char * p,size_t len,zprop_desc_t * prop_entry)248eda14cbcSMatt Macy propname_match(const char *p, size_t len, zprop_desc_t *prop_entry)
249eda14cbcSMatt Macy {
250eda14cbcSMatt Macy 	const char *propname = prop_entry->pd_name;
251eda14cbcSMatt Macy #ifndef _KERNEL
252eda14cbcSMatt Macy 	const char *colname = prop_entry->pd_colname;
253eda14cbcSMatt Macy 	int c;
254eda14cbcSMatt Macy #endif
255eda14cbcSMatt Macy 
256681ce946SMartin Matuska 	ASSERT(propname != NULL);
257681ce946SMartin Matuska 
258eda14cbcSMatt Macy 	if (len == strlen(propname) &&
259eda14cbcSMatt Macy 	    strncmp(p, propname, len) == 0)
260eda14cbcSMatt Macy 		return (B_TRUE);
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy #ifndef _KERNEL
263eda14cbcSMatt Macy 	if (colname == NULL || len != strlen(colname))
264eda14cbcSMatt Macy 		return (B_FALSE);
265eda14cbcSMatt Macy 
266eda14cbcSMatt Macy 	for (c = 0; c < len; c++)
267eda14cbcSMatt Macy 		if (p[c] != tolower(colname[c]))
268eda14cbcSMatt Macy 			break;
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	return (colname[c] == '\0');
271eda14cbcSMatt Macy #else
272eda14cbcSMatt Macy 	return (B_FALSE);
273eda14cbcSMatt Macy #endif
274eda14cbcSMatt Macy }
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy typedef struct name_to_prop_cb {
277eda14cbcSMatt Macy 	const char *propname;
278eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
279eda14cbcSMatt Macy } name_to_prop_cb_t;
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy static int
zprop_name_to_prop_cb(int prop,void * cb_data)282eda14cbcSMatt Macy zprop_name_to_prop_cb(int prop, void *cb_data)
283eda14cbcSMatt Macy {
284eda14cbcSMatt Macy 	name_to_prop_cb_t *data = cb_data;
285eda14cbcSMatt Macy 
286eda14cbcSMatt Macy 	if (propname_match(data->propname, strlen(data->propname),
287eda14cbcSMatt Macy 	    &data->prop_tbl[prop]))
288eda14cbcSMatt Macy 		return (prop);
289eda14cbcSMatt Macy 
290eda14cbcSMatt Macy 	return (ZPROP_CONT);
291eda14cbcSMatt Macy }
292eda14cbcSMatt Macy 
293eda14cbcSMatt Macy int
zprop_name_to_prop(const char * propname,zfs_type_t type)294eda14cbcSMatt Macy zprop_name_to_prop(const char *propname, zfs_type_t type)
295eda14cbcSMatt Macy {
296eda14cbcSMatt Macy 	int prop;
297eda14cbcSMatt Macy 	name_to_prop_cb_t cb_data;
298eda14cbcSMatt Macy 
299eda14cbcSMatt Macy 	cb_data.propname = propname;
300eda14cbcSMatt Macy 	cb_data.prop_tbl = zprop_get_proptable(type);
301eda14cbcSMatt Macy 
302eda14cbcSMatt Macy 	prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data,
303eda14cbcSMatt Macy 	    B_TRUE, B_FALSE, type);
304eda14cbcSMatt Macy 
305eda14cbcSMatt Macy 	return (prop == ZPROP_CONT ? ZPROP_INVAL : prop);
306eda14cbcSMatt Macy }
307eda14cbcSMatt Macy 
308eda14cbcSMatt Macy int
zprop_string_to_index(int prop,const char * string,uint64_t * index,zfs_type_t type)309eda14cbcSMatt Macy zprop_string_to_index(int prop, const char *string, uint64_t *index,
310eda14cbcSMatt Macy     zfs_type_t type)
311eda14cbcSMatt Macy {
312eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
313eda14cbcSMatt Macy 	const zprop_index_t *idx_tbl;
314eda14cbcSMatt Macy 	int i;
315eda14cbcSMatt Macy 
316eda14cbcSMatt Macy 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
317eda14cbcSMatt Macy 		return (-1);
318eda14cbcSMatt Macy 
319eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
320eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
321eda14cbcSMatt Macy 	if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
322eda14cbcSMatt Macy 		return (-1);
323eda14cbcSMatt Macy 
324eda14cbcSMatt Macy 	for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
325eda14cbcSMatt Macy 		if (strcmp(string, idx_tbl[i].pi_name) == 0) {
326eda14cbcSMatt Macy 			*index = idx_tbl[i].pi_value;
327eda14cbcSMatt Macy 			return (0);
328eda14cbcSMatt Macy 		}
329eda14cbcSMatt Macy 	}
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	return (-1);
332eda14cbcSMatt Macy }
333eda14cbcSMatt Macy 
334eda14cbcSMatt Macy int
zprop_index_to_string(int prop,uint64_t index,const char ** string,zfs_type_t type)335eda14cbcSMatt Macy zprop_index_to_string(int prop, uint64_t index, const char **string,
336eda14cbcSMatt Macy     zfs_type_t type)
337eda14cbcSMatt Macy {
338eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
339eda14cbcSMatt Macy 	const zprop_index_t *idx_tbl;
340eda14cbcSMatt Macy 	int i;
341eda14cbcSMatt Macy 
342eda14cbcSMatt Macy 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
343eda14cbcSMatt Macy 		return (-1);
344eda14cbcSMatt Macy 
345eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
346eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
347eda14cbcSMatt Macy 	if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
348eda14cbcSMatt Macy 		return (-1);
349eda14cbcSMatt Macy 
350eda14cbcSMatt Macy 	for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
351eda14cbcSMatt Macy 		if (idx_tbl[i].pi_value == index) {
352eda14cbcSMatt Macy 			*string = idx_tbl[i].pi_name;
353eda14cbcSMatt Macy 			return (0);
354eda14cbcSMatt Macy 		}
355eda14cbcSMatt Macy 	}
356eda14cbcSMatt Macy 
357eda14cbcSMatt Macy 	return (-1);
358eda14cbcSMatt Macy }
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy /*
361eda14cbcSMatt Macy  * Return a random valid property value.  Used by ztest.
362eda14cbcSMatt Macy  */
363eda14cbcSMatt Macy uint64_t
zprop_random_value(int prop,uint64_t seed,zfs_type_t type)364eda14cbcSMatt Macy zprop_random_value(int prop, uint64_t seed, zfs_type_t type)
365eda14cbcSMatt Macy {
366eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
367eda14cbcSMatt Macy 	const zprop_index_t *idx_tbl;
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 	ASSERT((uint_t)prop < zprop_get_numprops(type));
370eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
371eda14cbcSMatt Macy 	idx_tbl = prop_tbl[prop].pd_table;
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	if (idx_tbl == NULL)
374eda14cbcSMatt Macy 		return (seed);
375eda14cbcSMatt Macy 
376eda14cbcSMatt Macy 	return (idx_tbl[seed % prop_tbl[prop].pd_table_size].pi_value);
377eda14cbcSMatt Macy }
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy const char *
zprop_values(int prop,zfs_type_t type)380eda14cbcSMatt Macy zprop_values(int prop, zfs_type_t type)
381eda14cbcSMatt Macy {
382eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy 	ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
385eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
386eda14cbcSMatt Macy 
387eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
388eda14cbcSMatt Macy 
389eda14cbcSMatt Macy 	return (prop_tbl[prop].pd_values);
390eda14cbcSMatt Macy }
391eda14cbcSMatt Macy 
392eda14cbcSMatt Macy /*
393eda14cbcSMatt Macy  * Returns TRUE if the property applies to any of the given dataset types.
394eda14cbcSMatt Macy  *
395eda14cbcSMatt Macy  * If headcheck is set, the check is being made against the head dataset
396eda14cbcSMatt Macy  * type of a snapshot which requires to return B_TRUE when the property
397eda14cbcSMatt Macy  * is only valid for snapshots.
398eda14cbcSMatt Macy  */
399eda14cbcSMatt Macy boolean_t
zprop_valid_for_type(int prop,zfs_type_t type,boolean_t headcheck)400eda14cbcSMatt Macy zprop_valid_for_type(int prop, zfs_type_t type, boolean_t headcheck)
401eda14cbcSMatt Macy {
402eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
403eda14cbcSMatt Macy 
404eda14cbcSMatt Macy 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
405eda14cbcSMatt Macy 		return (B_FALSE);
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
408eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
409eda14cbcSMatt Macy 	if (headcheck && prop_tbl[prop].pd_types == ZFS_TYPE_SNAPSHOT)
410eda14cbcSMatt Macy 		return (B_TRUE);
411eda14cbcSMatt Macy 	return ((prop_tbl[prop].pd_types & type) != 0);
412eda14cbcSMatt Macy }
413eda14cbcSMatt Macy 
414681ce946SMartin Matuska /*
415681ce946SMartin Matuska  * For user property names, we allow all lowercase alphanumeric characters, plus
416681ce946SMartin Matuska  * a few useful punctuation characters.
417681ce946SMartin Matuska  */
418681ce946SMartin Matuska int
zprop_valid_char(char c)419681ce946SMartin Matuska zprop_valid_char(char c)
420681ce946SMartin Matuska {
421681ce946SMartin Matuska 	return ((c >= 'a' && c <= 'z') ||
422681ce946SMartin Matuska 	    (c >= '0' && c <= '9') ||
423681ce946SMartin Matuska 	    c == '-' || c == '_' || c == '.' || c == ':');
424681ce946SMartin Matuska }
425681ce946SMartin Matuska 
426eda14cbcSMatt Macy #ifndef _KERNEL
427eda14cbcSMatt Macy 
428eda14cbcSMatt Macy /*
429eda14cbcSMatt Macy  * Determines the minimum width for the column, and indicates whether it's fixed
430eda14cbcSMatt Macy  * or not.  Only string columns are non-fixed.
431eda14cbcSMatt Macy  */
432eda14cbcSMatt Macy size_t
zprop_width(int prop,boolean_t * fixed,zfs_type_t type)433eda14cbcSMatt Macy zprop_width(int prop, boolean_t *fixed, zfs_type_t type)
434eda14cbcSMatt Macy {
435eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl, *pd;
436eda14cbcSMatt Macy 	const zprop_index_t *idx;
437eda14cbcSMatt Macy 	size_t ret;
438eda14cbcSMatt Macy 	int i;
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 	ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
441eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
442eda14cbcSMatt Macy 
443eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
444eda14cbcSMatt Macy 	pd = &prop_tbl[prop];
445eda14cbcSMatt Macy 
446c03c5b1cSMartin Matuska 	if (type != ZFS_TYPE_POOL && type != ZFS_TYPE_VDEV)
447c03c5b1cSMartin Matuska 		type = ZFS_TYPE_FILESYSTEM;
448c03c5b1cSMartin Matuska 
449c03c5b1cSMartin Matuska 	*fixed = !pd->pd_always_flex;
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	/*
452eda14cbcSMatt Macy 	 * Start with the width of the column name.
453eda14cbcSMatt Macy 	 */
454eda14cbcSMatt Macy 	ret = strlen(pd->pd_colname);
455eda14cbcSMatt Macy 
456eda14cbcSMatt Macy 	/*
457eda14cbcSMatt Macy 	 * For fixed-width values, make sure the width is large enough to hold
458eda14cbcSMatt Macy 	 * any possible value.
459eda14cbcSMatt Macy 	 */
460eda14cbcSMatt Macy 	switch (pd->pd_proptype) {
461eda14cbcSMatt Macy 	case PROP_TYPE_NUMBER:
462eda14cbcSMatt Macy 		/*
463eda14cbcSMatt Macy 		 * The maximum length of a human-readable number is 5 characters
464eda14cbcSMatt Macy 		 * ("20.4M", for example).
465eda14cbcSMatt Macy 		 */
466eda14cbcSMatt Macy 		if (ret < 5)
467eda14cbcSMatt Macy 			ret = 5;
468eda14cbcSMatt Macy 		/*
469eda14cbcSMatt Macy 		 * 'health' is handled specially because it's a number
470eda14cbcSMatt Macy 		 * internally, but displayed as a fixed 8 character string.
471eda14cbcSMatt Macy 		 */
472c03c5b1cSMartin Matuska 		if (type == ZFS_TYPE_POOL && prop == ZPOOL_PROP_HEALTH)
473eda14cbcSMatt Macy 			ret = 8;
474eda14cbcSMatt Macy 		break;
475c03c5b1cSMartin Matuska 
476eda14cbcSMatt Macy 	case PROP_TYPE_INDEX:
477eda14cbcSMatt Macy 		idx = prop_tbl[prop].pd_table;
478eda14cbcSMatt Macy 		for (i = 0; idx[i].pi_name != NULL; i++) {
479eda14cbcSMatt Macy 			if (strlen(idx[i].pi_name) > ret)
480eda14cbcSMatt Macy 				ret = strlen(idx[i].pi_name);
481eda14cbcSMatt Macy 		}
482eda14cbcSMatt Macy 		break;
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy 	case PROP_TYPE_STRING:
485eda14cbcSMatt Macy 		break;
486eda14cbcSMatt Macy 	}
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy 	return (ret);
489eda14cbcSMatt Macy }
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy #endif
492eda14cbcSMatt Macy 
493eda14cbcSMatt Macy #if defined(_KERNEL)
494eda14cbcSMatt Macy /* Common routines to initialize property tables */
495eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_impl);
496eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_string);
497eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_number);
498eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_index);
499eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_hidden);
500eda14cbcSMatt Macy 
501eda14cbcSMatt Macy /* Common routines for zfs and zpool property management */
502eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_iter_common);
503eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_name_to_prop);
504eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_string_to_index);
505eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_index_to_string);
506eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_random_value);
507eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_values);
508eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_valid_for_type);
509681ce946SMartin Matuska EXPORT_SYMBOL(zprop_valid_char);
510eda14cbcSMatt Macy #endif
511