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