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