xref: /titanic_51/usr/src/common/zfs/zfs_prop.c (revision 9a09d68d9dc5e0bdb1fbc9945e1091efa737f98f)
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 2007 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 #include <sys/zio.h>
29 #include <sys/spa.h>
30 #include <sys/zfs_acl.h>
31 #include <sys/zfs_ioctl.h>
32 #include <sys/zfs_znode.h>
33 #include <sys/zfs_i18n.h>
34 
35 #include "zfs_prop.h"
36 #include "zfs_deleg.h"
37 
38 #if defined(_KERNEL)
39 #include <sys/systm.h>
40 #else
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #endif
45 
46 static zprop_desc_t zfs_prop_table[ZFS_NUM_PROPS];
47 
48 zprop_desc_t *
49 zfs_prop_get_table(void)
50 {
51 	return (zfs_prop_table);
52 }
53 
54 void
55 zfs_prop_init(void)
56 {
57 	static zprop_index_t checksum_table[] = {
58 		{ "on",		ZIO_CHECKSUM_ON },
59 		{ "off",	ZIO_CHECKSUM_OFF },
60 		{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
61 		{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
62 		{ "sha256",	ZIO_CHECKSUM_SHA256 },
63 		{ NULL }
64 	};
65 
66 	static zprop_index_t compress_table[] = {
67 		{ "on",		ZIO_COMPRESS_ON },
68 		{ "off",	ZIO_COMPRESS_OFF },
69 		{ "lzjb",	ZIO_COMPRESS_LZJB },
70 		{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* gzip default */
71 		{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
72 		{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
73 		{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
74 		{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
75 		{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
76 		{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
77 		{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
78 		{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
79 		{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
80 		{ NULL }
81 	};
82 
83 	static zprop_index_t snapdir_table[] = {
84 		{ "hidden",	ZFS_SNAPDIR_HIDDEN },
85 		{ "visible",	ZFS_SNAPDIR_VISIBLE },
86 		{ NULL }
87 	};
88 
89 	static zprop_index_t acl_mode_table[] = {
90 		{ "discard",	ZFS_ACL_DISCARD },
91 		{ "groupmask",	ZFS_ACL_GROUPMASK },
92 		{ "passthrough", ZFS_ACL_PASSTHROUGH },
93 		{ NULL }
94 	};
95 
96 	static zprop_index_t acl_inherit_table[] = {
97 		{ "discard",	ZFS_ACL_DISCARD },
98 		{ "noallow",	ZFS_ACL_NOALLOW },
99 		{ "secure",	ZFS_ACL_SECURE },
100 		{ "passthrough", ZFS_ACL_PASSTHROUGH },
101 		{ NULL }
102 	};
103 
104 	static zprop_index_t case_table[] = {
105 		{ "sensitive",		ZFS_CASE_SENSITIVE },
106 		{ "insensitive",	ZFS_CASE_INSENSITIVE },
107 		{ "mixed",		ZFS_CASE_MIXED },
108 		{ NULL }
109 	};
110 
111 	static zprop_index_t copies_table[] = {
112 		{ "1",		1 },
113 		{ "2",		2 },
114 		{ "3",		3 },
115 		{ NULL }
116 	};
117 
118 	static zprop_index_t normalize_table[] = {
119 		{ "none",	ZFS_NORMALIZE_NONE },
120 		{ "formD",	ZFS_NORMALIZE_D },
121 		{ "formKC",	ZFS_NORMALIZE_KC },
122 		{ "formC",	ZFS_NORMALIZE_C },
123 		{ "formKD",	ZFS_NORMALIZE_KD },
124 		{ NULL }
125 	};
126 
127 	static zprop_index_t version_table[] = {
128 		{ "1",		1 },
129 		{ "2",		2 },
130 		{ "3",		3 },
131 		{ "current",	ZPL_VERSION },
132 		{ NULL }
133 	};
134 
135 	static zprop_index_t boolean_table[] = {
136 		{ "off",	0 },
137 		{ "on",		1 },
138 		{ NULL }
139 	};
140 
141 	/* inherit index properties */
142 	register_index(ZFS_PROP_CHECKSUM, "checksum", ZIO_CHECKSUM_DEFAULT,
143 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
144 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM",
145 	    checksum_table);
146 	register_index(ZFS_PROP_COMPRESSION, "compression",
147 	    ZIO_COMPRESS_DEFAULT, PROP_INHERIT,
148 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
149 	    "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", compress_table);
150 	register_index(ZFS_PROP_SNAPDIR, "snapdir", ZFS_SNAPDIR_HIDDEN,
151 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
152 	    "hidden | visible", "SNAPDIR", snapdir_table);
153 	register_index(ZFS_PROP_ACLMODE, "aclmode", ZFS_ACL_GROUPMASK,
154 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
155 	    "discard | groupmask | passthrough", "ACLMODE", acl_mode_table);
156 	register_index(ZFS_PROP_ACLINHERIT, "aclinherit", ZFS_ACL_SECURE,
157 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
158 	    "discard | noallow | secure | passthrough", "ACLINHERIT",
159 	    acl_inherit_table);
160 	register_index(ZFS_PROP_COPIES, "copies", 1,
161 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
162 	    "1 | 2 | 3", "COPIES", copies_table);
163 
164 	/* inherit index (boolean) properties */
165 	register_index(ZFS_PROP_ATIME, "atime", 1, PROP_INHERIT,
166 	    ZFS_TYPE_FILESYSTEM, "on | off", "ATIME", boolean_table);
167 	register_index(ZFS_PROP_DEVICES, "devices", 1, PROP_INHERIT,
168 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "DEVICES",
169 	    boolean_table);
170 	register_index(ZFS_PROP_EXEC, "exec", 1, PROP_INHERIT,
171 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "EXEC",
172 	    boolean_table);
173 	register_index(ZFS_PROP_SETUID, "setuid", 1, PROP_INHERIT,
174 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
175 	    boolean_table);
176 	register_index(ZFS_PROP_READONLY, "readonly", 0, PROP_INHERIT,
177 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "on | off", "RDONLY",
178 	    boolean_table);
179 	register_index(ZFS_PROP_ZONED, "zoned", 0, PROP_INHERIT,
180 	    ZFS_TYPE_FILESYSTEM, "on | off", "ZONED", boolean_table);
181 	register_index(ZFS_PROP_XATTR, "xattr", 1, PROP_INHERIT,
182 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "XATTR",
183 	    boolean_table);
184 	register_index(ZFS_PROP_VSCAN, "vscan", 0, PROP_INHERIT,
185 	    ZFS_TYPE_FILESYSTEM, "on | off", "VSCAN",
186 	    boolean_table);
187 	register_index(ZFS_PROP_NBMAND, "nbmand", 0, PROP_INHERIT,
188 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "NBMAND",
189 	    boolean_table);
190 
191 	/* default index properties */
192 	register_index(ZFS_PROP_VERSION, "version", 0, PROP_DEFAULT,
193 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
194 	    "1 | 2 | 3 | current", "VERSION", version_table);
195 
196 	/* default index (boolean) properties */
197 	register_index(ZFS_PROP_CANMOUNT, "canmount", 1, PROP_DEFAULT,
198 	    ZFS_TYPE_FILESYSTEM, "on | off", "CANMOUNT", boolean_table);
199 
200 	/* readonly index (boolean) properties */
201 	register_index(ZFS_PROP_MOUNTED, "mounted", 0, PROP_READONLY,
202 	    ZFS_TYPE_FILESYSTEM, "yes | no", "MOUNTED", boolean_table);
203 
204 	/* set once index properties */
205 	register_index(ZFS_PROP_NORMALIZE, "normalization", ZFS_NORMALIZE_NONE,
206 	    PROP_ONETIME, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
207 	    "none | formC | formD | formKC | formKD", "NORMALIZATION",
208 	    normalize_table);
209 	register_index(ZFS_PROP_CASE, "casesensitivity", ZFS_CASE_SENSITIVE,
210 	    PROP_ONETIME, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
211 	    "sensitive | insensitive | mixed", "CASE", case_table);
212 
213 	/* set once index (boolean) properties */
214 	register_index(ZFS_PROP_UTF8ONLY, "utf8only", 0, PROP_ONETIME,
215 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
216 	    "on | off", "UTF8ONLY", boolean_table);
217 
218 	/* string properties */
219 	register_string(ZFS_PROP_ORIGIN, "origin", NULL, PROP_READONLY,
220 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN");
221 	register_string(ZFS_PROP_MOUNTPOINT, "mountpoint", "/", PROP_INHERIT,
222 	    ZFS_TYPE_FILESYSTEM, "<path> | legacy | none", "MOUNTPOINT");
223 	register_string(ZFS_PROP_SHARENFS, "sharenfs", "off", PROP_INHERIT,
224 	    ZFS_TYPE_FILESYSTEM, "on | off | share(1M) options", "SHARENFS");
225 	register_string(ZFS_PROP_SHAREISCSI, "shareiscsi", "off", PROP_INHERIT,
226 	    ZFS_TYPE_DATASET, "on | off | type=<type>", "SHAREISCSI");
227 	register_string(ZFS_PROP_TYPE, "type", NULL, PROP_READONLY,
228 	    ZFS_TYPE_DATASET, "filesystem | volume | snapshot", "TYPE");
229 	register_string(ZFS_PROP_SHARESMB, "sharesmb", "off", PROP_INHERIT,
230 	    ZFS_TYPE_FILESYSTEM, "on | off | sharemgr(1M) options", "SHARESMB");
231 
232 	/* readonly number properties */
233 	register_number(ZFS_PROP_USED, "used", 0, PROP_READONLY,
234 	    ZFS_TYPE_DATASET, "<size>", "USED");
235 	register_number(ZFS_PROP_AVAILABLE, "available", 0, PROP_READONLY,
236 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL");
237 	register_number(ZFS_PROP_REFERENCED, "referenced", 0, PROP_READONLY,
238 	    ZFS_TYPE_DATASET, "<size>", "REFER");
239 	register_number(ZFS_PROP_COMPRESSRATIO, "compressratio", 0,
240 	    PROP_READONLY, ZFS_TYPE_DATASET,
241 	    "<1.00x or higher if compressed>", "RATIO");
242 	register_number(ZFS_PROP_VOLBLOCKSIZE, "volblocksize", 8192,
243 	    PROP_ONETIME,
244 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK");
245 
246 	/* default number properties */
247 	register_number(ZFS_PROP_QUOTA, "quota", 0, PROP_DEFAULT,
248 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA");
249 	register_number(ZFS_PROP_RESERVATION, "reservation", 0, PROP_DEFAULT,
250 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size> | none", "RESERV");
251 	register_number(ZFS_PROP_VOLSIZE, "volsize", 0, PROP_DEFAULT,
252 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE");
253 	register_number(ZFS_PROP_REFQUOTA, "refquota", 0, PROP_DEFAULT,
254 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "REFQUOTA");
255 	register_number(ZFS_PROP_REFRESERVATION, "refreservation", 0,
256 	    PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
257 	    "<size> | none", "REFRESERV");
258 
259 	/* inherit number properties */
260 	register_number(ZFS_PROP_RECORDSIZE, "recordsize", SPA_MAXBLOCKSIZE,
261 	    PROP_INHERIT,
262 	    ZFS_TYPE_FILESYSTEM, "512 to 128k, power of 2", "RECSIZE");
263 
264 	/* hidden properties */
265 	register_hidden(ZFS_PROP_CREATETXG, "createtxg", PROP_TYPE_NUMBER,
266 	    PROP_READONLY, ZFS_TYPE_DATASET, NULL);
267 	register_hidden(ZFS_PROP_NUMCLONES, "numclones", PROP_TYPE_NUMBER,
268 	    PROP_READONLY, ZFS_TYPE_SNAPSHOT, NULL);
269 	register_hidden(ZFS_PROP_NAME, "name", PROP_TYPE_STRING,
270 	    PROP_READONLY, ZFS_TYPE_DATASET, "NAME");
271 	register_hidden(ZFS_PROP_ISCSIOPTIONS, "iscsioptions", PROP_TYPE_STRING,
272 	    PROP_INHERIT, ZFS_TYPE_VOLUME, "ISCSIOPTIONS");
273 
274 	/* oddball properties */
275 	register_impl(ZFS_PROP_CREATION, "creation", PROP_TYPE_NUMBER, 0, NULL,
276 	    PROP_READONLY, ZFS_TYPE_DATASET,
277 	    "<date>", "CREATION", B_FALSE, B_TRUE, NULL);
278 }
279 
280 boolean_t
281 zfs_prop_delegatable(zfs_prop_t prop)
282 {
283 	zprop_desc_t *pd = &zfs_prop_table[prop];
284 	return (pd->pd_attr != PROP_READONLY);
285 }
286 
287 /*
288  * Given a zfs dataset property name, returns the corresponding property ID.
289  */
290 zfs_prop_t
291 zfs_name_to_prop(const char *propname)
292 {
293 	return (zprop_name_to_prop(propname, ZFS_TYPE_DATASET));
294 }
295 
296 
297 /*
298  * For user property names, we allow all lowercase alphanumeric characters, plus
299  * a few useful punctuation characters.
300  */
301 static int
302 valid_char(char c)
303 {
304 	return ((c >= 'a' && c <= 'z') ||
305 	    (c >= '0' && c <= '9') ||
306 	    c == '-' || c == '_' || c == '.' || c == ':');
307 }
308 
309 /*
310  * Returns true if this is a valid user-defined property (one with a ':').
311  */
312 boolean_t
313 zfs_prop_user(const char *name)
314 {
315 	int i;
316 	char c;
317 	boolean_t foundsep = B_FALSE;
318 
319 	for (i = 0; i < strlen(name); i++) {
320 		c = name[i];
321 		if (!valid_char(c))
322 			return (B_FALSE);
323 		if (c == ':')
324 			foundsep = B_TRUE;
325 	}
326 
327 	if (!foundsep)
328 		return (B_FALSE);
329 
330 	return (B_TRUE);
331 }
332 
333 /*
334  * Tables of index types, plus functions to convert between the user view
335  * (strings) and internal representation (uint64_t).
336  */
337 int
338 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
339 {
340 	return (zprop_string_to_index(prop, string, index, ZFS_TYPE_DATASET));
341 }
342 
343 int
344 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
345 {
346 	return (zprop_index_to_string(prop, index, string, ZFS_TYPE_DATASET));
347 }
348 
349 /*
350  * Returns TRUE if the property applies to any of the given dataset types.
351  */
352 int
353 zfs_prop_valid_for_type(int prop, zfs_type_t types)
354 {
355 	return (zprop_valid_for_type(prop, types));
356 }
357 
358 zprop_type_t
359 zfs_prop_get_type(zfs_prop_t prop)
360 {
361 	return (zfs_prop_table[prop].pd_proptype);
362 }
363 
364 /*
365  * Returns TRUE if the property is readonly.
366  */
367 boolean_t
368 zfs_prop_readonly(zfs_prop_t prop)
369 {
370 	return (zfs_prop_table[prop].pd_attr == PROP_READONLY ||
371 	    zfs_prop_table[prop].pd_attr == PROP_ONETIME);
372 }
373 
374 /*
375  * Returns TRUE if the property is only allowed to be set once.
376  */
377 boolean_t
378 zfs_prop_setonce(zfs_prop_t prop)
379 {
380 	return (zfs_prop_table[prop].pd_attr == PROP_ONETIME);
381 }
382 
383 const char *
384 zfs_prop_default_string(zfs_prop_t prop)
385 {
386 	return (zfs_prop_table[prop].pd_strdefault);
387 }
388 
389 uint64_t
390 zfs_prop_default_numeric(zfs_prop_t prop)
391 {
392 	return (zfs_prop_table[prop].pd_numdefault);
393 }
394 
395 /*
396  * Given a dataset property ID, returns the corresponding name.
397  * Assuming the zfs dataset property ID is valid.
398  */
399 const char *
400 zfs_prop_to_name(zfs_prop_t prop)
401 {
402 	return (zfs_prop_table[prop].pd_name);
403 }
404 
405 /*
406  * Returns TRUE if the property is inheritable.
407  */
408 boolean_t
409 zfs_prop_inheritable(zfs_prop_t prop)
410 {
411 	return (zfs_prop_table[prop].pd_attr == PROP_INHERIT ||
412 	    zfs_prop_table[prop].pd_attr == PROP_ONETIME);
413 }
414 
415 #ifndef _KERNEL
416 
417 /*
418  * Returns a string describing the set of acceptable values for the given
419  * zfs property, or NULL if it cannot be set.
420  */
421 const char *
422 zfs_prop_values(zfs_prop_t prop)
423 {
424 	return (zfs_prop_table[prop].pd_values);
425 }
426 
427 /*
428  * Returns TRUE if this property is a string type.  Note that index types
429  * (compression, checksum) are treated as strings in userland, even though they
430  * are stored numerically on disk.
431  */
432 int
433 zfs_prop_is_string(zfs_prop_t prop)
434 {
435 	return (zfs_prop_table[prop].pd_proptype == PROP_TYPE_STRING ||
436 	    zfs_prop_table[prop].pd_proptype == PROP_TYPE_INDEX);
437 }
438 
439 /*
440  * Returns the column header for the given property.  Used only in
441  * 'zfs list -o', but centralized here with the other property information.
442  */
443 const char *
444 zfs_prop_column_name(zfs_prop_t prop)
445 {
446 	return (zfs_prop_table[prop].pd_colname);
447 }
448 
449 /*
450  * Returns whether the given property should be displayed right-justified for
451  * 'zfs list'.
452  */
453 boolean_t
454 zfs_prop_align_right(zfs_prop_t prop)
455 {
456 	return (zfs_prop_table[prop].pd_rightalign);
457 }
458 
459 #endif
460