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