xref: /illumos-gate/usr/src/common/zfs/zfs_prop.c (revision 3bb79bece53191f2cf27aa61a72ea1784a7ce700)
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 #ifndef _KERNEL
177 	if (colname == NULL)
178 		return (0);
179 #endif
180 
181 	if (len == strlen(propname) &&
182 	    strncmp(p, propname, len) == 0)
183 		return (1);
184 
185 #ifndef _KERNEL
186 	if (len != strlen(colname))
187 		return (0);
188 
189 	for (c = 0; c < len; c++)
190 		if (p[c] != tolower(colname[c]))
191 			break;
192 
193 	return (colname[c] == '\0');
194 #else
195 	return (0);
196 #endif
197 }
198 
199 
200 /*
201  * Given a property name, returns the corresponding property ID.
202  */
203 zfs_prop_t
204 zfs_name_to_prop(const char *propname)
205 {
206 	int i;
207 
208 	for (i = 0; i < ZFS_NPROP_ALL; i++) {
209 		if (propname_match(propname, i, strlen(propname)))
210 			return (i);
211 	}
212 
213 	return (ZFS_PROP_INVAL);
214 }
215 
216 /*
217  * Return the default value for the given property.
218  */
219 const char *
220 zfs_prop_default_string(zfs_prop_t prop)
221 {
222 	return (zfs_prop_table[prop].pd_strdefault);
223 }
224 
225 uint64_t
226 zfs_prop_default_numeric(zfs_prop_t prop)
227 {
228 	return (zfs_prop_table[prop].pd_numdefault);
229 }
230 
231 /*
232  * Returns TRUE if the property is readonly.
233  */
234 int
235 zfs_prop_readonly(zfs_prop_t prop)
236 {
237 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
238 }
239 
240 #ifndef _KERNEL
241 /*
242  * Given a property ID, returns the corresponding name.
243  */
244 const char *
245 zfs_prop_to_name(zfs_prop_t prop)
246 {
247 	return (zfs_prop_table[prop].pd_name);
248 }
249 
250 /*
251  * Returns TRUE if the property is inheritable.
252  */
253 int
254 zfs_prop_inheritable(zfs_prop_t prop)
255 {
256 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
257 }
258 
259 /*
260  * Returns TRUE if the property applies to the given dataset types.
261  */
262 int
263 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
264 {
265 	return ((zfs_prop_table[prop].pd_types & types) != 0);
266 }
267 
268 /*
269  * Returns a string describing the set of acceptable values for the given
270  * property, or NULL if it cannot be set.
271  */
272 const char *
273 zfs_prop_values(zfs_prop_t prop)
274 {
275 	return (zfs_prop_table[prop].pd_values);
276 }
277 
278 /*
279  * Returns TRUE if this property is a string type.  Note that index types
280  * (compression, checksum) are treated as strings in userland, even though they
281  * are stored numerically on disk.
282  */
283 int
284 zfs_prop_is_string(zfs_prop_t prop)
285 {
286 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
287 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
288 }
289 
290 /*
291  * Returns the column header for the given property.  Used only in
292  * 'zfs list -o', but centralized here with the other property information.
293  */
294 const char *
295 zfs_prop_column_name(zfs_prop_t prop)
296 {
297 	return (zfs_prop_table[prop].pd_colname);
298 }
299 
300 /*
301  * Returns the column formatting for the given property.  Used only in
302  * 'zfs list -o', but centralized here with the other property information.
303  */
304 const char *
305 zfs_prop_column_format(zfs_prop_t prop)
306 {
307 	return (zfs_prop_table[prop].pd_colfmt);
308 }
309 
310 /*
311  * Given a comma-separated list of fields, fills in the specified array with a
312  * list of properties.  The keyword "all" can be used to specify all properties.
313  * The 'count' parameter returns the number of matching properties placed into
314  * the list.  The 'props' parameter must point to an array of at least
315  * ZFS_NPROP_ALL elements.
316  */
317 int
318 zfs_get_proplist(char *fields, zfs_prop_t *props, int max,
319     int *count, char **badopt)
320 {
321 	int i;
322 	size_t len;
323 	char *s, *p;
324 
325 	*count = 0;
326 
327 	/*
328 	 * Check for the special value "all", which indicates all properties
329 	 * should be displayed.
330 	 */
331 	if (strcmp(fields, "all") == 0) {
332 		if (max < ZFS_NPROP_VISIBLE)
333 			return (EOVERFLOW);
334 
335 		for (i = 0; i < ZFS_NPROP_VISIBLE; i++)
336 			props[i] = i;
337 		*count = ZFS_NPROP_VISIBLE;
338 		return (0);
339 	}
340 
341 	/*
342 	 * It would be nice to use getsubopt() here, but the inclusion of column
343 	 * aliases makes this more effort than it's worth.
344 	 */
345 	s = fields;
346 	while (*s != '\0') {
347 		if ((p = strchr(s, ',')) == NULL) {
348 			len = strlen(s);
349 			p = s + len;
350 		} else {
351 			len = p - s;
352 		}
353 
354 		/*
355 		 * Check for empty options.
356 		 */
357 		if (len == 0) {
358 			*badopt = "";
359 			return (EINVAL);
360 		}
361 
362 		/*
363 		 * Check all regular property names.
364 		 */
365 		for (i = 0; i < ZFS_NPROP_ALL; i++) {
366 			if (propname_match(s, i, len))
367 				break;
368 		}
369 
370 		/*
371 		 * If no column is specified, then return failure, setting
372 		 * 'badopt' to point to the invalid option.
373 		 */
374 		if (i == ZFS_NPROP_ALL) {
375 			s[len] = '\0';
376 			*badopt = s;
377 			return (EINVAL);
378 		}
379 
380 		/*
381 		 * If the user specified too many options (by using the same one
382 		 * multiple times). return an error with 'badopt' set to NULL to
383 		 * indicate this condition.
384 		 */
385 		if (*count == max)
386 			return (EOVERFLOW);
387 
388 		props[*count] = i;
389 		*count += 1;
390 
391 		s = p;
392 		if (*s == ',')
393 			s++;
394 	}
395 
396 	/*
397 	 * If no fields were specified, return an error.
398 	 */
399 	if (*count == 0) {
400 		*badopt = "";
401 		return (EINVAL);
402 	}
403 
404 	return (0);
405 }
406 
407 #endif
408