xref: /titanic_44/usr/src/common/zfs/zfs_prop.c (revision 164c0dd6f561db19bdaf1d0b7f2a8dec44355b69)
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 	const char	*pd_colfmt;
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", "%10s" },
86 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
87 	    ZFS_TYPE_ANY, "<date>", "CREATION", "%-20s" },
88 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
89 	    ZFS_TYPE_ANY, "<size>",	"USED", "%5s" },
90 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
91 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", "%5s" },
92 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
93 	    ZFS_TYPE_ANY,
94 	    "<size>", "REFER", "%5s" },
95 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
96 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", "%5s" },
97 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
98 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", "%7s" },
99 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
100 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
101 	    "%-20s" },
102 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
103 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", "%5s" },
104 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
105 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
106 	    "<size> | none", "RESERV", "%6s" },
107 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
108 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", "%7s" },
109 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
110 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", "%8s" },
111 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
112 	    prop_inherit,
113 	    ZFS_TYPE_FILESYSTEM,
114 	    "512 to 128k, power of 2", "RECSIZE", "%7s" },
115 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
116 	    ZFS_TYPE_FILESYSTEM,
117 	    "<path> | legacy | none", "MOUNTPOINT", "%-20s" },
118 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
119 	    ZFS_TYPE_FILESYSTEM,
120 	    "on | off | share(1M) options", "SHARENFS", "%-15s" },
121 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
122 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
123 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", "%10s" },
124 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
125 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
126 	    "on | off | lzjb", "COMPRESS", "%8s" },
127 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
128 	    ZFS_TYPE_FILESYSTEM,
129 	    "on | off", "ATIME", "%5s" },
130 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
131 	    ZFS_TYPE_FILESYSTEM,
132 	    "on | off", "DEVICES", "%7s" },
133 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
134 	    ZFS_TYPE_FILESYSTEM,
135 	    "on | off", "EXEC", "%4s" },
136 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
137 	    ZFS_TYPE_FILESYSTEM, "on | off", "SETUID", "%6s" },
138 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
139 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
140 	    "on | off", "RDONLY", "%6s" },
141 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
142 	    ZFS_TYPE_FILESYSTEM,
143 	    "on | off", "ZONED", "%5s" },
144 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_HIDDEN, "hidden",
145 	    prop_inherit,
146 	    ZFS_TYPE_FILESYSTEM,
147 	    "hidden | visible", "SNAPDIR", "%7s" },
148 	{ "aclmode", prop_type_index,	GROUPMASK, "groupmask", prop_inherit,
149 	    ZFS_TYPE_FILESYSTEM,
150 	    "discard | groupmask | passthrough", "ACLMODE", "%11s" },
151 	{ "aclinherit", prop_type_index,	SECURE,	"secure", prop_inherit,
152 	    ZFS_TYPE_FILESYSTEM,
153 	    "discard | noallow | secure | passthrough", "ACLINHERIT", "%11s" },
154 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
155 	    ZFS_TYPE_ANY, NULL, NULL, NULL},
156 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
157 	    ZFS_TYPE_ANY,
158 	    NULL, "NAME", "%-20s" },
159 };
160 
161 zfs_proptype_t
162 zfs_prop_get_type(zfs_prop_t prop)
163 {
164 	return (zfs_prop_table[prop].pd_proptype);
165 }
166 
167 static int
168 propname_match(const char *p, int prop, size_t len)
169 {
170 	const char *propname = zfs_prop_table[prop].pd_name;
171 #ifndef _KERNEL
172 	const char *colname = zfs_prop_table[prop].pd_colname;
173 	int c;
174 #endif
175 
176 	if (len == strlen(propname) &&
177 	    strncmp(p, propname, len) == 0)
178 		return (1);
179 
180 #ifndef _KERNEL
181 	if (colname == NULL || len != strlen(colname))
182 		return (0);
183 
184 	for (c = 0; c < len; c++)
185 		if (p[c] != tolower(colname[c]))
186 			break;
187 
188 	return (colname[c] == '\0');
189 #else
190 	return (0);
191 #endif
192 }
193 
194 
195 /*
196  * Given a property name, returns the corresponding property ID.
197  */
198 zfs_prop_t
199 zfs_name_to_prop(const char *propname)
200 {
201 	int i;
202 
203 	for (i = 0; i < ZFS_NPROP_ALL; i++) {
204 		if (propname_match(propname, i, strlen(propname)))
205 			return (i);
206 	}
207 
208 	return (ZFS_PROP_INVAL);
209 }
210 
211 /*
212  * Return the default value for the given property.
213  */
214 const char *
215 zfs_prop_default_string(zfs_prop_t prop)
216 {
217 	return (zfs_prop_table[prop].pd_strdefault);
218 }
219 
220 uint64_t
221 zfs_prop_default_numeric(zfs_prop_t prop)
222 {
223 	return (zfs_prop_table[prop].pd_numdefault);
224 }
225 
226 /*
227  * Returns TRUE if the property is readonly.
228  */
229 int
230 zfs_prop_readonly(zfs_prop_t prop)
231 {
232 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
233 }
234 
235 #ifndef _KERNEL
236 /*
237  * Given a property ID, returns the corresponding name.
238  */
239 const char *
240 zfs_prop_to_name(zfs_prop_t prop)
241 {
242 	return (zfs_prop_table[prop].pd_name);
243 }
244 
245 /*
246  * Returns TRUE if the property is inheritable.
247  */
248 int
249 zfs_prop_inheritable(zfs_prop_t prop)
250 {
251 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
252 }
253 
254 /*
255  * Returns TRUE if the property applies to the given dataset types.
256  */
257 int
258 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
259 {
260 	return ((zfs_prop_table[prop].pd_types & types) != 0);
261 }
262 
263 /*
264  * Returns a string describing the set of acceptable values for the given
265  * property, or NULL if it cannot be set.
266  */
267 const char *
268 zfs_prop_values(zfs_prop_t prop)
269 {
270 	return (zfs_prop_table[prop].pd_values);
271 }
272 
273 /*
274  * Returns TRUE if this property is a string type.  Note that index types
275  * (compression, checksum) are treated as strings in userland, even though they
276  * are stored numerically on disk.
277  */
278 int
279 zfs_prop_is_string(zfs_prop_t prop)
280 {
281 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
282 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
283 }
284 
285 /*
286  * Returns the column header for the given property.  Used only in
287  * 'zfs list -o', but centralized here with the other property information.
288  */
289 const char *
290 zfs_prop_column_name(zfs_prop_t prop)
291 {
292 	return (zfs_prop_table[prop].pd_colname);
293 }
294 
295 /*
296  * Returns the column formatting for the given property.  Used only in
297  * 'zfs list -o', but centralized here with the other property information.
298  */
299 const char *
300 zfs_prop_column_format(zfs_prop_t prop)
301 {
302 	return (zfs_prop_table[prop].pd_colfmt);
303 }
304 
305 /*
306  * Given a comma-separated list of fields, fills in the specified array with a
307  * list of properties.  The keyword "all" can be used to specify all properties.
308  * The 'count' parameter returns the number of matching properties placed into
309  * the list.  The 'props' parameter must point to an array of at least
310  * ZFS_NPROP_ALL elements.
311  */
312 int
313 zfs_get_proplist(char *fields, zfs_prop_t *props, int max,
314     int *count, char **badopt)
315 {
316 	int i;
317 	size_t len;
318 	char *s, *p;
319 
320 	*count = 0;
321 
322 	/*
323 	 * Check for the special value "all", which indicates all properties
324 	 * should be displayed.
325 	 */
326 	if (strcmp(fields, "all") == 0) {
327 		if (max < ZFS_NPROP_VISIBLE)
328 			return (EOVERFLOW);
329 
330 		for (i = 0; i < ZFS_NPROP_VISIBLE; i++)
331 			props[i] = i;
332 		*count = ZFS_NPROP_VISIBLE;
333 		return (0);
334 	}
335 
336 	/*
337 	 * It would be nice to use getsubopt() here, but the inclusion of column
338 	 * aliases makes this more effort than it's worth.
339 	 */
340 	s = fields;
341 	while (*s != '\0') {
342 		if ((p = strchr(s, ',')) == NULL) {
343 			len = strlen(s);
344 			p = s + len;
345 		} else {
346 			len = p - s;
347 		}
348 
349 		/*
350 		 * Check for empty options.
351 		 */
352 		if (len == 0) {
353 			*badopt = "";
354 			return (EINVAL);
355 		}
356 
357 		/*
358 		 * Check all regular property names.
359 		 */
360 		for (i = 0; i < ZFS_NPROP_ALL; i++) {
361 			if (propname_match(s, i, len))
362 				break;
363 		}
364 
365 		/*
366 		 * If no column is specified, then return failure, setting
367 		 * 'badopt' to point to the invalid option.
368 		 */
369 		if (i == ZFS_NPROP_ALL) {
370 			s[len] = '\0';
371 			*badopt = s;
372 			return (EINVAL);
373 		}
374 
375 		/*
376 		 * If the user specified too many options (by using the same one
377 		 * multiple times). return an error with 'badopt' set to NULL to
378 		 * indicate this condition.
379 		 */
380 		if (*count == max)
381 			return (EOVERFLOW);
382 
383 		props[*count] = i;
384 		*count += 1;
385 
386 		s = p;
387 		if (*s == ',')
388 			s++;
389 	}
390 
391 	/*
392 	 * If no fields were specified, return an error.
393 	 */
394 	if (*count == 0) {
395 		*badopt = "";
396 		return (EINVAL);
397 	}
398 
399 	return (0);
400 }
401 
402 #endif
403