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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Master property table.
31 * This table keeps track of all the properties supported by ZFS, and their
32 * various attributes. Not all of these are needed by the kernel, and several
33 * are only used by a single libzfs client. But having them here centralizes
34 * all property information in one location.
36 * name The human-readable string representing this property
37 * proptype Basic type (string, boolean, number)
38 * default Default value for the property. Sadly, C only allows
39 * you to initialize the first member of a union, so we
40 * have two default members for each property.
41 * attr Attributes (readonly, inheritable) for the property
42 * types Valid dataset types to which this applies
43 * values String describing acceptable values for the property
44 * colname The column header for 'zfs list'
45 * colfmt The column formatting for 'zfs list'
47 * This table must match the order of property types in libzfs.h.
52 #include <sys/zfs_acl.h>
53 #include <sys/zfs_ioctl.h>
58 #include <sys/systm.h>
73 zfs_proptype_t pd_proptype;
74 uint64_t pd_numdefault;
75 const char *pd_strdefault;
78 const char *pd_values;
79 const char *pd_colname;
80 boolean_t pd_rightalign;
84 static prop_desc_t zfs_prop_table[] = {
85 { "type", prop_type_string, 0, NULL, prop_readonly,
86 ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE,
88 { "creation", prop_type_number, 0, NULL, prop_readonly,
89 ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE },
90 { "used", prop_type_number, 0, NULL, prop_readonly,
91 ZFS_TYPE_ANY, "<size>", "USED", B_TRUE, B_TRUE },
92 { "available", prop_type_number, 0, NULL, prop_readonly,
93 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE,
95 { "referenced", prop_type_number, 0, NULL, prop_readonly,
97 "<size>", "REFER", B_TRUE, B_TRUE },
98 { "compressratio", prop_type_number, 0, NULL, prop_readonly,
99 ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE,
101 { "mounted", prop_type_boolean, 0, NULL, prop_readonly,
102 ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE, B_TRUE },
103 { "origin", prop_type_string, 0, NULL, prop_readonly,
104 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
106 { "quota", prop_type_number, 0, NULL, prop_default,
107 ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE, B_TRUE },
108 { "reservation", prop_type_number, 0, NULL, prop_default,
109 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
110 "<size> | none", "RESERV", B_TRUE, B_TRUE },
111 { "volsize", prop_type_number, 0, NULL, prop_default,
112 ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE },
113 { "volblocksize", prop_type_number, 8192, NULL, prop_readonly,
114 ZFS_TYPE_VOLUME, "512 to 128k, power of 2", "VOLBLOCK", B_TRUE,
116 { "recordsize", prop_type_number, SPA_MAXBLOCKSIZE, NULL,
119 "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE },
120 { "mountpoint", prop_type_string, 0, "/", prop_inherit,
122 "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE },
123 { "sharenfs", prop_type_string, 0, "off", prop_inherit,
125 "on | off | exports(5) options", "SHARENFS", B_FALSE, B_TRUE },
126 { "checksum", prop_type_index, ZIO_CHECKSUM_DEFAULT, "on",
127 prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
128 "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE,
130 { "compression", prop_type_index, ZIO_COMPRESS_DEFAULT, "off",
131 prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
132 "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", B_TRUE, B_TRUE },
133 { "atime", prop_type_boolean, 1, NULL, prop_inherit,
135 "on | off", "ATIME", B_TRUE, B_TRUE },
136 { "devices", prop_type_boolean, 1, NULL, prop_inherit,
137 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
138 "on | off", "DEVICES", B_TRUE, B_TRUE },
139 { "exec", prop_type_boolean, 1, NULL, prop_inherit,
140 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
141 "on | off", "EXEC", B_TRUE, B_TRUE },
142 { "setuid", prop_type_boolean, 1, NULL, prop_inherit,
143 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
145 { "readonly", prop_type_boolean, 0, NULL, prop_inherit,
146 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
147 "on | off", "RDONLY", B_TRUE, B_TRUE },
148 { "jailed", prop_type_boolean, 0, NULL, prop_inherit,
150 "on | off", "JAILED", B_TRUE, B_TRUE },
151 { "snapdir", prop_type_index, ZFS_SNAPDIR_HIDDEN, "hidden",
154 "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE },
155 { "aclmode", prop_type_index, ZFS_ACL_GROUPMASK, "groupmask",
156 prop_inherit, ZFS_TYPE_FILESYSTEM,
157 "discard | groupmask | passthrough", "ACLMODE", B_TRUE, B_TRUE },
158 { "aclinherit", prop_type_index, ZFS_ACL_SECURE, "secure",
159 prop_inherit, ZFS_TYPE_FILESYSTEM,
160 "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE,
162 { "createtxg", prop_type_number, 0, NULL, prop_readonly,
163 ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE },
164 { "name", prop_type_string, 0, NULL, prop_readonly,
165 ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE },
166 { "canmount", prop_type_boolean, 1, NULL, prop_default,
168 "on | off", "CANMOUNT", B_TRUE, B_TRUE },
169 { "shareiscsi", prop_type_string, 0, "off", prop_inherit,
171 "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE },
172 { "iscsioptions", prop_type_string, 0, NULL, prop_inherit,
173 ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE },
174 { "xattr", prop_type_boolean, 1, NULL, prop_inherit,
175 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
176 "on | off", "XATTR", B_TRUE, B_TRUE },
177 { "numclones", prop_type_number, 0, NULL, prop_readonly,
178 ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE, B_FALSE },
179 { "copies", prop_type_index, 1, "1", prop_inherit,
180 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
181 "1 | 2 | 3", "COPIES", B_TRUE, B_TRUE },
182 { "bootfs", prop_type_string, 0, NULL, prop_default,
183 ZFS_TYPE_POOL, "<filesystem>", "BOOTFS", B_FALSE, B_TRUE },
186 #define ZFS_PROP_COUNT ((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
189 * Returns TRUE if the property applies to the given dataset types.
192 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
194 return ((zfs_prop_table[prop].pd_types & types) != 0);
198 * Determine if the specified property is visible or not.
201 zfs_prop_is_visible(zfs_prop_t prop)
206 return (zfs_prop_table[prop].pd_visible);
210 * Iterate over all properties, calling back into the specified function
211 * for each property. We will continue to iterate until we either
212 * reach the end or the callback function something other than
216 zfs_prop_iter_common(zfs_prop_f func, void *cb, zfs_type_t type,
221 for (i = 0; i < ZFS_PROP_COUNT; i++) {
222 if (zfs_prop_valid_for_type(i, type) &&
223 (zfs_prop_is_visible(i) || show_all)) {
224 if (func(i, cb) != ZFS_PROP_CONT)
228 return (ZFS_PROP_CONT);
232 zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
234 return (zfs_prop_iter_common(func, cb, ZFS_TYPE_ANY, show_all));
238 zpool_prop_iter(zpool_prop_f func, void *cb, boolean_t show_all)
240 return (zfs_prop_iter_common(func, cb, ZFS_TYPE_POOL, show_all));
244 zfs_prop_get_type(zfs_prop_t prop)
246 return (zfs_prop_table[prop].pd_proptype);
250 propname_match(const char *p, zfs_prop_t prop, size_t len)
252 const char *propname = zfs_prop_table[prop].pd_name;
254 const char *colname = zfs_prop_table[prop].pd_colname;
263 if (len == strlen(propname) &&
264 strncmp(p, propname, len) == 0)
268 if (len != strlen(colname))
271 for (c = 0; c < len; c++)
272 if (p[c] != tolower(colname[c]))
275 return (colname[c] == '\0');
282 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
284 const char *propname = cb_data;
286 if (propname_match(propname, prop, strlen(propname)))
289 return (ZFS_PROP_CONT);
293 * Given a property name and its type, returns the corresponding property ID.
296 zfs_name_to_prop_common(const char *propname, zfs_type_t type)
300 prop = zfs_prop_iter_common(zfs_name_to_prop_cb, (void *)propname,
302 return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
306 * Given a zfs dataset property name, returns the corresponding property ID.
309 zfs_name_to_prop(const char *propname)
311 return (zfs_name_to_prop_common(propname, ZFS_TYPE_ANY));
315 * Given a pool property name, returns the corresponding property ID.
318 zpool_name_to_prop(const char *propname)
320 return (zfs_name_to_prop_common(propname, ZFS_TYPE_POOL));
324 * For user property names, we allow all lowercase alphanumeric characters, plus
325 * a few useful punctuation characters.
330 return ((c >= 'a' && c <= 'z') ||
331 (c >= '0' && c <= '9') ||
332 c == '-' || c == '_' || c == '.' || c == ':');
336 * Returns true if this is a valid user-defined property (one with a ':').
339 zfs_prop_user(const char *name)
343 boolean_t foundsep = B_FALSE;
345 for (i = 0; i < strlen(name); i++) {
360 * Return the default value for the given property.
363 zfs_prop_default_string(zfs_prop_t prop)
365 return (zfs_prop_table[prop].pd_strdefault);
369 zfs_prop_default_numeric(zfs_prop_t prop)
371 return (zfs_prop_table[prop].pd_numdefault);
375 * Returns TRUE if the property is readonly.
378 zfs_prop_readonly(zfs_prop_t prop)
380 return (zfs_prop_table[prop].pd_attr == prop_readonly);
384 * Given a dataset property ID, returns the corresponding name.
385 * Assuming the zfs dataset propety ID is valid.
388 zfs_prop_to_name(zfs_prop_t prop)
390 return (zfs_prop_table[prop].pd_name);
394 * Given a pool property ID, returns the corresponding name.
395 * Assuming the pool propety ID is valid.
398 zpool_prop_to_name(zpool_prop_t prop)
400 return (zfs_prop_table[prop].pd_name);
404 * Returns TRUE if the property is inheritable.
407 zfs_prop_inheritable(zfs_prop_t prop)
409 return (zfs_prop_table[prop].pd_attr == prop_inherit);
412 typedef struct zfs_index {
417 static zfs_index_t checksum_table[] = {
418 { "on", ZIO_CHECKSUM_ON },
419 { "off", ZIO_CHECKSUM_OFF },
420 { "fletcher2", ZIO_CHECKSUM_FLETCHER_2 },
421 { "fletcher4", ZIO_CHECKSUM_FLETCHER_4 },
422 { "sha256", ZIO_CHECKSUM_SHA256 },
426 static zfs_index_t compress_table[] = {
427 { "on", ZIO_COMPRESS_ON },
428 { "off", ZIO_COMPRESS_OFF },
429 { "lzjb", ZIO_COMPRESS_LZJB },
430 { "gzip", ZIO_COMPRESS_GZIP_6 }, /* the default gzip level */
431 { "gzip-1", ZIO_COMPRESS_GZIP_1 },
432 { "gzip-2", ZIO_COMPRESS_GZIP_2 },
433 { "gzip-3", ZIO_COMPRESS_GZIP_3 },
434 { "gzip-4", ZIO_COMPRESS_GZIP_4 },
435 { "gzip-5", ZIO_COMPRESS_GZIP_5 },
436 { "gzip-6", ZIO_COMPRESS_GZIP_6 },
437 { "gzip-7", ZIO_COMPRESS_GZIP_7 },
438 { "gzip-8", ZIO_COMPRESS_GZIP_8 },
439 { "gzip-9", ZIO_COMPRESS_GZIP_9 },
443 static zfs_index_t snapdir_table[] = {
444 { "hidden", ZFS_SNAPDIR_HIDDEN },
445 { "visible", ZFS_SNAPDIR_VISIBLE },
449 static zfs_index_t acl_mode_table[] = {
450 { "discard", ZFS_ACL_DISCARD },
451 { "groupmask", ZFS_ACL_GROUPMASK },
452 { "passthrough", ZFS_ACL_PASSTHROUGH },
456 static zfs_index_t acl_inherit_table[] = {
457 { "discard", ZFS_ACL_DISCARD },
458 { "noallow", ZFS_ACL_NOALLOW },
459 { "secure", ZFS_ACL_SECURE },
460 { "passthrough", ZFS_ACL_PASSTHROUGH },
464 static zfs_index_t copies_table[] = {
472 zfs_prop_index_table(zfs_prop_t prop)
475 case ZFS_PROP_CHECKSUM:
476 return (checksum_table);
477 case ZFS_PROP_COMPRESSION:
478 return (compress_table);
479 case ZFS_PROP_SNAPDIR:
480 return (snapdir_table);
481 case ZFS_PROP_ACLMODE:
482 return (acl_mode_table);
483 case ZFS_PROP_ACLINHERIT:
484 return (acl_inherit_table);
485 case ZFS_PROP_COPIES:
486 return (copies_table);
494 * Tables of index types, plus functions to convert between the user view
495 * (strings) and internal representation (uint64_t).
498 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
503 if ((table = zfs_prop_index_table(prop)) == NULL)
506 for (i = 0; table[i].name != NULL; i++) {
507 if (strcmp(string, table[i].name) == 0) {
508 *index = table[i].index;
517 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
522 if ((table = zfs_prop_index_table(prop)) == NULL)
525 for (i = 0; table[i].name != NULL; i++) {
526 if (table[i].index == index) {
527 *string = table[i].name;
538 * Returns a string describing the set of acceptable values for the given
539 * zfs property, or NULL if it cannot be set.
542 zfs_prop_values(zfs_prop_t prop)
544 if (zfs_prop_table[prop].pd_types == ZFS_TYPE_POOL)
547 return (zfs_prop_table[prop].pd_values);
551 * Returns a string describing the set of acceptable values for the given
552 * zpool property, or NULL if it cannot be set.
555 zpool_prop_values(zfs_prop_t prop)
557 if (zfs_prop_table[prop].pd_types != ZFS_TYPE_POOL)
560 return (zfs_prop_table[prop].pd_values);
564 * Returns TRUE if this property is a string type. Note that index types
565 * (compression, checksum) are treated as strings in userland, even though they
566 * are stored numerically on disk.
569 zfs_prop_is_string(zfs_prop_t prop)
571 return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
572 zfs_prop_table[prop].pd_proptype == prop_type_index);
576 * Returns the column header for the given property. Used only in
577 * 'zfs list -o', but centralized here with the other property information.
580 zfs_prop_column_name(zfs_prop_t prop)
582 return (zfs_prop_table[prop].pd_colname);
586 * Returns whether the given property should be displayed right-justified for
590 zfs_prop_align_right(zfs_prop_t prop)
592 return (zfs_prop_table[prop].pd_rightalign);
596 * Determines the minimum width for the column, and indicates whether it's fixed
597 * or not. Only string columns are non-fixed.
600 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
602 prop_desc_t *pd = &zfs_prop_table[prop];
610 * Start with the width of the column name.
612 ret = strlen(pd->pd_colname);
615 * For fixed-width values, make sure the width is large enough to hold
616 * any possible value.
618 switch (pd->pd_proptype) {
619 case prop_type_number:
621 * The maximum length of a human-readable number is 5 characters
622 * ("20.4M", for example).
627 * 'creation' is handled specially because it's a number
628 * internally, but displayed as a date string.
630 if (prop == ZFS_PROP_CREATION)
633 case prop_type_boolean:
635 * The maximum length of a boolean value is 3 characters, for
641 case prop_type_index:
642 idx = zfs_prop_index_table(prop);
643 for (i = 0; idx[i].name != NULL; i++) {
644 if (strlen(idx[i].name) > ret)
645 ret = strlen(idx[i].name);
649 case prop_type_string: