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