4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Common routines used by zfs and zpool property management.
34 #include <sys/zfs_acl.h>
35 #include <sys/zfs_ioctl.h>
36 #include <sys/zfs_znode.h>
37 #include <sys/fs/zfs.h>
40 #include "zfs_deleg.h"
43 #include <sys/systm.h>
44 #include <sys/libkern.h>
52 zprop_get_proptable(zfs_type_t type)
54 if (type == ZFS_TYPE_POOL)
55 return (zpool_prop_get_table());
57 return (zfs_prop_get_table());
61 zprop_get_numprops(zfs_type_t type)
63 if (type == ZFS_TYPE_POOL)
64 return (ZPOOL_NUM_PROPS);
66 return (ZFS_NUM_PROPS);
70 register_impl(int prop, const char *name, zprop_type_t type,
71 uint64_t numdefault, const char *strdefault, zprop_attr_t attr,
72 int objset_types, const char *values, const char *colname,
73 boolean_t rightalign, boolean_t visible, const zprop_index_t *idx_tbl)
75 zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types);
80 ASSERT(pd->pd_name == NULL || pd->pd_name == name);
83 pd->pd_propnum = prop;
84 pd->pd_proptype = type;
85 pd->pd_numdefault = numdefault;
86 pd->pd_strdefault = strdefault;
88 pd->pd_types = objset_types;
89 pd->pd_values = values;
90 pd->pd_colname = colname;
91 pd->pd_rightalign = rightalign;
92 pd->pd_visible = visible;
93 pd->pd_table = idx_tbl;
97 register_string(int prop, const char *name, const char *def,
98 zprop_attr_t attr, int objset_types, const char *values,
101 register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr,
102 objset_types, values, colname, B_FALSE, B_TRUE, NULL);
107 register_number(int prop, const char *name, uint64_t def, zprop_attr_t attr,
108 int objset_types, const char *values, const char *colname)
110 register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr,
111 objset_types, values, colname, B_TRUE, B_TRUE, NULL);
115 register_index(int prop, const char *name, uint64_t def, zprop_attr_t attr,
116 int objset_types, const char *values, const char *colname,
117 const zprop_index_t *idx_tbl)
119 register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr,
120 objset_types, values, colname, B_TRUE, B_TRUE, idx_tbl);
124 register_hidden(int prop, const char *name, zprop_type_t type,
125 zprop_attr_t attr, int objset_types, const char *colname)
127 register_impl(prop, name, type, 0, NULL, attr,
128 objset_types, NULL, colname, B_FALSE, B_FALSE, NULL);
133 * A comparison function we can use to order indexes into property tables.
136 zprop_compare(const void *arg1, const void *arg2)
138 const zprop_desc_t *p1 = *((zprop_desc_t **)arg1);
139 const zprop_desc_t *p2 = *((zprop_desc_t **)arg2);
140 boolean_t p1ro, p2ro;
142 p1ro = (p1->pd_attr == PROP_READONLY);
143 p2ro = (p2->pd_attr == PROP_READONLY);
146 return (strcmp(p1->pd_name, p2->pd_name));
148 return (p1ro ? -1 : 1);
152 * Iterate over all properties in the given property table, calling back
153 * into the specified function for each property. We will continue to
154 * iterate until we either reach the end or the callback function returns
155 * something other than ZPROP_CONT.
158 zprop_iter_common(zprop_func func, void *cb, boolean_t show_all,
159 boolean_t ordered, zfs_type_t type)
161 int i, j, num_props, size, prop;
162 zprop_desc_t *prop_tbl;
163 zprop_desc_t **order;
165 prop_tbl = zprop_get_proptable(type);
166 num_props = zprop_get_numprops(type);
167 size = num_props * sizeof (zprop_desc_t *);
170 order = kmem_alloc(size, KM_SLEEP);
172 if ((order = malloc(size)) == NULL)
176 for (j = 0; j < num_props; j++)
177 order[j] = &prop_tbl[j];
180 qsort((void *)order, num_props, sizeof (zprop_desc_t *),
185 for (i = 0; i < num_props; i++) {
186 if ((order[i]->pd_visible || show_all) &&
187 (func(order[i]->pd_propnum, cb) != ZPROP_CONT)) {
188 prop = order[i]->pd_propnum;
194 kmem_free(order, size);
202 propname_match(const char *p, size_t len, zprop_desc_t *prop_entry)
204 const char *propname = prop_entry->pd_name;
206 const char *colname = prop_entry->pd_colname;
213 if (len == strlen(propname) &&
214 strncmp(p, propname, len) == 0)
218 if (len != strlen(colname))
221 for (c = 0; c < len; c++)
222 if (p[c] != tolower(colname[c]))
225 return (colname[c] == '\0');
231 typedef struct name_to_prop_cb {
232 const char *propname;
233 zprop_desc_t *prop_tbl;
237 zprop_name_to_prop_cb(int prop, void *cb_data)
239 name_to_prop_cb_t *data = cb_data;
241 if (propname_match(data->propname, strlen(data->propname),
242 &data->prop_tbl[prop]))
249 zprop_name_to_prop(const char *propname, zfs_type_t type)
252 name_to_prop_cb_t cb_data;
254 cb_data.propname = propname;
255 cb_data.prop_tbl = zprop_get_proptable(type);
257 prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data,
258 B_TRUE, B_FALSE, type);
260 return (prop == ZPROP_CONT ? ZPROP_INVAL : prop);
264 zprop_string_to_index(int prop, const char *string, uint64_t *index,
267 zprop_desc_t *prop_tbl;
268 const zprop_index_t *idx_tbl;
271 if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
274 ASSERT(prop < zprop_get_numprops(type));
275 prop_tbl = zprop_get_proptable(type);
276 if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
279 for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
280 if (strcmp(string, idx_tbl[i].pi_name) == 0) {
281 *index = idx_tbl[i].pi_value;
290 zprop_index_to_string(int prop, uint64_t index, const char **string,
293 zprop_desc_t *prop_tbl;
294 const zprop_index_t *idx_tbl;
297 if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
300 ASSERT(prop < zprop_get_numprops(type));
301 prop_tbl = zprop_get_proptable(type);
302 if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
305 for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
306 if (idx_tbl[i].pi_value == index) {
307 *string = idx_tbl[i].pi_name;
316 zprop_values(int prop, zfs_type_t type)
318 zprop_desc_t *prop_tbl;
320 ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
321 ASSERT(prop < zprop_get_numprops(type));
323 prop_tbl = zprop_get_proptable(type);
325 return (prop_tbl[prop].pd_values);
329 * Returns TRUE if the property applies to any of the given dataset types.
332 zprop_valid_for_type(int prop, zfs_type_t type)
334 zprop_desc_t *prop_tbl;
336 if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
339 ASSERT(prop < zprop_get_numprops(type));
340 prop_tbl = zprop_get_proptable(type);
341 return ((prop_tbl[prop].pd_types & type) != 0);
347 * Determines the minimum width for the column, and indicates whether it's fixed
348 * or not. Only string columns are non-fixed.
351 zprop_width(int prop, boolean_t *fixed, zfs_type_t type)
353 zprop_desc_t *prop_tbl, *pd;
354 const zprop_index_t *idx;
358 ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
359 ASSERT(prop < zprop_get_numprops(type));
361 prop_tbl = zprop_get_proptable(type);
362 pd = &prop_tbl[prop];
367 * Start with the width of the column name.
369 ret = strlen(pd->pd_colname);
372 * For fixed-width values, make sure the width is large enough to hold
373 * any possible value.
375 switch (pd->pd_proptype) {
376 case PROP_TYPE_NUMBER:
378 * The maximum length of a human-readable number is 5 characters
379 * ("20.4M", for example).
384 * 'creation' is handled specially because it's a number
385 * internally, but displayed as a date string.
387 if (prop == ZFS_PROP_CREATION)
390 case PROP_TYPE_INDEX:
391 idx = prop_tbl[prop].pd_table;
392 for (i = 0; idx[i].pi_name != NULL; i++) {
393 if (strlen(idx[i].pi_name) > ret)
394 ret = strlen(idx[i].pi_name);
398 case PROP_TYPE_STRING: