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