xref: /illumos-gate/usr/src/common/zfs/zfs_prop.c (revision a0965f35d4137b1f6bb1655ae1cb8fee88dfa66f)
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_SNAPSHOT | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
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, "<snapshot>", "ORIGIN", "%-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_default,
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 | ZFS_TYPE_SNAPSHOT,
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,	NULL,
122 	    prop_inherit,	ZFS_TYPE_ANY,
123 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", "%10s" },
124 	{ "compression", prop_type_index,	ZIO_COMPRESS_DEFAULT,	NULL,
125 	    prop_inherit,	ZFS_TYPE_ANY,
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_ANY, "on | off", "RDONLY", "%6s" },
140 	{ "zoned",	prop_type_boolean,	0,	NULL,	prop_inherit,
141 	    ZFS_TYPE_ANY,
142 	    "on | off", "ZONED", "%5s" },
143 	{ "snapdir",	prop_type_index,	ZFS_SNAPDIR_VISIBLE,	NULL,
144 	    prop_inherit,
145 	    ZFS_TYPE_FILESYSTEM,
146 	    "hidden | visible", "SNAPDIR", "%7s" },
147 	{ "aclmode", prop_type_index,	GROUPMASK,	 NULL,	prop_inherit,
148 	    ZFS_TYPE_FILESYSTEM,
149 	    "discard | groupmask | passthrough", "ACLMODE", "%11s" },
150 	{ "aclinherit", prop_type_index,	SECURE,	NULL, 	prop_inherit,
151 	    ZFS_TYPE_FILESYSTEM,
152 	    "discard | noallow | secure | passthrough", "ACLINHERIT", "%11s" },
153 	{ "createtxg",	prop_type_number,	0,	NULL,	prop_readonly,
154 	    ZFS_TYPE_ANY, NULL, NULL, NULL},
155 	{ "name",	prop_type_string,	0,	NULL,	prop_readonly,
156 	    ZFS_TYPE_ANY,
157 	    NULL, "NAME", "%-20s" },
158 };
159 
160 zfs_proptype_t
161 zfs_prop_get_type(zfs_prop_t prop)
162 {
163 	return (zfs_prop_table[prop].pd_proptype);
164 }
165 
166 /*
167  * Given a property name, returns the corresponding property ID.
168  */
169 zfs_prop_t
170 zfs_name_to_prop(const char *propname)
171 {
172 	int i;
173 
174 	for (i = 0; i < ZFS_NPROP_ALL; i++) {
175 		if (strcmp(zfs_prop_table[i].pd_name, propname) == 0)
176 			return (i);
177 #ifndef _KERNEL
178 		if (zfs_prop_table[i].pd_colname != NULL &&
179 		    strcasecmp(zfs_prop_table[i].pd_colname, propname) == 0)
180 			return (i);
181 #endif
182 	}
183 
184 	return (ZFS_PROP_INVAL);
185 }
186 
187 /*
188  * Return the default value for the given property.
189  */
190 void
191 zfs_prop_default_string(zfs_prop_t prop, char *buf, size_t buflen)
192 {
193 	/*
194 	 * For index types (compression and checksum), we want the numeric value
195 	 * in the kernel, but the string value in userland.  The kernel will
196 	 * call zfs_prop_default_numeric() based on the property type.  In
197 	 * userland, the zfs_prop_is_string() will return TRUE for index types,
198 	 * and we'll return "on" from this function.
199 	 */
200 	if (zfs_prop_table[prop].pd_proptype == prop_type_index)
201 		(void) strncpy(buf, "on", buflen);
202 	else
203 		(void) strncpy(buf, zfs_prop_table[prop].pd_strdefault, buflen);
204 }
205 
206 uint64_t
207 zfs_prop_default_numeric(zfs_prop_t prop)
208 {
209 	return (zfs_prop_table[prop].pd_numdefault);
210 }
211 
212 /*
213  * Returns TRUE if the property is readonly.
214  */
215 int
216 zfs_prop_readonly(zfs_prop_t prop)
217 {
218 	return (zfs_prop_table[prop].pd_attr == prop_readonly);
219 }
220 
221 #ifndef _KERNEL
222 /*
223  * Given a property ID, returns the corresponding name.
224  */
225 const char *
226 zfs_prop_to_name(zfs_prop_t prop)
227 {
228 	return (zfs_prop_table[prop].pd_name);
229 }
230 
231 /*
232  * Returns TRUE if the property is inheritable.
233  */
234 int
235 zfs_prop_inheritable(zfs_prop_t prop)
236 {
237 	return (zfs_prop_table[prop].pd_attr == prop_inherit);
238 }
239 
240 /*
241  * Returns TRUE if the property applies to the given dataset types.
242  */
243 int
244 zfs_prop_valid_for_type(zfs_prop_t prop, int types)
245 {
246 	return ((zfs_prop_table[prop].pd_types & types) != 0);
247 }
248 
249 /*
250  * Returns a string describing the set of acceptable values for the given
251  * property, or NULL if it cannot be set.
252  */
253 const char *
254 zfs_prop_values(zfs_prop_t prop)
255 {
256 	return (zfs_prop_table[prop].pd_values);
257 }
258 
259 /*
260  * Returns TRUE if this property is a string type.  Note that index types
261  * (compression, checksum) are treated as strings in userland, even though they
262  * are stored numerically on disk.
263  */
264 int
265 zfs_prop_is_string(zfs_prop_t prop)
266 {
267 	return (zfs_prop_table[prop].pd_proptype == prop_type_string ||
268 	    zfs_prop_table[prop].pd_proptype == prop_type_index);
269 }
270 
271 /*
272  * Returns the column header for the given property.  Used only in
273  * 'zfs list -o', but centralized here with the other property information.
274  */
275 const char *
276 zfs_prop_column_name(zfs_prop_t prop)
277 {
278 	return (zfs_prop_table[prop].pd_colname);
279 }
280 
281 /*
282  * Returns the column formatting for the given property.  Used only in
283  * 'zfs list -o', but centralized here with the other property information.
284  */
285 const char *
286 zfs_prop_column_format(zfs_prop_t prop)
287 {
288 	return (zfs_prop_table[prop].pd_colfmt);
289 }
290 
291 /*
292  * Returns an array of names suitable for passing to getsubopt() to determine
293  * the property index.
294  */
295 char **
296 zfs_prop_column_subopts(void)
297 {
298 	char **ret = malloc((ZFS_NPROP_ALL + 1) * sizeof (char *));
299 	int i;
300 
301 	for (i = 0; i < ZFS_NPROP_ALL; i++)
302 		ret[i] = (char *)zfs_prop_table[i].pd_name;
303 
304 	ret[i] = NULL;
305 
306 	return (ret);
307 }
308 
309 /*
310  * Same as above, but using the short (abbreviated) column names as indices.
311  */
312 char **
313 zfs_prop_column_short_subopts(void)
314 {
315 	char **ret = malloc((ZFS_NPROP_ALL + 1) * sizeof (char *) * 2);
316 	char *cur;
317 	int i;
318 
319 	for (i = 0; i < ZFS_NPROP_ALL; i++) {
320 		if (zfs_prop_table[i].pd_colname == NULL) {
321 			ret[i] = "";
322 		} else {
323 			ret[i] = strdup(zfs_prop_table[i].pd_colname);
324 			for (cur = ret[i]; *cur != '\0'; cur++)
325 				*cur = tolower(*cur);
326 		}
327 	}
328 
329 
330 	ret[i] = NULL;
331 
332 	return (ret);
333 }
334 
335 #endif
336