]> git.proxmox.com Git - mirror_zfs.git/blame - module/zcommon/zprop_common.c
Set send_realloc_files.ksh to use properties.shlib
[mirror_zfs.git] / module / zcommon / zprop_common.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
23 * Use is subject to license terms.
24 */
6f1ffb06
MA
25/*
26 * Copyright (c) 2012 by Delphix. All rights reserved.
27 */
34dc7c2f 28
34dc7c2f
BB
29/*
30 * Common routines used by zfs and zpool property management.
31 */
32
33#include <sys/zio.h>
34#include <sys/spa.h>
35#include <sys/zfs_acl.h>
36#include <sys/zfs_ioctl.h>
e8bcb693 37#include <sys/zfs_sysfs.h>
34dc7c2f
BB
38#include <sys/zfs_znode.h>
39#include <sys/fs/zfs.h>
40
41#include "zfs_prop.h"
42#include "zfs_deleg.h"
43
74d1d749 44#if !defined(_KERNEL)
34dc7c2f
BB
45#include <stdlib.h>
46#include <string.h>
47#include <ctype.h>
e8bcb693 48#include <sys/stat.h>
34dc7c2f
BB
49#endif
50
51static zprop_desc_t *
52zprop_get_proptable(zfs_type_t type)
53{
54 if (type == ZFS_TYPE_POOL)
55 return (zpool_prop_get_table());
56 else
57 return (zfs_prop_get_table());
58}
59
60static int
61zprop_get_numprops(zfs_type_t type)
62{
63 if (type == ZFS_TYPE_POOL)
64 return (ZPOOL_NUM_PROPS);
65 else
66 return (ZFS_NUM_PROPS);
67}
68
e8bcb693
DB
69static boolean_t
70zfs_mod_supported_prop(const char *name, zfs_type_t type)
71{
72/*
73 * The zfs module spa_feature_table[], whether in-kernel or in libzpool,
74 * always supports all the properties. libzfs needs to query the running
75 * module, via sysfs, to determine which properties are supported.
76 */
77#if defined(_KERNEL) || defined(LIB_ZPOOL_BUILD)
78 return (B_TRUE);
79#else
73a5ec30
DB
80 return (zfs_mod_supported(type == ZFS_TYPE_POOL ?
81 ZFS_SYSFS_POOL_PROPERTIES : ZFS_SYSFS_DATASET_PROPERTIES, name));
e8bcb693
DB
82#endif
83}
84
34dc7c2f 85void
428870ff 86zprop_register_impl(int prop, const char *name, zprop_type_t type,
34dc7c2f
BB
87 uint64_t numdefault, const char *strdefault, zprop_attr_t attr,
88 int objset_types, const char *values, const char *colname,
89 boolean_t rightalign, boolean_t visible, const zprop_index_t *idx_tbl)
90{
91 zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types);
92 zprop_desc_t *pd;
93
94 pd = &prop_tbl[prop];
95
96 ASSERT(pd->pd_name == NULL || pd->pd_name == name);
428870ff
BB
97 ASSERT(name != NULL);
98 ASSERT(colname != NULL);
34dc7c2f
BB
99
100 pd->pd_name = name;
101 pd->pd_propnum = prop;
102 pd->pd_proptype = type;
103 pd->pd_numdefault = numdefault;
104 pd->pd_strdefault = strdefault;
105 pd->pd_attr = attr;
106 pd->pd_types = objset_types;
107 pd->pd_values = values;
108 pd->pd_colname = colname;
109 pd->pd_rightalign = rightalign;
110 pd->pd_visible = visible;
e8bcb693 111 pd->pd_zfs_mod_supported = zfs_mod_supported_prop(name, objset_types);
34dc7c2f 112 pd->pd_table = idx_tbl;
428870ff
BB
113 pd->pd_table_size = 0;
114 while (idx_tbl && (idx_tbl++)->pi_name != NULL)
115 pd->pd_table_size++;
34dc7c2f
BB
116}
117
118void
428870ff 119zprop_register_string(int prop, const char *name, const char *def,
34dc7c2f
BB
120 zprop_attr_t attr, int objset_types, const char *values,
121 const char *colname)
122{
428870ff 123 zprop_register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr,
34dc7c2f
BB
124 objset_types, values, colname, B_FALSE, B_TRUE, NULL);
125
126}
127
128void
428870ff
BB
129zprop_register_number(int prop, const char *name, uint64_t def,
130 zprop_attr_t attr, int objset_types, const char *values,
131 const char *colname)
34dc7c2f 132{
428870ff 133 zprop_register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr,
34dc7c2f
BB
134 objset_types, values, colname, B_TRUE, B_TRUE, NULL);
135}
136
137void
428870ff
BB
138zprop_register_index(int prop, const char *name, uint64_t def,
139 zprop_attr_t attr, int objset_types, const char *values,
140 const char *colname, const zprop_index_t *idx_tbl)
34dc7c2f 141{
428870ff 142 zprop_register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr,
34dc7c2f
BB
143 objset_types, values, colname, B_TRUE, B_TRUE, idx_tbl);
144}
145
146void
428870ff 147zprop_register_hidden(int prop, const char *name, zprop_type_t type,
34dc7c2f
BB
148 zprop_attr_t attr, int objset_types, const char *colname)
149{
428870ff 150 zprop_register_impl(prop, name, type, 0, NULL, attr,
6f1ffb06
MA
151 objset_types, NULL, colname,
152 type == PROP_TYPE_NUMBER, B_FALSE, NULL);
34dc7c2f
BB
153}
154
155
156/*
157 * A comparison function we can use to order indexes into property tables.
158 */
159static int
160zprop_compare(const void *arg1, const void *arg2)
161{
162 const zprop_desc_t *p1 = *((zprop_desc_t **)arg1);
163 const zprop_desc_t *p2 = *((zprop_desc_t **)arg2);
164 boolean_t p1ro, p2ro;
165
166 p1ro = (p1->pd_attr == PROP_READONLY);
167 p2ro = (p2->pd_attr == PROP_READONLY);
168
169 if (p1ro == p2ro)
170 return (strcmp(p1->pd_name, p2->pd_name));
171
172 return (p1ro ? -1 : 1);
173}
174
175/*
176 * Iterate over all properties in the given property table, calling back
177 * into the specified function for each property. We will continue to
178 * iterate until we either reach the end or the callback function returns
179 * something other than ZPROP_CONT.
180 */
181int
182zprop_iter_common(zprop_func func, void *cb, boolean_t show_all,
183 boolean_t ordered, zfs_type_t type)
184{
1c27024e 185 int i, num_props, size, prop;
34dc7c2f
BB
186 zprop_desc_t *prop_tbl;
187 zprop_desc_t **order;
188
189 prop_tbl = zprop_get_proptable(type);
190 num_props = zprop_get_numprops(type);
191 size = num_props * sizeof (zprop_desc_t *);
192
193#if defined(_KERNEL)
79c76d5b 194 order = kmem_alloc(size, KM_SLEEP);
34dc7c2f
BB
195#else
196 if ((order = malloc(size)) == NULL)
197 return (ZPROP_CONT);
198#endif
199
1c27024e 200 for (int j = 0; j < num_props; j++)
34dc7c2f
BB
201 order[j] = &prop_tbl[j];
202
203 if (ordered) {
204 qsort((void *)order, num_props, sizeof (zprop_desc_t *),
205 zprop_compare);
206 }
207
208 prop = ZPROP_CONT;
209 for (i = 0; i < num_props; i++) {
210 if ((order[i]->pd_visible || show_all) &&
e8bcb693 211 order[i]->pd_zfs_mod_supported &&
34dc7c2f
BB
212 (func(order[i]->pd_propnum, cb) != ZPROP_CONT)) {
213 prop = order[i]->pd_propnum;
214 break;
215 }
216 }
217
218#if defined(_KERNEL)
219 kmem_free(order, size);
220#else
221 free(order);
222#endif
223 return (prop);
224}
225
226static boolean_t
227propname_match(const char *p, size_t len, zprop_desc_t *prop_entry)
228{
229 const char *propname = prop_entry->pd_name;
230#ifndef _KERNEL
231 const char *colname = prop_entry->pd_colname;
232 int c;
34dc7c2f
BB
233#endif
234
235 if (len == strlen(propname) &&
236 strncmp(p, propname, len) == 0)
237 return (B_TRUE);
238
239#ifndef _KERNEL
9babb374 240 if (colname == NULL || len != strlen(colname))
34dc7c2f
BB
241 return (B_FALSE);
242
243 for (c = 0; c < len; c++)
244 if (p[c] != tolower(colname[c]))
245 break;
246
247 return (colname[c] == '\0');
248#else
249 return (B_FALSE);
250#endif
251}
252
253typedef struct name_to_prop_cb {
254 const char *propname;
255 zprop_desc_t *prop_tbl;
256} name_to_prop_cb_t;
257
258static int
259zprop_name_to_prop_cb(int prop, void *cb_data)
260{
261 name_to_prop_cb_t *data = cb_data;
262
263 if (propname_match(data->propname, strlen(data->propname),
264 &data->prop_tbl[prop]))
265 return (prop);
266
267 return (ZPROP_CONT);
268}
269
270int
271zprop_name_to_prop(const char *propname, zfs_type_t type)
272{
273 int prop;
274 name_to_prop_cb_t cb_data;
275
276 cb_data.propname = propname;
277 cb_data.prop_tbl = zprop_get_proptable(type);
278
279 prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data,
280 B_TRUE, B_FALSE, type);
281
282 return (prop == ZPROP_CONT ? ZPROP_INVAL : prop);
283}
284
285int
286zprop_string_to_index(int prop, const char *string, uint64_t *index,
287 zfs_type_t type)
288{
289 zprop_desc_t *prop_tbl;
290 const zprop_index_t *idx_tbl;
291 int i;
292
b128c09f
BB
293 if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
294 return (-1);
295
296 ASSERT(prop < zprop_get_numprops(type));
34dc7c2f
BB
297 prop_tbl = zprop_get_proptable(type);
298 if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
299 return (-1);
300
301 for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
302 if (strcmp(string, idx_tbl[i].pi_name) == 0) {
303 *index = idx_tbl[i].pi_value;
304 return (0);
305 }
306 }
307
308 return (-1);
309}
310
311int
312zprop_index_to_string(int prop, uint64_t index, const char **string,
313 zfs_type_t type)
314{
315 zprop_desc_t *prop_tbl;
316 const zprop_index_t *idx_tbl;
317 int i;
318
b128c09f
BB
319 if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
320 return (-1);
321
322 ASSERT(prop < zprop_get_numprops(type));
34dc7c2f
BB
323 prop_tbl = zprop_get_proptable(type);
324 if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
325 return (-1);
326
327 for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
328 if (idx_tbl[i].pi_value == index) {
329 *string = idx_tbl[i].pi_name;
330 return (0);
331 }
332 }
333
334 return (-1);
335}
336
428870ff
BB
337/*
338 * Return a random valid property value. Used by ztest.
339 */
340uint64_t
341zprop_random_value(int prop, uint64_t seed, zfs_type_t type)
342{
343 zprop_desc_t *prop_tbl;
344 const zprop_index_t *idx_tbl;
345
346 ASSERT((uint_t)prop < zprop_get_numprops(type));
347 prop_tbl = zprop_get_proptable(type);
348 idx_tbl = prop_tbl[prop].pd_table;
349
350 if (idx_tbl == NULL)
351 return (seed);
352
353 return (idx_tbl[seed % prop_tbl[prop].pd_table_size].pi_value);
354}
355
34dc7c2f
BB
356const char *
357zprop_values(int prop, zfs_type_t type)
358{
359 zprop_desc_t *prop_tbl;
360
b128c09f
BB
361 ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
362 ASSERT(prop < zprop_get_numprops(type));
363
34dc7c2f
BB
364 prop_tbl = zprop_get_proptable(type);
365
366 return (prop_tbl[prop].pd_values);
367}
368
369/*
370 * Returns TRUE if the property applies to any of the given dataset types.
962d5242
TC
371 *
372 * If headcheck is set, the check is being made against the head dataset
373 * type of a snapshot which requires to return B_TRUE when the property
374 * is only valid for snapshots.
34dc7c2f 375 */
b128c09f 376boolean_t
962d5242 377zprop_valid_for_type(int prop, zfs_type_t type, boolean_t headcheck)
34dc7c2f 378{
b128c09f
BB
379 zprop_desc_t *prop_tbl;
380
381 if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
382 return (B_FALSE);
34dc7c2f 383
b128c09f
BB
384 ASSERT(prop < zprop_get_numprops(type));
385 prop_tbl = zprop_get_proptable(type);
962d5242
TC
386 if (headcheck && prop_tbl[prop].pd_types == ZFS_TYPE_SNAPSHOT)
387 return (B_TRUE);
34dc7c2f
BB
388 return ((prop_tbl[prop].pd_types & type) != 0);
389}
390
391#ifndef _KERNEL
392
393/*
394 * Determines the minimum width for the column, and indicates whether it's fixed
395 * or not. Only string columns are non-fixed.
396 */
397size_t
398zprop_width(int prop, boolean_t *fixed, zfs_type_t type)
399{
400 zprop_desc_t *prop_tbl, *pd;
401 const zprop_index_t *idx;
402 size_t ret;
403 int i;
404
b128c09f
BB
405 ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
406 ASSERT(prop < zprop_get_numprops(type));
407
34dc7c2f
BB
408 prop_tbl = zprop_get_proptable(type);
409 pd = &prop_tbl[prop];
410
411 *fixed = B_TRUE;
412
413 /*
414 * Start with the width of the column name.
415 */
416 ret = strlen(pd->pd_colname);
417
418 /*
419 * For fixed-width values, make sure the width is large enough to hold
420 * any possible value.
421 */
422 switch (pd->pd_proptype) {
423 case PROP_TYPE_NUMBER:
424 /*
425 * The maximum length of a human-readable number is 5 characters
426 * ("20.4M", for example).
427 */
428 if (ret < 5)
429 ret = 5;
430 /*
431 * 'creation' is handled specially because it's a number
432 * internally, but displayed as a date string.
433 */
434 if (prop == ZFS_PROP_CREATION)
435 *fixed = B_FALSE;
c5eea0ab
BB
436 /*
437 * 'health' is handled specially because it's a number
438 * internally, but displayed as a fixed 8 character string.
439 */
440 if (prop == ZPOOL_PROP_HEALTH)
441 ret = 8;
34dc7c2f
BB
442 break;
443 case PROP_TYPE_INDEX:
444 idx = prop_tbl[prop].pd_table;
445 for (i = 0; idx[i].pi_name != NULL; i++) {
446 if (strlen(idx[i].pi_name) > ret)
447 ret = strlen(idx[i].pi_name);
448 }
449 break;
450
451 case PROP_TYPE_STRING:
452 *fixed = B_FALSE;
453 break;
454 }
455
456 return (ret);
457}
458
459#endif
c28b2279 460
93ce2b4c 461#if defined(_KERNEL)
c28b2279
BB
462/* Common routines to initialize property tables */
463EXPORT_SYMBOL(zprop_register_impl);
464EXPORT_SYMBOL(zprop_register_string);
465EXPORT_SYMBOL(zprop_register_number);
466EXPORT_SYMBOL(zprop_register_index);
467EXPORT_SYMBOL(zprop_register_hidden);
468
469/* Common routines for zfs and zpool property management */
470EXPORT_SYMBOL(zprop_iter_common);
471EXPORT_SYMBOL(zprop_name_to_prop);
472EXPORT_SYMBOL(zprop_string_to_index);
473EXPORT_SYMBOL(zprop_index_to_string);
474EXPORT_SYMBOL(zprop_random_value);
475EXPORT_SYMBOL(zprop_values);
476EXPORT_SYMBOL(zprop_valid_for_type);
477#endif