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