xref: /titanic_44/usr/src/common/zfs/zfs_prop.c (revision ee519a1f9541a20bb76ef306dfc8e5616f8a5e26)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Master property table.
31  *
32  * This table keeps track of all the properties supported by ZFS, and their
33  * various attributes.  Not all of these are needed by the kernel, and several
34  * are only used by a single libzfs client.  But having them here centralizes
35  * all property information in one location.
36  *
37  * 	name		The human-readable string representing this property
38  * 	proptype	Basic type (string, boolean, number)
39  * 	default		Default value for the property.  Sadly, C only allows
40  * 			you to initialize the first member of a union, so we
41  * 			have two default members for each property.
42  * 	attr		Attributes (readonly, inheritable) for the property
43  * 	types		Valid dataset types to which this applies
44  * 	values		String describing acceptable values for the property
45  * 	colname		The column header for 'zfs list'
46  *	colfmt		The column formatting for 'zfs list'
47  *
48  * This table must match the order of property types in libzfs.h.
49  */
50 
51 #include <sys/zio.h>
52 #include <sys/spa.h>
53 #include <sys/zfs_acl.h>
54 #include <sys/zfs_ioctl.h>
55 
56 #include "zfs_prop.h"
57 
58 #if defined(_KERNEL)
59 #include <sys/systm.h>
60 #else
61 #include <stdlib.h>
62 #include <string.h>
63 #include <ctype.h>
64 #endif
65 
66 typedef enum {
67 	prop_default,
68 	prop_readonly,
69 	prop_inherit
70 } prop_attr_t;
71 
72 typedef struct {
73 	const char	*pd_name;
74 	zfs_proptype_t	pd_proptype;
75 	uint64_t	pd_numdefault;
76 	const char	*pd_strdefault;
77 	prop_attr_t	pd_attr;
78 	int		pd_types;
79 	const char	*pd_values;
80 	const char	*pd_colname;
81 	const char	*pd_colfmt;
82 } prop_desc_t;
83 
84 static prop_desc_t zfs_prop_table[ZFS_NPROP_ALL] = {
85 	{ "type",	prop_type_string,	0,	NULL,	prop_readonly,
86 	    ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", "%10s" },
87 	{ "creation",	prop_type_number,	0,	NULL,	prop_readonly,
88 	    ZFS_TYPE_ANY, "<date>", "CREATION", "%-20s" },
89 	{ "used",	prop_type_number,	0,	NULL,	prop_readonly,
90 	    ZFS_TYPE_ANY, "<size>",	"USED", "%5s" },
91 	{ "available",	prop_type_number,	0,	NULL,	prop_readonly,
92 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", "%5s" },
93 	{ "referenced",	prop_type_number,	0,	NULL,	prop_readonly,
94 	    ZFS_TYPE_ANY,
95 	    "<size>", "REFER", "%5s" },
96 	{ "compressratio", prop_type_number,	0,	NULL,	prop_readonly,
97 	    ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", "%5s" },
98 	{ "mounted",	prop_type_boolean,	0,	NULL,	prop_readonly,
99 	    ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", "%7s" },
100 	{ "origin",	prop_type_string,	0,	NULL,	prop_readonly,
101 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN",
102 	    "%-20s" },
103 	{ "quota",	prop_type_number,	0,	NULL,	prop_default,
104 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", "%5s" },
105 	{ "reservation", prop_type_number,	0,	NULL,	prop_default,
106 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
107 	    "<size> | none", "RESERV", "%6s" },
108 	{ "volsize",	prop_type_number,	0,	NULL,	prop_default,
109 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", "%7s" },
110 	{ "volblocksize", prop_type_number,	8192,	NULL,	prop_readonly,
111 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK", "%8s" },
112 	{ "recordsize",	prop_type_number,	SPA_MAXBLOCKSIZE,	NULL,
113 	    prop_inherit,
114 	    ZFS_TYPE_FILESYSTEM,
115 	    "512 to 128k, power of 2", "RECSIZE", "%7s" },
116 	{ "mountpoint",	prop_type_string,	0,	"/",	prop_inherit,
117 	    ZFS_TYPE_FILESYSTEM,
118 	    "<path> | legacy | none", "MOUNTPOINT", "%-20s" },
119 	{ "sharenfs",	prop_type_string,	0,	"off",	prop_inherit,
120 	    ZFS_TYPE_FILESYSTEM,
121 	    "on | off | share(1M) options", "SHARENFS", "%-15s" },
122 	{ "checksum",	prop_type_index,	ZIO_CHECKSUM_DEFAULT,	"on",
123 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
124 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", "%10s" },
125 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	"off",
126 	    prop_inherit,	ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
127 	    "on | off | lzjb", "COMPRESS", "%8s" },
128 	{ "atime",	prop_type_boolean,	1,	NULL,	prop_inherit,
129 	    ZFS_TYPE_FILESYSTEM,
130 	    "on | off", "ATIME", "%5s" },
131 	{ "devices",	prop_type_boolean,	1,	NULL,	prop_inherit,
132 	    ZFS_TYPE_FILESYSTEM,
133 	    "on | off", "DEVICES", "%7s" },
134 	{ "exec",	prop_type_boolean,	1,	NULL,	prop_inherit,
135 	    ZFS_TYPE_FILESYSTEM,
136 	    "on | off", "EXEC", "%4s" },
137 	{ "setuid",	prop_type_boolean,	1,	NULL,	prop_inherit,
138 	    ZFS_TYPE_FILESYSTEM, "on | off", "SETUID", "%6s" },
139 	{ "readonly",	prop_type_boolean,	0,	NULL,	prop_inherit,
140 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
141 	    "on | off", "RDONLY", "%6s" },
142 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
143 	    ZFS_TYPE_FILESYSTEM,
144 	    "on | off", "ZONED", "%5s" },
145 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_VISIBLE, "visible",
146 	    prop_inherit,
147 	    ZFS_TYPE_FILESYSTEM,
148 	    "hidden | visible", "SNAPDIR", "%7s" },
149 	{ "aclmode", prop_type_index,	GROUPMASK, "groupmask", prop_inherit,
150 	    ZFS_TYPE_FILESYSTEM,
151 	    "discard | groupmask | passthrough", "ACLMODE", "%11s" },
152 	{ "aclinherit", prop_type_index,	SECURE,	"secure", prop_inherit,
153 	    ZFS_TYPE_FILESYSTEM,
154 	    "discard | noallow | secure | passthrough", "ACLINHERIT", "%11s" },
155 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
156 	    ZFS_TYPE_ANY, NULL, NULL, NULL},
157 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
158 	    ZFS_TYPE_ANY,
159 	    NULL, "NAME", "%-20s" },
160 };
161 
162 zfs_proptype_t
163 zfs_prop_get_type(zfs_prop_t prop)
164 {
165 	return (zfs_prop_table[prop].pd_proptype);
166 }
167 
168 static int
169 propname_match(const char *p, int prop, size_t len)
170 {
171 	const char *propname = zfs_prop_table[prop].pd_name;
172 #ifndef _KERNEL
173 	const char *colname = zfs_prop_table[prop].pd_colname;
174 	int c;
175 #endif
176 
177 	if (len == strlen(propname) &&
178 	    strncmp(p, propname, len) == 0)
179 		return (1);
180 
181 #ifndef _KERNEL
182 	if (colname == NULL || len != strlen(colname))
183 		return (0);
184 
185 	for (c = 0; c < len; c++)
186 		if (p[c] != tolower(colname[c]))
187 			break;
188 
189 	return (colname[c] == '\0');
190 #else
191 	return (0);
192 #endif
193 }
194 
195 
196 /*
197  * Given a property name, returns the corresponding property ID.
198  */
199 zfs_prop_t
200 zfs_name_to_prop(const char *propname)
201 {
202 	int i;
203 
204 	for (i = 0; i < ZFS_NPROP_ALL; i++) {
205 		if (propname_match(propname, i, strlen(propname)))
206 			return (i);
207 	}
208 
209 	return (ZFS_PROP_INVAL);
210 }
211 
212 /*
213  * Return the default value for the given property.
214  */
215 const char *
216 zfs_prop_default_string(zfs_prop_t prop)
217 {
218 	return (zfs_prop_table[prop].pd_strdefault);
219 }
220 
221 uint64_t
222 zfs_prop_default_numeric(zfs_prop_t prop)
223 {
224 	return (zfs_prop_table[prop].pd_numdefault);
225 }
226 
227 /*
228  * Returns TRUE if the property is readonly.
229  */
230 int
231 zfs_prop_readonly(zfs_prop_t prop)
232 {
233 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
234 }
235 
236 #ifndef _KERNEL
237 /*
238  * Given a property ID, returns the corresponding name.
239  */
240 const char *
241 zfs_prop_to_name(zfs_prop_t prop)
242 {
243 	return (zfs_prop_table[prop].pd_name);
244 }
245 
246 /*
247  * Returns TRUE if the property is inheritable.
248  */
249 int
250 zfs_prop_inheritable(zfs_prop_t prop)
251 {
252 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
253 }
254 
255 /*
256  * Returns TRUE if the property applies to the given dataset types.
257  */
258 int
259 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
260 {
261 	return ((zfs_prop_table[prop].pd_types & types) != 0);
262 }
263 
264 /*
265  * Returns a string describing the set of acceptable values for the given
266  * property, or NULL if it cannot be set.
267  */
268 const char *
269 zfs_prop_values(zfs_prop_t prop)
270 {
271 	return (zfs_prop_table[prop].pd_values);
272 }
273 
274 /*
275  * Returns TRUE if this property is a string type.  Note that index types
276  * (compression, checksum) are treated as strings in userland, even though they
277  * are stored numerically on disk.
278  */
279 int
280 zfs_prop_is_string(zfs_prop_t prop)
281 {
282 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
283 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
284 }
285 
286 /*
287  * Returns the column header for the given property.  Used only in
288  * 'zfs list -o', but centralized here with the other property information.
289  */
290 const char *
291 zfs_prop_column_name(zfs_prop_t prop)
292 {
293 	return (zfs_prop_table[prop].pd_colname);
294 }
295 
296 /*
297  * Returns the column formatting for the given property.  Used only in
298  * 'zfs list -o', but centralized here with the other property information.
299  */
300 const char *
301 zfs_prop_column_format(zfs_prop_t prop)
302 {
303 	return (zfs_prop_table[prop].pd_colfmt);
304 }
305 
306 /*
307  * Given a comma-separated list of fields, fills in the specified array with a
308  * list of properties.  The keyword "all" can be used to specify all properties.
309  * The 'count' parameter returns the number of matching properties placed into
310  * the list.  The 'props' parameter must point to an array of at least
311  * ZFS_NPROP_ALL elements.
312  */
313 int
314 zfs_get_proplist(char *fields, zfs_prop_t *props, int max,
315     int *count, char **badopt)
316 {
317 	int i;
318 	size_t len;
319 	char *s, *p;
320 
321 	*count = 0;
322 
323 	/*
324 	 * Check for the special value "all", which indicates all properties
325 	 * should be displayed.
326 	 */
327 	if (strcmp(fields, "all") == 0) {
328 		if (max < ZFS_NPROP_VISIBLE)
329 			return (EOVERFLOW);
330 
331 		for (i = 0; i < ZFS_NPROP_VISIBLE; i++)
332 			props[i] = i;
333 		*count = ZFS_NPROP_VISIBLE;
334 		return (0);
335 	}
336 
337 	/*
338 	 * It would be nice to use getsubopt() here, but the inclusion of column
339 	 * aliases makes this more effort than it's worth.
340 	 */
341 	s = fields;
342 	while (*s != '\0') {
343 		if ((p = strchr(s, ',')) == NULL) {
344 			len = strlen(s);
345 			p = s + len;
346 		} else {
347 			len = p - s;
348 		}
349 
350 		/*
351 		 * Check for empty options.
352 		 */
353 		if (len == 0) {
354 			*badopt = "";
355 			return (EINVAL);
356 		}
357 
358 		/*
359 		 * Check all regular property names.
360 		 */
361 		for (i = 0; i < ZFS_NPROP_ALL; i++) {
362 			if (propname_match(s, i, len))
363 				break;
364 		}
365 
366 		/*
367 		 * If no column is specified, then return failure, setting
368 		 * 'badopt' to point to the invalid option.
369 		 */
370 		if (i == ZFS_NPROP_ALL) {
371 			s[len] = '\0';
372 			*badopt = s;
373 			return (EINVAL);
374 		}
375 
376 		/*
377 		 * If the user specified too many options (by using the same one
378 		 * multiple times). return an error with 'badopt' set to NULL to
379 		 * indicate this condition.
380 		 */
381 		if (*count == max)
382 			return (EOVERFLOW);
383 
384 		props[*count] = i;
385 		*count += 1;
386 
387 		s = p;
388 		if (*s == ',')
389 			s++;
390 	}
391 
392 	/*
393 	 * If no fields were specified, return an error.
394 	 */
395 	if (*count == 0) {
396 		*badopt = "";
397 		return (EINVAL);
398 	}
399 
400 	return (0);
401 }
402 
403 #endif
404