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