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