xref: /illumos-gate/usr/src/common/zfs/zfs_prop.c (revision 7b55fa8ea6046becb3b72f8886a503979c322084)
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 2006 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 } prop_desc_t;
82 
83 static prop_desc_t zfs_prop_table[ZFS_NPROP_ALL] = {
84 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
85 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE },
86 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
87 	    ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE },
88 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
89 	    ZFS_TYPE_ANY, "<size>",	"USED", B_TRUE },
90 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
91 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE },
92 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
93 	    ZFS_TYPE_ANY,
94 	    "<size>", "REFER", B_TRUE },
95 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
96 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE },
97 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
98 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE },
99 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
100 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
101 	    B_FALSE },
102 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
103 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE },
104 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
105 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
106 	    "<size> | none", "RESERV", B_TRUE },
107 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
108 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE },
109 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
110 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", B_TRUE },
111 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
112 	    prop_inherit,
113 	    ZFS_TYPE_FILESYSTEM,
114 	    "512 to 128k, power of 2", "RECSIZE", B_TRUE },
115 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
116 	    ZFS_TYPE_FILESYSTEM,
117 	    "<path> | legacy | none", "MOUNTPOINT", B_FALSE },
118 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
119 	    ZFS_TYPE_FILESYSTEM,
120 	    "on | off | share(1M) options", "SHARENFS", B_FALSE },
121 	{ "shareiscsi",	prop_type_string,	0,	"off",	prop_inherit,
122 	    ZFS_TYPE_ANY,
123 	    "on | off | type=<type>", "SHAREISCSI", B_FALSE },
124 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
125 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
126 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE },
127 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
128 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
129 	    "on | off | lzjb", "COMPRESS", B_TRUE },
130 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
131 	    ZFS_TYPE_FILESYSTEM,
132 	    "on | off", "ATIME", B_TRUE },
133 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
134 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
135 	    "on | off", "DEVICES", B_TRUE },
136 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
137 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
138 	    "on | off", "EXEC", B_TRUE },
139 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
140 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
141 	    B_TRUE },
142 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
143 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
144 	    "on | off", "RDONLY", B_TRUE },
145 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
146 	    ZFS_TYPE_FILESYSTEM,
147 	    "on | off", "ZONED", B_TRUE },
148 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
149 	    prop_inherit,
150 	    ZFS_TYPE_FILESYSTEM,
151 	    "hidden | visible", "SNAPDIR", B_TRUE },
152 	{ "aclmode", prop_type_index,	ZFS_ACL_GROUPMASK, "groupmask",
153 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
154 	    "discard | groupmask | passthrough", "ACLMODE", B_TRUE },
155 	{ "aclinherit", prop_type_index,	ZFS_ACL_SECURE,	"secure",
156 	    prop_inherit, ZFS_TYPE_FILESYSTEM,
157 	    "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE },
158 	{ "canmount",	prop_type_boolean,	1,	NULL,	prop_default,
159 	    ZFS_TYPE_FILESYSTEM,
160 	    "on | off", "CANMOUNT", B_TRUE },
161 	{ "xattr",	prop_type_boolean,	1,	NULL,	prop_inherit,
162 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
163 	    "on | off", "XATTR", B_TRUE },
164 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
165 	    ZFS_TYPE_ANY, NULL, NULL, B_FALSE},
166 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
167 	    ZFS_TYPE_ANY, NULL, "NAME", B_FALSE },
168 	{ "iscsioptions", prop_type_string,	0,	NULL,	prop_inherit,
169 	    ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE },
170 };
171 
172 zfs_proptype_t
173 zfs_prop_get_type(zfs_prop_t prop)
174 {
175 	return (zfs_prop_table[prop].pd_proptype);
176 }
177 
178 static boolean_t
179 propname_match(const char *p, zfs_prop_t prop, size_t len)
180 {
181 	const char *propname = zfs_prop_table[prop].pd_name;
182 #ifndef _KERNEL
183 	const char *colname = zfs_prop_table[prop].pd_colname;
184 	int c;
185 #endif
186 
187 #ifndef _KERNEL
188 	if (colname == NULL)
189 		return (B_FALSE);
190 #endif
191 
192 	if (len == strlen(propname) &&
193 	    strncmp(p, propname, len) == 0)
194 		return (B_TRUE);
195 
196 #ifndef _KERNEL
197 	if (len != strlen(colname))
198 		return (B_FALSE);
199 
200 	for (c = 0; c < len; c++)
201 		if (p[c] != tolower(colname[c]))
202 			break;
203 
204 	return (colname[c] == '\0');
205 #else
206 	return (B_FALSE);
207 #endif
208 }
209 
210 
211 /*
212  * Given a property name, returns the corresponding property ID.
213  */
214 zfs_prop_t
215 zfs_name_to_prop(const char *propname)
216 {
217 	int i;
218 
219 	for (i = 0; i < ZFS_NPROP_ALL; i++) {
220 		if (propname_match(propname, i, strlen(propname)))
221 			return (i);
222 	}
223 
224 	return (ZFS_PROP_INVAL);
225 }
226 
227 /*
228  * For user property names, we allow all lowercase alphanumeric characters, plus
229  * a few useful punctuation characters.
230  */
231 static int
232 valid_char(char c)
233 {
234 	return ((c >= 'a' && c <= 'z') ||
235 	    (c >= '0' && c <= '9') ||
236 	    c == '-' || c == '_' || c == '.' || c == ':');
237 }
238 
239 /*
240  * Returns true if this is a valid user-defined property (one with a ':').
241  */
242 boolean_t
243 zfs_prop_user(const char *name)
244 {
245 	int i;
246 	char c;
247 	boolean_t foundsep = B_FALSE;
248 
249 	for (i = 0; i < strlen(name); i++) {
250 		c = name[i];
251 		if (!valid_char(c))
252 			return (B_FALSE);
253 		if (c == ':')
254 			foundsep = B_TRUE;
255 	}
256 
257 	if (!foundsep)
258 		return (B_FALSE);
259 
260 	return (B_TRUE);
261 }
262 
263 /*
264  * Return the default value for the given property.
265  */
266 const char *
267 zfs_prop_default_string(zfs_prop_t prop)
268 {
269 	return (zfs_prop_table[prop].pd_strdefault);
270 }
271 
272 uint64_t
273 zfs_prop_default_numeric(zfs_prop_t prop)
274 {
275 	return (zfs_prop_table[prop].pd_numdefault);
276 }
277 
278 /*
279  * Returns TRUE if the property is readonly.
280  */
281 int
282 zfs_prop_readonly(zfs_prop_t prop)
283 {
284 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
285 }
286 
287 /*
288  * Given a property ID, returns the corresponding name.
289  */
290 const char *
291 zfs_prop_to_name(zfs_prop_t prop)
292 {
293 	return (zfs_prop_table[prop].pd_name);
294 }
295 
296 /*
297  * Returns TRUE if the property is inheritable.
298  */
299 int
300 zfs_prop_inheritable(zfs_prop_t prop)
301 {
302 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
303 }
304 
305 typedef struct zfs_index {
306 	const char *name;
307 	uint64_t index;
308 } zfs_index_t;
309 
310 static zfs_index_t checksum_table[] = {
311 	{ "on",		ZIO_CHECKSUM_ON },
312 	{ "off",	ZIO_CHECKSUM_OFF },
313 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
314 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
315 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
316 	{ NULL }
317 };
318 
319 static zfs_index_t compress_table[] = {
320 	{ "on",		ZIO_COMPRESS_ON },
321 	{ "off",	ZIO_COMPRESS_OFF },
322 	{ "lzjb",	ZIO_COMPRESS_LZJB },
323 	{ NULL }
324 };
325 
326 static zfs_index_t snapdir_table[] = {
327 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
328 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
329 	{ NULL }
330 };
331 
332 static zfs_index_t acl_mode_table[] = {
333 	{ "discard",	ZFS_ACL_DISCARD },
334 	{ "groupmask",	ZFS_ACL_GROUPMASK },
335 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
336 	{ NULL }
337 };
338 
339 static zfs_index_t acl_inherit_table[] = {
340 	{ "discard",	ZFS_ACL_DISCARD },
341 	{ "noallow",	ZFS_ACL_NOALLOW },
342 	{ "secure",	ZFS_ACL_SECURE },
343 	{ "passthrough", ZFS_ACL_PASSTHROUGH },
344 	{ NULL }
345 };
346 
347 static zfs_index_t *
348 zfs_prop_index_table(zfs_prop_t prop)
349 {
350 	switch (prop) {
351 	case ZFS_PROP_CHECKSUM:
352 		return (checksum_table);
353 	case ZFS_PROP_COMPRESSION:
354 		return (compress_table);
355 	case ZFS_PROP_SNAPDIR:
356 		return (snapdir_table);
357 	case ZFS_PROP_ACLMODE:
358 		return (acl_mode_table);
359 	case ZFS_PROP_ACLINHERIT:
360 		return (acl_inherit_table);
361 	default:
362 		return (NULL);
363 	}
364 }
365 
366 
367 /*
368  * Tables of index types, plus functions to convert between the user view
369  * (strings) and internal representation (uint64_t).
370  */
371 int
372 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
373 {
374 	zfs_index_t *table;
375 	int i;
376 
377 	if ((table = zfs_prop_index_table(prop)) == NULL)
378 		return (-1);
379 
380 	for (i = 0; table[i].name != NULL; i++) {
381 		if (strcmp(string, table[i].name) == 0) {
382 			*index = table[i].index;
383 			return (0);
384 		}
385 	}
386 
387 	return (-1);
388 }
389 
390 int
391 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
392 {
393 	zfs_index_t *table;
394 	int i;
395 
396 	if ((table = zfs_prop_index_table(prop)) == NULL)
397 		return (-1);
398 
399 	for (i = 0; table[i].name != NULL; i++) {
400 		if (table[i].index == index) {
401 			*string = table[i].name;
402 			return (0);
403 		}
404 	}
405 
406 	return (-1);
407 }
408 
409 #ifndef _KERNEL
410 
411 /*
412  * Returns TRUE if the property applies to the given dataset types.
413  */
414 int
415 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
416 {
417 	return ((zfs_prop_table[prop].pd_types & types) != 0);
418 }
419 
420 /*
421  * Returns a string describing the set of acceptable values for the given
422  * property, or NULL if it cannot be set.
423  */
424 const char *
425 zfs_prop_values(zfs_prop_t prop)
426 {
427 	return (zfs_prop_table[prop].pd_values);
428 }
429 
430 /*
431  * Returns TRUE if this property is a string type.  Note that index types
432  * (compression, checksum) are treated as strings in userland, even though they
433  * are stored numerically on disk.
434  */
435 int
436 zfs_prop_is_string(zfs_prop_t prop)
437 {
438 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
439 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
440 }
441 
442 /*
443  * Returns the column header for the given property.  Used only in
444  * 'zfs list -o', but centralized here with the other property information.
445  */
446 const char *
447 zfs_prop_column_name(zfs_prop_t prop)
448 {
449 	return (zfs_prop_table[prop].pd_colname);
450 }
451 
452 /*
453  * Returns whether the given property should be displayed right-justified for
454  * 'zfs list'.
455  */
456 boolean_t
457 zfs_prop_align_right(zfs_prop_t prop)
458 {
459 	return (zfs_prop_table[prop].pd_rightalign);
460 }
461 
462 /*
463  * Determines the minimum width for the column, and indicates whether it's fixed
464  * or not.  Only string columns are non-fixed.
465  */
466 size_t
467 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed)
468 {
469 	prop_desc_t *pd = &zfs_prop_table[prop];
470 	zfs_index_t *idx;
471 	size_t ret;
472 	int i;
473 
474 	*fixed = B_TRUE;
475 
476 	/*
477 	 * Start with the width of the column name.
478 	 */
479 	ret = strlen(pd->pd_colname);
480 
481 	/*
482 	 * For fixed-width values, make sure the width is large enough to hold
483 	 * any possible value.
484 	 */
485 	switch (pd->pd_proptype) {
486 	case prop_type_number:
487 		/*
488 		 * The maximum length of a human-readable number is 5 characters
489 		 * ("20.4M", for example).
490 		 */
491 		if (ret < 5)
492 			ret = 5;
493 		/*
494 		 * 'creation' is handled specially because it's a number
495 		 * internally, but displayed as a date string.
496 		 */
497 		if (prop == ZFS_PROP_CREATION)
498 			*fixed = B_FALSE;
499 		break;
500 	case prop_type_boolean:
501 		/*
502 		 * The maximum length of a boolean value is 3 characters, for
503 		 * "off".
504 		 */
505 		if (ret < 3)
506 			ret = 3;
507 		break;
508 	case prop_type_index:
509 		idx = zfs_prop_index_table(prop);
510 		for (i = 0; idx[i].name != NULL; i++) {
511 			if (strlen(idx[i].name) > ret)
512 				ret = strlen(idx[i].name);
513 		}
514 		break;
515 
516 	case prop_type_string:
517 		*fixed = B_FALSE;
518 		break;
519 	}
520 
521 	return (ret);
522 }
523 
524 #endif
525