xref: /titanic_50/usr/src/common/zfs/zfs_prop.c (revision 41efec2219526a9b3ecce26f97aba761ef1e1d0d)
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 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Master property table.
30  *
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.
35  *
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'
46  *
47  * This table must match the order of property types in libzfs.h.
48  */
49 
50 #include <sys/zio.h>
51 #include <sys/spa.h>
52 #include <sys/zfs_acl.h>
53 #include <sys/zfs_ioctl.h>
54 
55 #include "zfs_prop.h"
56 #include "zfs_deleg.h"
57 
58 #if defined(_KERNEL)
59 #include <sys/systm.h>
60 #else
61 #include <stdlib.h>
62 #include <string.h>
63 #include <ctype.h>
64 #endif
65 
66 typedef enum {
67 	prop_default,
68 	prop_readonly,
69 	prop_inherit
70 } prop_attr_t;
71 
72 typedef struct {
73 	const char	*pd_name;
74 	zfs_proptype_t	pd_proptype;
75 	uint64_t	pd_numdefault;
76 	const char	*pd_strdefault;
77 	prop_attr_t	pd_attr;
78 	int		pd_types;
79 	const char	*pd_values;
80 	const char	*pd_colname;
81 	boolean_t	pd_rightalign;
82 	boolean_t	pd_visible;
83 	const char	*pd_perm;
84 } prop_desc_t;
85 
86 static prop_desc_t zfs_prop_table[] = {
87 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
88 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE,
89 	    B_TRUE, ZFS_DELEG_PERM_NONE },
90 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
91 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE,
92 	    ZFS_DELEG_PERM_NONE },
93 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
94 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE, B_TRUE,
95 	    ZFS_DELEG_PERM_NONE },
96 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
97 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE,
98 	    B_TRUE, ZFS_DELEG_PERM_NONE },
99 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
100 	    ZFS_TYPE_ANY,
101 	    "<size>", "REFER", B_TRUE, B_TRUE, ZFS_DELEG_PERM_NONE },
102 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
103 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE,
104 	    B_TRUE, ZFS_DELEG_PERM_NONE },
105 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
106 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE, B_TRUE,
107 	    ZFS_DELEG_PERM_NONE },
108 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
109 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
110 	    B_FALSE, B_TRUE, ZFS_DELEG_PERM_NONE },
111 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
112 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE, B_TRUE,
113 	    ZFS_DELEG_PERM_QUOTA },
114 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
115 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
116 	    "<size> | none", "RESERV", B_TRUE, B_TRUE,
117 	    ZFS_DELEG_PERM_RESERVATION },
118 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
119 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE,
120 	    ZFS_DELEG_PERM_VOLSIZE },
121 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
122 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE,
123 	    B_TRUE, ZFS_DELEG_PERM_NONE },
124 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
125 	    prop_inherit,
126 	    ZFS_TYPE_FILESYSTEM,
127 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE,
128 	    ZFS_DELEG_PERM_RECORDSIZE },
129 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
130 	    ZFS_TYPE_FILESYSTEM,
131 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE,
132 	    ZFS_DELEG_PERM_MOUNTPOINT },
133 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
134 	    ZFS_TYPE_FILESYSTEM,
135 	    "on | off | share(1M) options", "SHARENFS", B_FALSE, B_TRUE,
136 	    ZFS_DELEG_PERM_SHARENFS },
137 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
138 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
139 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE,
140 	    B_TRUE, ZFS_DELEG_PERM_CHECKSUM },
141 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
142 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
143 	    "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", B_TRUE, B_TRUE,
144 	    ZFS_DELEG_PERM_COMPRESSION },
145 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
146 	    ZFS_TYPE_FILESYSTEM,
147 	    "on | off", "ATIME", B_TRUE, B_TRUE, ZFS_DELEG_PERM_ATIME },
148 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
149 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
150 	    "on | off", "DEVICES", B_TRUE, B_TRUE, ZFS_DELEG_PERM_DEVICES },
151 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
152 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
153 	    "on | off", "EXEC", B_TRUE, B_TRUE, ZFS_DELEG_PERM_EXEC },
154 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
155 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
156 	    B_TRUE, B_TRUE, ZFS_DELEG_PERM_SETUID },
157 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
158 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
159 	    "on | off", "RDONLY", B_TRUE, B_TRUE, ZFS_DELEG_PERM_READONLY },
160 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
161 	    ZFS_TYPE_FILESYSTEM,
162 	    "on | off", "ZONED", B_TRUE, B_TRUE, ZFS_DELEG_PERM_ZONED },
163 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
164 	    prop_inherit,
165 	    ZFS_TYPE_FILESYSTEM,
166 	    "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE,
167 	    ZFS_DELEG_PERM_SNAPDIR },
168 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
169 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
170 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE,
171 	    B_TRUE, ZFS_DELEG_PERM_ACLMODE },
172 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
173 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
174 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE,
175 	    B_TRUE, ZFS_DELEG_PERM_ACLINHERIT },
176 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
177 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE, ZFS_DELEG_PERM_NONE },
178 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
179 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE, ZFS_DELEG_PERM_NONE },
180 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
181 	    ZFS_TYPE_FILESYSTEM,
182 	    "on | off", "CANMOUNT", B_TRUE, B_TRUE, ZFS_DELEG_PERM_CANMOUNT },
183 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
184 	    ZFS_TYPE_ANY,
185 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE,
186 	    ZFS_DELEG_PERM_SHAREISCSI },
187 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
188 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE,
189 	    ZFS_DELEG_PERM_NONE },
190 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
191 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
192 	    "on | off", "XATTR", B_TRUE, B_TRUE, ZFS_DELEG_PERM_XATTR },
193 	{ "numclones", prop_type_number,	0,	NULL,	prop_readonly,
194 	    ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE, B_FALSE,
195 	    ZFS_DELEG_PERM_NONE },
196 	{ "copies",	prop_type_index,	1,	"1",	prop_inherit,
197 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
198 	    "1 | 2 | 3", "COPIES", B_TRUE, B_TRUE, ZFS_DELEG_PERM_COPIES },
199 	{ "bootfs", prop_type_string,	0,	NULL,	prop_default,
200 	    ZFS_TYPE_POOL, "<filesystem>", "BOOTFS", B_FALSE,
201 	    B_TRUE, ZFS_DELEG_PERM_NONE },
202 	{ "autoreplace", prop_type_boolean,	0,	NULL, prop_default,
203 	    ZFS_TYPE_POOL, "on | off", "REPLACE", B_FALSE, B_TRUE,
204 	    ZFS_DELEG_PERM_NONE },
205 	{ "delegation", prop_type_boolean,	1,	NULL,	prop_default,
206 	    ZFS_TYPE_POOL, "on | off", "DELEGATION", B_TRUE,
207 	    B_TRUE, ZFS_DELEG_PERM_NONE }
208 };
209 
210 #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
211 
212 /*
213  * Returns TRUE if the property applies to the given dataset types.
214  */
215 int
216 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
217 {
218 	return ((zfs_prop_table[prop].pd_types & types) != 0);
219 }
220 
221 /*
222  * Determine if the specified property is visible or not.
223  */
224 boolean_t
225 zfs_prop_is_visible(zfs_prop_t prop)
226 {
227 	if (prop < 0)
228 		return (B_FALSE);
229 
230 	return (zfs_prop_table[prop].pd_visible);
231 }
232 
233 /*
234  * Iterate over all properties, calling back into the specified function
235  * for each property. We will continue to iterate until we either
236  * reach the end or the callback function something other than
237  * ZFS_PROP_CONT.
238  */
239 zfs_prop_t
240 zfs_prop_iter_common(zfs_prop_f func, void *cb, zfs_type_t type,
241     boolean_t show_all)
242 {
243 	int i;
244 
245 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
246 		if (zfs_prop_valid_for_type(i, type) &&
247 		    (zfs_prop_is_visible(i) || show_all)) {
248 			if (func(i, cb) != ZFS_PROP_CONT)
249 				return (i);
250 		}
251 	}
252 	return (ZFS_PROP_CONT);
253 }
254 
255 zfs_prop_t
256 zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
257 {
258 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_ANY, show_all));
259 }
260 
261 zpool_prop_t
262 zpool_prop_iter(zpool_prop_f func, void *cb, boolean_t show_all)
263 {
264 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_POOL, show_all));
265 }
266 
267 zfs_proptype_t
268 zfs_prop_get_type(zfs_prop_t prop)
269 {
270 	return (zfs_prop_table[prop].pd_proptype);
271 }
272 
273 zfs_proptype_t
274 zpool_prop_get_type(zfs_prop_t prop)
275 {
276 	return (zfs_prop_table[prop].pd_proptype);
277 }
278 
279 static boolean_t
280 propname_match(const char *p, zfs_prop_t prop, size_t len)
281 {
282 	const char *propname = zfs_prop_table[prop].pd_name;
283 #ifndef _KERNEL
284 	const char *colname = zfs_prop_table[prop].pd_colname;
285 	int c;
286 #endif
287 
288 #ifndef _KERNEL
289 	if (colname == NULL)
290 		return (B_FALSE);
291 #endif
292 
293 	if (len == strlen(propname) &&
294 	    strncmp(p, propname, len) == 0)
295 		return (B_TRUE);
296 
297 #ifndef _KERNEL
298 	if (len != strlen(colname))
299 		return (B_FALSE);
300 
301 	for (c = 0; c < len; c++)
302 		if (p[c] != tolower(colname[c]))
303 			break;
304 
305 	return (colname[c] == '\0');
306 #else
307 	return (B_FALSE);
308 #endif
309 }
310 
311 zfs_prop_t
312 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
313 {
314 	const char *propname = cb_data;
315 
316 	if (propname_match(propname, prop, strlen(propname))) {
317 		return (prop);
318 	}
319 
320 	return (ZFS_PROP_CONT);
321 }
322 
323 /*
324  * Given a property name and its type, returns the corresponding property ID.
325  */
326 zfs_prop_t
327 zfs_name_to_prop_common(const char *propname, zfs_type_t type)
328 {
329 	zfs_prop_t prop;
330 
331 	prop = zfs_prop_iter_common(zfs_name_to_prop_cb, (void *)propname,
332 	    type, B_TRUE);
333 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
334 }
335 
336 /*
337  * Given a zfs dataset property name, returns the corresponding property ID.
338  */
339 zfs_prop_t
340 zfs_name_to_prop(const char *propname)
341 {
342 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_ANY));
343 }
344 
345 /*
346  * Given a pool property name, returns the corresponding property ID.
347  */
348 zpool_prop_t
349 zpool_name_to_prop(const char *propname)
350 {
351 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_POOL));
352 }
353 
354 const char *
355 zfs_prop_perm(zfs_prop_t prop)
356 {
357 	return (zfs_prop_table[prop].pd_perm);
358 }
359 
360 /*
361  * For user property names, we allow all lowercase alphanumeric characters, plus
362  * a few useful punctuation characters.
363  */
364 static int
365 valid_char(char c)
366 {
367 	return ((c >= 'a' && c <= 'z') ||
368 	    (c >= '0' && c <= '9') ||
369 	    c == '-' || c == '_' || c == '.' || c == ':');
370 }
371 
372 /*
373  * Returns true if this is a valid user-defined property (one with a ':').
374  */
375 boolean_t
376 zfs_prop_user(const char *name)
377 {
378 	int i;
379 	char c;
380 	boolean_t foundsep = B_FALSE;
381 
382 	for (i = 0; i < strlen(name); i++) {
383 		c = name[i];
384 		if (!valid_char(c))
385 			return (B_FALSE);
386 		if (c == ':')
387 			foundsep = B_TRUE;
388 	}
389 
390 	if (!foundsep)
391 		return (B_FALSE);
392 
393 	return (B_TRUE);
394 }
395 
396 /*
397  * Return the default value for the given property.
398  */
399 const char *
400 zfs_prop_default_string(zfs_prop_t prop)
401 {
402 	return (zfs_prop_table[prop].pd_strdefault);
403 }
404 
405 const char *
406 zpool_prop_default_string(zpool_prop_t prop)
407 {
408 	return (zfs_prop_table[prop].pd_strdefault);
409 }
410 
411 uint64_t
412 zfs_prop_default_numeric(zfs_prop_t prop)
413 {
414 	return (zfs_prop_table[prop].pd_numdefault);
415 }
416 
417 uint64_t
418 zpool_prop_default_numeric(zpool_prop_t prop)
419 {
420 	return (zfs_prop_table[prop].pd_numdefault);
421 }
422 
423 /*
424  * Returns TRUE if the property is readonly.
425  */
426 int
427 zfs_prop_readonly(zfs_prop_t prop)
428 {
429 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
430 }
431 
432 /*
433  * Given a dataset property ID, returns the corresponding name.
434  * Assuming the zfs dataset property ID is valid.
435  */
436 const char *
437 zfs_prop_to_name(zfs_prop_t prop)
438 {
439 	return (zfs_prop_table[prop].pd_name);
440 }
441 
442 /*
443  * Given a pool property ID, returns the corresponding name.
444  * Assuming the pool property ID is valid.
445  */
446 const char *
447 zpool_prop_to_name(zpool_prop_t prop)
448 {
449 	return (zfs_prop_table[prop].pd_name);
450 }
451 
452 /*
453  * Returns TRUE if the property is inheritable.
454  */
455 int
456 zfs_prop_inheritable(zfs_prop_t prop)
457 {
458 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
459 }
460 
461 typedef struct zfs_index {
462 	const char *name;
463 	uint64_t index;
464 } zfs_index_t;
465 
466 static zfs_index_t checksum_table[] = {
467 	{ "on",		ZIO_CHECKSUM_ON },
468 	{ "off",	ZIO_CHECKSUM_OFF },
469 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
470 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
471 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
472 	{ NULL }
473 };
474 
475 static zfs_index_t compress_table[] = {
476 	{ "on",		ZIO_COMPRESS_ON },
477 	{ "off",	ZIO_COMPRESS_OFF },
478 	{ "lzjb",	ZIO_COMPRESS_LZJB },
479 	{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* the default gzip level */
480 	{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
481 	{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
482 	{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
483 	{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
484 	{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
485 	{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
486 	{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
487 	{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
488 	{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
489 	{ NULL }
490 };
491 
492 static zfs_index_t snapdir_table[] = {
493 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
494 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
495 	{ NULL }
496 };
497 
498 static zfs_index_t acl_mode_table[] = {
499 	{ "discard",	ZFS_ACL_DISCARD },
500 	{ "groupmask",	ZFS_ACL_GROUPMASK },
501 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
502 	{ NULL }
503 };
504 
505 static zfs_index_t acl_inherit_table[] = {
506 	{ "discard",	ZFS_ACL_DISCARD },
507 	{ "noallow",	ZFS_ACL_NOALLOW },
508 	{ "secure",	ZFS_ACL_SECURE },
509 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
510 	{ NULL }
511 };
512 
513 static zfs_index_t copies_table[] = {
514 	{ "1",	1 },
515 	{ "2",	2 },
516 	{ "3",	3 },
517 	{ NULL }
518 };
519 
520 static zfs_index_t *
521 zfs_prop_index_table(zfs_prop_t prop)
522 {
523 	switch (prop) {
524 	case ZFS_PROP_CHECKSUM:
525 		return (checksum_table);
526 	case ZFS_PROP_COMPRESSION:
527 		return (compress_table);
528 	case ZFS_PROP_SNAPDIR:
529 		return (snapdir_table);
530 	case ZFS_PROP_ACLMODE:
531 		return (acl_mode_table);
532 	case ZFS_PROP_ACLINHERIT:
533 		return (acl_inherit_table);
534 	case ZFS_PROP_COPIES:
535 		return (copies_table);
536 	default:
537 		return (NULL);
538 	}
539 }
540 
541 
542 /*
543  * Tables of index types, plus functions to convert between the user view
544  * (strings) and internal representation (uint64_t).
545  */
546 int
547 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
548 {
549 	zfs_index_t *table;
550 	int i;
551 
552 	if ((table = zfs_prop_index_table(prop)) == NULL)
553 		return (-1);
554 
555 	for (i = 0; table[i].name != NULL; i++) {
556 		if (strcmp(string, table[i].name) == 0) {
557 			*index = table[i].index;
558 			return (0);
559 		}
560 	}
561 
562 	return (-1);
563 }
564 
565 int
566 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
567 {
568 	zfs_index_t *table;
569 	int i;
570 
571 	if ((table = zfs_prop_index_table(prop)) == NULL)
572 		return (-1);
573 
574 	for (i = 0; table[i].name != NULL; i++) {
575 		if (table[i].index == index) {
576 			*string = table[i].name;
577 			return (0);
578 		}
579 	}
580 
581 	return (-1);
582 }
583 
584 #ifndef _KERNEL
585 
586 /*
587  * Returns a string describing the set of acceptable values for the given
588  * zfs property, or NULL if it cannot be set.
589  */
590 const char *
591 zfs_prop_values(zfs_prop_t prop)
592 {
593 	if (zfs_prop_table[prop].pd_types == ZFS_TYPE_POOL)
594 		return (NULL);
595 
596 	return (zfs_prop_table[prop].pd_values);
597 }
598 
599 /*
600  * Returns a string describing the set of acceptable values for the given
601  * zpool property, or NULL if it cannot be set.
602  */
603 const char *
604 zpool_prop_values(zfs_prop_t prop)
605 {
606 	if (zfs_prop_table[prop].pd_types != ZFS_TYPE_POOL)
607 		return (NULL);
608 
609 	return (zfs_prop_table[prop].pd_values);
610 }
611 
612 /*
613  * Returns TRUE if this property is a string type.  Note that index types
614  * (compression, checksum) are treated as strings in userland, even though they
615  * are stored numerically on disk.
616  */
617 int
618 zfs_prop_is_string(zfs_prop_t prop)
619 {
620 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
621 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
622 }
623 
624 /*
625  * Returns the column header for the given property.  Used only in
626  * 'zfs list -o', but centralized here with the other property information.
627  */
628 const char *
629 zfs_prop_column_name(zfs_prop_t prop)
630 {
631 	return (zfs_prop_table[prop].pd_colname);
632 }
633 
634 /*
635  * Returns whether the given property should be displayed right-justified for
636  * 'zfs list'.
637  */
638 boolean_t
639 zfs_prop_align_right(zfs_prop_t prop)
640 {
641 	return (zfs_prop_table[prop].pd_rightalign);
642 }
643 
644 /*
645  * Determines the minimum width for the column, and indicates whether it's fixed
646  * or not.  Only string columns are non-fixed.
647  */
648 size_t
649 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
650 {
651 	prop_desc_t *pd = &zfs_prop_table[prop];
652 	zfs_index_t *idx;
653 	size_t ret;
654 	int i;
655 
656 	*fixed = B_TRUE;
657 
658 	/*
659 	 * Start with the width of the column name.
660 	 */
661 	ret = strlen(pd->pd_colname);
662 
663 	/*
664 	 * For fixed-width values, make sure the width is large enough to hold
665 	 * any possible value.
666 	 */
667 	switch (pd->pd_proptype) {
668 	case prop_type_number:
669 		/*
670 		 * The maximum length of a human-readable number is 5 characters
671 		 * ("20.4M", for example).
672 		 */
673 		if (ret < 5)
674 			ret = 5;
675 		/*
676 		 * 'creation' is handled specially because it's a number
677 		 * internally, but displayed as a date string.
678 		 */
679 		if (prop == ZFS_PROP_CREATION)
680 			*fixed = B_FALSE;
681 		break;
682 	case prop_type_boolean:
683 		/*
684 		 * The maximum length of a boolean value is 3 characters, for
685 		 * "off".
686 		 */
687 		if (ret < 3)
688 			ret = 3;
689 		break;
690 	case prop_type_index:
691 		idx = zfs_prop_index_table(prop);
692 		for (i = 0; idx[i].name != NULL; i++) {
693 			if (strlen(idx[i].name) > ret)
694 				ret = strlen(idx[i].name);
695 		}
696 		break;
697 
698 	case prop_type_string:
699 		*fixed = B_FALSE;
700 		break;
701 	}
702 
703 	return (ret);
704 }
705 
706 #endif
707