xref: /illumos-gate/usr/src/common/zfs/zfs_prop.c (revision 3d7072f8bd27709dba14f6fe336f149d25d9e207)
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 
57 #if defined(_KERNEL)
58 #include <sys/systm.h>
59 #else
60 #include <stdlib.h>
61 #include <string.h>
62 #include <ctype.h>
63 #endif
64 
65 typedef enum {
66 	prop_default,
67 	prop_readonly,
68 	prop_inherit
69 } prop_attr_t;
70 
71 typedef struct {
72 	const char	*pd_name;
73 	zfs_proptype_t	pd_proptype;
74 	uint64_t	pd_numdefault;
75 	const char	*pd_strdefault;
76 	prop_attr_t	pd_attr;
77 	int		pd_types;
78 	const char	*pd_values;
79 	const char	*pd_colname;
80 	boolean_t	pd_rightalign;
81 	boolean_t	pd_visible;
82 } prop_desc_t;
83 
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,
87 	    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,
94 	    B_TRUE },
95 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
96 	    ZFS_TYPE_ANY,
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,
100 	    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",
105 	    B_FALSE, B_TRUE },
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,
115 	    B_TRUE },
116 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
117 	    prop_inherit,
118 	    ZFS_TYPE_FILESYSTEM,
119 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE },
120 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
121 	    ZFS_TYPE_FILESYSTEM,
122 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE },
123 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
124 	    ZFS_TYPE_FILESYSTEM,
125 	    "on | off | share(1M) 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,
129 	    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,
134 	    ZFS_TYPE_FILESYSTEM,
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",
144 	    B_TRUE, B_TRUE },
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 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
149 	    ZFS_TYPE_FILESYSTEM,
150 	    "on | off", "ZONED", B_TRUE, B_TRUE },
151 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
152 	    prop_inherit,
153 	    ZFS_TYPE_FILESYSTEM,
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,
161 	    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,
167 	    ZFS_TYPE_FILESYSTEM,
168 	    "on | off", "CANMOUNT", B_TRUE, B_TRUE },
169 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
170 	    ZFS_TYPE_ANY,
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 },
184 	{ "autoreplace", prop_type_boolean,	0,	NULL, prop_default,
185 	    ZFS_TYPE_POOL, "on | off", "REPLACE", B_FALSE, B_TRUE },
186 };
187 
188 #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
189 
190 /*
191  * Returns TRUE if the property applies to the given dataset types.
192  */
193 int
194 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
195 {
196 	return ((zfs_prop_table[prop].pd_types & types) != 0);
197 }
198 
199 /*
200  * Determine if the specified property is visible or not.
201  */
202 boolean_t
203 zfs_prop_is_visible(zfs_prop_t prop)
204 {
205 	if (prop < 0)
206 		return (B_FALSE);
207 
208 	return (zfs_prop_table[prop].pd_visible);
209 }
210 
211 /*
212  * Iterate over all properties, calling back into the specified function
213  * for each property. We will continue to iterate until we either
214  * reach the end or the callback function something other than
215  * ZFS_PROP_CONT.
216  */
217 zfs_prop_t
218 zfs_prop_iter_common(zfs_prop_f func, void *cb, zfs_type_t type,
219     boolean_t show_all)
220 {
221 	int i;
222 
223 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
224 		if (zfs_prop_valid_for_type(i, type) &&
225 		    (zfs_prop_is_visible(i) || show_all)) {
226 			if (func(i, cb) != ZFS_PROP_CONT)
227 				return (i);
228 		}
229 	}
230 	return (ZFS_PROP_CONT);
231 }
232 
233 zfs_prop_t
234 zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
235 {
236 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_ANY, show_all));
237 }
238 
239 zpool_prop_t
240 zpool_prop_iter(zpool_prop_f func, void *cb, boolean_t show_all)
241 {
242 	return (zfs_prop_iter_common(func, cb, ZFS_TYPE_POOL, show_all));
243 }
244 
245 zfs_proptype_t
246 zfs_prop_get_type(zfs_prop_t prop)
247 {
248 	return (zfs_prop_table[prop].pd_proptype);
249 }
250 
251 zfs_proptype_t
252 zpool_prop_get_type(zfs_prop_t prop)
253 {
254 	return (zfs_prop_table[prop].pd_proptype);
255 }
256 
257 static boolean_t
258 propname_match(const char *p, zfs_prop_t prop, size_t len)
259 {
260 	const char *propname = zfs_prop_table[prop].pd_name;
261 #ifndef _KERNEL
262 	const char *colname = zfs_prop_table[prop].pd_colname;
263 	int c;
264 #endif
265 
266 #ifndef _KERNEL
267 	if (colname == NULL)
268 		return (B_FALSE);
269 #endif
270 
271 	if (len == strlen(propname) &&
272 	    strncmp(p, propname, len) == 0)
273 		return (B_TRUE);
274 
275 #ifndef _KERNEL
276 	if (len != strlen(colname))
277 		return (B_FALSE);
278 
279 	for (c = 0; c < len; c++)
280 		if (p[c] != tolower(colname[c]))
281 			break;
282 
283 	return (colname[c] == '\0');
284 #else
285 	return (B_FALSE);
286 #endif
287 }
288 
289 zfs_prop_t
290 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
291 {
292 	const char *propname = cb_data;
293 
294 	if (propname_match(propname, prop, strlen(propname)))
295 		return (prop);
296 
297 	return (ZFS_PROP_CONT);
298 }
299 
300 /*
301  * Given a property name and its type, returns the corresponding property ID.
302  */
303 zfs_prop_t
304 zfs_name_to_prop_common(const char *propname, zfs_type_t type)
305 {
306 	zfs_prop_t prop;
307 
308 	prop = zfs_prop_iter_common(zfs_name_to_prop_cb, (void *)propname,
309 	    type, B_TRUE);
310 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
311 }
312 
313 /*
314  * Given a zfs dataset property name, returns the corresponding property ID.
315  */
316 zfs_prop_t
317 zfs_name_to_prop(const char *propname)
318 {
319 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_ANY));
320 }
321 
322 /*
323  * Given a pool property name, returns the corresponding property ID.
324  */
325 zpool_prop_t
326 zpool_name_to_prop(const char *propname)
327 {
328 	return (zfs_name_to_prop_common(propname, ZFS_TYPE_POOL));
329 }
330 
331 /*
332  * For user property names, we allow all lowercase alphanumeric characters, plus
333  * a few useful punctuation characters.
334  */
335 static int
336 valid_char(char c)
337 {
338 	return ((c >= 'a' && c <= 'z') ||
339 	    (c >= '0' && c <= '9') ||
340 	    c == '-' || c == '_' || c == '.' || c == ':');
341 }
342 
343 /*
344  * Returns true if this is a valid user-defined property (one with a ':').
345  */
346 boolean_t
347 zfs_prop_user(const char *name)
348 {
349 	int i;
350 	char c;
351 	boolean_t foundsep = B_FALSE;
352 
353 	for (i = 0; i < strlen(name); i++) {
354 		c = name[i];
355 		if (!valid_char(c))
356 			return (B_FALSE);
357 		if (c == ':')
358 			foundsep = B_TRUE;
359 	}
360 
361 	if (!foundsep)
362 		return (B_FALSE);
363 
364 	return (B_TRUE);
365 }
366 
367 /*
368  * Return the default value for the given property.
369  */
370 const char *
371 zfs_prop_default_string(zfs_prop_t prop)
372 {
373 	return (zfs_prop_table[prop].pd_strdefault);
374 }
375 
376 const char *
377 zpool_prop_default_string(zpool_prop_t prop)
378 {
379 	return (zfs_prop_table[prop].pd_strdefault);
380 }
381 
382 uint64_t
383 zfs_prop_default_numeric(zfs_prop_t prop)
384 {
385 	return (zfs_prop_table[prop].pd_numdefault);
386 }
387 
388 uint64_t
389 zpool_prop_default_numeric(zpool_prop_t prop)
390 {
391 	return (zfs_prop_table[prop].pd_numdefault);
392 }
393 
394 /*
395  * Returns TRUE if the property is readonly.
396  */
397 int
398 zfs_prop_readonly(zfs_prop_t prop)
399 {
400 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
401 }
402 
403 /*
404  * Given a dataset property ID, returns the corresponding name.
405  * Assuming the zfs dataset property ID is valid.
406  */
407 const char *
408 zfs_prop_to_name(zfs_prop_t prop)
409 {
410 	return (zfs_prop_table[prop].pd_name);
411 }
412 
413 /*
414  * Given a pool property ID, returns the corresponding name.
415  * Assuming the pool property ID is valid.
416  */
417 const char *
418 zpool_prop_to_name(zpool_prop_t prop)
419 {
420 	return (zfs_prop_table[prop].pd_name);
421 }
422 
423 /*
424  * Returns TRUE if the property is inheritable.
425  */
426 int
427 zfs_prop_inheritable(zfs_prop_t prop)
428 {
429 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
430 }
431 
432 typedef struct zfs_index {
433 	const char *name;
434 	uint64_t index;
435 } zfs_index_t;
436 
437 static zfs_index_t checksum_table[] = {
438 	{ "on",		ZIO_CHECKSUM_ON },
439 	{ "off",	ZIO_CHECKSUM_OFF },
440 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
441 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
442 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
443 	{ NULL }
444 };
445 
446 static zfs_index_t compress_table[] = {
447 	{ "on",		ZIO_COMPRESS_ON },
448 	{ "off",	ZIO_COMPRESS_OFF },
449 	{ "lzjb",	ZIO_COMPRESS_LZJB },
450 	{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* the default gzip level */
451 	{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
452 	{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
453 	{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
454 	{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
455 	{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
456 	{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
457 	{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
458 	{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
459 	{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
460 	{ NULL }
461 };
462 
463 static zfs_index_t snapdir_table[] = {
464 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
465 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
466 	{ NULL }
467 };
468 
469 static zfs_index_t acl_mode_table[] = {
470 	{ "discard",	ZFS_ACL_DISCARD },
471 	{ "groupmask",	ZFS_ACL_GROUPMASK },
472 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
473 	{ NULL }
474 };
475 
476 static zfs_index_t acl_inherit_table[] = {
477 	{ "discard",	ZFS_ACL_DISCARD },
478 	{ "noallow",	ZFS_ACL_NOALLOW },
479 	{ "secure",	ZFS_ACL_SECURE },
480 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
481 	{ NULL }
482 };
483 
484 static zfs_index_t copies_table[] = {
485 	{ "1",	1 },
486 	{ "2",	2 },
487 	{ "3",	3 },
488 	{ NULL }
489 };
490 
491 static zfs_index_t *
492 zfs_prop_index_table(zfs_prop_t prop)
493 {
494 	switch (prop) {
495 	case ZFS_PROP_CHECKSUM:
496 		return (checksum_table);
497 	case ZFS_PROP_COMPRESSION:
498 		return (compress_table);
499 	case ZFS_PROP_SNAPDIR:
500 		return (snapdir_table);
501 	case ZFS_PROP_ACLMODE:
502 		return (acl_mode_table);
503 	case ZFS_PROP_ACLINHERIT:
504 		return (acl_inherit_table);
505 	case ZFS_PROP_COPIES:
506 		return (copies_table);
507 	default:
508 		return (NULL);
509 	}
510 }
511 
512 
513 /*
514  * Tables of index types, plus functions to convert between the user view
515  * (strings) and internal representation (uint64_t).
516  */
517 int
518 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
519 {
520 	zfs_index_t *table;
521 	int i;
522 
523 	if ((table = zfs_prop_index_table(prop)) == NULL)
524 		return (-1);
525 
526 	for (i = 0; table[i].name != NULL; i++) {
527 		if (strcmp(string, table[i].name) == 0) {
528 			*index = table[i].index;
529 			return (0);
530 		}
531 	}
532 
533 	return (-1);
534 }
535 
536 int
537 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
538 {
539 	zfs_index_t *table;
540 	int i;
541 
542 	if ((table = zfs_prop_index_table(prop)) == NULL)
543 		return (-1);
544 
545 	for (i = 0; table[i].name != NULL; i++) {
546 		if (table[i].index == index) {
547 			*string = table[i].name;
548 			return (0);
549 		}
550 	}
551 
552 	return (-1);
553 }
554 
555 #ifndef _KERNEL
556 
557 /*
558  * Returns a string describing the set of acceptable values for the given
559  * zfs property, or NULL if it cannot be set.
560  */
561 const char *
562 zfs_prop_values(zfs_prop_t prop)
563 {
564 	if (zfs_prop_table[prop].pd_types == ZFS_TYPE_POOL)
565 		return (NULL);
566 
567 	return (zfs_prop_table[prop].pd_values);
568 }
569 
570 /*
571  * Returns a string describing the set of acceptable values for the given
572  * zpool property, or NULL if it cannot be set.
573  */
574 const char *
575 zpool_prop_values(zfs_prop_t prop)
576 {
577 	if (zfs_prop_table[prop].pd_types != ZFS_TYPE_POOL)
578 		return (NULL);
579 
580 	return (zfs_prop_table[prop].pd_values);
581 }
582 
583 /*
584  * Returns TRUE if this property is a string type.  Note that index types
585  * (compression, checksum) are treated as strings in userland, even though they
586  * are stored numerically on disk.
587  */
588 int
589 zfs_prop_is_string(zfs_prop_t prop)
590 {
591 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
592 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
593 }
594 
595 /*
596  * Returns the column header for the given property.  Used only in
597  * 'zfs list -o', but centralized here with the other property information.
598  */
599 const char *
600 zfs_prop_column_name(zfs_prop_t prop)
601 {
602 	return (zfs_prop_table[prop].pd_colname);
603 }
604 
605 /*
606  * Returns whether the given property should be displayed right-justified for
607  * 'zfs list'.
608  */
609 boolean_t
610 zfs_prop_align_right(zfs_prop_t prop)
611 {
612 	return (zfs_prop_table[prop].pd_rightalign);
613 }
614 
615 /*
616  * Determines the minimum width for the column, and indicates whether it's fixed
617  * or not.  Only string columns are non-fixed.
618  */
619 size_t
620 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
621 {
622 	prop_desc_t *pd = &zfs_prop_table[prop];
623 	zfs_index_t *idx;
624 	size_t ret;
625 	int i;
626 
627 	*fixed = B_TRUE;
628 
629 	/*
630 	 * Start with the width of the column name.
631 	 */
632 	ret = strlen(pd->pd_colname);
633 
634 	/*
635 	 * For fixed-width values, make sure the width is large enough to hold
636 	 * any possible value.
637 	 */
638 	switch (pd->pd_proptype) {
639 	case prop_type_number:
640 		/*
641 		 * The maximum length of a human-readable number is 5 characters
642 		 * ("20.4M", for example).
643 		 */
644 		if (ret < 5)
645 			ret = 5;
646 		/*
647 		 * 'creation' is handled specially because it's a number
648 		 * internally, but displayed as a date string.
649 		 */
650 		if (prop == ZFS_PROP_CREATION)
651 			*fixed = B_FALSE;
652 		break;
653 	case prop_type_boolean:
654 		/*
655 		 * The maximum length of a boolean value is 3 characters, for
656 		 * "off".
657 		 */
658 		if (ret < 3)
659 			ret = 3;
660 		break;
661 	case prop_type_index:
662 		idx = zfs_prop_index_table(prop);
663 		for (i = 0; idx[i].name != NULL; i++) {
664 			if (strlen(idx[i].name) > ret)
665 				ret = strlen(idx[i].name);
666 		}
667 		break;
668 
669 	case prop_type_string:
670 		*fixed = B_FALSE;
671 		break;
672 	}
673 
674 	return (ret);
675 }
676 
677 #endif
678