xref: /illumos-gate/usr/src/common/zfs/zfs_prop.c (revision 66e2aacc02a4625d105fb249ad16c27e79604ff2)
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", "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 };
180 
181 #define	ZFS_PROP_COUNT	((sizeof (zfs_prop_table))/(sizeof (prop_desc_t)))
182 
183 zfs_proptype_t
184 zfs_prop_get_type(zfs_prop_t prop)
185 {
186 	return (zfs_prop_table[prop].pd_proptype);
187 }
188 
189 static boolean_t
190 propname_match(const char *p, zfs_prop_t prop, size_t len)
191 {
192 	const char *propname = zfs_prop_table[prop].pd_name;
193 #ifndef _KERNEL
194 	const char *colname = zfs_prop_table[prop].pd_colname;
195 	int c;
196 #endif
197 
198 #ifndef _KERNEL
199 	if (colname == NULL)
200 		return (B_FALSE);
201 #endif
202 
203 	if (len == strlen(propname) &&
204 	    strncmp(p, propname, len) == 0)
205 		return (B_TRUE);
206 
207 #ifndef _KERNEL
208 	if (len != strlen(colname))
209 		return (B_FALSE);
210 
211 	for (c = 0; c < len; c++)
212 		if (p[c] != tolower(colname[c]))
213 			break;
214 
215 	return (colname[c] == '\0');
216 #else
217 	return (B_FALSE);
218 #endif
219 }
220 
221 zfs_prop_t
222 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data)
223 {
224 	const char *propname = cb_data;
225 
226 	if (propname_match(propname, prop, strlen(propname)))
227 		return (prop);
228 
229 	return (ZFS_PROP_CONT);
230 }
231 
232 /*
233  * Given a property name, returns the corresponding property ID.
234  */
235 zfs_prop_t
236 zfs_name_to_prop(const char *propname)
237 {
238 	zfs_prop_t prop;
239 
240 	prop = zfs_prop_iter(zfs_name_to_prop_cb, (void *)propname, B_TRUE);
241 	return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop);
242 }
243 
244 /*
245  * For user property names, we allow all lowercase alphanumeric characters, plus
246  * a few useful punctuation characters.
247  */
248 static int
249 valid_char(char c)
250 {
251 	return ((c >= 'a' && c <= 'z') ||
252 	    (c >= '0' && c <= '9') ||
253 	    c == '-' || c == '_' || c == '.' || c == ':');
254 }
255 
256 /*
257  * Returns true if this is a valid user-defined property (one with a ':').
258  */
259 boolean_t
260 zfs_prop_user(const char *name)
261 {
262 	int i;
263 	char c;
264 	boolean_t foundsep = B_FALSE;
265 
266 	for (i = 0; i < strlen(name); i++) {
267 		c = name[i];
268 		if (!valid_char(c))
269 			return (B_FALSE);
270 		if (c == ':')
271 			foundsep = B_TRUE;
272 	}
273 
274 	if (!foundsep)
275 		return (B_FALSE);
276 
277 	return (B_TRUE);
278 }
279 
280 /*
281  * Return the default value for the given property.
282  */
283 const char *
284 zfs_prop_default_string(zfs_prop_t prop)
285 {
286 	return (zfs_prop_table[prop].pd_strdefault);
287 }
288 
289 uint64_t
290 zfs_prop_default_numeric(zfs_prop_t prop)
291 {
292 	return (zfs_prop_table[prop].pd_numdefault);
293 }
294 
295 /*
296  * Returns TRUE if the property is readonly.
297  */
298 int
299 zfs_prop_readonly(zfs_prop_t prop)
300 {
301 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
302 }
303 
304 /*
305  * Given a property ID, returns the corresponding name.
306  */
307 const char *
308 zfs_prop_to_name(zfs_prop_t prop)
309 {
310 	return (zfs_prop_table[prop].pd_name);
311 }
312 
313 /*
314  * Returns TRUE if the property is inheritable.
315  */
316 int
317 zfs_prop_inheritable(zfs_prop_t prop)
318 {
319 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
320 }
321 
322 typedef struct zfs_index {
323 	const char *name;
324 	uint64_t index;
325 } zfs_index_t;
326 
327 static zfs_index_t checksum_table[] = {
328 	{ "on",		ZIO_CHECKSUM_ON },
329 	{ "off",	ZIO_CHECKSUM_OFF },
330 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
331 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
332 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
333 	{ NULL }
334 };
335 
336 static zfs_index_t compress_table[] = {
337 	{ "on",		ZIO_COMPRESS_ON },
338 	{ "off",	ZIO_COMPRESS_OFF },
339 	{ "lzjb",	ZIO_COMPRESS_LZJB },
340 	{ NULL }
341 };
342 
343 static zfs_index_t snapdir_table[] = {
344 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
345 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
346 	{ NULL }
347 };
348 
349 static zfs_index_t acl_mode_table[] = {
350 	{ "discard",	ZFS_ACL_DISCARD },
351 	{ "groupmask",	ZFS_ACL_GROUPMASK },
352 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
353 	{ NULL }
354 };
355 
356 static zfs_index_t acl_inherit_table[] = {
357 	{ "discard",	ZFS_ACL_DISCARD },
358 	{ "noallow",	ZFS_ACL_NOALLOW },
359 	{ "secure",	ZFS_ACL_SECURE },
360 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
361 	{ NULL }
362 };
363 
364 static zfs_index_t *
365 zfs_prop_index_table(zfs_prop_t prop)
366 {
367 	switch (prop) {
368 	case ZFS_PROP_CHECKSUM:
369 		return (checksum_table);
370 	case ZFS_PROP_COMPRESSION:
371 		return (compress_table);
372 	case ZFS_PROP_SNAPDIR:
373 		return (snapdir_table);
374 	case ZFS_PROP_ACLMODE:
375 		return (acl_mode_table);
376 	case ZFS_PROP_ACLINHERIT:
377 		return (acl_inherit_table);
378 	default:
379 		return (NULL);
380 	}
381 }
382 
383 
384 /*
385  * Tables of index types, plus functions to convert between the user view
386  * (strings) and internal representation (uint64_t).
387  */
388 int
389 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
390 {
391 	zfs_index_t *table;
392 	int i;
393 
394 	if ((table = zfs_prop_index_table(prop)) == NULL)
395 		return (-1);
396 
397 	for (i = 0; table[i].name != NULL; i++) {
398 		if (strcmp(string, table[i].name) == 0) {
399 			*index = table[i].index;
400 			return (0);
401 		}
402 	}
403 
404 	return (-1);
405 }
406 
407 int
408 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
409 {
410 	zfs_index_t *table;
411 	int i;
412 
413 	if ((table = zfs_prop_index_table(prop)) == NULL)
414 		return (-1);
415 
416 	for (i = 0; table[i].name != NULL; i++) {
417 		if (table[i].index == index) {
418 			*string = table[i].name;
419 			return (0);
420 		}
421 	}
422 
423 	return (-1);
424 }
425 
426 /*
427  * Determine if the specified property is visible or not.
428  */
429 boolean_t
430 zfs_prop_is_visible(zfs_prop_t prop)
431 {
432 	if (prop < 0)
433 		return (B_FALSE);
434 
435 	return (zfs_prop_table[prop].pd_visible);
436 }
437 
438 /*
439  * Iterate over all properties, calling back into the specified function
440  * for each property. We will continue to iterate until we either
441  * reach the end or the callback function something other than
442  * ZFS_PROP_CONT.
443  */
444 zfs_prop_t
445 zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all)
446 {
447 	int i;
448 
449 	for (i = 0; i < ZFS_PROP_COUNT; i++) {
450 		if (zfs_prop_is_visible(i) || show_all) {
451 			if (func(i, cb) != ZFS_PROP_CONT)
452 				return (i);
453 		}
454 	}
455 	return (ZFS_PROP_CONT);
456 }
457 
458 #ifndef _KERNEL
459 
460 /*
461  * Returns TRUE if the property applies to the given dataset types.
462  */
463 int
464 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
465 {
466 	return ((zfs_prop_table[prop].pd_types & types) != 0);
467 }
468 
469 /*
470  * Returns a string describing the set of acceptable values for the given
471  * property, or NULL if it cannot be set.
472  */
473 const char *
474 zfs_prop_values(zfs_prop_t prop)
475 {
476 	return (zfs_prop_table[prop].pd_values);
477 }
478 
479 /*
480  * Returns TRUE if this property is a string type.  Note that index types
481  * (compression, checksum) are treated as strings in userland, even though they
482  * are stored numerically on disk.
483  */
484 int
485 zfs_prop_is_string(zfs_prop_t prop)
486 {
487 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
488 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
489 }
490 
491 /*
492  * Returns the column header for the given property.  Used only in
493  * 'zfs list -o', but centralized here with the other property information.
494  */
495 const char *
496 zfs_prop_column_name(zfs_prop_t prop)
497 {
498 	return (zfs_prop_table[prop].pd_colname);
499 }
500 
501 /*
502  * Returns whether the given property should be displayed right-justified for
503  * 'zfs list'.
504  */
505 boolean_t
506 zfs_prop_align_right(zfs_prop_t prop)
507 {
508 	return (zfs_prop_table[prop].pd_rightalign);
509 }
510 
511 /*
512  * Determines the minimum width for the column, and indicates whether it's fixed
513  * or not.  Only string columns are non-fixed.
514  */
515 size_t
516 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
517 {
518 	prop_desc_t *pd = &zfs_prop_table[prop];
519 	zfs_index_t *idx;
520 	size_t ret;
521 	int i;
522 
523 	*fixed = B_TRUE;
524 
525 	/*
526 	 * Start with the width of the column name.
527 	 */
528 	ret = strlen(pd->pd_colname);
529 
530 	/*
531 	 * For fixed-width values, make sure the width is large enough to hold
532 	 * any possible value.
533 	 */
534 	switch (pd->pd_proptype) {
535 	case prop_type_number:
536 		/*
537 		 * The maximum length of a human-readable number is 5 characters
538 		 * ("20.4M", for example).
539 		 */
540 		if (ret < 5)
541 			ret = 5;
542 		/*
543 		 * 'creation' is handled specially because it's a number
544 		 * internally, but displayed as a date string.
545 		 */
546 		if (prop == ZFS_PROP_CREATION)
547 			*fixed = B_FALSE;
548 		break;
549 	case prop_type_boolean:
550 		/*
551 		 * The maximum length of a boolean value is 3 characters, for
552 		 * "off".
553 		 */
554 		if (ret < 3)
555 			ret = 3;
556 		break;
557 	case prop_type_index:
558 		idx = zfs_prop_index_table(prop);
559 		for (i = 0; idx[i].name != NULL; i++) {
560 			if (strlen(idx[i].name) > ret)
561 				ret = strlen(idx[i].name);
562 		}
563 		break;
564 
565 	case prop_type_string:
566 		*fixed = B_FALSE;
567 		break;
568 	}
569 
570 	return (ret);
571 }
572 
573 #endif
574