xref: /freebsd/sys/contrib/openzfs/module/zcommon/zprop_common.c (revision e92ffd9b626833ebdbf2742c8ffddc6cd94b963e)
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
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
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 *
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
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
74*e92ffd9bSMartin Matuska zfs_mod_supported_prop(const char *name, zfs_type_t type,
75*e92ffd9bSMartin 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__)
86*e92ffd9bSMartin 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 ?
91*e92ffd9bSMartin Matuska 	    ZFS_SYSFS_VDEV_PROPERTIES : ZFS_SYSFS_DATASET_PROPERTIES),
92*e92ffd9bSMartin Matuska 	    name, sfeatures));
93eda14cbcSMatt Macy #endif
94eda14cbcSMatt Macy }
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy void
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,
100*e92ffd9bSMartin Matuska     boolean_t rightalign, boolean_t visible, const zprop_index_t *idx_tbl,
101*e92ffd9bSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
102eda14cbcSMatt Macy {
103eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types);
104eda14cbcSMatt Macy 	zprop_desc_t *pd;
105eda14cbcSMatt Macy 
106eda14cbcSMatt Macy 	pd = &prop_tbl[prop];
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy 	ASSERT(pd->pd_name == NULL || pd->pd_name == name);
109eda14cbcSMatt Macy 	ASSERT(name != NULL);
110eda14cbcSMatt Macy 	ASSERT(colname != NULL);
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 	pd->pd_name = name;
113eda14cbcSMatt Macy 	pd->pd_propnum = prop;
114eda14cbcSMatt Macy 	pd->pd_proptype = type;
115eda14cbcSMatt Macy 	pd->pd_numdefault = numdefault;
116eda14cbcSMatt Macy 	pd->pd_strdefault = strdefault;
117eda14cbcSMatt Macy 	pd->pd_attr = attr;
118eda14cbcSMatt Macy 	pd->pd_types = objset_types;
119eda14cbcSMatt Macy 	pd->pd_values = values;
120eda14cbcSMatt Macy 	pd->pd_colname = colname;
121eda14cbcSMatt Macy 	pd->pd_rightalign = rightalign;
122eda14cbcSMatt Macy 	pd->pd_visible = visible;
123*e92ffd9bSMartin Matuska 	pd->pd_zfs_mod_supported =
124*e92ffd9bSMartin Matuska 	    zfs_mod_supported_prop(name, objset_types, sfeatures);
125eda14cbcSMatt Macy 	pd->pd_table = idx_tbl;
126eda14cbcSMatt Macy 	pd->pd_table_size = 0;
127eda14cbcSMatt Macy 	while (idx_tbl && (idx_tbl++)->pi_name != NULL)
128eda14cbcSMatt Macy 		pd->pd_table_size++;
129eda14cbcSMatt Macy }
130eda14cbcSMatt Macy 
131eda14cbcSMatt Macy void
132eda14cbcSMatt Macy zprop_register_string(int prop, const char *name, const char *def,
133eda14cbcSMatt Macy     zprop_attr_t attr, int objset_types, const char *values,
134*e92ffd9bSMartin Matuska     const char *colname, const struct zfs_mod_supported_features *sfeatures)
135eda14cbcSMatt Macy {
136eda14cbcSMatt Macy 	zprop_register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr,
137*e92ffd9bSMartin Matuska 	    objset_types, values, colname, B_FALSE, B_TRUE, NULL, sfeatures);
138eda14cbcSMatt Macy 
139eda14cbcSMatt Macy }
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy void
142eda14cbcSMatt Macy zprop_register_number(int prop, const char *name, uint64_t def,
143eda14cbcSMatt Macy     zprop_attr_t attr, int objset_types, const char *values,
144*e92ffd9bSMartin Matuska     const char *colname, const struct zfs_mod_supported_features *sfeatures)
145eda14cbcSMatt Macy {
146eda14cbcSMatt Macy 	zprop_register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr,
147*e92ffd9bSMartin Matuska 	    objset_types, values, colname, B_TRUE, B_TRUE, NULL, sfeatures);
148eda14cbcSMatt Macy }
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy void
151eda14cbcSMatt Macy zprop_register_index(int prop, const char *name, uint64_t def,
152eda14cbcSMatt Macy     zprop_attr_t attr, int objset_types, const char *values,
153*e92ffd9bSMartin Matuska     const char *colname, const zprop_index_t *idx_tbl,
154*e92ffd9bSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
155eda14cbcSMatt Macy {
156eda14cbcSMatt Macy 	zprop_register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr,
157*e92ffd9bSMartin Matuska 	    objset_types, values, colname, B_FALSE, B_TRUE, idx_tbl, sfeatures);
158eda14cbcSMatt Macy }
159eda14cbcSMatt Macy 
160eda14cbcSMatt Macy void
161eda14cbcSMatt Macy zprop_register_hidden(int prop, const char *name, zprop_type_t type,
162*e92ffd9bSMartin Matuska     zprop_attr_t attr, int objset_types, const char *colname,
163*e92ffd9bSMartin Matuska     const struct zfs_mod_supported_features *sfeatures)
164eda14cbcSMatt Macy {
165eda14cbcSMatt Macy 	zprop_register_impl(prop, name, type, 0, NULL, attr,
166eda14cbcSMatt Macy 	    objset_types, NULL, colname,
167*e92ffd9bSMartin Matuska 	    type == PROP_TYPE_NUMBER, B_FALSE, NULL, sfeatures);
168eda14cbcSMatt Macy }
169eda14cbcSMatt Macy 
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy /*
172eda14cbcSMatt Macy  * A comparison function we can use to order indexes into property tables.
173eda14cbcSMatt Macy  */
174eda14cbcSMatt Macy static int
175eda14cbcSMatt Macy zprop_compare(const void *arg1, const void *arg2)
176eda14cbcSMatt Macy {
177eda14cbcSMatt Macy 	const zprop_desc_t *p1 = *((zprop_desc_t **)arg1);
178eda14cbcSMatt Macy 	const zprop_desc_t *p2 = *((zprop_desc_t **)arg2);
179eda14cbcSMatt Macy 	boolean_t p1ro, p2ro;
180eda14cbcSMatt Macy 
181eda14cbcSMatt Macy 	p1ro = (p1->pd_attr == PROP_READONLY);
182eda14cbcSMatt Macy 	p2ro = (p2->pd_attr == PROP_READONLY);
183eda14cbcSMatt Macy 
184eda14cbcSMatt Macy 	if (p1ro == p2ro)
185eda14cbcSMatt Macy 		return (strcmp(p1->pd_name, p2->pd_name));
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 	return (p1ro ? -1 : 1);
188eda14cbcSMatt Macy }
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy /*
191eda14cbcSMatt Macy  * Iterate over all properties in the given property table, calling back
192eda14cbcSMatt Macy  * into the specified function for each property. We will continue to
193eda14cbcSMatt Macy  * iterate until we either reach the end or the callback function returns
194eda14cbcSMatt Macy  * something other than ZPROP_CONT.
195eda14cbcSMatt Macy  */
196eda14cbcSMatt Macy int
197eda14cbcSMatt Macy zprop_iter_common(zprop_func func, void *cb, boolean_t show_all,
198eda14cbcSMatt Macy     boolean_t ordered, zfs_type_t type)
199eda14cbcSMatt Macy {
200eda14cbcSMatt Macy 	int i, num_props, size, prop;
201eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
202eda14cbcSMatt Macy 	zprop_desc_t **order;
203eda14cbcSMatt Macy 
204eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
205eda14cbcSMatt Macy 	num_props = zprop_get_numprops(type);
206eda14cbcSMatt Macy 	size = num_props * sizeof (zprop_desc_t *);
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy #if defined(_KERNEL)
209eda14cbcSMatt Macy 	order = kmem_alloc(size, KM_SLEEP);
210eda14cbcSMatt Macy #else
211eda14cbcSMatt Macy 	if ((order = malloc(size)) == NULL)
212eda14cbcSMatt Macy 		return (ZPROP_CONT);
213eda14cbcSMatt Macy #endif
214eda14cbcSMatt Macy 
215eda14cbcSMatt Macy 	for (int j = 0; j < num_props; j++)
216eda14cbcSMatt Macy 		order[j] = &prop_tbl[j];
217eda14cbcSMatt Macy 
218eda14cbcSMatt Macy 	if (ordered) {
219eda14cbcSMatt Macy 		qsort((void *)order, num_props, sizeof (zprop_desc_t *),
220eda14cbcSMatt Macy 		    zprop_compare);
221eda14cbcSMatt Macy 	}
222eda14cbcSMatt Macy 
223eda14cbcSMatt Macy 	prop = ZPROP_CONT;
224eda14cbcSMatt Macy 	for (i = 0; i < num_props; i++) {
225eda14cbcSMatt Macy 		if ((order[i]->pd_visible || show_all) &&
226eda14cbcSMatt Macy 		    order[i]->pd_zfs_mod_supported &&
227eda14cbcSMatt Macy 		    (func(order[i]->pd_propnum, cb) != ZPROP_CONT)) {
228eda14cbcSMatt Macy 			prop = order[i]->pd_propnum;
229eda14cbcSMatt Macy 			break;
230eda14cbcSMatt Macy 		}
231eda14cbcSMatt Macy 	}
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy #if defined(_KERNEL)
234eda14cbcSMatt Macy 	kmem_free(order, size);
235eda14cbcSMatt Macy #else
236eda14cbcSMatt Macy 	free(order);
237eda14cbcSMatt Macy #endif
238eda14cbcSMatt Macy 	return (prop);
239eda14cbcSMatt Macy }
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy static boolean_t
242eda14cbcSMatt Macy propname_match(const char *p, size_t len, zprop_desc_t *prop_entry)
243eda14cbcSMatt Macy {
244eda14cbcSMatt Macy 	const char *propname = prop_entry->pd_name;
245eda14cbcSMatt Macy #ifndef _KERNEL
246eda14cbcSMatt Macy 	const char *colname = prop_entry->pd_colname;
247eda14cbcSMatt Macy 	int c;
248eda14cbcSMatt Macy #endif
249eda14cbcSMatt Macy 
250681ce946SMartin Matuska 	ASSERT(propname != NULL);
251681ce946SMartin Matuska 
252eda14cbcSMatt Macy 	if (len == strlen(propname) &&
253eda14cbcSMatt Macy 	    strncmp(p, propname, len) == 0)
254eda14cbcSMatt Macy 		return (B_TRUE);
255eda14cbcSMatt Macy 
256eda14cbcSMatt Macy #ifndef _KERNEL
257eda14cbcSMatt Macy 	if (colname == NULL || len != strlen(colname))
258eda14cbcSMatt Macy 		return (B_FALSE);
259eda14cbcSMatt Macy 
260eda14cbcSMatt Macy 	for (c = 0; c < len; c++)
261eda14cbcSMatt Macy 		if (p[c] != tolower(colname[c]))
262eda14cbcSMatt Macy 			break;
263eda14cbcSMatt Macy 
264eda14cbcSMatt Macy 	return (colname[c] == '\0');
265eda14cbcSMatt Macy #else
266eda14cbcSMatt Macy 	return (B_FALSE);
267eda14cbcSMatt Macy #endif
268eda14cbcSMatt Macy }
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy typedef struct name_to_prop_cb {
271eda14cbcSMatt Macy 	const char *propname;
272eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
273eda14cbcSMatt Macy } name_to_prop_cb_t;
274eda14cbcSMatt Macy 
275eda14cbcSMatt Macy static int
276eda14cbcSMatt Macy zprop_name_to_prop_cb(int prop, void *cb_data)
277eda14cbcSMatt Macy {
278eda14cbcSMatt Macy 	name_to_prop_cb_t *data = cb_data;
279eda14cbcSMatt Macy 
280eda14cbcSMatt Macy 	if (propname_match(data->propname, strlen(data->propname),
281eda14cbcSMatt Macy 	    &data->prop_tbl[prop]))
282eda14cbcSMatt Macy 		return (prop);
283eda14cbcSMatt Macy 
284eda14cbcSMatt Macy 	return (ZPROP_CONT);
285eda14cbcSMatt Macy }
286eda14cbcSMatt Macy 
287eda14cbcSMatt Macy int
288eda14cbcSMatt Macy zprop_name_to_prop(const char *propname, zfs_type_t type)
289eda14cbcSMatt Macy {
290eda14cbcSMatt Macy 	int prop;
291eda14cbcSMatt Macy 	name_to_prop_cb_t cb_data;
292eda14cbcSMatt Macy 
293eda14cbcSMatt Macy 	cb_data.propname = propname;
294eda14cbcSMatt Macy 	cb_data.prop_tbl = zprop_get_proptable(type);
295eda14cbcSMatt Macy 
296eda14cbcSMatt Macy 	prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data,
297eda14cbcSMatt Macy 	    B_TRUE, B_FALSE, type);
298eda14cbcSMatt Macy 
299eda14cbcSMatt Macy 	return (prop == ZPROP_CONT ? ZPROP_INVAL : prop);
300eda14cbcSMatt Macy }
301eda14cbcSMatt Macy 
302eda14cbcSMatt Macy int
303eda14cbcSMatt Macy zprop_string_to_index(int prop, const char *string, uint64_t *index,
304eda14cbcSMatt Macy     zfs_type_t type)
305eda14cbcSMatt Macy {
306eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
307eda14cbcSMatt Macy 	const zprop_index_t *idx_tbl;
308eda14cbcSMatt Macy 	int i;
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
311eda14cbcSMatt Macy 		return (-1);
312eda14cbcSMatt Macy 
313eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
314eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
315eda14cbcSMatt Macy 	if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
316eda14cbcSMatt Macy 		return (-1);
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 	for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
319eda14cbcSMatt Macy 		if (strcmp(string, idx_tbl[i].pi_name) == 0) {
320eda14cbcSMatt Macy 			*index = idx_tbl[i].pi_value;
321eda14cbcSMatt Macy 			return (0);
322eda14cbcSMatt Macy 		}
323eda14cbcSMatt Macy 	}
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 	return (-1);
326eda14cbcSMatt Macy }
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy int
329eda14cbcSMatt Macy zprop_index_to_string(int prop, uint64_t index, const char **string,
330eda14cbcSMatt Macy     zfs_type_t type)
331eda14cbcSMatt Macy {
332eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
333eda14cbcSMatt Macy 	const zprop_index_t *idx_tbl;
334eda14cbcSMatt Macy 	int i;
335eda14cbcSMatt Macy 
336eda14cbcSMatt Macy 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
337eda14cbcSMatt Macy 		return (-1);
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
340eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
341eda14cbcSMatt Macy 	if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
342eda14cbcSMatt Macy 		return (-1);
343eda14cbcSMatt Macy 
344eda14cbcSMatt Macy 	for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
345eda14cbcSMatt Macy 		if (idx_tbl[i].pi_value == index) {
346eda14cbcSMatt Macy 			*string = idx_tbl[i].pi_name;
347eda14cbcSMatt Macy 			return (0);
348eda14cbcSMatt Macy 		}
349eda14cbcSMatt Macy 	}
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy 	return (-1);
352eda14cbcSMatt Macy }
353eda14cbcSMatt Macy 
354eda14cbcSMatt Macy /*
355eda14cbcSMatt Macy  * Return a random valid property value.  Used by ztest.
356eda14cbcSMatt Macy  */
357eda14cbcSMatt Macy uint64_t
358eda14cbcSMatt Macy zprop_random_value(int prop, uint64_t seed, zfs_type_t type)
359eda14cbcSMatt Macy {
360eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
361eda14cbcSMatt Macy 	const zprop_index_t *idx_tbl;
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	ASSERT((uint_t)prop < zprop_get_numprops(type));
364eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
365eda14cbcSMatt Macy 	idx_tbl = prop_tbl[prop].pd_table;
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy 	if (idx_tbl == NULL)
368eda14cbcSMatt Macy 		return (seed);
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy 	return (idx_tbl[seed % prop_tbl[prop].pd_table_size].pi_value);
371eda14cbcSMatt Macy }
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy const char *
374eda14cbcSMatt Macy zprop_values(int prop, zfs_type_t type)
375eda14cbcSMatt Macy {
376eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy 	ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
379eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
380eda14cbcSMatt Macy 
381eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
382eda14cbcSMatt Macy 
383eda14cbcSMatt Macy 	return (prop_tbl[prop].pd_values);
384eda14cbcSMatt Macy }
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy /*
387eda14cbcSMatt Macy  * Returns TRUE if the property applies to any of the given dataset types.
388eda14cbcSMatt Macy  *
389eda14cbcSMatt Macy  * If headcheck is set, the check is being made against the head dataset
390eda14cbcSMatt Macy  * type of a snapshot which requires to return B_TRUE when the property
391eda14cbcSMatt Macy  * is only valid for snapshots.
392eda14cbcSMatt Macy  */
393eda14cbcSMatt Macy boolean_t
394eda14cbcSMatt Macy zprop_valid_for_type(int prop, zfs_type_t type, boolean_t headcheck)
395eda14cbcSMatt Macy {
396eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl;
397eda14cbcSMatt Macy 
398eda14cbcSMatt Macy 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
399eda14cbcSMatt Macy 		return (B_FALSE);
400eda14cbcSMatt Macy 
401eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
402eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
403eda14cbcSMatt Macy 	if (headcheck && prop_tbl[prop].pd_types == ZFS_TYPE_SNAPSHOT)
404eda14cbcSMatt Macy 		return (B_TRUE);
405eda14cbcSMatt Macy 	return ((prop_tbl[prop].pd_types & type) != 0);
406eda14cbcSMatt Macy }
407eda14cbcSMatt Macy 
408681ce946SMartin Matuska /*
409681ce946SMartin Matuska  * For user property names, we allow all lowercase alphanumeric characters, plus
410681ce946SMartin Matuska  * a few useful punctuation characters.
411681ce946SMartin Matuska  */
412681ce946SMartin Matuska int
413681ce946SMartin Matuska zprop_valid_char(char c)
414681ce946SMartin Matuska {
415681ce946SMartin Matuska 	return ((c >= 'a' && c <= 'z') ||
416681ce946SMartin Matuska 	    (c >= '0' && c <= '9') ||
417681ce946SMartin Matuska 	    c == '-' || c == '_' || c == '.' || c == ':');
418681ce946SMartin Matuska }
419681ce946SMartin Matuska 
420eda14cbcSMatt Macy #ifndef _KERNEL
421eda14cbcSMatt Macy 
422eda14cbcSMatt Macy /*
423eda14cbcSMatt Macy  * Determines the minimum width for the column, and indicates whether it's fixed
424eda14cbcSMatt Macy  * or not.  Only string columns are non-fixed.
425eda14cbcSMatt Macy  */
426eda14cbcSMatt Macy size_t
427eda14cbcSMatt Macy zprop_width(int prop, boolean_t *fixed, zfs_type_t type)
428eda14cbcSMatt Macy {
429eda14cbcSMatt Macy 	zprop_desc_t *prop_tbl, *pd;
430eda14cbcSMatt Macy 	const zprop_index_t *idx;
431eda14cbcSMatt Macy 	size_t ret;
432eda14cbcSMatt Macy 	int i;
433eda14cbcSMatt Macy 
434eda14cbcSMatt Macy 	ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
435eda14cbcSMatt Macy 	ASSERT(prop < zprop_get_numprops(type));
436eda14cbcSMatt Macy 
437eda14cbcSMatt Macy 	prop_tbl = zprop_get_proptable(type);
438eda14cbcSMatt Macy 	pd = &prop_tbl[prop];
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 	*fixed = B_TRUE;
441eda14cbcSMatt Macy 
442eda14cbcSMatt Macy 	/*
443eda14cbcSMatt Macy 	 * Start with the width of the column name.
444eda14cbcSMatt Macy 	 */
445eda14cbcSMatt Macy 	ret = strlen(pd->pd_colname);
446eda14cbcSMatt Macy 
447eda14cbcSMatt Macy 	/*
448eda14cbcSMatt Macy 	 * For fixed-width values, make sure the width is large enough to hold
449eda14cbcSMatt Macy 	 * any possible value.
450eda14cbcSMatt Macy 	 */
451eda14cbcSMatt Macy 	switch (pd->pd_proptype) {
452eda14cbcSMatt Macy 	case PROP_TYPE_NUMBER:
453eda14cbcSMatt Macy 		/*
454eda14cbcSMatt Macy 		 * The maximum length of a human-readable number is 5 characters
455eda14cbcSMatt Macy 		 * ("20.4M", for example).
456eda14cbcSMatt Macy 		 */
457eda14cbcSMatt Macy 		if (ret < 5)
458eda14cbcSMatt Macy 			ret = 5;
459eda14cbcSMatt Macy 		/*
460eda14cbcSMatt Macy 		 * 'creation' is handled specially because it's a number
461eda14cbcSMatt Macy 		 * internally, but displayed as a date string.
462eda14cbcSMatt Macy 		 */
463eda14cbcSMatt Macy 		if (prop == ZFS_PROP_CREATION)
464eda14cbcSMatt Macy 			*fixed = B_FALSE;
465eda14cbcSMatt Macy 		/*
466eda14cbcSMatt Macy 		 * 'health' is handled specially because it's a number
467eda14cbcSMatt Macy 		 * internally, but displayed as a fixed 8 character string.
468eda14cbcSMatt Macy 		 */
469eda14cbcSMatt Macy 		if (prop == ZPOOL_PROP_HEALTH)
470eda14cbcSMatt Macy 			ret = 8;
471eda14cbcSMatt Macy 		break;
472eda14cbcSMatt Macy 	case PROP_TYPE_INDEX:
473eda14cbcSMatt Macy 		idx = prop_tbl[prop].pd_table;
474eda14cbcSMatt Macy 		for (i = 0; idx[i].pi_name != NULL; i++) {
475eda14cbcSMatt Macy 			if (strlen(idx[i].pi_name) > ret)
476eda14cbcSMatt Macy 				ret = strlen(idx[i].pi_name);
477eda14cbcSMatt Macy 		}
478eda14cbcSMatt Macy 		break;
479eda14cbcSMatt Macy 
480eda14cbcSMatt Macy 	case PROP_TYPE_STRING:
481eda14cbcSMatt Macy 		*fixed = B_FALSE;
482eda14cbcSMatt Macy 		break;
483eda14cbcSMatt Macy 	}
484eda14cbcSMatt Macy 
485eda14cbcSMatt Macy 	return (ret);
486eda14cbcSMatt Macy }
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy #endif
489eda14cbcSMatt Macy 
490eda14cbcSMatt Macy #if defined(_KERNEL)
491eda14cbcSMatt Macy /* Common routines to initialize property tables */
492eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_impl);
493eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_string);
494eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_number);
495eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_index);
496eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_register_hidden);
497eda14cbcSMatt Macy 
498eda14cbcSMatt Macy /* Common routines for zfs and zpool property management */
499eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_iter_common);
500eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_name_to_prop);
501eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_string_to_index);
502eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_index_to_string);
503eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_random_value);
504eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_values);
505eda14cbcSMatt Macy EXPORT_SYMBOL(zprop_valid_for_type);
506681ce946SMartin Matuska EXPORT_SYMBOL(zprop_valid_char);
507eda14cbcSMatt Macy #endif
508