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